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 {
33 timestamp_t expire_total;
34 timestamp_t expire_unreachable;
38 struct expire_reflog_policy_cb {
43 } unreachable_expire_kind;
44 struct commit_list *mark_list;
45 unsigned long mark_limit;
46 struct cmd_reflog_expire_cb cmd;
47 struct commit *tip_commit;
48 struct commit_list *tips;
51 struct collected_reflog {
53 char reflog[FLEX_ARRAY];
56 struct collect_reflog_cb {
57 struct collected_reflog **e;
63 /* Remember to update object flag allocation in object.h */
64 #define INCOMPLETE (1u<<10)
65 #define STUDYING (1u<<11)
66 #define REACHABLE (1u<<12)
68 static int tree_is_complete(const struct object_id *oid)
70 struct tree_desc desc;
71 struct name_entry entry;
75 tree = lookup_tree(the_repository, oid);
78 if (tree->object.flags & SEEN)
80 if (tree->object.flags & INCOMPLETE)
84 enum object_type type;
86 void *data = read_object_file(oid, &type, &size);
88 tree->object.flags |= INCOMPLETE;
94 init_tree_desc(&desc, tree->buffer, tree->size);
96 while (tree_entry(&desc, &entry)) {
97 if (!has_object_file(&entry.oid) ||
98 (S_ISDIR(entry.mode) && !tree_is_complete(&entry.oid))) {
99 tree->object.flags |= INCOMPLETE;
103 free_tree_buffer(tree);
106 tree->object.flags |= SEEN;
110 static int commit_is_complete(struct commit *commit)
112 struct object_array study;
113 struct object_array found;
114 int is_incomplete = 0;
118 if (commit->object.flags & SEEN)
120 if (commit->object.flags & INCOMPLETE)
123 * Find all commits that are reachable and are not marked as
124 * SEEN. Then make sure the trees and blobs contained are
125 * complete. After that, mark these commits also as SEEN.
126 * If some of the objects that are needed to complete this
127 * commit are missing, mark this commit as INCOMPLETE.
129 memset(&study, 0, sizeof(study));
130 memset(&found, 0, sizeof(found));
131 add_object_array(&commit->object, NULL, &study);
132 add_object_array(&commit->object, NULL, &found);
133 commit->object.flags |= STUDYING;
136 struct commit_list *parent;
138 c = (struct commit *)object_array_pop(&study);
139 if (!c->object.parsed && !parse_object(the_repository, &c->object.oid))
140 c->object.flags |= INCOMPLETE;
142 if (c->object.flags & INCOMPLETE) {
146 else if (c->object.flags & SEEN)
148 for (parent = c->parents; parent; parent = parent->next) {
149 struct commit *p = parent->item;
150 if (p->object.flags & STUDYING)
152 p->object.flags |= STUDYING;
153 add_object_array(&p->object, NULL, &study);
154 add_object_array(&p->object, NULL, &found);
157 if (!is_incomplete) {
159 * make sure all commits in "found" array have all the
162 for (i = 0; i < found.nr; i++) {
164 (struct commit *)found.objects[i].item;
165 if (!tree_is_complete(get_commit_tree_oid(c))) {
167 c->object.flags |= INCOMPLETE;
170 if (!is_incomplete) {
171 /* mark all found commits as complete, iow SEEN */
172 for (i = 0; i < found.nr; i++)
173 found.objects[i].item->flags |= SEEN;
176 /* clear flags from the objects we traversed */
177 for (i = 0; i < found.nr; i++)
178 found.objects[i].item->flags &= ~STUDYING;
180 commit->object.flags |= INCOMPLETE;
183 * If we come here, we have (1) traversed the ancestry chain
184 * from the "commit" until we reach SEEN commits (which are
185 * known to be complete), and (2) made sure that the commits
186 * encountered during the above traversal refer to trees that
187 * are complete. Which means that we know *all* the commits
188 * we have seen during this process are complete.
190 for (i = 0; i < found.nr; i++)
191 found.objects[i].item->flags |= SEEN;
193 /* free object arrays */
194 object_array_clear(&study);
195 object_array_clear(&found);
196 return !is_incomplete;
199 static int keep_entry(struct commit **it, struct object_id *oid)
201 struct commit *commit;
203 if (is_null_oid(oid))
205 commit = lookup_commit_reference_gently(the_repository, oid, 1);
210 * Make sure everything in this commit exists.
212 * We have walked all the objects reachable from the refs
213 * and cache earlier. The commits reachable by this commit
214 * must meet SEEN commits -- and then we should mark them as
217 if (!commit_is_complete(commit))
224 * Starting from commits in the cb->mark_list, mark commits that are
225 * reachable from them. Stop the traversal at commits older than
226 * the expire_limit and queue them back, so that the caller can call
227 * us again to restart the traversal with longer expire_limit.
229 static void mark_reachable(struct expire_reflog_policy_cb *cb)
231 struct commit_list *pending;
232 timestamp_t expire_limit = cb->mark_limit;
233 struct commit_list *leftover = NULL;
235 for (pending = cb->mark_list; pending; pending = pending->next)
236 pending->item->object.flags &= ~REACHABLE;
238 pending = cb->mark_list;
240 struct commit_list *parent;
241 struct commit *commit = pop_commit(&pending);
242 if (commit->object.flags & REACHABLE)
244 if (parse_commit(commit))
246 commit->object.flags |= REACHABLE;
247 if (commit->date < expire_limit) {
248 commit_list_insert(commit, &leftover);
251 commit->object.flags |= REACHABLE;
252 parent = commit->parents;
254 commit = parent->item;
255 parent = parent->next;
256 if (commit->object.flags & REACHABLE)
258 commit_list_insert(commit, &pending);
261 cb->mark_list = leftover;
264 static int unreachable(struct expire_reflog_policy_cb *cb, struct commit *commit, struct object_id *oid)
267 * We may or may not have the commit yet - if not, look it
268 * up using the supplied sha1.
271 if (is_null_oid(oid))
274 commit = lookup_commit_reference_gently(the_repository, oid,
277 /* Not a commit -- keep it */
282 /* Reachable from the current ref? Don't prune. */
283 if (commit->object.flags & REACHABLE)
286 if (cb->mark_list && cb->mark_limit) {
287 cb->mark_limit = 0; /* dig down to the root */
291 return !(commit->object.flags & REACHABLE);
295 * Return true iff the specified reflog entry should be expired.
297 static int should_expire_reflog_ent(struct object_id *ooid, struct object_id *noid,
298 const char *email, timestamp_t timestamp, int tz,
299 const char *message, void *cb_data)
301 struct expire_reflog_policy_cb *cb = cb_data;
302 struct commit *old_commit, *new_commit;
304 if (timestamp < cb->cmd.expire_total)
307 old_commit = new_commit = NULL;
308 if (cb->cmd.stalefix &&
309 (!keep_entry(&old_commit, ooid) || !keep_entry(&new_commit, noid)))
312 if (timestamp < cb->cmd.expire_unreachable) {
313 if (cb->unreachable_expire_kind == UE_ALWAYS)
315 if (unreachable(cb, old_commit, ooid) || unreachable(cb, new_commit, noid))
319 if (cb->cmd.recno && --(cb->cmd.recno) == 0)
325 static int push_tip_to_list(const char *refname, const struct object_id *oid,
326 int flags, void *cb_data)
328 struct commit_list **list = cb_data;
329 struct commit *tip_commit;
330 if (flags & REF_ISSYMREF)
332 tip_commit = lookup_commit_reference_gently(the_repository, oid, 1);
335 commit_list_insert(tip_commit, list);
339 static int is_head(const char *refname)
341 switch (ref_type(refname)) {
342 case REF_TYPE_OTHER_PSEUDOREF:
343 case REF_TYPE_MAIN_PSEUDOREF:
344 if (parse_worktree_ref(refname, NULL, NULL, &refname))
345 BUG("not a worktree ref: %s", refname);
350 return !strcmp(refname, "HEAD");
353 static void reflog_expiry_prepare(const char *refname,
354 const struct object_id *oid,
357 struct expire_reflog_policy_cb *cb = cb_data;
359 if (!cb->cmd.expire_unreachable || is_head(refname)) {
360 cb->tip_commit = NULL;
361 cb->unreachable_expire_kind = UE_HEAD;
363 cb->tip_commit = lookup_commit_reference_gently(the_repository,
366 cb->unreachable_expire_kind = UE_ALWAYS;
368 cb->unreachable_expire_kind = UE_NORMAL;
371 if (cb->cmd.expire_unreachable <= cb->cmd.expire_total)
372 cb->unreachable_expire_kind = UE_ALWAYS;
374 cb->mark_list = NULL;
376 if (cb->unreachable_expire_kind != UE_ALWAYS) {
377 if (cb->unreachable_expire_kind == UE_HEAD) {
378 struct commit_list *elem;
380 for_each_ref(push_tip_to_list, &cb->tips);
381 for (elem = cb->tips; elem; elem = elem->next)
382 commit_list_insert(elem->item, &cb->mark_list);
384 commit_list_insert(cb->tip_commit, &cb->mark_list);
386 cb->mark_limit = cb->cmd.expire_total;
391 static void reflog_expiry_cleanup(void *cb_data)
393 struct expire_reflog_policy_cb *cb = cb_data;
395 if (cb->unreachable_expire_kind != UE_ALWAYS) {
396 if (cb->unreachable_expire_kind == UE_HEAD) {
397 struct commit_list *elem;
398 for (elem = cb->tips; elem; elem = elem->next)
399 clear_commit_marks(elem->item, REACHABLE);
400 free_commit_list(cb->tips);
402 clear_commit_marks(cb->tip_commit, REACHABLE);
407 static int collect_reflog(const char *ref, const struct object_id *oid, int unused, void *cb_data)
409 struct collected_reflog *e;
410 struct collect_reflog_cb *cb = cb_data;
411 struct strbuf newref = STRBUF_INIT;
414 * Avoid collecting the same shared ref multiple times because
415 * they are available via all worktrees.
417 if (!cb->wt->is_current && ref_type(ref) == REF_TYPE_NORMAL)
420 strbuf_worktree_ref(cb->wt, &newref, ref);
421 FLEX_ALLOC_STR(e, reflog, newref.buf);
422 strbuf_release(&newref);
424 oidcpy(&e->oid, oid);
425 ALLOC_GROW(cb->e, cb->nr + 1, cb->alloc);
430 static struct reflog_expire_cfg {
431 struct reflog_expire_cfg *next;
432 timestamp_t expire_total;
433 timestamp_t expire_unreachable;
434 char pattern[FLEX_ARRAY];
435 } *reflog_expire_cfg, **reflog_expire_cfg_tail;
437 static struct reflog_expire_cfg *find_cfg_ent(const char *pattern, size_t len)
439 struct reflog_expire_cfg *ent;
441 if (!reflog_expire_cfg_tail)
442 reflog_expire_cfg_tail = &reflog_expire_cfg;
444 for (ent = reflog_expire_cfg; ent; ent = ent->next)
445 if (!strncmp(ent->pattern, pattern, len) &&
446 ent->pattern[len] == '\0')
449 FLEX_ALLOC_MEM(ent, pattern, pattern, len);
450 *reflog_expire_cfg_tail = ent;
451 reflog_expire_cfg_tail = &(ent->next);
455 /* expiry timer slot */
456 #define EXPIRE_TOTAL 01
457 #define EXPIRE_UNREACH 02
459 static int reflog_expire_config(const char *var, const char *value, void *cb)
461 const char *pattern, *key;
465 struct reflog_expire_cfg *ent;
467 if (parse_config_key(var, "gc", &pattern, &pattern_len, &key) < 0)
468 return git_default_config(var, value, cb);
470 if (!strcmp(key, "reflogexpire")) {
472 if (git_config_expiry_date(&expire, var, value))
474 } else if (!strcmp(key, "reflogexpireunreachable")) {
475 slot = EXPIRE_UNREACH;
476 if (git_config_expiry_date(&expire, var, value))
479 return git_default_config(var, value, cb);
484 default_reflog_expire = expire;
487 default_reflog_expire_unreachable = expire;
493 ent = find_cfg_ent(pattern, pattern_len);
498 ent->expire_total = expire;
501 ent->expire_unreachable = expire;
507 static void set_reflog_expiry_param(struct cmd_reflog_expire_cb *cb, int slot, const char *ref)
509 struct reflog_expire_cfg *ent;
511 if (slot == (EXPIRE_TOTAL|EXPIRE_UNREACH))
512 return; /* both given explicitly -- nothing to tweak */
514 for (ent = reflog_expire_cfg; ent; ent = ent->next) {
515 if (!wildmatch(ent->pattern, ref, 0)) {
516 if (!(slot & EXPIRE_TOTAL))
517 cb->expire_total = ent->expire_total;
518 if (!(slot & EXPIRE_UNREACH))
519 cb->expire_unreachable = ent->expire_unreachable;
525 * If unconfigured, make stash never expire
527 if (!strcmp(ref, "refs/stash")) {
528 if (!(slot & EXPIRE_TOTAL))
529 cb->expire_total = 0;
530 if (!(slot & EXPIRE_UNREACH))
531 cb->expire_unreachable = 0;
535 /* Nothing matched -- use the default value */
536 if (!(slot & EXPIRE_TOTAL))
537 cb->expire_total = default_reflog_expire;
538 if (!(slot & EXPIRE_UNREACH))
539 cb->expire_unreachable = default_reflog_expire_unreachable;
542 static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
544 struct expire_reflog_policy_cb cb;
545 timestamp_t now = time(NULL);
546 int i, status, do_all, all_worktrees = 1;
547 int explicit_expiry = 0;
548 unsigned int flags = 0;
550 default_reflog_expire_unreachable = now - 30 * 24 * 3600;
551 default_reflog_expire = now - 90 * 24 * 3600;
552 git_config(reflog_expire_config, NULL);
554 save_commit_buffer = 0;
556 memset(&cb, 0, sizeof(cb));
558 cb.cmd.expire_total = default_reflog_expire;
559 cb.cmd.expire_unreachable = default_reflog_expire_unreachable;
561 for (i = 1; i < argc; i++) {
562 const char *arg = argv[i];
564 if (!strcmp(arg, "--dry-run") || !strcmp(arg, "-n"))
565 flags |= EXPIRE_REFLOGS_DRY_RUN;
566 else if (skip_prefix(arg, "--expire=", &arg)) {
567 if (parse_expiry_date(arg, &cb.cmd.expire_total))
568 die(_("'%s' is not a valid timestamp"), arg);
569 explicit_expiry |= EXPIRE_TOTAL;
571 else if (skip_prefix(arg, "--expire-unreachable=", &arg)) {
572 if (parse_expiry_date(arg, &cb.cmd.expire_unreachable))
573 die(_("'%s' is not a valid timestamp"), arg);
574 explicit_expiry |= EXPIRE_UNREACH;
576 else if (!strcmp(arg, "--stale-fix"))
578 else if (!strcmp(arg, "--rewrite"))
579 flags |= EXPIRE_REFLOGS_REWRITE;
580 else if (!strcmp(arg, "--updateref"))
581 flags |= EXPIRE_REFLOGS_UPDATE_REF;
582 else if (!strcmp(arg, "--all"))
584 else if (!strcmp(arg, "--single-worktree"))
586 else if (!strcmp(arg, "--verbose"))
587 flags |= EXPIRE_REFLOGS_VERBOSE;
588 else if (!strcmp(arg, "--")) {
592 else if (arg[0] == '-')
593 usage(_(reflog_expire_usage));
599 * We can trust the commits and objects reachable from refs
600 * even in older repository. We cannot trust what's reachable
601 * from reflog if the repository was pruned with older git.
603 if (cb.cmd.stalefix) {
604 repo_init_revisions(the_repository, &cb.cmd.revs, prefix);
605 if (flags & EXPIRE_REFLOGS_VERBOSE)
606 printf(_("Marking reachable objects..."));
607 mark_reachable_objects(&cb.cmd.revs, 0, 0, NULL);
608 if (flags & EXPIRE_REFLOGS_VERBOSE)
613 struct collect_reflog_cb collected;
614 struct worktree **worktrees, **p;
617 memset(&collected, 0, sizeof(collected));
618 worktrees = get_worktrees(0);
619 for (p = worktrees; *p; p++) {
620 if (!all_worktrees && !(*p)->is_current)
623 refs_for_each_reflog(get_worktree_ref_store(*p),
624 collect_reflog, &collected);
626 free_worktrees(worktrees);
627 for (i = 0; i < collected.nr; i++) {
628 struct collected_reflog *e = collected.e[i];
629 set_reflog_expiry_param(&cb.cmd, explicit_expiry, e->reflog);
630 status |= reflog_expire(e->reflog, &e->oid, flags,
631 reflog_expiry_prepare,
632 should_expire_reflog_ent,
633 reflog_expiry_cleanup,
640 for (; i < argc; i++) {
642 struct object_id oid;
643 if (!dwim_log(argv[i], strlen(argv[i]), &oid, &ref)) {
644 status |= error(_("%s points nowhere!"), argv[i]);
647 set_reflog_expiry_param(&cb.cmd, explicit_expiry, ref);
648 status |= reflog_expire(ref, &oid, flags,
649 reflog_expiry_prepare,
650 should_expire_reflog_ent,
651 reflog_expiry_cleanup,
657 static int count_reflog_ent(struct object_id *ooid, struct object_id *noid,
658 const char *email, timestamp_t timestamp, int tz,
659 const char *message, void *cb_data)
661 struct expire_reflog_policy_cb *cb = cb_data;
662 if (!cb->cmd.expire_total || timestamp < cb->cmd.expire_total)
667 static int cmd_reflog_delete(int argc, const char **argv, const char *prefix)
669 struct expire_reflog_policy_cb cb;
671 unsigned int flags = 0;
673 memset(&cb, 0, sizeof(cb));
675 for (i = 1; i < argc; i++) {
676 const char *arg = argv[i];
677 if (!strcmp(arg, "--dry-run") || !strcmp(arg, "-n"))
678 flags |= EXPIRE_REFLOGS_DRY_RUN;
679 else if (!strcmp(arg, "--rewrite"))
680 flags |= EXPIRE_REFLOGS_REWRITE;
681 else if (!strcmp(arg, "--updateref"))
682 flags |= EXPIRE_REFLOGS_UPDATE_REF;
683 else if (!strcmp(arg, "--verbose"))
684 flags |= EXPIRE_REFLOGS_VERBOSE;
685 else if (!strcmp(arg, "--")) {
689 else if (arg[0] == '-')
690 usage(_(reflog_delete_usage));
696 return error(_("no reflog specified to delete"));
698 for ( ; i < argc; i++) {
699 const char *spec = strstr(argv[i], "@{");
700 struct object_id oid;
705 status |= error(_("not a reflog: %s"), argv[i]);
709 if (!dwim_log(argv[i], spec - argv[i], &oid, &ref)) {
710 status |= error(_("no reflog for '%s'"), argv[i]);
714 recno = strtoul(spec + 2, &ep, 10);
716 cb.cmd.recno = -recno;
717 for_each_reflog_ent(ref, count_reflog_ent, &cb);
719 cb.cmd.expire_total = approxidate(spec + 2);
720 for_each_reflog_ent(ref, count_reflog_ent, &cb);
721 cb.cmd.expire_total = 0;
724 status |= reflog_expire(ref, &oid, flags,
725 reflog_expiry_prepare,
726 should_expire_reflog_ent,
727 reflog_expiry_cleanup,
734 static int cmd_reflog_exists(int argc, const char **argv, const char *prefix)
738 for (i = 1; i < argc; i++) {
739 const char *arg = argv[i];
740 if (!strcmp(arg, "--")) {
744 else if (arg[0] == '-')
745 usage(_(reflog_exists_usage));
752 if (argc - start != 1)
753 usage(_(reflog_exists_usage));
755 if (check_refname_format(argv[start], REFNAME_ALLOW_ONELEVEL))
756 die(_("invalid ref format: %s"), argv[start]);
757 return !reflog_exists(argv[start]);
764 static const char reflog_usage[] =
765 N_("git reflog [ show | expire | delete | exists ]");
767 int cmd_reflog(int argc, const char **argv, const char *prefix)
769 if (argc > 1 && !strcmp(argv[1], "-h"))
770 usage(_(reflog_usage));
772 /* With no command, we default to showing it. */
773 if (argc < 2 || *argv[1] == '-')
774 return cmd_log_reflog(argc, argv, prefix);
776 if (!strcmp(argv[1], "show"))
777 return cmd_log_reflog(argc - 1, argv + 1, prefix);
779 if (!strcmp(argv[1], "expire"))
780 return cmd_reflog_expire(argc - 1, argv + 1, prefix);
782 if (!strcmp(argv[1], "delete"))
783 return cmd_reflog_delete(argc - 1, argv + 1, prefix);
785 if (!strcmp(argv[1], "exists"))
786 return cmd_reflog_exists(argc - 1, argv + 1, prefix);
788 return cmd_log_reflog(argc, argv, prefix);