15 static const char reflog_expire_usage[] =
16 "git reflog (show|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 cmd_reflog_expire_cb *cmd;
40 unsigned char last_kept_sha1[20];
43 struct collected_reflog {
44 unsigned char sha1[20];
45 char reflog[FLEX_ARRAY];
47 struct collect_reflog_cb {
48 struct collected_reflog **e;
53 #define INCOMPLETE (1u<<10)
54 #define STUDYING (1u<<11)
55 #define REACHABLE (1u<<12)
57 static int tree_is_complete(const unsigned char *sha1)
59 struct tree_desc desc;
60 struct name_entry entry;
64 tree = lookup_tree(sha1);
67 if (tree->object.flags & SEEN)
69 if (tree->object.flags & INCOMPLETE)
73 enum object_type type;
75 void *data = read_sha1_file(sha1, &type, &size);
77 tree->object.flags |= INCOMPLETE;
83 init_tree_desc(&desc, tree->buffer, tree->size);
85 while (tree_entry(&desc, &entry)) {
86 if (!has_sha1_file(entry.sha1) ||
87 (S_ISDIR(entry.mode) && !tree_is_complete(entry.sha1))) {
88 tree->object.flags |= INCOMPLETE;
96 tree->object.flags |= SEEN;
100 static int commit_is_complete(struct commit *commit)
102 struct object_array study;
103 struct object_array found;
104 int is_incomplete = 0;
108 if (commit->object.flags & SEEN)
110 if (commit->object.flags & INCOMPLETE)
113 * Find all commits that are reachable and are not marked as
114 * SEEN. Then make sure the trees and blobs contained are
115 * complete. After that, mark these commits also as SEEN.
116 * If some of the objects that are needed to complete this
117 * commit are missing, mark this commit as INCOMPLETE.
119 memset(&study, 0, sizeof(study));
120 memset(&found, 0, sizeof(found));
121 add_object_array(&commit->object, NULL, &study);
122 add_object_array(&commit->object, NULL, &found);
123 commit->object.flags |= STUDYING;
126 struct commit_list *parent;
128 c = (struct commit *)study.objects[--study.nr].item;
129 if (!c->object.parsed && !parse_object(c->object.sha1))
130 c->object.flags |= INCOMPLETE;
132 if (c->object.flags & INCOMPLETE) {
136 else if (c->object.flags & SEEN)
138 for (parent = c->parents; parent; parent = parent->next) {
139 struct commit *p = parent->item;
140 if (p->object.flags & STUDYING)
142 p->object.flags |= STUDYING;
143 add_object_array(&p->object, NULL, &study);
144 add_object_array(&p->object, NULL, &found);
147 if (!is_incomplete) {
149 * make sure all commits in "found" array have all the
152 for (i = 0; i < found.nr; i++) {
154 (struct commit *)found.objects[i].item;
155 if (!tree_is_complete(c->tree->object.sha1)) {
157 c->object.flags |= INCOMPLETE;
160 if (!is_incomplete) {
161 /* mark all found commits as complete, iow SEEN */
162 for (i = 0; i < found.nr; i++)
163 found.objects[i].item->flags |= SEEN;
166 /* clear flags from the objects we traversed */
167 for (i = 0; i < found.nr; i++)
168 found.objects[i].item->flags &= ~STUDYING;
170 commit->object.flags |= INCOMPLETE;
173 * If we come here, we have (1) traversed the ancestry chain
174 * from the "commit" until we reach SEEN commits (which are
175 * known to be complete), and (2) made sure that the commits
176 * encountered during the above traversal refer to trees that
177 * are complete. Which means that we know *all* the commits
178 * we have seen during this process are complete.
180 for (i = 0; i < found.nr; i++)
181 found.objects[i].item->flags |= SEEN;
183 /* free object arrays */
186 return !is_incomplete;
189 static int keep_entry(struct commit **it, unsigned char *sha1)
191 struct commit *commit;
193 if (is_null_sha1(sha1))
195 commit = lookup_commit_reference_gently(sha1, 1);
200 * Make sure everything in this commit exists.
202 * We have walked all the objects reachable from the refs
203 * and cache earlier. The commits reachable by this commit
204 * must meet SEEN commits -- and then we should mark them as
207 if (!commit_is_complete(commit))
213 static int unreachable(struct expire_reflog_cb *cb, struct commit *commit, unsigned char *sha1)
216 * We may or may not have the commit yet - if not, look it
217 * up using the supplied sha1.
220 if (is_null_sha1(sha1))
223 commit = lookup_commit_reference_gently(sha1, 1);
225 /* Not a commit -- keep it */
230 /* Reachable from the current ref? Don't prune. */
231 if (commit->object.flags & REACHABLE)
233 if (in_merge_bases(commit, &cb->ref_commit, 1))
236 /* We can't reach it - prune it. */
240 static void mark_reachable(struct commit *commit, unsigned long expire_limit)
243 * We need to compute whether the commit on either side of a reflog
244 * entry is reachable from the tip of the ref for all entries.
245 * Mark commits that are reachable from the tip down to the
246 * time threshold first; we know a commit marked thusly is
247 * reachable from the tip without running in_merge_bases()
250 struct commit_list *pending = NULL;
252 commit_list_insert(commit, &pending);
254 struct commit_list *entry = pending;
255 struct commit_list *parent;
256 pending = entry->next;
257 commit = entry->item;
259 if (commit->object.flags & REACHABLE)
261 if (parse_commit(commit))
263 commit->object.flags |= REACHABLE;
264 if (commit->date < expire_limit)
266 parent = commit->parents;
268 commit = parent->item;
269 parent = parent->next;
270 if (commit->object.flags & REACHABLE)
272 commit_list_insert(commit, &pending);
277 static int expire_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
278 const char *email, unsigned long timestamp, int tz,
279 const char *message, void *cb_data)
281 struct expire_reflog_cb *cb = cb_data;
282 struct commit *old, *new;
284 if (timestamp < cb->cmd->expire_total)
287 if (cb->cmd->rewrite)
288 osha1 = cb->last_kept_sha1;
291 if (cb->cmd->stalefix &&
292 (!keep_entry(&old, osha1) || !keep_entry(&new, nsha1)))
295 if (timestamp < cb->cmd->expire_unreachable) {
298 if (unreachable(cb, old, osha1) || unreachable(cb, new, nsha1))
302 if (cb->cmd->recno && --(cb->cmd->recno) == 0)
306 char sign = (tz < 0) ? '-' : '+';
307 int zone = (tz < 0) ? (-tz) : tz;
308 fprintf(cb->newlog, "%s %s %s %lu %c%04d\t%s",
309 sha1_to_hex(osha1), sha1_to_hex(nsha1),
310 email, timestamp, sign, zone,
312 hashcpy(cb->last_kept_sha1, nsha1);
314 if (cb->cmd->verbose)
315 printf("keep %s", message);
318 if (!cb->newlog || cb->cmd->verbose)
319 printf("%sprune %s", cb->newlog ? "" : "would ", message);
323 static int expire_reflog(const char *ref, const unsigned char *sha1, int unused, void *cb_data)
325 struct cmd_reflog_expire_cb *cmd = cb_data;
326 struct expire_reflog_cb cb;
327 struct ref_lock *lock;
328 char *log_file, *newlog_path = NULL;
331 memset(&cb, 0, sizeof(cb));
334 * we take the lock for the ref itself to prevent it from
337 lock = lock_any_ref_for_update(ref, sha1, 0);
339 return error("cannot lock ref '%s'", ref);
340 log_file = git_pathdup("logs/%s", ref);
341 if (!file_exists(log_file))
344 newlog_path = git_pathdup("logs/%s.lock", ref);
345 cb.newlog = fopen(newlog_path, "w");
348 cb.ref_commit = lookup_commit_reference_gently(sha1, 1);
352 mark_reachable(cb.ref_commit, cmd->expire_total);
353 for_each_reflog_ent(ref, expire_reflog_ent, &cb);
355 clear_commit_marks(cb.ref_commit, REACHABLE);
358 if (fclose(cb.newlog)) {
359 status |= error("%s: %s", strerror(errno),
362 } else if (cmd->updateref &&
363 (write_in_full(lock->lock_fd,
364 sha1_to_hex(cb.last_kept_sha1), 40) != 40 ||
365 write_in_full(lock->lock_fd, "\n", 1) != 1 ||
366 close_ref(lock) < 0)) {
367 status |= error("Couldn't write %s",
370 } else if (rename(newlog_path, log_file)) {
371 status |= error("cannot rename %s to %s",
372 newlog_path, log_file);
374 } else if (cmd->updateref && commit_ref(lock)) {
375 status |= error("Couldn't set %s", lock->ref_name);
377 adjust_shared_perm(log_file);
386 static int collect_reflog(const char *ref, const unsigned char *sha1, int unused, void *cb_data)
388 struct collected_reflog *e;
389 struct collect_reflog_cb *cb = cb_data;
390 size_t namelen = strlen(ref);
392 e = xmalloc(sizeof(*e) + namelen + 1);
393 hashcpy(e->sha1, sha1);
394 memcpy(e->reflog, ref, namelen + 1);
395 ALLOC_GROW(cb->e, cb->nr + 1, cb->alloc);
400 static struct reflog_expire_cfg {
401 struct reflog_expire_cfg *next;
402 unsigned long expire_total;
403 unsigned long expire_unreachable;
405 char pattern[FLEX_ARRAY];
406 } *reflog_expire_cfg, **reflog_expire_cfg_tail;
408 static struct reflog_expire_cfg *find_cfg_ent(const char *pattern, size_t len)
410 struct reflog_expire_cfg *ent;
412 if (!reflog_expire_cfg_tail)
413 reflog_expire_cfg_tail = &reflog_expire_cfg;
415 for (ent = reflog_expire_cfg; ent; ent = ent->next)
416 if (ent->len == len &&
417 !memcmp(ent->pattern, pattern, len))
420 ent = xcalloc(1, (sizeof(*ent) + len));
421 memcpy(ent->pattern, pattern, len);
423 *reflog_expire_cfg_tail = ent;
424 reflog_expire_cfg_tail = &(ent->next);
428 static int parse_expire_cfg_value(const char *var, const char *value, unsigned long *expire)
431 return config_error_nonbool(var);
432 if (!strcmp(value, "never") || !strcmp(value, "false")) {
436 *expire = approxidate(value);
440 /* expiry timer slot */
441 #define EXPIRE_TOTAL 01
442 #define EXPIRE_UNREACH 02
444 static int reflog_expire_config(const char *var, const char *value, void *cb)
446 const char *lastdot = strrchr(var, '.');
447 unsigned long expire;
449 struct reflog_expire_cfg *ent;
451 if (!lastdot || prefixcmp(var, "gc."))
452 return git_default_config(var, value, cb);
454 if (!strcmp(lastdot, ".reflogexpire")) {
456 if (parse_expire_cfg_value(var, value, &expire))
458 } else if (!strcmp(lastdot, ".reflogexpireunreachable")) {
459 slot = EXPIRE_UNREACH;
460 if (parse_expire_cfg_value(var, value, &expire))
463 return git_default_config(var, value, cb);
465 if (lastdot == var + 2) {
468 default_reflog_expire = expire;
471 default_reflog_expire_unreachable = expire;
477 ent = find_cfg_ent(var + 3, lastdot - (var+3));
482 ent->expire_total = expire;
485 ent->expire_unreachable = expire;
491 static void set_reflog_expiry_param(struct cmd_reflog_expire_cb *cb, int slot, const char *ref)
493 struct reflog_expire_cfg *ent;
495 if (slot == (EXPIRE_TOTAL|EXPIRE_UNREACH))
496 return; /* both given explicitly -- nothing to tweak */
498 for (ent = reflog_expire_cfg; ent; ent = ent->next) {
499 if (!fnmatch(ent->pattern, ref, 0)) {
500 if (!(slot & EXPIRE_TOTAL))
501 cb->expire_total = ent->expire_total;
502 if (!(slot & EXPIRE_UNREACH))
503 cb->expire_unreachable = ent->expire_unreachable;
509 * If unconfigured, make stash never expire
511 if (!strcmp(ref, "refs/stash")) {
512 if (!(slot & EXPIRE_TOTAL))
513 cb->expire_total = 0;
514 if (!(slot & EXPIRE_UNREACH))
515 cb->expire_unreachable = 0;
519 /* Nothing matched -- use the default value */
520 if (!(slot & EXPIRE_TOTAL))
521 cb->expire_total = default_reflog_expire;
522 if (!(slot & EXPIRE_UNREACH))
523 cb->expire_unreachable = default_reflog_expire_unreachable;
526 static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
528 struct cmd_reflog_expire_cb cb;
529 unsigned long now = time(NULL);
530 int i, status, do_all;
531 int explicit_expiry = 0;
533 git_config(reflog_expire_config, NULL);
535 save_commit_buffer = 0;
537 memset(&cb, 0, sizeof(cb));
539 if (!default_reflog_expire_unreachable)
540 default_reflog_expire_unreachable = now - 30 * 24 * 3600;
541 if (!default_reflog_expire)
542 default_reflog_expire = now - 90 * 24 * 3600;
543 cb.expire_total = default_reflog_expire;
544 cb.expire_unreachable = default_reflog_expire_unreachable;
546 for (i = 1; i < argc; i++) {
547 const char *arg = argv[i];
548 if (!strcmp(arg, "--dry-run") || !strcmp(arg, "-n"))
550 else if (!prefixcmp(arg, "--expire=")) {
551 cb.expire_total = approxidate(arg + 9);
552 explicit_expiry |= EXPIRE_TOTAL;
554 else if (!prefixcmp(arg, "--expire-unreachable=")) {
555 cb.expire_unreachable = approxidate(arg + 21);
556 explicit_expiry |= EXPIRE_UNREACH;
558 else if (!strcmp(arg, "--stale-fix"))
560 else if (!strcmp(arg, "--rewrite"))
562 else if (!strcmp(arg, "--updateref"))
564 else if (!strcmp(arg, "--all"))
566 else if (!strcmp(arg, "--verbose"))
568 else if (!strcmp(arg, "--")) {
572 else if (arg[0] == '-')
573 usage(reflog_expire_usage);
579 * We can trust the commits and objects reachable from refs
580 * even in older repository. We cannot trust what's reachable
581 * from reflog if the repository was pruned with older git.
584 init_revisions(&cb.revs, prefix);
586 printf("Marking reachable objects...");
587 mark_reachable_objects(&cb.revs, 0);
593 struct collect_reflog_cb collected;
596 memset(&collected, 0, sizeof(collected));
597 for_each_reflog(collect_reflog, &collected);
598 for (i = 0; i < collected.nr; i++) {
599 struct collected_reflog *e = collected.e[i];
600 set_reflog_expiry_param(&cb, explicit_expiry, e->reflog);
601 status |= expire_reflog(e->reflog, e->sha1, 0, &cb);
607 for (; i < argc; i++) {
609 unsigned char sha1[20];
610 if (!dwim_log(argv[i], strlen(argv[i]), sha1, &ref)) {
611 status |= error("%s points nowhere!", argv[i]);
614 set_reflog_expiry_param(&cb, explicit_expiry, ref);
615 status |= expire_reflog(ref, sha1, 0, &cb);
620 static int count_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
621 const char *email, unsigned long timestamp, int tz,
622 const char *message, void *cb_data)
624 struct cmd_reflog_expire_cb *cb = cb_data;
625 if (!cb->expire_total || timestamp < cb->expire_total)
630 static int cmd_reflog_delete(int argc, const char **argv, const char *prefix)
632 struct cmd_reflog_expire_cb cb;
635 memset(&cb, 0, sizeof(cb));
637 for (i = 1; i < argc; i++) {
638 const char *arg = argv[i];
639 if (!strcmp(arg, "--dry-run") || !strcmp(arg, "-n"))
641 else if (!strcmp(arg, "--rewrite"))
643 else if (!strcmp(arg, "--updateref"))
645 else if (!strcmp(arg, "--verbose"))
647 else if (!strcmp(arg, "--")) {
651 else if (arg[0] == '-')
652 usage(reflog_delete_usage);
658 return error("Nothing to delete?");
660 for ( ; i < argc; i++) {
661 const char *spec = strstr(argv[i], "@{");
662 unsigned char sha1[20];
667 status |= error("Not a reflog: %s", argv[i]);
671 if (!dwim_log(argv[i], spec - argv[i], sha1, &ref)) {
672 status |= error("no reflog for '%s'", argv[i]);
676 recno = strtoul(spec + 2, &ep, 10);
679 for_each_reflog_ent(ref, count_reflog_ent, &cb);
681 cb.expire_total = approxidate(spec + 2);
682 for_each_reflog_ent(ref, count_reflog_ent, &cb);
686 status |= expire_reflog(ref, sha1, 0, &cb);
696 static const char reflog_usage[] =
697 "git reflog [ show | expire | delete ]";
699 int cmd_reflog(int argc, const char **argv, const char *prefix)
701 /* With no command, we default to showing it. */
702 if (argc < 2 || *argv[1] == '-')
703 return cmd_log_reflog(argc, argv, prefix);
705 if (!strcmp(argv[1], "show"))
706 return cmd_log_reflog(argc - 1, argv + 1, prefix);
708 if (!strcmp(argv[1], "expire"))
709 return cmd_reflog_expire(argc - 1, argv + 1, prefix);
711 if (!strcmp(argv[1], "delete"))
712 return cmd_reflog_delete(argc - 1, argv + 1, prefix);
714 /* Not a recognized reflog command..*/