]> git.ipfire.org Git - thirdparty/git.git/blob - builtin-reflog.c
more war on "sleep" in tests
[thirdparty/git.git] / builtin-reflog.c
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"
10
11 /*
12 * reflog expire
13 */
14
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>...";
19
20 static unsigned long default_reflog_expire;
21 static unsigned long default_reflog_expire_unreachable;
22
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;
33 };
34
35 struct expire_reflog_cb {
36 FILE *newlog;
37 const char *ref;
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];
43 };
44
45 struct collected_reflog {
46 unsigned char sha1[20];
47 char reflog[FLEX_ARRAY];
48 };
49 struct collect_reflog_cb {
50 struct collected_reflog **e;
51 int alloc;
52 int nr;
53 };
54
55 #define INCOMPLETE (1u<<10)
56 #define STUDYING (1u<<11)
57 #define REACHABLE (1u<<12)
58
59 static int tree_is_complete(const unsigned char *sha1)
60 {
61 struct tree_desc desc;
62 struct name_entry entry;
63 int complete;
64 struct tree *tree;
65
66 tree = lookup_tree(sha1);
67 if (!tree)
68 return 0;
69 if (tree->object.flags & SEEN)
70 return 1;
71 if (tree->object.flags & INCOMPLETE)
72 return 0;
73
74 if (!tree->buffer) {
75 enum object_type type;
76 unsigned long size;
77 void *data = read_sha1_file(sha1, &type, &size);
78 if (!data) {
79 tree->object.flags |= INCOMPLETE;
80 return 0;
81 }
82 tree->buffer = data;
83 tree->size = size;
84 }
85 init_tree_desc(&desc, tree->buffer, tree->size);
86 complete = 1;
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;
91 complete = 0;
92 }
93 }
94 free(tree->buffer);
95 tree->buffer = NULL;
96
97 if (complete)
98 tree->object.flags |= SEEN;
99 return complete;
100 }
101
102 static int commit_is_complete(struct commit *commit)
103 {
104 struct object_array study;
105 struct object_array found;
106 int is_incomplete = 0;
107 int i;
108
109 /* early return */
110 if (commit->object.flags & SEEN)
111 return 1;
112 if (commit->object.flags & INCOMPLETE)
113 return 0;
114 /*
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.
120 */
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;
126 while (study.nr) {
127 struct commit *c;
128 struct commit_list *parent;
129
130 c = (struct commit *)study.objects[--study.nr].item;
131 if (!c->object.parsed && !parse_object(c->object.sha1))
132 c->object.flags |= INCOMPLETE;
133
134 if (c->object.flags & INCOMPLETE) {
135 is_incomplete = 1;
136 break;
137 }
138 else if (c->object.flags & SEEN)
139 continue;
140 for (parent = c->parents; parent; parent = parent->next) {
141 struct commit *p = parent->item;
142 if (p->object.flags & STUDYING)
143 continue;
144 p->object.flags |= STUDYING;
145 add_object_array(&p->object, NULL, &study);
146 add_object_array(&p->object, NULL, &found);
147 }
148 }
149 if (!is_incomplete) {
150 /*
151 * make sure all commits in "found" array have all the
152 * necessary objects.
153 */
154 for (i = 0; i < found.nr; i++) {
155 struct commit *c =
156 (struct commit *)found.objects[i].item;
157 if (!tree_is_complete(c->tree->object.sha1)) {
158 is_incomplete = 1;
159 c->object.flags |= INCOMPLETE;
160 }
161 }
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;
166 }
167 }
168 /* clear flags from the objects we traversed */
169 for (i = 0; i < found.nr; i++)
170 found.objects[i].item->flags &= ~STUDYING;
171 if (is_incomplete)
172 commit->object.flags |= INCOMPLETE;
173 else {
174 /*
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.
181 */
182 for (i = 0; i < found.nr; i++)
183 found.objects[i].item->flags |= SEEN;
184 }
185 /* free object arrays */
186 free(study.objects);
187 free(found.objects);
188 return !is_incomplete;
189 }
190
191 static int keep_entry(struct commit **it, unsigned char *sha1)
192 {
193 struct commit *commit;
194
195 if (is_null_sha1(sha1))
196 return 1;
197 commit = lookup_commit_reference_gently(sha1, 1);
198 if (!commit)
199 return 0;
200
201 /*
202 * Make sure everything in this commit exists.
203 *
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
207 * SEEN as well.
208 */
209 if (!commit_is_complete(commit))
210 return 0;
211 *it = commit;
212 return 1;
213 }
214
215 /*
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.
220 */
221 static void mark_reachable(struct expire_reflog_cb *cb)
222 {
223 struct commit *commit;
224 struct commit_list *pending;
225 unsigned long expire_limit = cb->mark_limit;
226 struct commit_list *leftover = NULL;
227
228 for (pending = cb->mark_list; pending; pending = pending->next)
229 pending->item->object.flags &= ~REACHABLE;
230
231 pending = cb->mark_list;
232 while (pending) {
233 struct commit_list *entry = pending;
234 struct commit_list *parent;
235 pending = entry->next;
236 commit = entry->item;
237 free(entry);
238 if (commit->object.flags & REACHABLE)
239 continue;
240 if (parse_commit(commit))
241 continue;
242 commit->object.flags |= REACHABLE;
243 if (commit->date < expire_limit) {
244 commit_list_insert(commit, &leftover);
245 continue;
246 }
247 commit->object.flags |= REACHABLE;
248 parent = commit->parents;
249 while (parent) {
250 commit = parent->item;
251 parent = parent->next;
252 if (commit->object.flags & REACHABLE)
253 continue;
254 commit_list_insert(commit, &pending);
255 }
256 }
257 cb->mark_list = leftover;
258 }
259
260 static int unreachable(struct expire_reflog_cb *cb, struct commit *commit, unsigned char *sha1)
261 {
262 /*
263 * We may or may not have the commit yet - if not, look it
264 * up using the supplied sha1.
265 */
266 if (!commit) {
267 if (is_null_sha1(sha1))
268 return 0;
269
270 commit = lookup_commit_reference_gently(sha1, 1);
271
272 /* Not a commit -- keep it */
273 if (!commit)
274 return 0;
275 }
276
277 /* Reachable from the current ref? Don't prune. */
278 if (commit->object.flags & REACHABLE)
279 return 0;
280
281 if (cb->mark_list && cb->mark_limit) {
282 cb->mark_limit = 0; /* dig down to the root */
283 mark_reachable(cb);
284 }
285
286 return !(commit->object.flags & REACHABLE);
287 }
288
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)
292 {
293 struct expire_reflog_cb *cb = cb_data;
294 struct commit *old, *new;
295
296 if (timestamp < cb->cmd->expire_total)
297 goto prune;
298
299 if (cb->cmd->rewrite)
300 osha1 = cb->last_kept_sha1;
301
302 old = new = NULL;
303 if (cb->cmd->stalefix &&
304 (!keep_entry(&old, osha1) || !keep_entry(&new, nsha1)))
305 goto prune;
306
307 if (timestamp < cb->cmd->expire_unreachable) {
308 if (!cb->ref_commit)
309 goto prune;
310 if (unreachable(cb, old, osha1) || unreachable(cb, new, nsha1))
311 goto prune;
312 }
313
314 if (cb->cmd->recno && --(cb->cmd->recno) == 0)
315 goto prune;
316
317 if (cb->newlog) {
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,
323 message);
324 hashcpy(cb->last_kept_sha1, nsha1);
325 }
326 if (cb->cmd->verbose)
327 printf("keep %s", message);
328 return 0;
329 prune:
330 if (!cb->newlog || cb->cmd->verbose)
331 printf("%sprune %s", cb->newlog ? "" : "would ", message);
332 return 0;
333 }
334
335 static int expire_reflog(const char *ref, const unsigned char *sha1, int unused, void *cb_data)
336 {
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;
341 int status = 0;
342
343 memset(&cb, 0, sizeof(cb));
344
345 /*
346 * we take the lock for the ref itself to prevent it from
347 * getting updated.
348 */
349 lock = lock_any_ref_for_update(ref, sha1, 0);
350 if (!lock)
351 return error("cannot lock ref '%s'", ref);
352 log_file = git_pathdup("logs/%s", ref);
353 if (!file_exists(log_file))
354 goto finish;
355 if (!cmd->dry_run) {
356 newlog_path = git_pathdup("logs/%s.lock", ref);
357 cb.newlog = fopen(newlog_path, "w");
358 }
359
360 cb.ref_commit = lookup_commit_reference_gently(sha1, 1);
361 cb.ref = ref;
362 cb.cmd = cmd;
363 if (cb.ref_commit) {
364 cb.mark_list = NULL;
365 commit_list_insert(cb.ref_commit, &cb.mark_list);
366 cb.mark_limit = cmd->expire_total;
367 mark_reachable(&cb);
368 }
369 for_each_reflog_ent(ref, expire_reflog_ent, &cb);
370 if (cb.ref_commit)
371 clear_commit_marks(cb.ref_commit, REACHABLE);
372 finish:
373 if (cb.newlog) {
374 if (fclose(cb.newlog)) {
375 status |= error("%s: %s", strerror(errno),
376 newlog_path);
377 unlink(newlog_path);
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",
384 lock->lk->filename);
385 unlink(newlog_path);
386 } else if (rename(newlog_path, log_file)) {
387 status |= error("cannot rename %s to %s",
388 newlog_path, log_file);
389 unlink(newlog_path);
390 } else if (cmd->updateref && commit_ref(lock)) {
391 status |= error("Couldn't set %s", lock->ref_name);
392 } else {
393 adjust_shared_perm(log_file);
394 }
395 }
396 free(newlog_path);
397 free(log_file);
398 unlock_ref(lock);
399 return status;
400 }
401
402 static int collect_reflog(const char *ref, const unsigned char *sha1, int unused, void *cb_data)
403 {
404 struct collected_reflog *e;
405 struct collect_reflog_cb *cb = cb_data;
406 size_t namelen = strlen(ref);
407
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);
412 cb->e[cb->nr++] = e;
413 return 0;
414 }
415
416 static struct reflog_expire_cfg {
417 struct reflog_expire_cfg *next;
418 unsigned long expire_total;
419 unsigned long expire_unreachable;
420 size_t len;
421 char pattern[FLEX_ARRAY];
422 } *reflog_expire_cfg, **reflog_expire_cfg_tail;
423
424 static struct reflog_expire_cfg *find_cfg_ent(const char *pattern, size_t len)
425 {
426 struct reflog_expire_cfg *ent;
427
428 if (!reflog_expire_cfg_tail)
429 reflog_expire_cfg_tail = &reflog_expire_cfg;
430
431 for (ent = reflog_expire_cfg; ent; ent = ent->next)
432 if (ent->len == len &&
433 !memcmp(ent->pattern, pattern, len))
434 return ent;
435
436 ent = xcalloc(1, (sizeof(*ent) + len));
437 memcpy(ent->pattern, pattern, len);
438 ent->len = len;
439 *reflog_expire_cfg_tail = ent;
440 reflog_expire_cfg_tail = &(ent->next);
441 return ent;
442 }
443
444 static int parse_expire_cfg_value(const char *var, const char *value, unsigned long *expire)
445 {
446 if (!value)
447 return config_error_nonbool(var);
448 if (!strcmp(value, "never") || !strcmp(value, "false")) {
449 *expire = 0;
450 return 0;
451 }
452 *expire = approxidate(value);
453 return 0;
454 }
455
456 /* expiry timer slot */
457 #define EXPIRE_TOTAL 01
458 #define EXPIRE_UNREACH 02
459
460 static int reflog_expire_config(const char *var, const char *value, void *cb)
461 {
462 const char *lastdot = strrchr(var, '.');
463 unsigned long expire;
464 int slot;
465 struct reflog_expire_cfg *ent;
466
467 if (!lastdot || prefixcmp(var, "gc."))
468 return git_default_config(var, value, cb);
469
470 if (!strcmp(lastdot, ".reflogexpire")) {
471 slot = EXPIRE_TOTAL;
472 if (parse_expire_cfg_value(var, value, &expire))
473 return -1;
474 } else if (!strcmp(lastdot, ".reflogexpireunreachable")) {
475 slot = EXPIRE_UNREACH;
476 if (parse_expire_cfg_value(var, value, &expire))
477 return -1;
478 } else
479 return git_default_config(var, value, cb);
480
481 if (lastdot == var + 2) {
482 switch (slot) {
483 case EXPIRE_TOTAL:
484 default_reflog_expire = expire;
485 break;
486 case EXPIRE_UNREACH:
487 default_reflog_expire_unreachable = expire;
488 break;
489 }
490 return 0;
491 }
492
493 ent = find_cfg_ent(var + 3, lastdot - (var+3));
494 if (!ent)
495 return -1;
496 switch (slot) {
497 case EXPIRE_TOTAL:
498 ent->expire_total = expire;
499 break;
500 case EXPIRE_UNREACH:
501 ent->expire_unreachable = expire;
502 break;
503 }
504 return 0;
505 }
506
507 static void set_reflog_expiry_param(struct cmd_reflog_expire_cb *cb, int slot, const char *ref)
508 {
509 struct reflog_expire_cfg *ent;
510
511 if (slot == (EXPIRE_TOTAL|EXPIRE_UNREACH))
512 return; /* both given explicitly -- nothing to tweak */
513
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;
520 return;
521 }
522 }
523
524 /*
525 * If unconfigured, make stash never expire
526 */
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;
532 return;
533 }
534
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;
540 }
541
542 static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
543 {
544 struct cmd_reflog_expire_cb cb;
545 unsigned long now = time(NULL);
546 int i, status, do_all;
547 int explicit_expiry = 0;
548
549 git_config(reflog_expire_config, NULL);
550
551 save_commit_buffer = 0;
552 do_all = status = 0;
553 memset(&cb, 0, sizeof(cb));
554
555 if (!default_reflog_expire_unreachable)
556 default_reflog_expire_unreachable = now - 30 * 24 * 3600;
557 if (!default_reflog_expire)
558 default_reflog_expire = now - 90 * 24 * 3600;
559 cb.expire_total = default_reflog_expire;
560 cb.expire_unreachable = default_reflog_expire_unreachable;
561
562 for (i = 1; i < argc; i++) {
563 const char *arg = argv[i];
564 if (!strcmp(arg, "--dry-run") || !strcmp(arg, "-n"))
565 cb.dry_run = 1;
566 else if (!prefixcmp(arg, "--expire=")) {
567 cb.expire_total = approxidate(arg + 9);
568 explicit_expiry |= EXPIRE_TOTAL;
569 }
570 else if (!prefixcmp(arg, "--expire-unreachable=")) {
571 cb.expire_unreachable = approxidate(arg + 21);
572 explicit_expiry |= EXPIRE_UNREACH;
573 }
574 else if (!strcmp(arg, "--stale-fix"))
575 cb.stalefix = 1;
576 else if (!strcmp(arg, "--rewrite"))
577 cb.rewrite = 1;
578 else if (!strcmp(arg, "--updateref"))
579 cb.updateref = 1;
580 else if (!strcmp(arg, "--all"))
581 do_all = 1;
582 else if (!strcmp(arg, "--verbose"))
583 cb.verbose = 1;
584 else if (!strcmp(arg, "--")) {
585 i++;
586 break;
587 }
588 else if (arg[0] == '-')
589 usage(reflog_expire_usage);
590 else
591 break;
592 }
593
594 /*
595 * We can trust the commits and objects reachable from refs
596 * even in older repository. We cannot trust what's reachable
597 * from reflog if the repository was pruned with older git.
598 */
599 if (cb.stalefix) {
600 init_revisions(&cb.revs, prefix);
601 if (cb.verbose)
602 printf("Marking reachable objects...");
603 mark_reachable_objects(&cb.revs, 0);
604 if (cb.verbose)
605 putchar('\n');
606 }
607
608 if (do_all) {
609 struct collect_reflog_cb collected;
610 int i;
611
612 memset(&collected, 0, sizeof(collected));
613 for_each_reflog(collect_reflog, &collected);
614 for (i = 0; i < collected.nr; i++) {
615 struct collected_reflog *e = collected.e[i];
616 set_reflog_expiry_param(&cb, explicit_expiry, e->reflog);
617 status |= expire_reflog(e->reflog, e->sha1, 0, &cb);
618 free(e);
619 }
620 free(collected.e);
621 }
622
623 for (; i < argc; i++) {
624 char *ref;
625 unsigned char sha1[20];
626 if (!dwim_log(argv[i], strlen(argv[i]), sha1, &ref)) {
627 status |= error("%s points nowhere!", argv[i]);
628 continue;
629 }
630 set_reflog_expiry_param(&cb, explicit_expiry, ref);
631 status |= expire_reflog(ref, sha1, 0, &cb);
632 }
633 return status;
634 }
635
636 static int count_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
637 const char *email, unsigned long timestamp, int tz,
638 const char *message, void *cb_data)
639 {
640 struct cmd_reflog_expire_cb *cb = cb_data;
641 if (!cb->expire_total || timestamp < cb->expire_total)
642 cb->recno++;
643 return 0;
644 }
645
646 static int cmd_reflog_delete(int argc, const char **argv, const char *prefix)
647 {
648 struct cmd_reflog_expire_cb cb;
649 int i, status = 0;
650
651 memset(&cb, 0, sizeof(cb));
652
653 for (i = 1; i < argc; i++) {
654 const char *arg = argv[i];
655 if (!strcmp(arg, "--dry-run") || !strcmp(arg, "-n"))
656 cb.dry_run = 1;
657 else if (!strcmp(arg, "--rewrite"))
658 cb.rewrite = 1;
659 else if (!strcmp(arg, "--updateref"))
660 cb.updateref = 1;
661 else if (!strcmp(arg, "--verbose"))
662 cb.verbose = 1;
663 else if (!strcmp(arg, "--")) {
664 i++;
665 break;
666 }
667 else if (arg[0] == '-')
668 usage(reflog_delete_usage);
669 else
670 break;
671 }
672
673 if (argc - i < 1)
674 return error("Nothing to delete?");
675
676 for ( ; i < argc; i++) {
677 const char *spec = strstr(argv[i], "@{");
678 unsigned char sha1[20];
679 char *ep, *ref;
680 int recno;
681
682 if (!spec) {
683 status |= error("Not a reflog: %s", argv[i]);
684 continue;
685 }
686
687 if (!dwim_log(argv[i], spec - argv[i], sha1, &ref)) {
688 status |= error("no reflog for '%s'", argv[i]);
689 continue;
690 }
691
692 recno = strtoul(spec + 2, &ep, 10);
693 if (*ep == '}') {
694 cb.recno = -recno;
695 for_each_reflog_ent(ref, count_reflog_ent, &cb);
696 } else {
697 cb.expire_total = approxidate(spec + 2);
698 for_each_reflog_ent(ref, count_reflog_ent, &cb);
699 cb.expire_total = 0;
700 }
701
702 status |= expire_reflog(ref, sha1, 0, &cb);
703 free(ref);
704 }
705 return status;
706 }
707
708 /*
709 * main "reflog"
710 */
711
712 static const char reflog_usage[] =
713 "git reflog [ show | expire | delete ]";
714
715 int cmd_reflog(int argc, const char **argv, const char *prefix)
716 {
717 if (argc > 1 && !strcmp(argv[1], "-h"))
718 usage(reflog_usage);
719
720 /* With no command, we default to showing it. */
721 if (argc < 2 || *argv[1] == '-')
722 return cmd_log_reflog(argc, argv, prefix);
723
724 if (!strcmp(argv[1], "show"))
725 return cmd_log_reflog(argc - 1, argv + 1, prefix);
726
727 if (!strcmp(argv[1], "expire"))
728 return cmd_reflog_expire(argc - 1, argv + 1, prefix);
729
730 if (!strcmp(argv[1], "delete"))
731 return cmd_reflog_delete(argc - 1, argv + 1, prefix);
732
733 /* Not a recognized reflog command..*/
734 usage(reflog_usage);
735 }