4 #include "object-store.h"
5 #include "repository.h"
12 #include "reachable.h"
14 /* NEEDSWORK: switch to using parse_options */
15 static const char reflog_expire_usage[] =
16 "git reflog expire [--expire=<time>] [--expire-unreachable=<time>] [--rewrite] [--updateref] [--stale-fix] [--dry-run | -n] [--verbose] [--all] <refs>...";
17 static const char reflog_delete_usage[] =
18 "git reflog delete [--rewrite] [--updateref] [--dry-run | -n] [--verbose] <refs>...";
19 static const char reflog_exists_usage[] =
20 "git reflog exists <ref>";
22 static timestamp_t default_reflog_expire;
23 static timestamp_t default_reflog_expire_unreachable;
25 struct cmd_reflog_expire_cb {
28 timestamp_t expire_total;
29 timestamp_t expire_unreachable;
33 struct expire_reflog_policy_cb {
38 } unreachable_expire_kind;
39 struct commit_list *mark_list;
40 unsigned long mark_limit;
41 struct cmd_reflog_expire_cb cmd;
42 struct commit *tip_commit;
43 struct commit_list *tips;
46 struct collected_reflog {
48 char reflog[FLEX_ARRAY];
51 struct collect_reflog_cb {
52 struct collected_reflog **e;
57 /* Remember to update object flag allocation in object.h */
58 #define INCOMPLETE (1u<<10)
59 #define STUDYING (1u<<11)
60 #define REACHABLE (1u<<12)
62 static int tree_is_complete(const struct object_id *oid)
64 struct tree_desc desc;
65 struct name_entry entry;
69 tree = lookup_tree(the_repository, oid);
72 if (tree->object.flags & SEEN)
74 if (tree->object.flags & INCOMPLETE)
78 enum object_type type;
80 void *data = read_object_file(oid, &type, &size);
82 tree->object.flags |= INCOMPLETE;
88 init_tree_desc(&desc, tree->buffer, tree->size);
90 while (tree_entry(&desc, &entry)) {
91 if (!has_sha1_file(entry.oid->hash) ||
92 (S_ISDIR(entry.mode) && !tree_is_complete(entry.oid))) {
93 tree->object.flags |= INCOMPLETE;
97 free_tree_buffer(tree);
100 tree->object.flags |= SEEN;
104 static int commit_is_complete(struct commit *commit)
106 struct object_array study;
107 struct object_array found;
108 int is_incomplete = 0;
112 if (commit->object.flags & SEEN)
114 if (commit->object.flags & INCOMPLETE)
117 * Find all commits that are reachable and are not marked as
118 * SEEN. Then make sure the trees and blobs contained are
119 * complete. After that, mark these commits also as SEEN.
120 * If some of the objects that are needed to complete this
121 * commit are missing, mark this commit as INCOMPLETE.
123 memset(&study, 0, sizeof(study));
124 memset(&found, 0, sizeof(found));
125 add_object_array(&commit->object, NULL, &study);
126 add_object_array(&commit->object, NULL, &found);
127 commit->object.flags |= STUDYING;
130 struct commit_list *parent;
132 c = (struct commit *)object_array_pop(&study);
133 if (!c->object.parsed && !parse_object(the_repository, &c->object.oid))
134 c->object.flags |= INCOMPLETE;
136 if (c->object.flags & INCOMPLETE) {
140 else if (c->object.flags & SEEN)
142 for (parent = c->parents; parent; parent = parent->next) {
143 struct commit *p = parent->item;
144 if (p->object.flags & STUDYING)
146 p->object.flags |= STUDYING;
147 add_object_array(&p->object, NULL, &study);
148 add_object_array(&p->object, NULL, &found);
151 if (!is_incomplete) {
153 * make sure all commits in "found" array have all the
156 for (i = 0; i < found.nr; i++) {
158 (struct commit *)found.objects[i].item;
159 if (!tree_is_complete(get_commit_tree_oid(c))) {
161 c->object.flags |= INCOMPLETE;
164 if (!is_incomplete) {
165 /* mark all found commits as complete, iow SEEN */
166 for (i = 0; i < found.nr; i++)
167 found.objects[i].item->flags |= SEEN;
170 /* clear flags from the objects we traversed */
171 for (i = 0; i < found.nr; i++)
172 found.objects[i].item->flags &= ~STUDYING;
174 commit->object.flags |= INCOMPLETE;
177 * If we come here, we have (1) traversed the ancestry chain
178 * from the "commit" until we reach SEEN commits (which are
179 * known to be complete), and (2) made sure that the commits
180 * encountered during the above traversal refer to trees that
181 * are complete. Which means that we know *all* the commits
182 * we have seen during this process are complete.
184 for (i = 0; i < found.nr; i++)
185 found.objects[i].item->flags |= SEEN;
187 /* free object arrays */
188 object_array_clear(&study);
189 object_array_clear(&found);
190 return !is_incomplete;
193 static int keep_entry(struct commit **it, struct object_id *oid)
195 struct commit *commit;
197 if (is_null_oid(oid))
199 commit = lookup_commit_reference_gently(oid, 1);
204 * Make sure everything in this commit exists.
206 * We have walked all the objects reachable from the refs
207 * and cache earlier. The commits reachable by this commit
208 * must meet SEEN commits -- and then we should mark them as
211 if (!commit_is_complete(commit))
218 * Starting from commits in the cb->mark_list, mark commits that are
219 * reachable from them. Stop the traversal at commits older than
220 * the expire_limit and queue them back, so that the caller can call
221 * us again to restart the traversal with longer expire_limit.
223 static void mark_reachable(struct expire_reflog_policy_cb *cb)
225 struct commit_list *pending;
226 timestamp_t expire_limit = cb->mark_limit;
227 struct commit_list *leftover = NULL;
229 for (pending = cb->mark_list; pending; pending = pending->next)
230 pending->item->object.flags &= ~REACHABLE;
232 pending = cb->mark_list;
234 struct commit_list *parent;
235 struct commit *commit = pop_commit(&pending);
236 if (commit->object.flags & REACHABLE)
238 if (parse_commit(commit))
240 commit->object.flags |= REACHABLE;
241 if (commit->date < expire_limit) {
242 commit_list_insert(commit, &leftover);
245 commit->object.flags |= REACHABLE;
246 parent = commit->parents;
248 commit = parent->item;
249 parent = parent->next;
250 if (commit->object.flags & REACHABLE)
252 commit_list_insert(commit, &pending);
255 cb->mark_list = leftover;
258 static int unreachable(struct expire_reflog_policy_cb *cb, struct commit *commit, struct object_id *oid)
261 * We may or may not have the commit yet - if not, look it
262 * up using the supplied sha1.
265 if (is_null_oid(oid))
268 commit = lookup_commit_reference_gently(oid, 1);
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 if (cb->unreachable_expire_kind == UE_ALWAYS)
308 if (unreachable(cb, old_commit, ooid) || unreachable(cb, new_commit, noid))
312 if (cb->cmd.recno && --(cb->cmd.recno) == 0)
318 static int push_tip_to_list(const char *refname, const struct object_id *oid,
319 int flags, void *cb_data)
321 struct commit_list **list = cb_data;
322 struct commit *tip_commit;
323 if (flags & REF_ISSYMREF)
325 tip_commit = lookup_commit_reference_gently(oid, 1);
328 commit_list_insert(tip_commit, list);
332 static void reflog_expiry_prepare(const char *refname,
333 const struct object_id *oid,
336 struct expire_reflog_policy_cb *cb = cb_data;
338 if (!cb->cmd.expire_unreachable || !strcmp(refname, "HEAD")) {
339 cb->tip_commit = NULL;
340 cb->unreachable_expire_kind = UE_HEAD;
342 cb->tip_commit = lookup_commit_reference_gently(oid, 1);
344 cb->unreachable_expire_kind = UE_ALWAYS;
346 cb->unreachable_expire_kind = UE_NORMAL;
349 if (cb->cmd.expire_unreachable <= cb->cmd.expire_total)
350 cb->unreachable_expire_kind = UE_ALWAYS;
352 cb->mark_list = NULL;
354 if (cb->unreachable_expire_kind != UE_ALWAYS) {
355 if (cb->unreachable_expire_kind == UE_HEAD) {
356 struct commit_list *elem;
358 for_each_ref(push_tip_to_list, &cb->tips);
359 for (elem = cb->tips; elem; elem = elem->next)
360 commit_list_insert(elem->item, &cb->mark_list);
362 commit_list_insert(cb->tip_commit, &cb->mark_list);
364 cb->mark_limit = cb->cmd.expire_total;
369 static void reflog_expiry_cleanup(void *cb_data)
371 struct expire_reflog_policy_cb *cb = cb_data;
373 if (cb->unreachable_expire_kind != UE_ALWAYS) {
374 if (cb->unreachable_expire_kind == UE_HEAD) {
375 struct commit_list *elem;
376 for (elem = cb->tips; elem; elem = elem->next)
377 clear_commit_marks(elem->item, REACHABLE);
378 free_commit_list(cb->tips);
380 clear_commit_marks(cb->tip_commit, REACHABLE);
385 static int collect_reflog(const char *ref, const struct object_id *oid, int unused, void *cb_data)
387 struct collected_reflog *e;
388 struct collect_reflog_cb *cb = cb_data;
390 FLEX_ALLOC_STR(e, reflog, ref);
391 oidcpy(&e->oid, oid);
392 ALLOC_GROW(cb->e, cb->nr + 1, cb->alloc);
397 static struct reflog_expire_cfg {
398 struct reflog_expire_cfg *next;
399 timestamp_t expire_total;
400 timestamp_t expire_unreachable;
401 char pattern[FLEX_ARRAY];
402 } *reflog_expire_cfg, **reflog_expire_cfg_tail;
404 static struct reflog_expire_cfg *find_cfg_ent(const char *pattern, size_t len)
406 struct reflog_expire_cfg *ent;
408 if (!reflog_expire_cfg_tail)
409 reflog_expire_cfg_tail = &reflog_expire_cfg;
411 for (ent = reflog_expire_cfg; ent; ent = ent->next)
412 if (!strncmp(ent->pattern, pattern, len) &&
413 ent->pattern[len] == '\0')
416 FLEX_ALLOC_MEM(ent, pattern, pattern, len);
417 *reflog_expire_cfg_tail = ent;
418 reflog_expire_cfg_tail = &(ent->next);
422 /* expiry timer slot */
423 #define EXPIRE_TOTAL 01
424 #define EXPIRE_UNREACH 02
426 static int reflog_expire_config(const char *var, const char *value, void *cb)
428 const char *pattern, *key;
432 struct reflog_expire_cfg *ent;
434 if (parse_config_key(var, "gc", &pattern, &pattern_len, &key) < 0)
435 return git_default_config(var, value, cb);
437 if (!strcmp(key, "reflogexpire")) {
439 if (git_config_expiry_date(&expire, var, value))
441 } else if (!strcmp(key, "reflogexpireunreachable")) {
442 slot = EXPIRE_UNREACH;
443 if (git_config_expiry_date(&expire, var, value))
446 return git_default_config(var, value, cb);
451 default_reflog_expire = expire;
454 default_reflog_expire_unreachable = expire;
460 ent = find_cfg_ent(pattern, pattern_len);
465 ent->expire_total = expire;
468 ent->expire_unreachable = expire;
474 static void set_reflog_expiry_param(struct cmd_reflog_expire_cb *cb, int slot, const char *ref)
476 struct reflog_expire_cfg *ent;
478 if (slot == (EXPIRE_TOTAL|EXPIRE_UNREACH))
479 return; /* both given explicitly -- nothing to tweak */
481 for (ent = reflog_expire_cfg; ent; ent = ent->next) {
482 if (!wildmatch(ent->pattern, ref, 0)) {
483 if (!(slot & EXPIRE_TOTAL))
484 cb->expire_total = ent->expire_total;
485 if (!(slot & EXPIRE_UNREACH))
486 cb->expire_unreachable = ent->expire_unreachable;
492 * If unconfigured, make stash never expire
494 if (!strcmp(ref, "refs/stash")) {
495 if (!(slot & EXPIRE_TOTAL))
496 cb->expire_total = 0;
497 if (!(slot & EXPIRE_UNREACH))
498 cb->expire_unreachable = 0;
502 /* Nothing matched -- use the default value */
503 if (!(slot & EXPIRE_TOTAL))
504 cb->expire_total = default_reflog_expire;
505 if (!(slot & EXPIRE_UNREACH))
506 cb->expire_unreachable = default_reflog_expire_unreachable;
509 static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
511 struct expire_reflog_policy_cb cb;
512 timestamp_t now = time(NULL);
513 int i, status, do_all;
514 int explicit_expiry = 0;
515 unsigned int flags = 0;
517 default_reflog_expire_unreachable = now - 30 * 24 * 3600;
518 default_reflog_expire = now - 90 * 24 * 3600;
519 git_config(reflog_expire_config, NULL);
521 save_commit_buffer = 0;
523 memset(&cb, 0, sizeof(cb));
525 cb.cmd.expire_total = default_reflog_expire;
526 cb.cmd.expire_unreachable = default_reflog_expire_unreachable;
528 for (i = 1; i < argc; i++) {
529 const char *arg = argv[i];
530 if (!strcmp(arg, "--dry-run") || !strcmp(arg, "-n"))
531 flags |= EXPIRE_REFLOGS_DRY_RUN;
532 else if (starts_with(arg, "--expire=")) {
533 if (parse_expiry_date(arg + 9, &cb.cmd.expire_total))
534 die(_("'%s' is not a valid timestamp"), arg);
535 explicit_expiry |= EXPIRE_TOTAL;
537 else if (starts_with(arg, "--expire-unreachable=")) {
538 if (parse_expiry_date(arg + 21, &cb.cmd.expire_unreachable))
539 die(_("'%s' is not a valid timestamp"), arg);
540 explicit_expiry |= EXPIRE_UNREACH;
542 else if (!strcmp(arg, "--stale-fix"))
544 else if (!strcmp(arg, "--rewrite"))
545 flags |= EXPIRE_REFLOGS_REWRITE;
546 else if (!strcmp(arg, "--updateref"))
547 flags |= EXPIRE_REFLOGS_UPDATE_REF;
548 else if (!strcmp(arg, "--all"))
550 else if (!strcmp(arg, "--verbose"))
551 flags |= EXPIRE_REFLOGS_VERBOSE;
552 else if (!strcmp(arg, "--")) {
556 else if (arg[0] == '-')
557 usage(reflog_expire_usage);
563 * We can trust the commits and objects reachable from refs
564 * even in older repository. We cannot trust what's reachable
565 * from reflog if the repository was pruned with older git.
567 if (cb.cmd.stalefix) {
568 init_revisions(&cb.cmd.revs, prefix);
569 if (flags & EXPIRE_REFLOGS_VERBOSE)
570 printf("Marking reachable objects...");
571 mark_reachable_objects(&cb.cmd.revs, 0, 0, NULL);
572 if (flags & EXPIRE_REFLOGS_VERBOSE)
577 struct collect_reflog_cb collected;
580 memset(&collected, 0, sizeof(collected));
581 for_each_reflog(collect_reflog, &collected);
582 for (i = 0; i < collected.nr; i++) {
583 struct collected_reflog *e = collected.e[i];
584 set_reflog_expiry_param(&cb.cmd, explicit_expiry, e->reflog);
585 status |= reflog_expire(e->reflog, &e->oid, flags,
586 reflog_expiry_prepare,
587 should_expire_reflog_ent,
588 reflog_expiry_cleanup,
595 for (; i < argc; i++) {
597 struct object_id oid;
598 if (!dwim_log(argv[i], strlen(argv[i]), &oid, &ref)) {
599 status |= error("%s points nowhere!", argv[i]);
602 set_reflog_expiry_param(&cb.cmd, explicit_expiry, ref);
603 status |= reflog_expire(ref, &oid, flags,
604 reflog_expiry_prepare,
605 should_expire_reflog_ent,
606 reflog_expiry_cleanup,
612 static int count_reflog_ent(struct object_id *ooid, struct object_id *noid,
613 const char *email, timestamp_t timestamp, int tz,
614 const char *message, void *cb_data)
616 struct expire_reflog_policy_cb *cb = cb_data;
617 if (!cb->cmd.expire_total || timestamp < cb->cmd.expire_total)
622 static int cmd_reflog_delete(int argc, const char **argv, const char *prefix)
624 struct expire_reflog_policy_cb cb;
626 unsigned int flags = 0;
628 memset(&cb, 0, sizeof(cb));
630 for (i = 1; i < argc; i++) {
631 const char *arg = argv[i];
632 if (!strcmp(arg, "--dry-run") || !strcmp(arg, "-n"))
633 flags |= EXPIRE_REFLOGS_DRY_RUN;
634 else if (!strcmp(arg, "--rewrite"))
635 flags |= EXPIRE_REFLOGS_REWRITE;
636 else if (!strcmp(arg, "--updateref"))
637 flags |= EXPIRE_REFLOGS_UPDATE_REF;
638 else if (!strcmp(arg, "--verbose"))
639 flags |= EXPIRE_REFLOGS_VERBOSE;
640 else if (!strcmp(arg, "--")) {
644 else if (arg[0] == '-')
645 usage(reflog_delete_usage);
651 return error("Nothing to delete?");
653 for ( ; i < argc; i++) {
654 const char *spec = strstr(argv[i], "@{");
655 struct object_id oid;
660 status |= error("Not a reflog: %s", argv[i]);
664 if (!dwim_log(argv[i], spec - argv[i], &oid, &ref)) {
665 status |= error("no reflog for '%s'", argv[i]);
669 recno = strtoul(spec + 2, &ep, 10);
671 cb.cmd.recno = -recno;
672 for_each_reflog_ent(ref, count_reflog_ent, &cb);
674 cb.cmd.expire_total = approxidate(spec + 2);
675 for_each_reflog_ent(ref, count_reflog_ent, &cb);
676 cb.cmd.expire_total = 0;
679 status |= reflog_expire(ref, &oid, flags,
680 reflog_expiry_prepare,
681 should_expire_reflog_ent,
682 reflog_expiry_cleanup,
689 static int cmd_reflog_exists(int argc, const char **argv, const char *prefix)
693 for (i = 1; i < argc; i++) {
694 const char *arg = argv[i];
695 if (!strcmp(arg, "--")) {
699 else if (arg[0] == '-')
700 usage(reflog_exists_usage);
707 if (argc - start != 1)
708 usage(reflog_exists_usage);
710 if (check_refname_format(argv[start], REFNAME_ALLOW_ONELEVEL))
711 die("invalid ref format: %s", argv[start]);
712 return !reflog_exists(argv[start]);
719 static const char reflog_usage[] =
720 "git reflog [ show | expire | delete | exists ]";
722 int cmd_reflog(int argc, const char **argv, const char *prefix)
724 if (argc > 1 && !strcmp(argv[1], "-h"))
727 /* With no command, we default to showing it. */
728 if (argc < 2 || *argv[1] == '-')
729 return cmd_log_reflog(argc, argv, prefix);
731 if (!strcmp(argv[1], "show"))
732 return cmd_log_reflog(argc - 1, argv + 1, prefix);
734 if (!strcmp(argv[1], "expire"))
735 return cmd_reflog_expire(argc - 1, argv + 1, prefix);
737 if (!strcmp(argv[1], "delete"))
738 return cmd_reflog_delete(argc - 1, argv + 1, prefix);
740 if (!strcmp(argv[1], "exists"))
741 return cmd_reflog_exists(argc - 1, argv + 1, prefix);
743 return cmd_log_reflog(argc, argv, prefix);