11 /* NEEDSWORK: switch to using parse_options */
12 static const char reflog_expire_usage[] =
13 "git reflog expire [--expire=<time>] [--expire-unreachable=<time>] [--rewrite] [--updateref] [--stale-fix] [--dry-run | -n] [--verbose] [--all] <refs>...";
14 static const char reflog_delete_usage[] =
15 "git reflog delete [--rewrite] [--updateref] [--dry-run | -n] [--verbose] <refs>...";
17 static unsigned long default_reflog_expire;
18 static unsigned long default_reflog_expire_unreachable;
20 struct cmd_reflog_expire_cb {
23 unsigned long expire_total;
24 unsigned long expire_unreachable;
28 struct expire_reflog_policy_cb {
33 } unreachable_expire_kind;
34 struct commit_list *mark_list;
35 unsigned long mark_limit;
36 struct cmd_reflog_expire_cb cmd;
37 struct commit *tip_commit;
38 struct commit_list *tips;
41 struct collected_reflog {
42 unsigned char sha1[20];
43 char reflog[FLEX_ARRAY];
46 struct collect_reflog_cb {
47 struct collected_reflog **e;
52 #define INCOMPLETE (1u<<10)
53 #define STUDYING (1u<<11)
54 #define REACHABLE (1u<<12)
56 static int tree_is_complete(const unsigned char *sha1)
58 struct tree_desc desc;
59 struct name_entry entry;
63 tree = lookup_tree(sha1);
66 if (tree->object.flags & SEEN)
68 if (tree->object.flags & INCOMPLETE)
72 enum object_type type;
74 void *data = read_sha1_file(sha1, &type, &size);
76 tree->object.flags |= INCOMPLETE;
82 init_tree_desc(&desc, tree->buffer, tree->size);
84 while (tree_entry(&desc, &entry)) {
85 if (!has_sha1_file(entry.sha1) ||
86 (S_ISDIR(entry.mode) && !tree_is_complete(entry.sha1))) {
87 tree->object.flags |= INCOMPLETE;
91 free_tree_buffer(tree);
94 tree->object.flags |= SEEN;
98 static int commit_is_complete(struct commit *commit)
100 struct object_array study;
101 struct object_array found;
102 int is_incomplete = 0;
106 if (commit->object.flags & SEEN)
108 if (commit->object.flags & INCOMPLETE)
111 * Find all commits that are reachable and are not marked as
112 * SEEN. Then make sure the trees and blobs contained are
113 * complete. After that, mark these commits also as SEEN.
114 * If some of the objects that are needed to complete this
115 * commit are missing, mark this commit as INCOMPLETE.
117 memset(&study, 0, sizeof(study));
118 memset(&found, 0, sizeof(found));
119 add_object_array(&commit->object, NULL, &study);
120 add_object_array(&commit->object, NULL, &found);
121 commit->object.flags |= STUDYING;
124 struct commit_list *parent;
126 c = (struct commit *)study.objects[--study.nr].item;
127 if (!c->object.parsed && !parse_object(c->object.sha1))
128 c->object.flags |= INCOMPLETE;
130 if (c->object.flags & INCOMPLETE) {
134 else if (c->object.flags & SEEN)
136 for (parent = c->parents; parent; parent = parent->next) {
137 struct commit *p = parent->item;
138 if (p->object.flags & STUDYING)
140 p->object.flags |= STUDYING;
141 add_object_array(&p->object, NULL, &study);
142 add_object_array(&p->object, NULL, &found);
145 if (!is_incomplete) {
147 * make sure all commits in "found" array have all the
150 for (i = 0; i < found.nr; i++) {
152 (struct commit *)found.objects[i].item;
153 if (!tree_is_complete(c->tree->object.sha1)) {
155 c->object.flags |= INCOMPLETE;
158 if (!is_incomplete) {
159 /* mark all found commits as complete, iow SEEN */
160 for (i = 0; i < found.nr; i++)
161 found.objects[i].item->flags |= SEEN;
164 /* clear flags from the objects we traversed */
165 for (i = 0; i < found.nr; i++)
166 found.objects[i].item->flags &= ~STUDYING;
168 commit->object.flags |= INCOMPLETE;
171 * If we come here, we have (1) traversed the ancestry chain
172 * from the "commit" until we reach SEEN commits (which are
173 * known to be complete), and (2) made sure that the commits
174 * encountered during the above traversal refer to trees that
175 * are complete. Which means that we know *all* the commits
176 * we have seen during this process are complete.
178 for (i = 0; i < found.nr; i++)
179 found.objects[i].item->flags |= SEEN;
181 /* free object arrays */
184 return !is_incomplete;
187 static int keep_entry(struct commit **it, unsigned char *sha1)
189 struct commit *commit;
191 if (is_null_sha1(sha1))
193 commit = lookup_commit_reference_gently(sha1, 1);
198 * Make sure everything in this commit exists.
200 * We have walked all the objects reachable from the refs
201 * and cache earlier. The commits reachable by this commit
202 * must meet SEEN commits -- and then we should mark them as
205 if (!commit_is_complete(commit))
212 * Starting from commits in the cb->mark_list, mark commits that are
213 * reachable from them. Stop the traversal at commits older than
214 * the expire_limit and queue them back, so that the caller can call
215 * us again to restart the traversal with longer expire_limit.
217 static void mark_reachable(struct expire_reflog_policy_cb *cb)
219 struct commit *commit;
220 struct commit_list *pending;
221 unsigned long expire_limit = cb->mark_limit;
222 struct commit_list *leftover = NULL;
224 for (pending = cb->mark_list; pending; pending = pending->next)
225 pending->item->object.flags &= ~REACHABLE;
227 pending = cb->mark_list;
229 struct commit_list *entry = pending;
230 struct commit_list *parent;
231 pending = entry->next;
232 commit = entry->item;
234 if (commit->object.flags & REACHABLE)
236 if (parse_commit(commit))
238 commit->object.flags |= REACHABLE;
239 if (commit->date < expire_limit) {
240 commit_list_insert(commit, &leftover);
243 commit->object.flags |= REACHABLE;
244 parent = commit->parents;
246 commit = parent->item;
247 parent = parent->next;
248 if (commit->object.flags & REACHABLE)
250 commit_list_insert(commit, &pending);
253 cb->mark_list = leftover;
256 static int unreachable(struct expire_reflog_policy_cb *cb, struct commit *commit, unsigned char *sha1)
259 * We may or may not have the commit yet - if not, look it
260 * up using the supplied sha1.
263 if (is_null_sha1(sha1))
266 commit = lookup_commit_reference_gently(sha1, 1);
268 /* Not a commit -- keep it */
273 /* Reachable from the current ref? Don't prune. */
274 if (commit->object.flags & REACHABLE)
277 if (cb->mark_list && cb->mark_limit) {
278 cb->mark_limit = 0; /* dig down to the root */
282 return !(commit->object.flags & REACHABLE);
286 * Return true iff the specified reflog entry should be expired.
288 static int should_expire_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
289 const char *email, unsigned long timestamp, int tz,
290 const char *message, void *cb_data)
292 struct expire_reflog_policy_cb *cb = cb_data;
293 struct commit *old, *new;
295 if (timestamp < cb->cmd.expire_total)
299 if (cb->cmd.stalefix &&
300 (!keep_entry(&old, osha1) || !keep_entry(&new, nsha1)))
303 if (timestamp < cb->cmd.expire_unreachable) {
304 if (cb->unreachable_expire_kind == UE_ALWAYS)
306 if (unreachable(cb, old, osha1) || unreachable(cb, new, nsha1))
310 if (cb->cmd.recno && --(cb->cmd.recno) == 0)
316 static int push_tip_to_list(const char *refname, const struct object_id *oid,
317 int flags, void *cb_data)
319 struct commit_list **list = cb_data;
320 struct commit *tip_commit;
321 if (flags & REF_ISSYMREF)
323 tip_commit = lookup_commit_reference_gently(oid->hash, 1);
326 commit_list_insert(tip_commit, list);
330 static void reflog_expiry_prepare(const char *refname,
331 const unsigned char *sha1,
334 struct expire_reflog_policy_cb *cb = cb_data;
336 if (!cb->cmd.expire_unreachable || !strcmp(refname, "HEAD")) {
337 cb->tip_commit = NULL;
338 cb->unreachable_expire_kind = UE_HEAD;
340 cb->tip_commit = lookup_commit_reference_gently(sha1, 1);
342 cb->unreachable_expire_kind = UE_ALWAYS;
344 cb->unreachable_expire_kind = UE_NORMAL;
347 if (cb->cmd.expire_unreachable <= cb->cmd.expire_total)
348 cb->unreachable_expire_kind = UE_ALWAYS;
350 cb->mark_list = NULL;
352 if (cb->unreachable_expire_kind != UE_ALWAYS) {
353 if (cb->unreachable_expire_kind == UE_HEAD) {
354 struct commit_list *elem;
356 for_each_ref(push_tip_to_list, &cb->tips);
357 for (elem = cb->tips; elem; elem = elem->next)
358 commit_list_insert(elem->item, &cb->mark_list);
360 commit_list_insert(cb->tip_commit, &cb->mark_list);
362 cb->mark_limit = cb->cmd.expire_total;
367 static void reflog_expiry_cleanup(void *cb_data)
369 struct expire_reflog_policy_cb *cb = cb_data;
371 if (cb->unreachable_expire_kind != UE_ALWAYS) {
372 if (cb->unreachable_expire_kind == UE_HEAD) {
373 struct commit_list *elem;
374 for (elem = cb->tips; elem; elem = elem->next)
375 clear_commit_marks(elem->item, REACHABLE);
376 free_commit_list(cb->tips);
378 clear_commit_marks(cb->tip_commit, REACHABLE);
383 static int collect_reflog(const char *ref, const struct object_id *oid, int unused, void *cb_data)
385 struct collected_reflog *e;
386 struct collect_reflog_cb *cb = cb_data;
387 size_t namelen = strlen(ref);
389 e = xmalloc(sizeof(*e) + namelen + 1);
390 hashcpy(e->sha1, oid->hash);
391 memcpy(e->reflog, ref, namelen + 1);
392 ALLOC_GROW(cb->e, cb->nr + 1, cb->alloc);
397 static struct reflog_expire_cfg {
398 struct reflog_expire_cfg *next;
399 unsigned long expire_total;
400 unsigned long expire_unreachable;
402 char pattern[FLEX_ARRAY];
403 } *reflog_expire_cfg, **reflog_expire_cfg_tail;
405 static struct reflog_expire_cfg *find_cfg_ent(const char *pattern, size_t len)
407 struct reflog_expire_cfg *ent;
409 if (!reflog_expire_cfg_tail)
410 reflog_expire_cfg_tail = &reflog_expire_cfg;
412 for (ent = reflog_expire_cfg; ent; ent = ent->next)
413 if (ent->len == len &&
414 !memcmp(ent->pattern, pattern, len))
417 ent = xcalloc(1, (sizeof(*ent) + len));
418 memcpy(ent->pattern, pattern, len);
420 *reflog_expire_cfg_tail = ent;
421 reflog_expire_cfg_tail = &(ent->next);
425 static int parse_expire_cfg_value(const char *var, const char *value, unsigned long *expire)
428 return config_error_nonbool(var);
429 if (parse_expiry_date(value, expire))
430 return error(_("%s' for '%s' is not a valid timestamp"),
435 /* expiry timer slot */
436 #define EXPIRE_TOTAL 01
437 #define EXPIRE_UNREACH 02
439 static int reflog_expire_config(const char *var, const char *value, void *cb)
441 const char *pattern, *key;
443 unsigned long expire;
445 struct reflog_expire_cfg *ent;
447 if (parse_config_key(var, "gc", &pattern, &pattern_len, &key) < 0)
448 return git_default_config(var, value, cb);
450 if (!strcmp(key, "reflogexpire")) {
452 if (parse_expire_cfg_value(var, value, &expire))
454 } else if (!strcmp(key, "reflogexpireunreachable")) {
455 slot = EXPIRE_UNREACH;
456 if (parse_expire_cfg_value(var, value, &expire))
459 return git_default_config(var, value, cb);
464 default_reflog_expire = expire;
467 default_reflog_expire_unreachable = expire;
473 ent = find_cfg_ent(pattern, pattern_len);
478 ent->expire_total = expire;
481 ent->expire_unreachable = expire;
487 static void set_reflog_expiry_param(struct cmd_reflog_expire_cb *cb, int slot, const char *ref)
489 struct reflog_expire_cfg *ent;
491 if (slot == (EXPIRE_TOTAL|EXPIRE_UNREACH))
492 return; /* both given explicitly -- nothing to tweak */
494 for (ent = reflog_expire_cfg; ent; ent = ent->next) {
495 if (!wildmatch(ent->pattern, ref, 0, NULL)) {
496 if (!(slot & EXPIRE_TOTAL))
497 cb->expire_total = ent->expire_total;
498 if (!(slot & EXPIRE_UNREACH))
499 cb->expire_unreachable = ent->expire_unreachable;
505 * If unconfigured, make stash never expire
507 if (!strcmp(ref, "refs/stash")) {
508 if (!(slot & EXPIRE_TOTAL))
509 cb->expire_total = 0;
510 if (!(slot & EXPIRE_UNREACH))
511 cb->expire_unreachable = 0;
515 /* Nothing matched -- use the default value */
516 if (!(slot & EXPIRE_TOTAL))
517 cb->expire_total = default_reflog_expire;
518 if (!(slot & EXPIRE_UNREACH))
519 cb->expire_unreachable = default_reflog_expire_unreachable;
522 static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
524 struct expire_reflog_policy_cb cb;
525 unsigned long now = time(NULL);
526 int i, status, do_all;
527 int explicit_expiry = 0;
528 unsigned int flags = 0;
530 default_reflog_expire_unreachable = now - 30 * 24 * 3600;
531 default_reflog_expire = now - 90 * 24 * 3600;
532 git_config(reflog_expire_config, NULL);
534 save_commit_buffer = 0;
536 memset(&cb, 0, sizeof(cb));
538 cb.cmd.expire_total = default_reflog_expire;
539 cb.cmd.expire_unreachable = default_reflog_expire_unreachable;
541 for (i = 1; i < argc; i++) {
542 const char *arg = argv[i];
543 if (!strcmp(arg, "--dry-run") || !strcmp(arg, "-n"))
544 flags |= EXPIRE_REFLOGS_DRY_RUN;
545 else if (starts_with(arg, "--expire=")) {
546 if (parse_expiry_date(arg + 9, &cb.cmd.expire_total))
547 die(_("'%s' is not a valid timestamp"), arg);
548 explicit_expiry |= EXPIRE_TOTAL;
550 else if (starts_with(arg, "--expire-unreachable=")) {
551 if (parse_expiry_date(arg + 21, &cb.cmd.expire_unreachable))
552 die(_("'%s' is not a valid timestamp"), arg);
553 explicit_expiry |= EXPIRE_UNREACH;
555 else if (!strcmp(arg, "--stale-fix"))
557 else if (!strcmp(arg, "--rewrite"))
558 flags |= EXPIRE_REFLOGS_REWRITE;
559 else if (!strcmp(arg, "--updateref"))
560 flags |= EXPIRE_REFLOGS_UPDATE_REF;
561 else if (!strcmp(arg, "--all"))
563 else if (!strcmp(arg, "--verbose"))
564 flags |= EXPIRE_REFLOGS_VERBOSE;
565 else if (!strcmp(arg, "--")) {
569 else if (arg[0] == '-')
570 usage(reflog_expire_usage);
576 * We can trust the commits and objects reachable from refs
577 * even in older repository. We cannot trust what's reachable
578 * from reflog if the repository was pruned with older git.
580 if (cb.cmd.stalefix) {
581 init_revisions(&cb.cmd.revs, prefix);
582 if (flags & EXPIRE_REFLOGS_VERBOSE)
583 printf("Marking reachable objects...");
584 mark_reachable_objects(&cb.cmd.revs, 0, 0, NULL);
585 if (flags & EXPIRE_REFLOGS_VERBOSE)
590 struct collect_reflog_cb collected;
593 memset(&collected, 0, sizeof(collected));
594 for_each_reflog(collect_reflog, &collected);
595 for (i = 0; i < collected.nr; i++) {
596 struct collected_reflog *e = collected.e[i];
597 set_reflog_expiry_param(&cb.cmd, explicit_expiry, e->reflog);
598 status |= reflog_expire(e->reflog, e->sha1, flags,
599 reflog_expiry_prepare,
600 should_expire_reflog_ent,
601 reflog_expiry_cleanup,
608 for (; i < argc; i++) {
610 unsigned char sha1[20];
611 if (!dwim_log(argv[i], strlen(argv[i]), sha1, &ref)) {
612 status |= error("%s points nowhere!", argv[i]);
615 set_reflog_expiry_param(&cb.cmd, explicit_expiry, ref);
616 status |= reflog_expire(ref, sha1, flags,
617 reflog_expiry_prepare,
618 should_expire_reflog_ent,
619 reflog_expiry_cleanup,
625 static int count_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
626 const char *email, unsigned long timestamp, int tz,
627 const char *message, void *cb_data)
629 struct expire_reflog_policy_cb *cb = cb_data;
630 if (!cb->cmd.expire_total || timestamp < cb->cmd.expire_total)
635 static int cmd_reflog_delete(int argc, const char **argv, const char *prefix)
637 struct expire_reflog_policy_cb cb;
639 unsigned int flags = 0;
641 memset(&cb, 0, sizeof(cb));
643 for (i = 1; i < argc; i++) {
644 const char *arg = argv[i];
645 if (!strcmp(arg, "--dry-run") || !strcmp(arg, "-n"))
646 flags |= EXPIRE_REFLOGS_DRY_RUN;
647 else if (!strcmp(arg, "--rewrite"))
648 flags |= EXPIRE_REFLOGS_REWRITE;
649 else if (!strcmp(arg, "--updateref"))
650 flags |= EXPIRE_REFLOGS_UPDATE_REF;
651 else if (!strcmp(arg, "--verbose"))
652 flags |= EXPIRE_REFLOGS_VERBOSE;
653 else if (!strcmp(arg, "--")) {
657 else if (arg[0] == '-')
658 usage(reflog_delete_usage);
664 return error("Nothing to delete?");
666 for ( ; i < argc; i++) {
667 const char *spec = strstr(argv[i], "@{");
668 unsigned char sha1[20];
673 status |= error("Not a reflog: %s", argv[i]);
677 if (!dwim_log(argv[i], spec - argv[i], sha1, &ref)) {
678 status |= error("no reflog for '%s'", argv[i]);
682 recno = strtoul(spec + 2, &ep, 10);
684 cb.cmd.recno = -recno;
685 for_each_reflog_ent(ref, count_reflog_ent, &cb);
687 cb.cmd.expire_total = approxidate(spec + 2);
688 for_each_reflog_ent(ref, count_reflog_ent, &cb);
689 cb.cmd.expire_total = 0;
692 status |= reflog_expire(ref, sha1, flags,
693 reflog_expiry_prepare,
694 should_expire_reflog_ent,
695 reflog_expiry_cleanup,
706 static const char reflog_usage[] =
707 "git reflog [ show | expire | delete ]";
709 int cmd_reflog(int argc, const char **argv, const char *prefix)
711 if (argc > 1 && !strcmp(argv[1], "-h"))
714 /* With no command, we default to showing it. */
715 if (argc < 2 || *argv[1] == '-')
716 return cmd_log_reflog(argc, argv, prefix);
718 if (!strcmp(argv[1], "show"))
719 return cmd_log_reflog(argc - 1, argv + 1, prefix);
721 if (!strcmp(argv[1], "expire"))
722 return cmd_reflog_expire(argc - 1, argv + 1, prefix);
724 if (!strcmp(argv[1], "delete"))
725 return cmd_reflog_delete(argc - 1, argv + 1, prefix);
727 return cmd_log_reflog(argc, argv, prefix);