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 "git reflog expire [--expire=<time>] [--expire-unreachable=<time>] [--rewrite] [--updateref] [--stale-fix] [--dry-run | -n] [--verbose] [--all] <refs>...";
18 static const char reflog_delete_usage
[] =
19 "git reflog delete [--rewrite] [--updateref] [--dry-run | -n] [--verbose] <refs>...";
20 static const char reflog_exists_usage
[] =
21 "git reflog exists <ref>";
23 static timestamp_t default_reflog_expire
;
24 static timestamp_t default_reflog_expire_unreachable
;
26 struct cmd_reflog_expire_cb
{
29 timestamp_t expire_total
;
30 timestamp_t expire_unreachable
;
34 struct expire_reflog_policy_cb
{
39 } unreachable_expire_kind
;
40 struct commit_list
*mark_list
;
41 unsigned long mark_limit
;
42 struct cmd_reflog_expire_cb cmd
;
43 struct commit
*tip_commit
;
44 struct commit_list
*tips
;
47 struct collected_reflog
{
49 char reflog
[FLEX_ARRAY
];
52 struct collect_reflog_cb
{
53 struct collected_reflog
**e
;
59 /* Remember to update object flag allocation in object.h */
60 #define INCOMPLETE (1u<<10)
61 #define STUDYING (1u<<11)
62 #define REACHABLE (1u<<12)
64 static int tree_is_complete(const struct object_id
*oid
)
66 struct tree_desc desc
;
67 struct name_entry entry
;
71 tree
= lookup_tree(the_repository
, oid
);
74 if (tree
->object
.flags
& SEEN
)
76 if (tree
->object
.flags
& INCOMPLETE
)
80 enum object_type type
;
82 void *data
= read_object_file(oid
, &type
, &size
);
84 tree
->object
.flags
|= INCOMPLETE
;
90 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
92 while (tree_entry(&desc
, &entry
)) {
93 if (!has_sha1_file(entry
.oid
->hash
) ||
94 (S_ISDIR(entry
.mode
) && !tree_is_complete(entry
.oid
))) {
95 tree
->object
.flags
|= INCOMPLETE
;
99 free_tree_buffer(tree
);
102 tree
->object
.flags
|= SEEN
;
106 static int commit_is_complete(struct commit
*commit
)
108 struct object_array study
;
109 struct object_array found
;
110 int is_incomplete
= 0;
114 if (commit
->object
.flags
& SEEN
)
116 if (commit
->object
.flags
& INCOMPLETE
)
119 * Find all commits that are reachable and are not marked as
120 * SEEN. Then make sure the trees and blobs contained are
121 * complete. After that, mark these commits also as SEEN.
122 * If some of the objects that are needed to complete this
123 * commit are missing, mark this commit as INCOMPLETE.
125 memset(&study
, 0, sizeof(study
));
126 memset(&found
, 0, sizeof(found
));
127 add_object_array(&commit
->object
, NULL
, &study
);
128 add_object_array(&commit
->object
, NULL
, &found
);
129 commit
->object
.flags
|= STUDYING
;
132 struct commit_list
*parent
;
134 c
= (struct commit
*)object_array_pop(&study
);
135 if (!c
->object
.parsed
&& !parse_object(the_repository
, &c
->object
.oid
))
136 c
->object
.flags
|= INCOMPLETE
;
138 if (c
->object
.flags
& INCOMPLETE
) {
142 else if (c
->object
.flags
& SEEN
)
144 for (parent
= c
->parents
; parent
; parent
= parent
->next
) {
145 struct commit
*p
= parent
->item
;
146 if (p
->object
.flags
& STUDYING
)
148 p
->object
.flags
|= STUDYING
;
149 add_object_array(&p
->object
, NULL
, &study
);
150 add_object_array(&p
->object
, NULL
, &found
);
153 if (!is_incomplete
) {
155 * make sure all commits in "found" array have all the
158 for (i
= 0; i
< found
.nr
; i
++) {
160 (struct commit
*)found
.objects
[i
].item
;
161 if (!tree_is_complete(get_commit_tree_oid(c
))) {
163 c
->object
.flags
|= INCOMPLETE
;
166 if (!is_incomplete
) {
167 /* mark all found commits as complete, iow SEEN */
168 for (i
= 0; i
< found
.nr
; i
++)
169 found
.objects
[i
].item
->flags
|= SEEN
;
172 /* clear flags from the objects we traversed */
173 for (i
= 0; i
< found
.nr
; i
++)
174 found
.objects
[i
].item
->flags
&= ~STUDYING
;
176 commit
->object
.flags
|= INCOMPLETE
;
179 * If we come here, we have (1) traversed the ancestry chain
180 * from the "commit" until we reach SEEN commits (which are
181 * known to be complete), and (2) made sure that the commits
182 * encountered during the above traversal refer to trees that
183 * are complete. Which means that we know *all* the commits
184 * we have seen during this process are complete.
186 for (i
= 0; i
< found
.nr
; i
++)
187 found
.objects
[i
].item
->flags
|= SEEN
;
189 /* free object arrays */
190 object_array_clear(&study
);
191 object_array_clear(&found
);
192 return !is_incomplete
;
195 static int keep_entry(struct commit
**it
, struct object_id
*oid
)
197 struct commit
*commit
;
199 if (is_null_oid(oid
))
201 commit
= lookup_commit_reference_gently(the_repository
, oid
, 1);
206 * Make sure everything in this commit exists.
208 * We have walked all the objects reachable from the refs
209 * and cache earlier. The commits reachable by this commit
210 * must meet SEEN commits -- and then we should mark them as
213 if (!commit_is_complete(commit
))
220 * Starting from commits in the cb->mark_list, mark commits that are
221 * reachable from them. Stop the traversal at commits older than
222 * the expire_limit and queue them back, so that the caller can call
223 * us again to restart the traversal with longer expire_limit.
225 static void mark_reachable(struct expire_reflog_policy_cb
*cb
)
227 struct commit_list
*pending
;
228 timestamp_t expire_limit
= cb
->mark_limit
;
229 struct commit_list
*leftover
= NULL
;
231 for (pending
= cb
->mark_list
; pending
; pending
= pending
->next
)
232 pending
->item
->object
.flags
&= ~REACHABLE
;
234 pending
= cb
->mark_list
;
236 struct commit_list
*parent
;
237 struct commit
*commit
= pop_commit(&pending
);
238 if (commit
->object
.flags
& REACHABLE
)
240 if (parse_commit(commit
))
242 commit
->object
.flags
|= REACHABLE
;
243 if (commit
->date
< expire_limit
) {
244 commit_list_insert(commit
, &leftover
);
247 commit
->object
.flags
|= REACHABLE
;
248 parent
= commit
->parents
;
250 commit
= parent
->item
;
251 parent
= parent
->next
;
252 if (commit
->object
.flags
& REACHABLE
)
254 commit_list_insert(commit
, &pending
);
257 cb
->mark_list
= leftover
;
260 static int unreachable(struct expire_reflog_policy_cb
*cb
, struct commit
*commit
, struct object_id
*oid
)
263 * We may or may not have the commit yet - if not, look it
264 * up using the supplied sha1.
267 if (is_null_oid(oid
))
270 commit
= lookup_commit_reference_gently(the_repository
, oid
,
273 /* Not a commit -- keep it */
278 /* Reachable from the current ref? Don't prune. */
279 if (commit
->object
.flags
& REACHABLE
)
282 if (cb
->mark_list
&& cb
->mark_limit
) {
283 cb
->mark_limit
= 0; /* dig down to the root */
287 return !(commit
->object
.flags
& REACHABLE
);
291 * Return true iff the specified reflog entry should be expired.
293 static int should_expire_reflog_ent(struct object_id
*ooid
, struct object_id
*noid
,
294 const char *email
, timestamp_t timestamp
, int tz
,
295 const char *message
, void *cb_data
)
297 struct expire_reflog_policy_cb
*cb
= cb_data
;
298 struct commit
*old_commit
, *new_commit
;
300 if (timestamp
< cb
->cmd
.expire_total
)
303 old_commit
= new_commit
= NULL
;
304 if (cb
->cmd
.stalefix
&&
305 (!keep_entry(&old_commit
, ooid
) || !keep_entry(&new_commit
, noid
)))
308 if (timestamp
< cb
->cmd
.expire_unreachable
) {
309 if (cb
->unreachable_expire_kind
== UE_ALWAYS
)
311 if (unreachable(cb
, old_commit
, ooid
) || unreachable(cb
, new_commit
, noid
))
315 if (cb
->cmd
.recno
&& --(cb
->cmd
.recno
) == 0)
321 static int push_tip_to_list(const char *refname
, const struct object_id
*oid
,
322 int flags
, void *cb_data
)
324 struct commit_list
**list
= cb_data
;
325 struct commit
*tip_commit
;
326 if (flags
& REF_ISSYMREF
)
328 tip_commit
= lookup_commit_reference_gently(the_repository
, oid
, 1);
331 commit_list_insert(tip_commit
, list
);
335 static int is_head(const char *refname
)
337 switch (ref_type(refname
)) {
338 case REF_TYPE_OTHER_PSEUDOREF
:
339 case REF_TYPE_MAIN_PSEUDOREF
:
340 if (parse_worktree_ref(refname
, NULL
, NULL
, &refname
))
341 BUG("not a worktree ref: %s", refname
);
346 return !strcmp(refname
, "HEAD");
349 static void reflog_expiry_prepare(const char *refname
,
350 const struct object_id
*oid
,
353 struct expire_reflog_policy_cb
*cb
= cb_data
;
355 if (!cb
->cmd
.expire_unreachable
|| is_head(refname
)) {
356 cb
->tip_commit
= NULL
;
357 cb
->unreachable_expire_kind
= UE_HEAD
;
359 cb
->tip_commit
= lookup_commit_reference_gently(the_repository
,
362 cb
->unreachable_expire_kind
= UE_ALWAYS
;
364 cb
->unreachable_expire_kind
= UE_NORMAL
;
367 if (cb
->cmd
.expire_unreachable
<= cb
->cmd
.expire_total
)
368 cb
->unreachable_expire_kind
= UE_ALWAYS
;
370 cb
->mark_list
= NULL
;
372 if (cb
->unreachable_expire_kind
!= UE_ALWAYS
) {
373 if (cb
->unreachable_expire_kind
== UE_HEAD
) {
374 struct commit_list
*elem
;
376 for_each_ref(push_tip_to_list
, &cb
->tips
);
377 for (elem
= cb
->tips
; elem
; elem
= elem
->next
)
378 commit_list_insert(elem
->item
, &cb
->mark_list
);
380 commit_list_insert(cb
->tip_commit
, &cb
->mark_list
);
382 cb
->mark_limit
= cb
->cmd
.expire_total
;
387 static void reflog_expiry_cleanup(void *cb_data
)
389 struct expire_reflog_policy_cb
*cb
= cb_data
;
391 if (cb
->unreachable_expire_kind
!= UE_ALWAYS
) {
392 if (cb
->unreachable_expire_kind
== UE_HEAD
) {
393 struct commit_list
*elem
;
394 for (elem
= cb
->tips
; elem
; elem
= elem
->next
)
395 clear_commit_marks(elem
->item
, REACHABLE
);
396 free_commit_list(cb
->tips
);
398 clear_commit_marks(cb
->tip_commit
, REACHABLE
);
403 static int collect_reflog(const char *ref
, const struct object_id
*oid
, int unused
, void *cb_data
)
405 struct collected_reflog
*e
;
406 struct collect_reflog_cb
*cb
= cb_data
;
407 struct strbuf newref
= STRBUF_INIT
;
410 * Avoid collecting the same shared ref multiple times because
411 * they are available via all worktrees.
413 if (!cb
->wt
->is_current
&& ref_type(ref
) == REF_TYPE_NORMAL
)
416 strbuf_worktree_ref(cb
->wt
, &newref
, ref
);
417 FLEX_ALLOC_STR(e
, reflog
, newref
.buf
);
418 strbuf_release(&newref
);
420 oidcpy(&e
->oid
, oid
);
421 ALLOC_GROW(cb
->e
, cb
->nr
+ 1, cb
->alloc
);
426 static struct reflog_expire_cfg
{
427 struct reflog_expire_cfg
*next
;
428 timestamp_t expire_total
;
429 timestamp_t expire_unreachable
;
430 char pattern
[FLEX_ARRAY
];
431 } *reflog_expire_cfg
, **reflog_expire_cfg_tail
;
433 static struct reflog_expire_cfg
*find_cfg_ent(const char *pattern
, size_t len
)
435 struct reflog_expire_cfg
*ent
;
437 if (!reflog_expire_cfg_tail
)
438 reflog_expire_cfg_tail
= &reflog_expire_cfg
;
440 for (ent
= reflog_expire_cfg
; ent
; ent
= ent
->next
)
441 if (!strncmp(ent
->pattern
, pattern
, len
) &&
442 ent
->pattern
[len
] == '\0')
445 FLEX_ALLOC_MEM(ent
, pattern
, pattern
, len
);
446 *reflog_expire_cfg_tail
= ent
;
447 reflog_expire_cfg_tail
= &(ent
->next
);
451 /* expiry timer slot */
452 #define EXPIRE_TOTAL 01
453 #define EXPIRE_UNREACH 02
455 static int reflog_expire_config(const char *var
, const char *value
, void *cb
)
457 const char *pattern
, *key
;
461 struct reflog_expire_cfg
*ent
;
463 if (parse_config_key(var
, "gc", &pattern
, &pattern_len
, &key
) < 0)
464 return git_default_config(var
, value
, cb
);
466 if (!strcmp(key
, "reflogexpire")) {
468 if (git_config_expiry_date(&expire
, var
, value
))
470 } else if (!strcmp(key
, "reflogexpireunreachable")) {
471 slot
= EXPIRE_UNREACH
;
472 if (git_config_expiry_date(&expire
, var
, value
))
475 return git_default_config(var
, value
, cb
);
480 default_reflog_expire
= expire
;
483 default_reflog_expire_unreachable
= expire
;
489 ent
= find_cfg_ent(pattern
, pattern_len
);
494 ent
->expire_total
= expire
;
497 ent
->expire_unreachable
= expire
;
503 static void set_reflog_expiry_param(struct cmd_reflog_expire_cb
*cb
, int slot
, const char *ref
)
505 struct reflog_expire_cfg
*ent
;
507 if (slot
== (EXPIRE_TOTAL
|EXPIRE_UNREACH
))
508 return; /* both given explicitly -- nothing to tweak */
510 for (ent
= reflog_expire_cfg
; ent
; ent
= ent
->next
) {
511 if (!wildmatch(ent
->pattern
, ref
, 0)) {
512 if (!(slot
& EXPIRE_TOTAL
))
513 cb
->expire_total
= ent
->expire_total
;
514 if (!(slot
& EXPIRE_UNREACH
))
515 cb
->expire_unreachable
= ent
->expire_unreachable
;
521 * If unconfigured, make stash never expire
523 if (!strcmp(ref
, "refs/stash")) {
524 if (!(slot
& EXPIRE_TOTAL
))
525 cb
->expire_total
= 0;
526 if (!(slot
& EXPIRE_UNREACH
))
527 cb
->expire_unreachable
= 0;
531 /* Nothing matched -- use the default value */
532 if (!(slot
& EXPIRE_TOTAL
))
533 cb
->expire_total
= default_reflog_expire
;
534 if (!(slot
& EXPIRE_UNREACH
))
535 cb
->expire_unreachable
= default_reflog_expire_unreachable
;
538 static int cmd_reflog_expire(int argc
, const char **argv
, const char *prefix
)
540 struct expire_reflog_policy_cb cb
;
541 timestamp_t now
= time(NULL
);
542 int i
, status
, do_all
, all_worktrees
= 1;
543 int explicit_expiry
= 0;
544 unsigned int flags
= 0;
546 default_reflog_expire_unreachable
= now
- 30 * 24 * 3600;
547 default_reflog_expire
= now
- 90 * 24 * 3600;
548 git_config(reflog_expire_config
, NULL
);
550 save_commit_buffer
= 0;
552 memset(&cb
, 0, sizeof(cb
));
554 cb
.cmd
.expire_total
= default_reflog_expire
;
555 cb
.cmd
.expire_unreachable
= default_reflog_expire_unreachable
;
557 for (i
= 1; i
< argc
; i
++) {
558 const char *arg
= argv
[i
];
559 if (!strcmp(arg
, "--dry-run") || !strcmp(arg
, "-n"))
560 flags
|= EXPIRE_REFLOGS_DRY_RUN
;
561 else if (starts_with(arg
, "--expire=")) {
562 if (parse_expiry_date(arg
+ 9, &cb
.cmd
.expire_total
))
563 die(_("'%s' is not a valid timestamp"), arg
);
564 explicit_expiry
|= EXPIRE_TOTAL
;
566 else if (starts_with(arg
, "--expire-unreachable=")) {
567 if (parse_expiry_date(arg
+ 21, &cb
.cmd
.expire_unreachable
))
568 die(_("'%s' is not a valid timestamp"), arg
);
569 explicit_expiry
|= EXPIRE_UNREACH
;
571 else if (!strcmp(arg
, "--stale-fix"))
573 else if (!strcmp(arg
, "--rewrite"))
574 flags
|= EXPIRE_REFLOGS_REWRITE
;
575 else if (!strcmp(arg
, "--updateref"))
576 flags
|= EXPIRE_REFLOGS_UPDATE_REF
;
577 else if (!strcmp(arg
, "--all"))
579 else if (!strcmp(arg
, "--single-worktree"))
581 else if (!strcmp(arg
, "--verbose"))
582 flags
|= EXPIRE_REFLOGS_VERBOSE
;
583 else if (!strcmp(arg
, "--")) {
587 else if (arg
[0] == '-')
588 usage(reflog_expire_usage
);
594 * We can trust the commits and objects reachable from refs
595 * even in older repository. We cannot trust what's reachable
596 * from reflog if the repository was pruned with older git.
598 if (cb
.cmd
.stalefix
) {
599 repo_init_revisions(the_repository
, &cb
.cmd
.revs
, prefix
);
600 if (flags
& EXPIRE_REFLOGS_VERBOSE
)
601 printf("Marking reachable objects...");
602 mark_reachable_objects(&cb
.cmd
.revs
, 0, 0, NULL
);
603 if (flags
& EXPIRE_REFLOGS_VERBOSE
)
608 struct collect_reflog_cb collected
;
609 struct worktree
**worktrees
, **p
;
612 memset(&collected
, 0, sizeof(collected
));
613 worktrees
= get_worktrees(0);
614 for (p
= worktrees
; *p
; p
++) {
615 if (!all_worktrees
&& !(*p
)->is_current
)
618 refs_for_each_reflog(get_worktree_ref_store(*p
),
619 collect_reflog
, &collected
);
621 free_worktrees(worktrees
);
622 for (i
= 0; i
< collected
.nr
; i
++) {
623 struct collected_reflog
*e
= collected
.e
[i
];
624 set_reflog_expiry_param(&cb
.cmd
, explicit_expiry
, e
->reflog
);
625 status
|= reflog_expire(e
->reflog
, &e
->oid
, flags
,
626 reflog_expiry_prepare
,
627 should_expire_reflog_ent
,
628 reflog_expiry_cleanup
,
635 for (; i
< argc
; i
++) {
637 struct object_id oid
;
638 if (!dwim_log(argv
[i
], strlen(argv
[i
]), &oid
, &ref
)) {
639 status
|= error("%s points nowhere!", argv
[i
]);
642 set_reflog_expiry_param(&cb
.cmd
, explicit_expiry
, ref
);
643 status
|= reflog_expire(ref
, &oid
, flags
,
644 reflog_expiry_prepare
,
645 should_expire_reflog_ent
,
646 reflog_expiry_cleanup
,
652 static int count_reflog_ent(struct object_id
*ooid
, struct object_id
*noid
,
653 const char *email
, timestamp_t timestamp
, int tz
,
654 const char *message
, void *cb_data
)
656 struct expire_reflog_policy_cb
*cb
= cb_data
;
657 if (!cb
->cmd
.expire_total
|| timestamp
< cb
->cmd
.expire_total
)
662 static int cmd_reflog_delete(int argc
, const char **argv
, const char *prefix
)
664 struct expire_reflog_policy_cb cb
;
666 unsigned int flags
= 0;
668 memset(&cb
, 0, sizeof(cb
));
670 for (i
= 1; i
< argc
; i
++) {
671 const char *arg
= argv
[i
];
672 if (!strcmp(arg
, "--dry-run") || !strcmp(arg
, "-n"))
673 flags
|= EXPIRE_REFLOGS_DRY_RUN
;
674 else if (!strcmp(arg
, "--rewrite"))
675 flags
|= EXPIRE_REFLOGS_REWRITE
;
676 else if (!strcmp(arg
, "--updateref"))
677 flags
|= EXPIRE_REFLOGS_UPDATE_REF
;
678 else if (!strcmp(arg
, "--verbose"))
679 flags
|= EXPIRE_REFLOGS_VERBOSE
;
680 else if (!strcmp(arg
, "--")) {
684 else if (arg
[0] == '-')
685 usage(reflog_delete_usage
);
691 return error("Nothing to delete?");
693 for ( ; i
< argc
; i
++) {
694 const char *spec
= strstr(argv
[i
], "@{");
695 struct object_id oid
;
700 status
|= error("Not a reflog: %s", argv
[i
]);
704 if (!dwim_log(argv
[i
], spec
- argv
[i
], &oid
, &ref
)) {
705 status
|= error("no reflog for '%s'", argv
[i
]);
709 recno
= strtoul(spec
+ 2, &ep
, 10);
711 cb
.cmd
.recno
= -recno
;
712 for_each_reflog_ent(ref
, count_reflog_ent
, &cb
);
714 cb
.cmd
.expire_total
= approxidate(spec
+ 2);
715 for_each_reflog_ent(ref
, count_reflog_ent
, &cb
);
716 cb
.cmd
.expire_total
= 0;
719 status
|= reflog_expire(ref
, &oid
, flags
,
720 reflog_expiry_prepare
,
721 should_expire_reflog_ent
,
722 reflog_expiry_cleanup
,
729 static int cmd_reflog_exists(int argc
, const char **argv
, const char *prefix
)
733 for (i
= 1; i
< argc
; i
++) {
734 const char *arg
= argv
[i
];
735 if (!strcmp(arg
, "--")) {
739 else if (arg
[0] == '-')
740 usage(reflog_exists_usage
);
747 if (argc
- start
!= 1)
748 usage(reflog_exists_usage
);
750 if (check_refname_format(argv
[start
], REFNAME_ALLOW_ONELEVEL
))
751 die("invalid ref format: %s", argv
[start
]);
752 return !reflog_exists(argv
[start
]);
759 static const char reflog_usage
[] =
760 "git reflog [ show | expire | delete | exists ]";
762 int cmd_reflog(int argc
, const char **argv
, const char *prefix
)
764 if (argc
> 1 && !strcmp(argv
[1], "-h"))
767 /* With no command, we default to showing it. */
768 if (argc
< 2 || *argv
[1] == '-')
769 return cmd_log_reflog(argc
, argv
, prefix
);
771 if (!strcmp(argv
[1], "show"))
772 return cmd_log_reflog(argc
- 1, argv
+ 1, prefix
);
774 if (!strcmp(argv
[1], "expire"))
775 return cmd_reflog_expire(argc
- 1, argv
+ 1, prefix
);
777 if (!strcmp(argv
[1], "delete"))
778 return cmd_reflog_delete(argc
- 1, argv
+ 1, prefix
);
780 if (!strcmp(argv
[1], "exists"))
781 return cmd_reflog_exists(argc
- 1, argv
+ 1, prefix
);
783 return cmd_log_reflog(argc
, argv
, prefix
);