4 #include "object-store.h"
5 #include "repository.h"
12 #include "reachable.h"
15 /* NEEDSWORK: switch to using parse_options */
16 static const char reflog_expire_usage
[] =
17 N_("git reflog expire [--expire=<time>] "
18 "[--expire-unreachable=<time>] "
19 "[--rewrite] [--updateref] [--stale-fix] [--dry-run | -n] "
20 "[--verbose] [--all] <refs>...");
21 static const char reflog_delete_usage
[] =
22 N_("git reflog delete [--rewrite] [--updateref] "
23 "[--dry-run | -n] [--verbose] <refs>...");
24 static const char reflog_exists_usage
[] =
25 N_("git reflog exists <ref>");
27 static timestamp_t default_reflog_expire
;
28 static timestamp_t default_reflog_expire_unreachable
;
30 struct cmd_reflog_expire_cb
{
32 timestamp_t expire_total
;
33 timestamp_t expire_unreachable
;
37 struct expire_reflog_policy_cb
{
42 } unreachable_expire_kind
;
43 struct commit_list
*mark_list
;
44 unsigned long mark_limit
;
45 struct cmd_reflog_expire_cb cmd
;
46 struct commit
*tip_commit
;
47 struct commit_list
*tips
;
48 unsigned int dry_run
:1;
51 struct worktree_reflogs
{
52 struct worktree
*worktree
;
53 struct string_list reflogs
;
56 /* Remember to update object flag allocation in object.h */
57 #define INCOMPLETE (1u<<10)
58 #define STUDYING (1u<<11)
59 #define REACHABLE (1u<<12)
61 static int tree_is_complete(const struct object_id
*oid
)
63 struct tree_desc desc
;
64 struct name_entry entry
;
68 tree
= lookup_tree(the_repository
, oid
);
71 if (tree
->object
.flags
& SEEN
)
73 if (tree
->object
.flags
& INCOMPLETE
)
77 enum object_type type
;
79 void *data
= read_object_file(oid
, &type
, &size
);
81 tree
->object
.flags
|= INCOMPLETE
;
87 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
89 while (tree_entry(&desc
, &entry
)) {
90 if (!has_object_file(&entry
.oid
) ||
91 (S_ISDIR(entry
.mode
) && !tree_is_complete(&entry
.oid
))) {
92 tree
->object
.flags
|= INCOMPLETE
;
96 free_tree_buffer(tree
);
99 tree
->object
.flags
|= SEEN
;
103 static int commit_is_complete(struct commit
*commit
)
105 struct object_array study
;
106 struct object_array found
;
107 int is_incomplete
= 0;
111 if (commit
->object
.flags
& SEEN
)
113 if (commit
->object
.flags
& INCOMPLETE
)
116 * Find all commits that are reachable and are not marked as
117 * SEEN. Then make sure the trees and blobs contained are
118 * complete. After that, mark these commits also as SEEN.
119 * If some of the objects that are needed to complete this
120 * commit are missing, mark this commit as INCOMPLETE.
122 memset(&study
, 0, sizeof(study
));
123 memset(&found
, 0, sizeof(found
));
124 add_object_array(&commit
->object
, NULL
, &study
);
125 add_object_array(&commit
->object
, NULL
, &found
);
126 commit
->object
.flags
|= STUDYING
;
129 struct commit_list
*parent
;
131 c
= (struct commit
*)object_array_pop(&study
);
132 if (!c
->object
.parsed
&& !parse_object(the_repository
, &c
->object
.oid
))
133 c
->object
.flags
|= INCOMPLETE
;
135 if (c
->object
.flags
& INCOMPLETE
) {
139 else if (c
->object
.flags
& SEEN
)
141 for (parent
= c
->parents
; parent
; parent
= parent
->next
) {
142 struct commit
*p
= parent
->item
;
143 if (p
->object
.flags
& STUDYING
)
145 p
->object
.flags
|= STUDYING
;
146 add_object_array(&p
->object
, NULL
, &study
);
147 add_object_array(&p
->object
, NULL
, &found
);
150 if (!is_incomplete
) {
152 * make sure all commits in "found" array have all the
155 for (i
= 0; i
< found
.nr
; i
++) {
157 (struct commit
*)found
.objects
[i
].item
;
158 if (!tree_is_complete(get_commit_tree_oid(c
))) {
160 c
->object
.flags
|= INCOMPLETE
;
163 if (!is_incomplete
) {
164 /* mark all found commits as complete, iow SEEN */
165 for (i
= 0; i
< found
.nr
; i
++)
166 found
.objects
[i
].item
->flags
|= SEEN
;
169 /* clear flags from the objects we traversed */
170 for (i
= 0; i
< found
.nr
; i
++)
171 found
.objects
[i
].item
->flags
&= ~STUDYING
;
173 commit
->object
.flags
|= INCOMPLETE
;
176 * If we come here, we have (1) traversed the ancestry chain
177 * from the "commit" until we reach SEEN commits (which are
178 * known to be complete), and (2) made sure that the commits
179 * encountered during the above traversal refer to trees that
180 * are complete. Which means that we know *all* the commits
181 * we have seen during this process are complete.
183 for (i
= 0; i
< found
.nr
; i
++)
184 found
.objects
[i
].item
->flags
|= SEEN
;
186 /* free object arrays */
187 object_array_clear(&study
);
188 object_array_clear(&found
);
189 return !is_incomplete
;
192 static int keep_entry(struct commit
**it
, struct object_id
*oid
)
194 struct commit
*commit
;
196 if (is_null_oid(oid
))
198 commit
= lookup_commit_reference_gently(the_repository
, oid
, 1);
203 * Make sure everything in this commit exists.
205 * We have walked all the objects reachable from the refs
206 * and cache earlier. The commits reachable by this commit
207 * must meet SEEN commits -- and then we should mark them as
210 if (!commit_is_complete(commit
))
217 * Starting from commits in the cb->mark_list, mark commits that are
218 * reachable from them. Stop the traversal at commits older than
219 * the expire_limit and queue them back, so that the caller can call
220 * us again to restart the traversal with longer expire_limit.
222 static void mark_reachable(struct expire_reflog_policy_cb
*cb
)
224 struct commit_list
*pending
;
225 timestamp_t expire_limit
= cb
->mark_limit
;
226 struct commit_list
*leftover
= NULL
;
228 for (pending
= cb
->mark_list
; pending
; pending
= pending
->next
)
229 pending
->item
->object
.flags
&= ~REACHABLE
;
231 pending
= cb
->mark_list
;
233 struct commit_list
*parent
;
234 struct commit
*commit
= pop_commit(&pending
);
235 if (commit
->object
.flags
& REACHABLE
)
237 if (parse_commit(commit
))
239 commit
->object
.flags
|= REACHABLE
;
240 if (commit
->date
< expire_limit
) {
241 commit_list_insert(commit
, &leftover
);
244 commit
->object
.flags
|= REACHABLE
;
245 parent
= commit
->parents
;
247 commit
= parent
->item
;
248 parent
= parent
->next
;
249 if (commit
->object
.flags
& REACHABLE
)
251 commit_list_insert(commit
, &pending
);
254 cb
->mark_list
= leftover
;
257 static int unreachable(struct expire_reflog_policy_cb
*cb
, struct commit
*commit
, struct object_id
*oid
)
260 * We may or may not have the commit yet - if not, look it
261 * up using the supplied sha1.
264 if (is_null_oid(oid
))
267 commit
= lookup_commit_reference_gently(the_repository
, oid
,
270 /* Not a commit -- keep it */
275 /* Reachable from the current ref? Don't prune. */
276 if (commit
->object
.flags
& REACHABLE
)
279 if (cb
->mark_list
&& cb
->mark_limit
) {
280 cb
->mark_limit
= 0; /* dig down to the root */
284 return !(commit
->object
.flags
& REACHABLE
);
288 * Return true iff the specified reflog entry should be expired.
290 static int should_expire_reflog_ent(struct object_id
*ooid
, struct object_id
*noid
,
291 const char *email
, timestamp_t timestamp
, int tz
,
292 const char *message
, void *cb_data
)
294 struct expire_reflog_policy_cb
*cb
= cb_data
;
295 struct commit
*old_commit
, *new_commit
;
297 if (timestamp
< cb
->cmd
.expire_total
)
300 old_commit
= new_commit
= NULL
;
301 if (cb
->cmd
.stalefix
&&
302 (!keep_entry(&old_commit
, ooid
) || !keep_entry(&new_commit
, noid
)))
305 if (timestamp
< cb
->cmd
.expire_unreachable
) {
306 switch (cb
->unreachable_expire_kind
) {
311 if (unreachable(cb
, old_commit
, ooid
) || unreachable(cb
, new_commit
, noid
))
317 if (cb
->cmd
.recno
&& --(cb
->cmd
.recno
) == 0)
323 static int should_expire_reflog_ent_verbose(struct object_id
*ooid
,
324 struct object_id
*noid
,
326 timestamp_t timestamp
, int tz
,
327 const char *message
, void *cb_data
)
329 struct expire_reflog_policy_cb
*cb
= cb_data
;
332 expire
= should_expire_reflog_ent(ooid
, noid
, email
, timestamp
, tz
,
336 printf("keep %s", message
);
337 else if (cb
->dry_run
)
338 printf("would prune %s", message
);
340 printf("prune %s", message
);
345 static int push_tip_to_list(const char *refname
, const struct object_id
*oid
,
346 int flags
, void *cb_data
)
348 struct commit_list
**list
= cb_data
;
349 struct commit
*tip_commit
;
350 if (flags
& REF_ISSYMREF
)
352 tip_commit
= lookup_commit_reference_gently(the_repository
, oid
, 1);
355 commit_list_insert(tip_commit
, list
);
359 static int is_head(const char *refname
)
361 switch (ref_type(refname
)) {
362 case REF_TYPE_OTHER_PSEUDOREF
:
363 case REF_TYPE_MAIN_PSEUDOREF
:
364 if (parse_worktree_ref(refname
, NULL
, NULL
, &refname
))
365 BUG("not a worktree ref: %s", refname
);
370 return !strcmp(refname
, "HEAD");
373 static void reflog_expiry_prepare(const char *refname
,
374 const struct object_id
*oid
,
377 struct expire_reflog_policy_cb
*cb
= cb_data
;
378 struct commit_list
*elem
;
379 struct commit
*commit
= NULL
;
381 if (!cb
->cmd
.expire_unreachable
|| is_head(refname
)) {
382 cb
->unreachable_expire_kind
= UE_HEAD
;
384 commit
= lookup_commit(the_repository
, oid
);
385 cb
->unreachable_expire_kind
= commit
? UE_NORMAL
: UE_ALWAYS
;
388 if (cb
->cmd
.expire_unreachable
<= cb
->cmd
.expire_total
)
389 cb
->unreachable_expire_kind
= UE_ALWAYS
;
391 switch (cb
->unreachable_expire_kind
) {
395 for_each_ref(push_tip_to_list
, &cb
->tips
);
396 for (elem
= cb
->tips
; elem
; elem
= elem
->next
)
397 commit_list_insert(elem
->item
, &cb
->mark_list
);
400 commit_list_insert(commit
, &cb
->mark_list
);
401 /* For reflog_expiry_cleanup() below */
402 cb
->tip_commit
= commit
;
404 cb
->mark_limit
= cb
->cmd
.expire_total
;
408 static void reflog_expiry_cleanup(void *cb_data
)
410 struct expire_reflog_policy_cb
*cb
= cb_data
;
411 struct commit_list
*elem
;
413 switch (cb
->unreachable_expire_kind
) {
417 for (elem
= cb
->tips
; elem
; elem
= elem
->next
)
418 clear_commit_marks(elem
->item
, REACHABLE
);
419 free_commit_list(cb
->tips
);
422 clear_commit_marks(cb
->tip_commit
, REACHABLE
);
427 static int collect_reflog(const char *ref
, const struct object_id
*oid
, int unused
, void *cb_data
)
429 struct worktree_reflogs
*cb
= cb_data
;
430 struct worktree
*worktree
= cb
->worktree
;
431 struct strbuf newref
= STRBUF_INIT
;
434 * Avoid collecting the same shared ref multiple times because
435 * they are available via all worktrees.
437 if (!worktree
->is_current
&& ref_type(ref
) == REF_TYPE_NORMAL
)
440 strbuf_worktree_ref(worktree
, &newref
, ref
);
441 string_list_append_nodup(&cb
->reflogs
, strbuf_detach(&newref
, NULL
));
446 static struct reflog_expire_cfg
{
447 struct reflog_expire_cfg
*next
;
448 timestamp_t expire_total
;
449 timestamp_t expire_unreachable
;
450 char pattern
[FLEX_ARRAY
];
451 } *reflog_expire_cfg
, **reflog_expire_cfg_tail
;
453 static struct reflog_expire_cfg
*find_cfg_ent(const char *pattern
, size_t len
)
455 struct reflog_expire_cfg
*ent
;
457 if (!reflog_expire_cfg_tail
)
458 reflog_expire_cfg_tail
= &reflog_expire_cfg
;
460 for (ent
= reflog_expire_cfg
; ent
; ent
= ent
->next
)
461 if (!strncmp(ent
->pattern
, pattern
, len
) &&
462 ent
->pattern
[len
] == '\0')
465 FLEX_ALLOC_MEM(ent
, pattern
, pattern
, len
);
466 *reflog_expire_cfg_tail
= ent
;
467 reflog_expire_cfg_tail
= &(ent
->next
);
471 /* expiry timer slot */
472 #define EXPIRE_TOTAL 01
473 #define EXPIRE_UNREACH 02
475 static int reflog_expire_config(const char *var
, const char *value
, void *cb
)
477 const char *pattern
, *key
;
481 struct reflog_expire_cfg
*ent
;
483 if (parse_config_key(var
, "gc", &pattern
, &pattern_len
, &key
) < 0)
484 return git_default_config(var
, value
, cb
);
486 if (!strcmp(key
, "reflogexpire")) {
488 if (git_config_expiry_date(&expire
, var
, value
))
490 } else if (!strcmp(key
, "reflogexpireunreachable")) {
491 slot
= EXPIRE_UNREACH
;
492 if (git_config_expiry_date(&expire
, var
, value
))
495 return git_default_config(var
, value
, cb
);
500 default_reflog_expire
= expire
;
503 default_reflog_expire_unreachable
= expire
;
509 ent
= find_cfg_ent(pattern
, pattern_len
);
514 ent
->expire_total
= expire
;
517 ent
->expire_unreachable
= expire
;
523 static void set_reflog_expiry_param(struct cmd_reflog_expire_cb
*cb
, int slot
, const char *ref
)
525 struct reflog_expire_cfg
*ent
;
527 if (slot
== (EXPIRE_TOTAL
|EXPIRE_UNREACH
))
528 return; /* both given explicitly -- nothing to tweak */
530 for (ent
= reflog_expire_cfg
; ent
; ent
= ent
->next
) {
531 if (!wildmatch(ent
->pattern
, ref
, 0)) {
532 if (!(slot
& EXPIRE_TOTAL
))
533 cb
->expire_total
= ent
->expire_total
;
534 if (!(slot
& EXPIRE_UNREACH
))
535 cb
->expire_unreachable
= ent
->expire_unreachable
;
541 * If unconfigured, make stash never expire
543 if (!strcmp(ref
, "refs/stash")) {
544 if (!(slot
& EXPIRE_TOTAL
))
545 cb
->expire_total
= 0;
546 if (!(slot
& EXPIRE_UNREACH
))
547 cb
->expire_unreachable
= 0;
551 /* Nothing matched -- use the default value */
552 if (!(slot
& EXPIRE_TOTAL
))
553 cb
->expire_total
= default_reflog_expire
;
554 if (!(slot
& EXPIRE_UNREACH
))
555 cb
->expire_unreachable
= default_reflog_expire_unreachable
;
558 static int cmd_reflog_expire(int argc
, const char **argv
, const char *prefix
)
560 struct cmd_reflog_expire_cb cmd
= { 0 };
561 timestamp_t now
= time(NULL
);
562 int i
, status
, do_all
, all_worktrees
= 1;
563 int explicit_expiry
= 0;
564 unsigned int flags
= 0;
566 reflog_expiry_should_prune_fn
*should_prune_fn
= should_expire_reflog_ent
;
568 default_reflog_expire_unreachable
= now
- 30 * 24 * 3600;
569 default_reflog_expire
= now
- 90 * 24 * 3600;
570 git_config(reflog_expire_config
, NULL
);
572 save_commit_buffer
= 0;
575 cmd
.expire_total
= default_reflog_expire
;
576 cmd
.expire_unreachable
= default_reflog_expire_unreachable
;
578 for (i
= 1; i
< argc
; i
++) {
579 const char *arg
= argv
[i
];
581 if (!strcmp(arg
, "--dry-run") || !strcmp(arg
, "-n"))
582 flags
|= EXPIRE_REFLOGS_DRY_RUN
;
583 else if (skip_prefix(arg
, "--expire=", &arg
)) {
584 if (parse_expiry_date(arg
, &cmd
.expire_total
))
585 die(_("'%s' is not a valid timestamp"), arg
);
586 explicit_expiry
|= EXPIRE_TOTAL
;
588 else if (skip_prefix(arg
, "--expire-unreachable=", &arg
)) {
589 if (parse_expiry_date(arg
, &cmd
.expire_unreachable
))
590 die(_("'%s' is not a valid timestamp"), arg
);
591 explicit_expiry
|= EXPIRE_UNREACH
;
593 else if (!strcmp(arg
, "--stale-fix"))
595 else if (!strcmp(arg
, "--rewrite"))
596 flags
|= EXPIRE_REFLOGS_REWRITE
;
597 else if (!strcmp(arg
, "--updateref"))
598 flags
|= EXPIRE_REFLOGS_UPDATE_REF
;
599 else if (!strcmp(arg
, "--all"))
601 else if (!strcmp(arg
, "--single-worktree"))
603 else if (!strcmp(arg
, "--verbose"))
605 else if (!strcmp(arg
, "--")) {
609 else if (arg
[0] == '-')
610 usage(_(reflog_expire_usage
));
616 should_prune_fn
= should_expire_reflog_ent_verbose
;
619 * We can trust the commits and objects reachable from refs
620 * even in older repository. We cannot trust what's reachable
621 * from reflog if the repository was pruned with older git.
624 struct rev_info revs
;
626 repo_init_revisions(the_repository
, &revs
, prefix
);
627 revs
.do_not_die_on_missing_tree
= 1;
628 revs
.ignore_missing
= 1;
629 revs
.ignore_missing_links
= 1;
631 printf(_("Marking reachable objects..."));
632 mark_reachable_objects(&revs
, 0, 0, NULL
);
638 struct worktree_reflogs collected
= {
639 .reflogs
= STRING_LIST_INIT_DUP
,
641 struct string_list_item
*item
;
642 struct worktree
**worktrees
, **p
;
644 worktrees
= get_worktrees();
645 for (p
= worktrees
; *p
; p
++) {
646 if (!all_worktrees
&& !(*p
)->is_current
)
648 collected
.worktree
= *p
;
649 refs_for_each_reflog(get_worktree_ref_store(*p
),
650 collect_reflog
, &collected
);
652 free_worktrees(worktrees
);
654 for_each_string_list_item(item
, &collected
.reflogs
) {
655 struct expire_reflog_policy_cb cb
= {
657 .dry_run
= !!(flags
& EXPIRE_REFLOGS_DRY_RUN
),
660 set_reflog_expiry_param(&cb
.cmd
, explicit_expiry
, item
->string
);
661 status
|= reflog_expire(item
->string
, flags
,
662 reflog_expiry_prepare
,
664 reflog_expiry_cleanup
,
667 string_list_clear(&collected
.reflogs
, 0);
670 for (; i
< argc
; i
++) {
672 struct expire_reflog_policy_cb cb
= { .cmd
= cmd
};
674 if (!dwim_log(argv
[i
], strlen(argv
[i
]), NULL
, &ref
)) {
675 status
|= error(_("%s points nowhere!"), argv
[i
]);
678 set_reflog_expiry_param(&cb
.cmd
, explicit_expiry
, ref
);
679 status
|= reflog_expire(ref
, flags
,
680 reflog_expiry_prepare
,
682 reflog_expiry_cleanup
,
689 static int count_reflog_ent(struct object_id
*ooid
, struct object_id
*noid
,
690 const char *email
, timestamp_t timestamp
, int tz
,
691 const char *message
, void *cb_data
)
693 struct cmd_reflog_expire_cb
*cb
= cb_data
;
694 if (!cb
->expire_total
|| timestamp
< cb
->expire_total
)
699 static int cmd_reflog_delete(int argc
, const char **argv
, const char *prefix
)
701 struct cmd_reflog_expire_cb cmd
= { 0 };
703 unsigned int flags
= 0;
705 reflog_expiry_should_prune_fn
*should_prune_fn
= should_expire_reflog_ent
;
707 for (i
= 1; i
< argc
; i
++) {
708 const char *arg
= argv
[i
];
709 if (!strcmp(arg
, "--dry-run") || !strcmp(arg
, "-n"))
710 flags
|= EXPIRE_REFLOGS_DRY_RUN
;
711 else if (!strcmp(arg
, "--rewrite"))
712 flags
|= EXPIRE_REFLOGS_REWRITE
;
713 else if (!strcmp(arg
, "--updateref"))
714 flags
|= EXPIRE_REFLOGS_UPDATE_REF
;
715 else if (!strcmp(arg
, "--verbose"))
717 else if (!strcmp(arg
, "--")) {
721 else if (arg
[0] == '-')
722 usage(_(reflog_delete_usage
));
728 should_prune_fn
= should_expire_reflog_ent_verbose
;
731 return error(_("no reflog specified to delete"));
733 for ( ; i
< argc
; i
++) {
734 const char *spec
= strstr(argv
[i
], "@{");
737 struct expire_reflog_policy_cb cb
= {
738 .dry_run
= !!(flags
& EXPIRE_REFLOGS_DRY_RUN
),
742 status
|= error(_("not a reflog: %s"), argv
[i
]);
746 if (!dwim_log(argv
[i
], spec
- argv
[i
], NULL
, &ref
)) {
747 status
|= error(_("no reflog for '%s'"), argv
[i
]);
751 recno
= strtoul(spec
+ 2, &ep
, 10);
754 for_each_reflog_ent(ref
, count_reflog_ent
, &cmd
);
756 cmd
.expire_total
= approxidate(spec
+ 2);
757 for_each_reflog_ent(ref
, count_reflog_ent
, &cmd
);
758 cmd
.expire_total
= 0;
762 status
|= reflog_expire(ref
, flags
,
763 reflog_expiry_prepare
,
765 reflog_expiry_cleanup
,
772 static int cmd_reflog_exists(int argc
, const char **argv
, const char *prefix
)
776 for (i
= 1; i
< argc
; i
++) {
777 const char *arg
= argv
[i
];
778 if (!strcmp(arg
, "--")) {
782 else if (arg
[0] == '-')
783 usage(_(reflog_exists_usage
));
790 if (argc
- start
!= 1)
791 usage(_(reflog_exists_usage
));
793 if (check_refname_format(argv
[start
], REFNAME_ALLOW_ONELEVEL
))
794 die(_("invalid ref format: %s"), argv
[start
]);
795 return !reflog_exists(argv
[start
]);
802 static const char reflog_usage
[] =
803 N_("git reflog [ show | expire | delete | exists ]");
805 int cmd_reflog(int argc
, const char **argv
, const char *prefix
)
807 if (argc
> 1 && !strcmp(argv
[1], "-h"))
808 usage(_(reflog_usage
));
810 /* With no command, we default to showing it. */
811 if (argc
< 2 || *argv
[1] == '-')
812 return cmd_log_reflog(argc
, argv
, prefix
);
814 if (!strcmp(argv
[1], "show"))
815 return cmd_log_reflog(argc
- 1, argv
+ 1, prefix
);
817 if (!strcmp(argv
[1], "expire"))
818 return cmd_reflog_expire(argc
- 1, argv
+ 1, prefix
);
820 if (!strcmp(argv
[1], "delete"))
821 return cmd_reflog_delete(argc
- 1, argv
+ 1, prefix
);
823 if (!strcmp(argv
[1], "exists"))
824 return cmd_reflog_exists(argc
- 1, argv
+ 1, prefix
);
826 return cmd_log_reflog(argc
, argv
, prefix
);