Clean up reflog unreachability pruning decision
[git/dscho.git] / builtin-reflog.c
bloba07960ff5e395b9e4fdd9816daf4da06b89162e2
1 #include "cache.h"
2 #include "builtin.h"
3 #include "commit.h"
4 #include "refs.h"
5 #include "dir.h"
6 #include "tree-walk.h"
7 #include "diff.h"
8 #include "revision.h"
9 #include "reachable.h"
12 * reflog expire
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 {
24 struct rev_info revs;
25 int dry_run;
26 int stalefix;
27 int rewrite;
28 int updateref;
29 int verbose;
30 unsigned long expire_total;
31 unsigned long expire_unreachable;
32 int recno;
35 struct expire_reflog_cb {
36 FILE *newlog;
37 const char *ref;
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;
49 int alloc;
50 int nr;
53 #define INCOMPLETE (1u<<10)
54 #define STUDYING (1u<<11)
56 static int tree_is_complete(const unsigned char *sha1)
58 struct tree_desc desc;
59 struct name_entry entry;
60 int complete;
61 struct tree *tree;
63 tree = lookup_tree(sha1);
64 if (!tree)
65 return 0;
66 if (tree->object.flags & SEEN)
67 return 1;
68 if (tree->object.flags & INCOMPLETE)
69 return 0;
71 if (!tree->buffer) {
72 enum object_type type;
73 unsigned long size;
74 void *data = read_sha1_file(sha1, &type, &size);
75 if (!data) {
76 tree->object.flags |= INCOMPLETE;
77 return 0;
79 tree->buffer = data;
80 tree->size = size;
82 init_tree_desc(&desc, tree->buffer, tree->size);
83 complete = 1;
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;
88 complete = 0;
91 free(tree->buffer);
92 tree->buffer = NULL;
94 if (complete)
95 tree->object.flags |= SEEN;
96 return complete;
99 static int commit_is_complete(struct commit *commit)
101 struct object_array study;
102 struct object_array found;
103 int is_incomplete = 0;
104 int i;
106 /* early return */
107 if (commit->object.flags & SEEN)
108 return 1;
109 if (commit->object.flags & INCOMPLETE)
110 return 0;
112 * Find all commits that are reachable and are not marked as
113 * SEEN. Then make sure the trees and blobs contained are
114 * complete. After that, mark these commits also as SEEN.
115 * If some of the objects that are needed to complete this
116 * commit are missing, mark this commit as INCOMPLETE.
118 memset(&study, 0, sizeof(study));
119 memset(&found, 0, sizeof(found));
120 add_object_array(&commit->object, NULL, &study);
121 add_object_array(&commit->object, NULL, &found);
122 commit->object.flags |= STUDYING;
123 while (study.nr) {
124 struct commit *c;
125 struct commit_list *parent;
127 c = (struct commit *)study.objects[--study.nr].item;
128 if (!c->object.parsed && !parse_object(c->object.sha1))
129 c->object.flags |= INCOMPLETE;
131 if (c->object.flags & INCOMPLETE) {
132 is_incomplete = 1;
133 break;
135 else if (c->object.flags & SEEN)
136 continue;
137 for (parent = c->parents; parent; parent = parent->next) {
138 struct commit *p = parent->item;
139 if (p->object.flags & STUDYING)
140 continue;
141 p->object.flags |= STUDYING;
142 add_object_array(&p->object, NULL, &study);
143 add_object_array(&p->object, NULL, &found);
146 if (!is_incomplete) {
148 * make sure all commits in "found" array have all the
149 * necessary objects.
151 for (i = 0; i < found.nr; i++) {
152 struct commit *c =
153 (struct commit *)found.objects[i].item;
154 if (!tree_is_complete(c->tree->object.sha1)) {
155 is_incomplete = 1;
156 c->object.flags |= INCOMPLETE;
159 if (!is_incomplete) {
160 /* mark all found commits as complete, iow SEEN */
161 for (i = 0; i < found.nr; i++)
162 found.objects[i].item->flags |= SEEN;
165 /* clear flags from the objects we traversed */
166 for (i = 0; i < found.nr; i++)
167 found.objects[i].item->flags &= ~STUDYING;
168 if (is_incomplete)
169 commit->object.flags |= INCOMPLETE;
170 else {
172 * If we come here, we have (1) traversed the ancestry chain
173 * from the "commit" until we reach SEEN commits (which are
174 * known to be complete), and (2) made sure that the commits
175 * encountered during the above traversal refer to trees that
176 * are complete. Which means that we know *all* the commits
177 * we have seen during this process are complete.
179 for (i = 0; i < found.nr; i++)
180 found.objects[i].item->flags |= SEEN;
182 /* free object arrays */
183 free(study.objects);
184 free(found.objects);
185 return !is_incomplete;
188 static int keep_entry(struct commit **it, unsigned char *sha1)
190 struct commit *commit;
192 if (is_null_sha1(sha1))
193 return 1;
194 commit = lookup_commit_reference_gently(sha1, 1);
195 if (!commit)
196 return 0;
199 * Make sure everything in this commit exists.
201 * We have walked all the objects reachable from the refs
202 * and cache earlier. The commits reachable by this commit
203 * must meet SEEN commits -- and then we should mark them as
204 * SEEN as well.
206 if (!commit_is_complete(commit))
207 return 0;
208 *it = commit;
209 return 1;
212 static int unreachable(struct expire_reflog_cb *cb, struct commit *commit, unsigned char *sha1)
215 * We may or may not have the commit yet - if not, look it
216 * up using the supplied sha1.
218 if (!commit) {
219 if (is_null_sha1(sha1))
220 return 0;
222 commit = lookup_commit_reference_gently(sha1, 1);
224 /* Not a commit -- keep it */
225 if (!commit)
226 return 0;
229 /* Reachable from the current ref? Don't prune. */
230 if (in_merge_bases(commit, &cb->ref_commit, 1))
231 return 0;
233 /* We can't reach it - prune it. */
234 return 1;
237 static int expire_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
238 const char *email, unsigned long timestamp, int tz,
239 const char *message, void *cb_data)
241 struct expire_reflog_cb *cb = cb_data;
242 struct commit *old, *new;
244 if (timestamp < cb->cmd->expire_total)
245 goto prune;
247 if (cb->cmd->rewrite)
248 osha1 = cb->last_kept_sha1;
250 old = new = NULL;
251 if (cb->cmd->stalefix &&
252 (!keep_entry(&old, osha1) || !keep_entry(&new, nsha1)))
253 goto prune;
255 if (timestamp < cb->cmd->expire_unreachable) {
256 if (!cb->ref_commit)
257 goto prune;
258 if (unreachable(cb, old, osha1) || unreachable(cb, new, nsha1))
259 goto prune;
262 if (cb->cmd->recno && --(cb->cmd->recno) == 0)
263 goto prune;
265 if (cb->newlog) {
266 char sign = (tz < 0) ? '-' : '+';
267 int zone = (tz < 0) ? (-tz) : tz;
268 fprintf(cb->newlog, "%s %s %s %lu %c%04d\t%s",
269 sha1_to_hex(osha1), sha1_to_hex(nsha1),
270 email, timestamp, sign, zone,
271 message);
272 hashcpy(cb->last_kept_sha1, nsha1);
274 if (cb->cmd->verbose)
275 printf("keep %s", message);
276 return 0;
277 prune:
278 if (!cb->newlog || cb->cmd->verbose)
279 printf("%sprune %s", cb->newlog ? "" : "would ", message);
280 return 0;
283 static int expire_reflog(const char *ref, const unsigned char *sha1, int unused, void *cb_data)
285 struct cmd_reflog_expire_cb *cmd = cb_data;
286 struct expire_reflog_cb cb;
287 struct ref_lock *lock;
288 char *log_file, *newlog_path = NULL;
289 int status = 0;
291 memset(&cb, 0, sizeof(cb));
294 * we take the lock for the ref itself to prevent it from
295 * getting updated.
297 lock = lock_any_ref_for_update(ref, sha1, 0);
298 if (!lock)
299 return error("cannot lock ref '%s'", ref);
300 log_file = git_pathdup("logs/%s", ref);
301 if (!file_exists(log_file))
302 goto finish;
303 if (!cmd->dry_run) {
304 newlog_path = git_pathdup("logs/%s.lock", ref);
305 cb.newlog = fopen(newlog_path, "w");
308 cb.ref_commit = lookup_commit_reference_gently(sha1, 1);
309 cb.ref = ref;
310 cb.cmd = cmd;
311 for_each_reflog_ent(ref, expire_reflog_ent, &cb);
312 finish:
313 if (cb.newlog) {
314 if (fclose(cb.newlog)) {
315 status |= error("%s: %s", strerror(errno),
316 newlog_path);
317 unlink(newlog_path);
318 } else if (cmd->updateref &&
319 (write_in_full(lock->lock_fd,
320 sha1_to_hex(cb.last_kept_sha1), 40) != 40 ||
321 write_in_full(lock->lock_fd, "\n", 1) != 1 ||
322 close_ref(lock) < 0)) {
323 status |= error("Couldn't write %s",
324 lock->lk->filename);
325 unlink(newlog_path);
326 } else if (rename(newlog_path, log_file)) {
327 status |= error("cannot rename %s to %s",
328 newlog_path, log_file);
329 unlink(newlog_path);
330 } else if (cmd->updateref && commit_ref(lock)) {
331 status |= error("Couldn't set %s", lock->ref_name);
332 } else {
333 adjust_shared_perm(log_file);
336 free(newlog_path);
337 free(log_file);
338 unlock_ref(lock);
339 return status;
342 static int collect_reflog(const char *ref, const unsigned char *sha1, int unused, void *cb_data)
344 struct collected_reflog *e;
345 struct collect_reflog_cb *cb = cb_data;
346 size_t namelen = strlen(ref);
348 e = xmalloc(sizeof(*e) + namelen + 1);
349 hashcpy(e->sha1, sha1);
350 memcpy(e->reflog, ref, namelen + 1);
351 ALLOC_GROW(cb->e, cb->nr + 1, cb->alloc);
352 cb->e[cb->nr++] = e;
353 return 0;
356 static struct reflog_expire_cfg {
357 struct reflog_expire_cfg *next;
358 unsigned long expire_total;
359 unsigned long expire_unreachable;
360 size_t len;
361 char pattern[FLEX_ARRAY];
362 } *reflog_expire_cfg, **reflog_expire_cfg_tail;
364 static struct reflog_expire_cfg *find_cfg_ent(const char *pattern, size_t len)
366 struct reflog_expire_cfg *ent;
368 if (!reflog_expire_cfg_tail)
369 reflog_expire_cfg_tail = &reflog_expire_cfg;
371 for (ent = reflog_expire_cfg; ent; ent = ent->next)
372 if (ent->len == len &&
373 !memcmp(ent->pattern, pattern, len))
374 return ent;
376 ent = xcalloc(1, (sizeof(*ent) + len));
377 memcpy(ent->pattern, pattern, len);
378 ent->len = len;
379 *reflog_expire_cfg_tail = ent;
380 reflog_expire_cfg_tail = &(ent->next);
381 return ent;
384 static int parse_expire_cfg_value(const char *var, const char *value, unsigned long *expire)
386 if (!value)
387 return config_error_nonbool(var);
388 if (!strcmp(value, "never") || !strcmp(value, "false")) {
389 *expire = 0;
390 return 0;
392 *expire = approxidate(value);
393 return 0;
396 /* expiry timer slot */
397 #define EXPIRE_TOTAL 01
398 #define EXPIRE_UNREACH 02
400 static int reflog_expire_config(const char *var, const char *value, void *cb)
402 const char *lastdot = strrchr(var, '.');
403 unsigned long expire;
404 int slot;
405 struct reflog_expire_cfg *ent;
407 if (!lastdot || prefixcmp(var, "gc."))
408 return git_default_config(var, value, cb);
410 if (!strcmp(lastdot, ".reflogexpire")) {
411 slot = EXPIRE_TOTAL;
412 if (parse_expire_cfg_value(var, value, &expire))
413 return -1;
414 } else if (!strcmp(lastdot, ".reflogexpireunreachable")) {
415 slot = EXPIRE_UNREACH;
416 if (parse_expire_cfg_value(var, value, &expire))
417 return -1;
418 } else
419 return git_default_config(var, value, cb);
421 if (lastdot == var + 2) {
422 switch (slot) {
423 case EXPIRE_TOTAL:
424 default_reflog_expire = expire;
425 break;
426 case EXPIRE_UNREACH:
427 default_reflog_expire_unreachable = expire;
428 break;
430 return 0;
433 ent = find_cfg_ent(var + 3, lastdot - (var+3));
434 if (!ent)
435 return -1;
436 switch (slot) {
437 case EXPIRE_TOTAL:
438 ent->expire_total = expire;
439 break;
440 case EXPIRE_UNREACH:
441 ent->expire_unreachable = expire;
442 break;
444 return 0;
447 static void set_reflog_expiry_param(struct cmd_reflog_expire_cb *cb, int slot, const char *ref)
449 struct reflog_expire_cfg *ent;
451 if (slot == (EXPIRE_TOTAL|EXPIRE_UNREACH))
452 return; /* both given explicitly -- nothing to tweak */
454 for (ent = reflog_expire_cfg; ent; ent = ent->next) {
455 if (!fnmatch(ent->pattern, ref, 0)) {
456 if (!(slot & EXPIRE_TOTAL))
457 cb->expire_total = ent->expire_total;
458 if (!(slot & EXPIRE_UNREACH))
459 cb->expire_unreachable = ent->expire_unreachable;
460 return;
465 * If unconfigured, make stash never expire
467 if (!strcmp(ref, "refs/stash")) {
468 if (!(slot & EXPIRE_TOTAL))
469 cb->expire_total = 0;
470 if (!(slot & EXPIRE_UNREACH))
471 cb->expire_unreachable = 0;
472 return;
475 /* Nothing matched -- use the default value */
476 if (!(slot & EXPIRE_TOTAL))
477 cb->expire_total = default_reflog_expire;
478 if (!(slot & EXPIRE_UNREACH))
479 cb->expire_unreachable = default_reflog_expire_unreachable;
482 static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
484 struct cmd_reflog_expire_cb cb;
485 unsigned long now = time(NULL);
486 int i, status, do_all;
487 int explicit_expiry = 0;
489 git_config(reflog_expire_config, NULL);
491 save_commit_buffer = 0;
492 do_all = status = 0;
493 memset(&cb, 0, sizeof(cb));
495 if (!default_reflog_expire_unreachable)
496 default_reflog_expire_unreachable = now - 30 * 24 * 3600;
497 if (!default_reflog_expire)
498 default_reflog_expire = now - 90 * 24 * 3600;
499 cb.expire_total = default_reflog_expire;
500 cb.expire_unreachable = default_reflog_expire_unreachable;
502 for (i = 1; i < argc; i++) {
503 const char *arg = argv[i];
504 if (!strcmp(arg, "--dry-run") || !strcmp(arg, "-n"))
505 cb.dry_run = 1;
506 else if (!prefixcmp(arg, "--expire=")) {
507 cb.expire_total = approxidate(arg + 9);
508 explicit_expiry |= EXPIRE_TOTAL;
510 else if (!prefixcmp(arg, "--expire-unreachable=")) {
511 cb.expire_unreachable = approxidate(arg + 21);
512 explicit_expiry |= EXPIRE_UNREACH;
514 else if (!strcmp(arg, "--stale-fix"))
515 cb.stalefix = 1;
516 else if (!strcmp(arg, "--rewrite"))
517 cb.rewrite = 1;
518 else if (!strcmp(arg, "--updateref"))
519 cb.updateref = 1;
520 else if (!strcmp(arg, "--all"))
521 do_all = 1;
522 else if (!strcmp(arg, "--verbose"))
523 cb.verbose = 1;
524 else if (!strcmp(arg, "--")) {
525 i++;
526 break;
528 else if (arg[0] == '-')
529 usage(reflog_expire_usage);
530 else
531 break;
535 * We can trust the commits and objects reachable from refs
536 * even in older repository. We cannot trust what's reachable
537 * from reflog if the repository was pruned with older git.
539 if (cb.stalefix) {
540 init_revisions(&cb.revs, prefix);
541 if (cb.verbose)
542 printf("Marking reachable objects...");
543 mark_reachable_objects(&cb.revs, 0);
544 if (cb.verbose)
545 putchar('\n');
548 if (do_all) {
549 struct collect_reflog_cb collected;
550 int i;
552 memset(&collected, 0, sizeof(collected));
553 for_each_reflog(collect_reflog, &collected);
554 for (i = 0; i < collected.nr; i++) {
555 struct collected_reflog *e = collected.e[i];
556 set_reflog_expiry_param(&cb, explicit_expiry, e->reflog);
557 status |= expire_reflog(e->reflog, e->sha1, 0, &cb);
558 free(e);
560 free(collected.e);
563 for (; i < argc; i++) {
564 char *ref;
565 unsigned char sha1[20];
566 if (!dwim_log(argv[i], strlen(argv[i]), sha1, &ref)) {
567 status |= error("%s points nowhere!", argv[i]);
568 continue;
570 set_reflog_expiry_param(&cb, explicit_expiry, ref);
571 status |= expire_reflog(ref, sha1, 0, &cb);
573 return status;
576 static int count_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
577 const char *email, unsigned long timestamp, int tz,
578 const char *message, void *cb_data)
580 struct cmd_reflog_expire_cb *cb = cb_data;
581 if (!cb->expire_total || timestamp < cb->expire_total)
582 cb->recno++;
583 return 0;
586 static int cmd_reflog_delete(int argc, const char **argv, const char *prefix)
588 struct cmd_reflog_expire_cb cb;
589 int i, status = 0;
591 memset(&cb, 0, sizeof(cb));
593 for (i = 1; i < argc; i++) {
594 const char *arg = argv[i];
595 if (!strcmp(arg, "--dry-run") || !strcmp(arg, "-n"))
596 cb.dry_run = 1;
597 else if (!strcmp(arg, "--rewrite"))
598 cb.rewrite = 1;
599 else if (!strcmp(arg, "--updateref"))
600 cb.updateref = 1;
601 else if (!strcmp(arg, "--verbose"))
602 cb.verbose = 1;
603 else if (!strcmp(arg, "--")) {
604 i++;
605 break;
607 else if (arg[0] == '-')
608 usage(reflog_delete_usage);
609 else
610 break;
613 if (argc - i < 1)
614 return error("Nothing to delete?");
616 for ( ; i < argc; i++) {
617 const char *spec = strstr(argv[i], "@{");
618 unsigned char sha1[20];
619 char *ep, *ref;
620 int recno;
622 if (!spec) {
623 status |= error("Not a reflog: %s", argv[i]);
624 continue;
627 if (!dwim_log(argv[i], spec - argv[i], sha1, &ref)) {
628 status |= error("no reflog for '%s'", argv[i]);
629 continue;
632 recno = strtoul(spec + 2, &ep, 10);
633 if (*ep == '}') {
634 cb.recno = -recno;
635 for_each_reflog_ent(ref, count_reflog_ent, &cb);
636 } else {
637 cb.expire_total = approxidate(spec + 2);
638 for_each_reflog_ent(ref, count_reflog_ent, &cb);
639 cb.expire_total = 0;
642 status |= expire_reflog(ref, sha1, 0, &cb);
643 free(ref);
645 return status;
649 * main "reflog"
652 static const char reflog_usage[] =
653 "git reflog (expire | ...)";
655 int cmd_reflog(int argc, const char **argv, const char *prefix)
657 /* With no command, we default to showing it. */
658 if (argc < 2 || *argv[1] == '-')
659 return cmd_log_reflog(argc, argv, prefix);
661 if (!strcmp(argv[1], "show"))
662 return cmd_log_reflog(argc - 1, argv + 1, prefix);
664 if (!strcmp(argv[1], "expire"))
665 return cmd_reflog_expire(argc - 1, argv + 1, prefix);
667 if (!strcmp(argv[1], "delete"))
668 return cmd_reflog_delete(argc - 1, argv + 1, prefix);
670 /* Not a recognized reflog command..*/
671 usage(reflog_usage);