Add a reminder test case for a merge with F/D transition
[tgit.git] / builtin-reflog.c
blobddfdf5a3cbc79003dfbad14af8d1f047e3594aa6
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)
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;
61 int complete;
62 struct tree *tree;
64 tree = lookup_tree(sha1);
65 if (!tree)
66 return 0;
67 if (tree->object.flags & SEEN)
68 return 1;
69 if (tree->object.flags & INCOMPLETE)
70 return 0;
72 if (!tree->buffer) {
73 enum object_type type;
74 unsigned long size;
75 void *data = read_sha1_file(sha1, &type, &size);
76 if (!data) {
77 tree->object.flags |= INCOMPLETE;
78 return 0;
80 tree->buffer = data;
81 tree->size = size;
83 init_tree_desc(&desc, tree->buffer, tree->size);
84 complete = 1;
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;
89 complete = 0;
92 free(tree->buffer);
93 tree->buffer = NULL;
95 if (complete)
96 tree->object.flags |= SEEN;
97 return complete;
100 static int commit_is_complete(struct commit *commit)
102 struct object_array study;
103 struct object_array found;
104 int is_incomplete = 0;
105 int i;
107 /* early return */
108 if (commit->object.flags & SEEN)
109 return 1;
110 if (commit->object.flags & INCOMPLETE)
111 return 0;
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;
124 while (study.nr) {
125 struct commit *c;
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) {
133 is_incomplete = 1;
134 break;
136 else if (c->object.flags & SEEN)
137 continue;
138 for (parent = c->parents; parent; parent = parent->next) {
139 struct commit *p = parent->item;
140 if (p->object.flags & STUDYING)
141 continue;
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
150 * necessary objects.
152 for (i = 0; i < found.nr; i++) {
153 struct commit *c =
154 (struct commit *)found.objects[i].item;
155 if (!tree_is_complete(c->tree->object.sha1)) {
156 is_incomplete = 1;
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;
169 if (is_incomplete)
170 commit->object.flags |= INCOMPLETE;
171 else {
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 */
184 free(study.objects);
185 free(found.objects);
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))
194 return 1;
195 commit = lookup_commit_reference_gently(sha1, 1);
196 if (!commit)
197 return 0;
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
205 * SEEN as well.
207 if (!commit_is_complete(commit))
208 return 0;
209 *it = commit;
210 return 1;
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.
219 if (!commit) {
220 if (is_null_sha1(sha1))
221 return 0;
223 commit = lookup_commit_reference_gently(sha1, 1);
225 /* Not a commit -- keep it */
226 if (!commit)
227 return 0;
230 /* Reachable from the current ref? Don't prune. */
231 if (commit->object.flags & REACHABLE)
232 return 0;
233 if (in_merge_bases(commit, &cb->ref_commit, 1))
234 return 0;
236 /* We can't reach it - prune it. */
237 return 1;
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()
248 * at all.
250 struct commit_list *pending = NULL;
252 commit_list_insert(commit, &pending);
253 while (pending) {
254 struct commit_list *entry = pending;
255 struct commit_list *parent;
256 pending = entry->next;
257 commit = entry->item;
258 free(entry);
259 if (commit->object.flags & REACHABLE)
260 continue;
261 if (parse_commit(commit))
262 continue;
263 commit->object.flags |= REACHABLE;
264 if (commit->date < expire_limit)
265 continue;
266 parent = commit->parents;
267 while (parent) {
268 commit = parent->item;
269 parent = parent->next;
270 if (commit->object.flags & REACHABLE)
271 continue;
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)
285 goto prune;
287 if (cb->cmd->rewrite)
288 osha1 = cb->last_kept_sha1;
290 old = new = NULL;
291 if (cb->cmd->stalefix &&
292 (!keep_entry(&old, osha1) || !keep_entry(&new, nsha1)))
293 goto prune;
295 if (timestamp < cb->cmd->expire_unreachable) {
296 if (!cb->ref_commit)
297 goto prune;
298 if (unreachable(cb, old, osha1) || unreachable(cb, new, nsha1))
299 goto prune;
302 if (cb->cmd->recno && --(cb->cmd->recno) == 0)
303 goto prune;
305 if (cb->newlog) {
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,
311 message);
312 hashcpy(cb->last_kept_sha1, nsha1);
314 if (cb->cmd->verbose)
315 printf("keep %s", message);
316 return 0;
317 prune:
318 if (!cb->newlog || cb->cmd->verbose)
319 printf("%sprune %s", cb->newlog ? "" : "would ", message);
320 return 0;
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;
329 int status = 0;
331 memset(&cb, 0, sizeof(cb));
334 * we take the lock for the ref itself to prevent it from
335 * getting updated.
337 lock = lock_any_ref_for_update(ref, sha1, 0);
338 if (!lock)
339 return error("cannot lock ref '%s'", ref);
340 log_file = git_pathdup("logs/%s", ref);
341 if (!file_exists(log_file))
342 goto finish;
343 if (!cmd->dry_run) {
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);
349 cb.ref = ref;
350 cb.cmd = cmd;
351 if (cb.ref_commit)
352 mark_reachable(cb.ref_commit, cmd->expire_total);
353 for_each_reflog_ent(ref, expire_reflog_ent, &cb);
354 if (cb.ref_commit)
355 clear_commit_marks(cb.ref_commit, REACHABLE);
356 finish:
357 if (cb.newlog) {
358 if (fclose(cb.newlog)) {
359 status |= error("%s: %s", strerror(errno),
360 newlog_path);
361 unlink(newlog_path);
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",
368 lock->lk->filename);
369 unlink(newlog_path);
370 } else if (rename(newlog_path, log_file)) {
371 status |= error("cannot rename %s to %s",
372 newlog_path, log_file);
373 unlink(newlog_path);
374 } else if (cmd->updateref && commit_ref(lock)) {
375 status |= error("Couldn't set %s", lock->ref_name);
376 } else {
377 adjust_shared_perm(log_file);
380 free(newlog_path);
381 free(log_file);
382 unlock_ref(lock);
383 return status;
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);
396 cb->e[cb->nr++] = e;
397 return 0;
400 static struct reflog_expire_cfg {
401 struct reflog_expire_cfg *next;
402 unsigned long expire_total;
403 unsigned long expire_unreachable;
404 size_t len;
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))
418 return ent;
420 ent = xcalloc(1, (sizeof(*ent) + len));
421 memcpy(ent->pattern, pattern, len);
422 ent->len = len;
423 *reflog_expire_cfg_tail = ent;
424 reflog_expire_cfg_tail = &(ent->next);
425 return ent;
428 static int parse_expire_cfg_value(const char *var, const char *value, unsigned long *expire)
430 if (!value)
431 return config_error_nonbool(var);
432 if (!strcmp(value, "never") || !strcmp(value, "false")) {
433 *expire = 0;
434 return 0;
436 *expire = approxidate(value);
437 return 0;
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;
448 int slot;
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")) {
455 slot = EXPIRE_TOTAL;
456 if (parse_expire_cfg_value(var, value, &expire))
457 return -1;
458 } else if (!strcmp(lastdot, ".reflogexpireunreachable")) {
459 slot = EXPIRE_UNREACH;
460 if (parse_expire_cfg_value(var, value, &expire))
461 return -1;
462 } else
463 return git_default_config(var, value, cb);
465 if (lastdot == var + 2) {
466 switch (slot) {
467 case EXPIRE_TOTAL:
468 default_reflog_expire = expire;
469 break;
470 case EXPIRE_UNREACH:
471 default_reflog_expire_unreachable = expire;
472 break;
474 return 0;
477 ent = find_cfg_ent(var + 3, lastdot - (var+3));
478 if (!ent)
479 return -1;
480 switch (slot) {
481 case EXPIRE_TOTAL:
482 ent->expire_total = expire;
483 break;
484 case EXPIRE_UNREACH:
485 ent->expire_unreachable = expire;
486 break;
488 return 0;
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;
504 return;
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;
516 return;
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;
536 do_all = status = 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"))
549 cb.dry_run = 1;
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"))
559 cb.stalefix = 1;
560 else if (!strcmp(arg, "--rewrite"))
561 cb.rewrite = 1;
562 else if (!strcmp(arg, "--updateref"))
563 cb.updateref = 1;
564 else if (!strcmp(arg, "--all"))
565 do_all = 1;
566 else if (!strcmp(arg, "--verbose"))
567 cb.verbose = 1;
568 else if (!strcmp(arg, "--")) {
569 i++;
570 break;
572 else if (arg[0] == '-')
573 usage(reflog_expire_usage);
574 else
575 break;
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.
583 if (cb.stalefix) {
584 init_revisions(&cb.revs, prefix);
585 if (cb.verbose)
586 printf("Marking reachable objects...");
587 mark_reachable_objects(&cb.revs, 0);
588 if (cb.verbose)
589 putchar('\n');
592 if (do_all) {
593 struct collect_reflog_cb collected;
594 int i;
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);
602 free(e);
604 free(collected.e);
607 for (; i < argc; i++) {
608 char *ref;
609 unsigned char sha1[20];
610 if (!dwim_log(argv[i], strlen(argv[i]), sha1, &ref)) {
611 status |= error("%s points nowhere!", argv[i]);
612 continue;
614 set_reflog_expiry_param(&cb, explicit_expiry, ref);
615 status |= expire_reflog(ref, sha1, 0, &cb);
617 return status;
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)
626 cb->recno++;
627 return 0;
630 static int cmd_reflog_delete(int argc, const char **argv, const char *prefix)
632 struct cmd_reflog_expire_cb cb;
633 int i, status = 0;
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"))
640 cb.dry_run = 1;
641 else if (!strcmp(arg, "--rewrite"))
642 cb.rewrite = 1;
643 else if (!strcmp(arg, "--updateref"))
644 cb.updateref = 1;
645 else if (!strcmp(arg, "--verbose"))
646 cb.verbose = 1;
647 else if (!strcmp(arg, "--")) {
648 i++;
649 break;
651 else if (arg[0] == '-')
652 usage(reflog_delete_usage);
653 else
654 break;
657 if (argc - i < 1)
658 return error("Nothing to delete?");
660 for ( ; i < argc; i++) {
661 const char *spec = strstr(argv[i], "@{");
662 unsigned char sha1[20];
663 char *ep, *ref;
664 int recno;
666 if (!spec) {
667 status |= error("Not a reflog: %s", argv[i]);
668 continue;
671 if (!dwim_log(argv[i], spec - argv[i], sha1, &ref)) {
672 status |= error("no reflog for '%s'", argv[i]);
673 continue;
676 recno = strtoul(spec + 2, &ep, 10);
677 if (*ep == '}') {
678 cb.recno = -recno;
679 for_each_reflog_ent(ref, count_reflog_ent, &cb);
680 } else {
681 cb.expire_total = approxidate(spec + 2);
682 for_each_reflog_ent(ref, count_reflog_ent, &cb);
683 cb.expire_total = 0;
686 status |= expire_reflog(ref, sha1, 0, &cb);
687 free(ref);
689 return status;
693 * main "reflog"
696 static const char reflog_usage[] =
697 "git reflog (expire | ...)";
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..*/
715 usage(reflog_usage);