Teach "git reflog" a subcommand to delete single entries
[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
18 static unsigned long default_reflog_expire;
19 static unsigned long default_reflog_expire_unreachable;
20
21 struct cmd_reflog_expire_cb {
22         struct rev_info revs;
23         int dry_run;
24         int stalefix;
25         int verbose;
26         unsigned long expire_total;
27         unsigned long expire_unreachable;
28         int recno;
29 };
30
31 struct expire_reflog_cb {
32         FILE *newlog;
33         const char *ref;
34         struct commit *ref_commit;
35         struct cmd_reflog_expire_cb *cmd;
36 };
37
38 #define INCOMPLETE      (1u<<10)
39 #define STUDYING        (1u<<11)
40
41 static int tree_is_complete(const unsigned char *sha1)
42 {
43         struct tree_desc desc;
44         struct name_entry entry;
45         int complete;
46         struct tree *tree;
47
48         tree = lookup_tree(sha1);
49         if (!tree)
50                 return 0;
51         if (tree->object.flags & SEEN)
52                 return 1;
53         if (tree->object.flags & INCOMPLETE)
54                 return 0;
55
56         if (!tree->buffer) {
57                 enum object_type type;
58                 unsigned long size;
59                 void *data = read_sha1_file(sha1, &type, &size);
60                 if (!data) {
61                         tree->object.flags |= INCOMPLETE;
62                         return 0;
63                 }
64                 tree->buffer = data;
65                 tree->size = size;
66         }
67         init_tree_desc(&desc, tree->buffer, tree->size);
68         complete = 1;
69         while (tree_entry(&desc, &entry)) {
70                 if (!has_sha1_file(entry.sha1) ||
71                     (S_ISDIR(entry.mode) && !tree_is_complete(entry.sha1))) {
72                         tree->object.flags |= INCOMPLETE;
73                         complete = 0;
74                 }
75         }
76         free(tree->buffer);
77         tree->buffer = NULL;
78
79         if (complete)
80                 tree->object.flags |= SEEN;
81         return complete;
82 }
83
84 static int commit_is_complete(struct commit *commit)
85 {
86         struct object_array study;
87         struct object_array found;
88         int is_incomplete = 0;
89         int i;
90
91         /* early return */
92         if (commit->object.flags & SEEN)
93                 return 1;
94         if (commit->object.flags & INCOMPLETE)
95                 return 0;
96         /*
97          * Find all commits that are reachable and are not marked as
98          * SEEN.  Then make sure the trees and blobs contained are
99          * complete.  After that, mark these commits also as SEEN.
100          * If some of the objects that are needed to complete this
101          * commit are missing, mark this commit as INCOMPLETE.
102          */
103         memset(&study, 0, sizeof(study));
104         memset(&found, 0, sizeof(found));
105         add_object_array(&commit->object, NULL, &study);
106         add_object_array(&commit->object, NULL, &found);
107         commit->object.flags |= STUDYING;
108         while (study.nr) {
109                 struct commit *c;
110                 struct commit_list *parent;
111
112                 c = (struct commit *)study.objects[--study.nr].item;
113                 if (!c->object.parsed && !parse_object(c->object.sha1))
114                         c->object.flags |= INCOMPLETE;
115
116                 if (c->object.flags & INCOMPLETE) {
117                         is_incomplete = 1;
118                         break;
119                 }
120                 else if (c->object.flags & SEEN)
121                         continue;
122                 for (parent = c->parents; parent; parent = parent->next) {
123                         struct commit *p = parent->item;
124                         if (p->object.flags & STUDYING)
125                                 continue;
126                         p->object.flags |= STUDYING;
127                         add_object_array(&p->object, NULL, &study);
128                         add_object_array(&p->object, NULL, &found);
129                 }
130         }
131         if (!is_incomplete) {
132                 /*
133                  * make sure all commits in "found" array have all the
134                  * necessary objects.
135                  */
136                 for (i = 0; i < found.nr; i++) {
137                         struct commit *c =
138                                 (struct commit *)found.objects[i].item;
139                         if (!tree_is_complete(c->tree->object.sha1)) {
140                                 is_incomplete = 1;
141                                 c->object.flags |= INCOMPLETE;
142                         }
143                 }
144                 if (!is_incomplete) {
145                         /* mark all found commits as complete, iow SEEN */
146                         for (i = 0; i < found.nr; i++)
147                                 found.objects[i].item->flags |= SEEN;
148                 }
149         }
150         /* clear flags from the objects we traversed */
151         for (i = 0; i < found.nr; i++)
152                 found.objects[i].item->flags &= ~STUDYING;
153         if (is_incomplete)
154                 commit->object.flags |= INCOMPLETE;
155         else {
156                 /*
157                  * If we come here, we have (1) traversed the ancestry chain
158                  * from the "commit" until we reach SEEN commits (which are
159                  * known to be complete), and (2) made sure that the commits
160                  * encountered during the above traversal refer to trees that
161                  * are complete.  Which means that we know *all* the commits
162                  * we have seen during this process are complete.
163                  */
164                 for (i = 0; i < found.nr; i++)
165                         found.objects[i].item->flags |= SEEN;
166         }
167         /* free object arrays */
168         free(study.objects);
169         free(found.objects);
170         return !is_incomplete;
171 }
172
173 static int keep_entry(struct commit **it, unsigned char *sha1)
174 {
175         struct commit *commit;
176
177         if (is_null_sha1(sha1))
178                 return 1;
179         commit = lookup_commit_reference_gently(sha1, 1);
180         if (!commit)
181                 return 0;
182
183         /*
184          * Make sure everything in this commit exists.
185          *
186          * We have walked all the objects reachable from the refs
187          * and cache earlier.  The commits reachable by this commit
188          * must meet SEEN commits -- and then we should mark them as
189          * SEEN as well.
190          */
191         if (!commit_is_complete(commit))
192                 return 0;
193         *it = commit;
194         return 1;
195 }
196
197 static int expire_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
198                 const char *email, unsigned long timestamp, int tz,
199                 const char *message, void *cb_data)
200 {
201         struct expire_reflog_cb *cb = cb_data;
202         struct commit *old, *new;
203
204         if (timestamp < cb->cmd->expire_total)
205                 goto prune;
206
207         old = new = NULL;
208         if (cb->cmd->stalefix &&
209             (!keep_entry(&old, osha1) || !keep_entry(&new, nsha1)))
210                 goto prune;
211
212         if (timestamp < cb->cmd->expire_unreachable) {
213                 if (!cb->ref_commit)
214                         goto prune;
215                 if (!old && !is_null_sha1(osha1))
216                         old = lookup_commit_reference_gently(osha1, 1);
217                 if (!new && !is_null_sha1(nsha1))
218                         new = lookup_commit_reference_gently(nsha1, 1);
219                 if ((old && !in_merge_bases(old, &cb->ref_commit, 1)) ||
220                     (new && !in_merge_bases(new, &cb->ref_commit, 1)))
221                         goto prune;
222         }
223
224         if (cb->cmd->recno && --(cb->cmd->recno) == 0)
225                 goto prune;
226
227         if (cb->newlog) {
228                 char sign = (tz < 0) ? '-' : '+';
229                 int zone = (tz < 0) ? (-tz) : tz;
230                 fprintf(cb->newlog, "%s %s %s %lu %c%04d\t%s",
231                         sha1_to_hex(osha1), sha1_to_hex(nsha1),
232                         email, timestamp, sign, zone,
233                         message);
234         }
235         if (cb->cmd->verbose)
236                 printf("keep %s", message);
237         return 0;
238  prune:
239         if (!cb->newlog || cb->cmd->verbose)
240                 printf("%sprune %s", cb->newlog ? "" : "would ", message);
241         return 0;
242 }
243
244 static int expire_reflog(const char *ref, const unsigned char *sha1, int unused, void *cb_data)
245 {
246         struct cmd_reflog_expire_cb *cmd = cb_data;
247         struct expire_reflog_cb cb;
248         struct ref_lock *lock;
249         char *log_file, *newlog_path = NULL;
250         int status = 0;
251
252         memset(&cb, 0, sizeof(cb));
253         /* we take the lock for the ref itself to prevent it from
254          * getting updated.
255          */
256         lock = lock_any_ref_for_update(ref, sha1, 0);
257         if (!lock)
258                 return error("cannot lock ref '%s'", ref);
259         log_file = xstrdup(git_path("logs/%s", ref));
260         if (!file_exists(log_file))
261                 goto finish;
262         if (!cmd->dry_run) {
263                 newlog_path = xstrdup(git_path("logs/%s.lock", ref));
264                 cb.newlog = fopen(newlog_path, "w");
265         }
266
267         cb.ref_commit = lookup_commit_reference_gently(sha1, 1);
268         cb.ref = ref;
269         cb.cmd = cmd;
270         for_each_reflog_ent(ref, expire_reflog_ent, &cb);
271  finish:
272         if (cb.newlog) {
273                 if (fclose(cb.newlog))
274                         status |= error("%s: %s", strerror(errno),
275                                         newlog_path);
276                 if (rename(newlog_path, log_file)) {
277                         status |= error("cannot rename %s to %s",
278                                         newlog_path, log_file);
279                         unlink(newlog_path);
280                 }
281         }
282         free(newlog_path);
283         free(log_file);
284         unlock_ref(lock);
285         return status;
286 }
287
288 static int reflog_expire_config(const char *var, const char *value)
289 {
290         if (!strcmp(var, "gc.reflogexpire"))
291                 default_reflog_expire = approxidate(value);
292         else if (!strcmp(var, "gc.reflogexpireunreachable"))
293                 default_reflog_expire_unreachable = approxidate(value);
294         else
295                 return git_default_config(var, value);
296         return 0;
297 }
298
299 static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
300 {
301         struct cmd_reflog_expire_cb cb;
302         unsigned long now = time(NULL);
303         int i, status, do_all;
304
305         git_config(reflog_expire_config);
306
307         save_commit_buffer = 0;
308         do_all = status = 0;
309         memset(&cb, 0, sizeof(cb));
310
311         if (!default_reflog_expire_unreachable)
312                 default_reflog_expire_unreachable = now - 30 * 24 * 3600;
313         if (!default_reflog_expire)
314                 default_reflog_expire = now - 90 * 24 * 3600;
315         cb.expire_total = default_reflog_expire;
316         cb.expire_unreachable = default_reflog_expire_unreachable;
317
318         /*
319          * We can trust the commits and objects reachable from refs
320          * even in older repository.  We cannot trust what's reachable
321          * from reflog if the repository was pruned with older git.
322          */
323
324         for (i = 1; i < argc; i++) {
325                 const char *arg = argv[i];
326                 if (!strcmp(arg, "--dry-run") || !strcmp(arg, "-n"))
327                         cb.dry_run = 1;
328                 else if (!prefixcmp(arg, "--expire="))
329                         cb.expire_total = approxidate(arg + 9);
330                 else if (!prefixcmp(arg, "--expire-unreachable="))
331                         cb.expire_unreachable = approxidate(arg + 21);
332                 else if (!strcmp(arg, "--stale-fix"))
333                         cb.stalefix = 1;
334                 else if (!strcmp(arg, "--all"))
335                         do_all = 1;
336                 else if (!strcmp(arg, "--verbose"))
337                         cb.verbose = 1;
338                 else if (!strcmp(arg, "--")) {
339                         i++;
340                         break;
341                 }
342                 else if (arg[0] == '-')
343                         usage(reflog_expire_usage);
344                 else
345                         break;
346         }
347         if (cb.stalefix) {
348                 init_revisions(&cb.revs, prefix);
349                 if (cb.verbose)
350                         printf("Marking reachable objects...");
351                 mark_reachable_objects(&cb.revs, 0);
352                 if (cb.verbose)
353                         putchar('\n');
354         }
355
356         if (do_all)
357                 status |= for_each_reflog(expire_reflog, &cb);
358         while (i < argc) {
359                 const char *ref = argv[i++];
360                 unsigned char sha1[20];
361                 if (!resolve_ref(ref, sha1, 1, NULL)) {
362                         status |= error("%s points nowhere!", ref);
363                         continue;
364                 }
365                 status |= expire_reflog(ref, sha1, 0, &cb);
366         }
367         return status;
368 }
369
370 static int count_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
371                 const char *email, unsigned long timestamp, int tz,
372                 const char *message, void *cb_data)
373 {
374         struct cmd_reflog_expire_cb *cb = cb_data;
375         if (!cb->expire_total || timestamp < cb->expire_total)
376                 cb->recno++;
377         return 0;
378 }
379
380 static int cmd_reflog_delete(int argc, const char **argv, const char *prefix)
381 {
382         struct cmd_reflog_expire_cb cb;
383         int i, status = 0;
384
385         if (argc < 2)
386                 return error("Nothing to delete?");
387
388         memset(&cb, 0, sizeof(cb));
389
390         for (i = 1; i < argc; i++) {
391                 const char *spec = strstr(argv[i], "@{");
392                 unsigned char sha1[20];
393                 char *ep, *ref;
394                 int recno;
395
396                 if (!spec) {
397                         status |= error("Not a reflog: %s", ref);
398                         continue;
399                 }
400
401                 if (!dwim_ref(argv[i], spec - argv[i], sha1, &ref)) {
402                         status |= error("%s points nowhere!", argv[i]);
403                         continue;
404                 }
405
406                 recno = strtoul(spec + 2, &ep, 10);
407                 if (*ep == '}') {
408                         cb.recno = -recno;
409                         for_each_reflog_ent(ref, count_reflog_ent, &cb);
410                 } else {
411                         cb.expire_total = approxidate(spec + 2);
412                         for_each_reflog_ent(ref, count_reflog_ent, &cb);
413                         cb.expire_total = 0;
414                 }
415
416                 status |= expire_reflog(ref, sha1, 0, &cb);
417                 free(ref);
418         }
419         return status;
420 }
421
422 /*
423  * main "reflog"
424  */
425
426 static const char reflog_usage[] =
427 "git-reflog (expire | ...)";
428
429 int cmd_reflog(int argc, const char **argv, const char *prefix)
430 {
431         /* With no command, we default to showing it. */
432         if (argc < 2 || *argv[1] == '-')
433                 return cmd_log_reflog(argc, argv, prefix);
434
435         if (!strcmp(argv[1], "show"))
436                 return cmd_log_reflog(argc - 1, argv + 1, prefix);
437
438         if (!strcmp(argv[1], "expire"))
439                 return cmd_reflog_expire(argc - 1, argv + 1, prefix);
440
441         if (!strcmp(argv[1], "delete"))
442                 return cmd_reflog_delete(argc - 1, argv + 1, prefix);
443
444         /* Not a recognized reflog command..*/
445         usage(reflog_usage);
446 }