15 static const char reflog_expire_usage[] =
16 "git reflog expire [--verbose] [--dry-run] [--stale-fix] [--expire=<time>] [--expire-unreachable=<time>] [--all] <refs>...";
17 static const char reflog_delete_usage[] =
18 "git reflog delete [--verbose] [--dry-run] [--rewrite] [--updateref] <refs>...";
20 static unsigned long default_reflog_expire;
21 static unsigned long default_reflog_expire_unreachable;
23 struct cmd_reflog_expire_cb {
30 unsigned long expire_total;
31 unsigned long expire_unreachable;
35 struct expire_reflog_cb {
38 struct commit *ref_commit;
39 struct commit_list *mark_list;
40 unsigned long mark_limit;
41 struct cmd_reflog_expire_cb *cmd;
42 unsigned char last_kept_sha1[20];
45 struct collected_reflog {
46 unsigned char sha1[20];
47 char reflog[FLEX_ARRAY];
49 struct collect_reflog_cb {
50 struct collected_reflog **e;
55 #define INCOMPLETE (1u<<10)
56 #define STUDYING (1u<<11)
57 #define REACHABLE (1u<<12)
59 static int tree_is_complete(const unsigned char *sha1)
61 struct tree_desc desc;
62 struct name_entry entry;
66 tree = lookup_tree(sha1);
69 if (tree->object.flags & SEEN)
71 if (tree->object.flags & INCOMPLETE)
75 enum object_type type;
77 void *data = read_sha1_file(sha1, &type, &size);
79 tree->object.flags |= INCOMPLETE;
85 init_tree_desc(&desc, tree->buffer, tree->size);
87 while (tree_entry(&desc, &entry)) {
88 if (!has_sha1_file(entry.sha1) ||
89 (S_ISDIR(entry.mode) && !tree_is_complete(entry.sha1))) {
90 tree->object.flags |= INCOMPLETE;
98 tree->object.flags |= SEEN;
102 static int commit_is_complete(struct commit *commit)
104 struct object_array study;
105 struct object_array found;
106 int is_incomplete = 0;
110 if (commit->object.flags & SEEN)
112 if (commit->object.flags & INCOMPLETE)
115 * Find all commits that are reachable and are not marked as
116 * SEEN. Then make sure the trees and blobs contained are
117 * complete. After that, mark these commits also as SEEN.
118 * If some of the objects that are needed to complete this
119 * commit are missing, mark this commit as INCOMPLETE.
121 memset(&study, 0, sizeof(study));
122 memset(&found, 0, sizeof(found));
123 add_object_array(&commit->object, NULL, &study);
124 add_object_array(&commit->object, NULL, &found);
125 commit->object.flags |= STUDYING;
128 struct commit_list *parent;
130 c = (struct commit *)study.objects[--study.nr].item;
131 if (!c->object.parsed && !parse_object(c->object.sha1))
132 c->object.flags |= INCOMPLETE;
134 if (c->object.flags & INCOMPLETE) {
138 else if (c->object.flags & SEEN)
140 for (parent = c->parents; parent; parent = parent->next) {
141 struct commit *p = parent->item;
142 if (p->object.flags & STUDYING)
144 p->object.flags |= STUDYING;
145 add_object_array(&p->object, NULL, &study);
146 add_object_array(&p->object, NULL, &found);
149 if (!is_incomplete) {
151 * make sure all commits in "found" array have all the
154 for (i = 0; i < found.nr; i++) {
156 (struct commit *)found.objects[i].item;
157 if (!tree_is_complete(c->tree->object.sha1)) {
159 c->object.flags |= INCOMPLETE;
162 if (!is_incomplete) {
163 /* mark all found commits as complete, iow SEEN */
164 for (i = 0; i < found.nr; i++)
165 found.objects[i].item->flags |= SEEN;
168 /* clear flags from the objects we traversed */
169 for (i = 0; i < found.nr; i++)
170 found.objects[i].item->flags &= ~STUDYING;
172 commit->object.flags |= INCOMPLETE;
175 * If we come here, we have (1) traversed the ancestry chain
176 * from the "commit" until we reach SEEN commits (which are
177 * known to be complete), and (2) made sure that the commits
178 * encountered during the above traversal refer to trees that
179 * are complete. Which means that we know *all* the commits
180 * we have seen during this process are complete.
182 for (i = 0; i < found.nr; i++)
183 found.objects[i].item->flags |= SEEN;
185 /* free object arrays */
188 return !is_incomplete;
191 static int keep_entry(struct commit **it, unsigned char *sha1)
193 struct commit *commit;
195 if (is_null_sha1(sha1))
197 commit = lookup_commit_reference_gently(sha1, 1);
202 * Make sure everything in this commit exists.
204 * We have walked all the objects reachable from the refs
205 * and cache earlier. The commits reachable by this commit
206 * must meet SEEN commits -- and then we should mark them as
209 if (!commit_is_complete(commit))
216 * Starting from commits in the cb->mark_list, mark commits that are
217 * reachable from them. Stop the traversal at commits older than
218 * the expire_limit and queue them back, so that the caller can call
219 * us again to restart the traversal with longer expire_limit.
221 static void mark_reachable(struct expire_reflog_cb *cb)
223 struct commit *commit;
224 struct commit_list *pending;
225 unsigned long 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 *entry = pending;
234 struct commit_list *parent;
235 pending = entry->next;
236 commit = entry->item;
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_cb *cb, struct commit *commit, unsigned char *sha1)
263 * We may or may not have the commit yet - if not, look it
264 * up using the supplied sha1.
267 if (is_null_sha1(sha1))
270 commit = lookup_commit_reference_gently(sha1, 1);
272 /* Not a commit -- keep it */
277 /* Reachable from the current ref? Don't prune. */
278 if (commit->object.flags & REACHABLE)
281 if (cb->mark_list && cb->mark_limit) {
282 cb->mark_limit = 0; /* dig down to the root */
286 return !(commit->object.flags & REACHABLE);
289 static int expire_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
290 const char *email, unsigned long timestamp, int tz,
291 const char *message, void *cb_data)
293 struct expire_reflog_cb *cb = cb_data;
294 struct commit *old, *new;
296 if (timestamp < cb->cmd->expire_total)
299 if (cb->cmd->rewrite)
300 osha1 = cb->last_kept_sha1;
303 if (cb->cmd->stalefix &&
304 (!keep_entry(&old, osha1) || !keep_entry(&new, nsha1)))
307 if (timestamp < cb->cmd->expire_unreachable) {
310 if (unreachable(cb, old, osha1) || unreachable(cb, new, nsha1))
314 if (cb->cmd->recno && --(cb->cmd->recno) == 0)
318 char sign = (tz < 0) ? '-' : '+';
319 int zone = (tz < 0) ? (-tz) : tz;
320 fprintf(cb->newlog, "%s %s %s %lu %c%04d\t%s",
321 sha1_to_hex(osha1), sha1_to_hex(nsha1),
322 email, timestamp, sign, zone,
324 hashcpy(cb->last_kept_sha1, nsha1);
326 if (cb->cmd->verbose)
327 printf("keep %s", message);
330 if (!cb->newlog || cb->cmd->verbose)
331 printf("%sprune %s", cb->newlog ? "" : "would ", message);
335 static int expire_reflog(const char *ref, const unsigned char *sha1, int unused, void *cb_data)
337 struct cmd_reflog_expire_cb *cmd = cb_data;
338 struct expire_reflog_cb cb;
339 struct ref_lock *lock;
340 char *log_file, *newlog_path = NULL;
343 memset(&cb, 0, sizeof(cb));
346 * we take the lock for the ref itself to prevent it from
349 lock = lock_any_ref_for_update(ref, sha1, 0);
351 return error("cannot lock ref '%s'", ref);
352 log_file = git_pathdup("logs/%s", ref);
353 if (!file_exists(log_file))
356 newlog_path = git_pathdup("logs/%s.lock", ref);
357 cb.newlog = fopen(newlog_path, "w");
360 cb.ref_commit = lookup_commit_reference_gently(sha1, 1);
365 commit_list_insert(cb.ref_commit, &cb.mark_list);
366 cb.mark_limit = cmd->expire_total;
369 for_each_reflog_ent(ref, expire_reflog_ent, &cb);
371 clear_commit_marks(cb.ref_commit, REACHABLE);
374 if (fclose(cb.newlog)) {
375 status |= error("%s: %s", strerror(errno),
378 } else if (cmd->updateref &&
379 (write_in_full(lock->lock_fd,
380 sha1_to_hex(cb.last_kept_sha1), 40) != 40 ||
381 write_str_in_full(lock->lock_fd, "\n") != 1 ||
382 close_ref(lock) < 0)) {
383 status |= error("Couldn't write %s",
386 } else if (rename(newlog_path, log_file)) {
387 status |= error("cannot rename %s to %s",
388 newlog_path, log_file);
390 } else if (cmd->updateref && commit_ref(lock)) {
391 status |= error("Couldn't set %s", lock->ref_name);
393 adjust_shared_perm(log_file);
402 static int collect_reflog(const char *ref, const unsigned char *sha1, int unused, void *cb_data)
404 struct collected_reflog *e;
405 struct collect_reflog_cb *cb = cb_data;
406 size_t namelen = strlen(ref);
408 e = xmalloc(sizeof(*e) + namelen + 1);
409 hashcpy(e->sha1, sha1);
410 memcpy(e->reflog, ref, namelen + 1);
411 ALLOC_GROW(cb->e, cb->nr + 1, cb->alloc);
416 static struct reflog_expire_cfg {
417 struct reflog_expire_cfg *next;
418 unsigned long expire_total;
419 unsigned long expire_unreachable;
421 char pattern[FLEX_ARRAY];
422 } *reflog_expire_cfg, **reflog_expire_cfg_tail;
424 static struct reflog_expire_cfg *find_cfg_ent(const char *pattern, size_t len)
426 struct reflog_expire_cfg *ent;
428 if (!reflog_expire_cfg_tail)
429 reflog_expire_cfg_tail = &reflog_expire_cfg;
431 for (ent = reflog_expire_cfg; ent; ent = ent->next)
432 if (ent->len == len &&
433 !memcmp(ent->pattern, pattern, len))
436 ent = xcalloc(1, (sizeof(*ent) + len));
437 memcpy(ent->pattern, pattern, len);
439 *reflog_expire_cfg_tail = ent;
440 reflog_expire_cfg_tail = &(ent->next);
444 static int parse_expire_cfg_value(const char *var, const char *value, unsigned long *expire)
447 return config_error_nonbool(var);
448 if (!strcmp(value, "never") || !strcmp(value, "false")) {
452 *expire = approxidate(value);
456 /* expiry timer slot */
457 #define EXPIRE_TOTAL 01
458 #define EXPIRE_UNREACH 02
460 static int reflog_expire_config(const char *var, const char *value, void *cb)
462 const char *lastdot = strrchr(var, '.');
463 unsigned long expire;
465 struct reflog_expire_cfg *ent;
467 if (!lastdot || prefixcmp(var, "gc."))
468 return git_default_config(var, value, cb);
470 if (!strcmp(lastdot, ".reflogexpire")) {
472 if (parse_expire_cfg_value(var, value, &expire))
474 } else if (!strcmp(lastdot, ".reflogexpireunreachable")) {
475 slot = EXPIRE_UNREACH;
476 if (parse_expire_cfg_value(var, value, &expire))
479 return git_default_config(var, value, cb);
481 if (lastdot == var + 2) {
484 default_reflog_expire = expire;
487 default_reflog_expire_unreachable = expire;
493 ent = find_cfg_ent(var + 3, lastdot - (var+3));
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 (!fnmatch(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 cmd_reflog_expire_cb cb;
545 unsigned long now = time(NULL);
546 int i, status, do_all;
547 int explicit_expiry = 0;
549 default_reflog_expire_unreachable = now - 30 * 24 * 3600;
550 default_reflog_expire = now - 90 * 24 * 3600;
551 git_config(reflog_expire_config, NULL);
553 save_commit_buffer = 0;
555 memset(&cb, 0, sizeof(cb));
557 cb.expire_total = default_reflog_expire;
558 cb.expire_unreachable = default_reflog_expire_unreachable;
560 for (i = 1; i < argc; i++) {
561 const char *arg = argv[i];
562 if (!strcmp(arg, "--dry-run") || !strcmp(arg, "-n"))
564 else if (!prefixcmp(arg, "--expire=")) {
565 cb.expire_total = approxidate(arg + 9);
566 explicit_expiry |= EXPIRE_TOTAL;
568 else if (!prefixcmp(arg, "--expire-unreachable=")) {
569 cb.expire_unreachable = approxidate(arg + 21);
570 explicit_expiry |= EXPIRE_UNREACH;
572 else if (!strcmp(arg, "--stale-fix"))
574 else if (!strcmp(arg, "--rewrite"))
576 else if (!strcmp(arg, "--updateref"))
578 else if (!strcmp(arg, "--all"))
580 else if (!strcmp(arg, "--verbose"))
582 else if (!strcmp(arg, "--")) {
586 else if (arg[0] == '-')
587 usage(reflog_expire_usage);
593 * We can trust the commits and objects reachable from refs
594 * even in older repository. We cannot trust what's reachable
595 * from reflog if the repository was pruned with older git.
598 init_revisions(&cb.revs, prefix);
600 printf("Marking reachable objects...");
601 mark_reachable_objects(&cb.revs, 0);
607 struct collect_reflog_cb collected;
610 memset(&collected, 0, sizeof(collected));
611 for_each_reflog(collect_reflog, &collected);
612 for (i = 0; i < collected.nr; i++) {
613 struct collected_reflog *e = collected.e[i];
614 set_reflog_expiry_param(&cb, explicit_expiry, e->reflog);
615 status |= expire_reflog(e->reflog, e->sha1, 0, &cb);
621 for (; i < argc; i++) {
623 unsigned char sha1[20];
624 if (!dwim_log(argv[i], strlen(argv[i]), sha1, &ref)) {
625 status |= error("%s points nowhere!", argv[i]);
628 set_reflog_expiry_param(&cb, explicit_expiry, ref);
629 status |= expire_reflog(ref, sha1, 0, &cb);
634 static int count_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
635 const char *email, unsigned long timestamp, int tz,
636 const char *message, void *cb_data)
638 struct cmd_reflog_expire_cb *cb = cb_data;
639 if (!cb->expire_total || timestamp < cb->expire_total)
644 static int cmd_reflog_delete(int argc, const char **argv, const char *prefix)
646 struct cmd_reflog_expire_cb cb;
649 memset(&cb, 0, sizeof(cb));
651 for (i = 1; i < argc; i++) {
652 const char *arg = argv[i];
653 if (!strcmp(arg, "--dry-run") || !strcmp(arg, "-n"))
655 else if (!strcmp(arg, "--rewrite"))
657 else if (!strcmp(arg, "--updateref"))
659 else if (!strcmp(arg, "--verbose"))
661 else if (!strcmp(arg, "--")) {
665 else if (arg[0] == '-')
666 usage(reflog_delete_usage);
672 return error("Nothing to delete?");
674 for ( ; i < argc; i++) {
675 const char *spec = strstr(argv[i], "@{");
676 unsigned char sha1[20];
681 status |= error("Not a reflog: %s", argv[i]);
685 if (!dwim_log(argv[i], spec - argv[i], sha1, &ref)) {
686 status |= error("no reflog for '%s'", argv[i]);
690 recno = strtoul(spec + 2, &ep, 10);
693 for_each_reflog_ent(ref, count_reflog_ent, &cb);
695 cb.expire_total = approxidate(spec + 2);
696 for_each_reflog_ent(ref, count_reflog_ent, &cb);
700 status |= expire_reflog(ref, sha1, 0, &cb);
710 static const char reflog_usage[] =
711 "git reflog [ show | expire | delete ]";
713 int cmd_reflog(int argc, const char **argv, const char *prefix)
715 if (argc > 1 && !strcmp(argv[1], "-h"))
718 /* With no command, we default to showing it. */
719 if (argc < 2 || *argv[1] == '-')
720 return cmd_log_reflog(argc, argv, prefix);
722 if (!strcmp(argv[1], "show"))
723 return cmd_log_reflog(argc - 1, argv + 1, prefix);
725 if (!strcmp(argv[1], "expire"))
726 return cmd_reflog_expire(argc - 1, argv + 1, prefix);
728 if (!strcmp(argv[1], "delete"))
729 return cmd_reflog_delete(argc - 1, argv + 1, prefix);
731 /* Not a recognized reflog command..*/