Code

Merge branch 'maint'
[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"
11 /*
12  * reflog expire
13  */
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 {
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 };
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 };
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 };
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)
60 {
61         struct tree_desc desc;
62         struct name_entry entry;
63         int complete;
64         struct tree *tree;
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;
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;
97         if (complete)
98                 tree->object.flags |= SEEN;
99         return complete;
102 static int commit_is_complete(struct commit *commit)
104         struct object_array study;
105         struct object_array found;
106         int is_incomplete = 0;
107         int i;
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;
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) {
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;
191 static int keep_entry(struct commit **it, unsigned char *sha1)
193         struct commit *commit;
195         if (is_null_sha1(sha1))
196                 return 1;
197         commit = lookup_commit_reference_gently(sha1, 1);
198         if (!commit)
199                 return 0;
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;
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)
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;
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;
260 static int unreachable(struct expire_reflog_cb *cb, struct commit *commit, unsigned char *sha1)
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;
270                 commit = lookup_commit_reference_gently(sha1, 1);
272                 /* Not a commit -- keep it */
273                 if (!commit)
274                         return 0;
275         }
277         /* Reachable from the current ref?  Don't prune. */
278         if (commit->object.flags & REACHABLE)
279                 return 0;
281         if (cb->mark_list && cb->mark_limit) {
282                 cb->mark_limit = 0; /* dig down to the root */
283                 mark_reachable(cb);
284         }
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)
297                 goto prune;
299         if (cb->cmd->rewrite)
300                 osha1 = cb->last_kept_sha1;
302         old = new = NULL;
303         if (cb->cmd->stalefix &&
304             (!keep_entry(&old, osha1) || !keep_entry(&new, nsha1)))
305                 goto prune;
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         }
314         if (cb->cmd->recno && --(cb->cmd->recno) == 0)
315                 goto prune;
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;
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;
341         int status = 0;
343         memset(&cb, 0, sizeof(cb));
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         }
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;
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);
412         cb->e[cb->nr++] = e;
413         return 0;
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;
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))
434                         return ent;
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;
444 static int parse_expire_cfg_value(const char *var, const char *value, unsigned long *expire)
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;
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;
464         int slot;
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")) {
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);
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         }
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;
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;
520                         return;
521                 }
522         }
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         }
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;
554         do_all = status = 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"))
563                         cb.dry_run = 1;
564                 else if (!prefixcmp(arg, "--expire=")) {
565                         cb.expire_total = approxidate(arg + 9);
566                         explicit_expiry |= EXPIRE_TOTAL;
567                 }
568                 else if (!prefixcmp(arg, "--expire-unreachable=")) {
569                         cb.expire_unreachable = approxidate(arg + 21);
570                         explicit_expiry |= EXPIRE_UNREACH;
571                 }
572                 else if (!strcmp(arg, "--stale-fix"))
573                         cb.stalefix = 1;
574                 else if (!strcmp(arg, "--rewrite"))
575                         cb.rewrite = 1;
576                 else if (!strcmp(arg, "--updateref"))
577                         cb.updateref = 1;
578                 else if (!strcmp(arg, "--all"))
579                         do_all = 1;
580                 else if (!strcmp(arg, "--verbose"))
581                         cb.verbose = 1;
582                 else if (!strcmp(arg, "--")) {
583                         i++;
584                         break;
585                 }
586                 else if (arg[0] == '-')
587                         usage(reflog_expire_usage);
588                 else
589                         break;
590         }
592         /*
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.
596          */
597         if (cb.stalefix) {
598                 init_revisions(&cb.revs, prefix);
599                 if (cb.verbose)
600                         printf("Marking reachable objects...");
601                 mark_reachable_objects(&cb.revs, 0);
602                 if (cb.verbose)
603                         putchar('\n');
604         }
606         if (do_all) {
607                 struct collect_reflog_cb collected;
608                 int i;
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);
616                         free(e);
617                 }
618                 free(collected.e);
619         }
621         for (; i < argc; i++) {
622                 char *ref;
623                 unsigned char sha1[20];
624                 if (!dwim_log(argv[i], strlen(argv[i]), sha1, &ref)) {
625                         status |= error("%s points nowhere!", argv[i]);
626                         continue;
627                 }
628                 set_reflog_expiry_param(&cb, explicit_expiry, ref);
629                 status |= expire_reflog(ref, sha1, 0, &cb);
630         }
631         return status;
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)
640                 cb->recno++;
641         return 0;
644 static int cmd_reflog_delete(int argc, const char **argv, const char *prefix)
646         struct cmd_reflog_expire_cb cb;
647         int i, status = 0;
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"))
654                         cb.dry_run = 1;
655                 else if (!strcmp(arg, "--rewrite"))
656                         cb.rewrite = 1;
657                 else if (!strcmp(arg, "--updateref"))
658                         cb.updateref = 1;
659                 else if (!strcmp(arg, "--verbose"))
660                         cb.verbose = 1;
661                 else if (!strcmp(arg, "--")) {
662                         i++;
663                         break;
664                 }
665                 else if (arg[0] == '-')
666                         usage(reflog_delete_usage);
667                 else
668                         break;
669         }
671         if (argc - i < 1)
672                 return error("Nothing to delete?");
674         for ( ; i < argc; i++) {
675                 const char *spec = strstr(argv[i], "@{");
676                 unsigned char sha1[20];
677                 char *ep, *ref;
678                 int recno;
680                 if (!spec) {
681                         status |= error("Not a reflog: %s", argv[i]);
682                         continue;
683                 }
685                 if (!dwim_log(argv[i], spec - argv[i], sha1, &ref)) {
686                         status |= error("no reflog for '%s'", argv[i]);
687                         continue;
688                 }
690                 recno = strtoul(spec + 2, &ep, 10);
691                 if (*ep == '}') {
692                         cb.recno = -recno;
693                         for_each_reflog_ent(ref, count_reflog_ent, &cb);
694                 } else {
695                         cb.expire_total = approxidate(spec + 2);
696                         for_each_reflog_ent(ref, count_reflog_ent, &cb);
697                         cb.expire_total = 0;
698                 }
700                 status |= expire_reflog(ref, sha1, 0, &cb);
701                 free(ref);
702         }
703         return status;
706 /*
707  * main "reflog"
708  */
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"))
716                 usage(reflog_usage);
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..*/
732         usage(reflog_usage);