git-reflog: add option --rewrite to update reflog entries while expiring
[git/mingw.git] / builtin-reflog.c
blobf685d76e8daec5f472475499f58cd03e986f790c
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] <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 verbose;
29 unsigned long expire_total;
30 unsigned long expire_unreachable;
31 int recno;
34 struct expire_reflog_cb {
35 FILE *newlog;
36 const char *ref;
37 struct commit *ref_commit;
38 struct cmd_reflog_expire_cb *cmd;
39 unsigned char last_kept_sha1[20];
42 struct collected_reflog {
43 unsigned char sha1[20];
44 char reflog[FLEX_ARRAY];
46 struct collect_reflog_cb {
47 struct collected_reflog **e;
48 int alloc;
49 int nr;
52 #define INCOMPLETE (1u<<10)
53 #define STUDYING (1u<<11)
55 static int tree_is_complete(const unsigned char *sha1)
57 struct tree_desc desc;
58 struct name_entry entry;
59 int complete;
60 struct tree *tree;
62 tree = lookup_tree(sha1);
63 if (!tree)
64 return 0;
65 if (tree->object.flags & SEEN)
66 return 1;
67 if (tree->object.flags & INCOMPLETE)
68 return 0;
70 if (!tree->buffer) {
71 enum object_type type;
72 unsigned long size;
73 void *data = read_sha1_file(sha1, &type, &size);
74 if (!data) {
75 tree->object.flags |= INCOMPLETE;
76 return 0;
78 tree->buffer = data;
79 tree->size = size;
81 init_tree_desc(&desc, tree->buffer, tree->size);
82 complete = 1;
83 while (tree_entry(&desc, &entry)) {
84 if (!has_sha1_file(entry.sha1) ||
85 (S_ISDIR(entry.mode) && !tree_is_complete(entry.sha1))) {
86 tree->object.flags |= INCOMPLETE;
87 complete = 0;
90 free(tree->buffer);
91 tree->buffer = NULL;
93 if (complete)
94 tree->object.flags |= SEEN;
95 return complete;
98 static int commit_is_complete(struct commit *commit)
100 struct object_array study;
101 struct object_array found;
102 int is_incomplete = 0;
103 int i;
105 /* early return */
106 if (commit->object.flags & SEEN)
107 return 1;
108 if (commit->object.flags & INCOMPLETE)
109 return 0;
111 * Find all commits that are reachable and are not marked as
112 * SEEN. Then make sure the trees and blobs contained are
113 * complete. After that, mark these commits also as SEEN.
114 * If some of the objects that are needed to complete this
115 * commit are missing, mark this commit as INCOMPLETE.
117 memset(&study, 0, sizeof(study));
118 memset(&found, 0, sizeof(found));
119 add_object_array(&commit->object, NULL, &study);
120 add_object_array(&commit->object, NULL, &found);
121 commit->object.flags |= STUDYING;
122 while (study.nr) {
123 struct commit *c;
124 struct commit_list *parent;
126 c = (struct commit *)study.objects[--study.nr].item;
127 if (!c->object.parsed && !parse_object(c->object.sha1))
128 c->object.flags |= INCOMPLETE;
130 if (c->object.flags & INCOMPLETE) {
131 is_incomplete = 1;
132 break;
134 else if (c->object.flags & SEEN)
135 continue;
136 for (parent = c->parents; parent; parent = parent->next) {
137 struct commit *p = parent->item;
138 if (p->object.flags & STUDYING)
139 continue;
140 p->object.flags |= STUDYING;
141 add_object_array(&p->object, NULL, &study);
142 add_object_array(&p->object, NULL, &found);
145 if (!is_incomplete) {
147 * make sure all commits in "found" array have all the
148 * necessary objects.
150 for (i = 0; i < found.nr; i++) {
151 struct commit *c =
152 (struct commit *)found.objects[i].item;
153 if (!tree_is_complete(c->tree->object.sha1)) {
154 is_incomplete = 1;
155 c->object.flags |= INCOMPLETE;
158 if (!is_incomplete) {
159 /* mark all found commits as complete, iow SEEN */
160 for (i = 0; i < found.nr; i++)
161 found.objects[i].item->flags |= SEEN;
164 /* clear flags from the objects we traversed */
165 for (i = 0; i < found.nr; i++)
166 found.objects[i].item->flags &= ~STUDYING;
167 if (is_incomplete)
168 commit->object.flags |= INCOMPLETE;
169 else {
171 * If we come here, we have (1) traversed the ancestry chain
172 * from the "commit" until we reach SEEN commits (which are
173 * known to be complete), and (2) made sure that the commits
174 * encountered during the above traversal refer to trees that
175 * are complete. Which means that we know *all* the commits
176 * we have seen during this process are complete.
178 for (i = 0; i < found.nr; i++)
179 found.objects[i].item->flags |= SEEN;
181 /* free object arrays */
182 free(study.objects);
183 free(found.objects);
184 return !is_incomplete;
187 static int keep_entry(struct commit **it, unsigned char *sha1)
189 struct commit *commit;
191 if (is_null_sha1(sha1))
192 return 1;
193 commit = lookup_commit_reference_gently(sha1, 1);
194 if (!commit)
195 return 0;
198 * Make sure everything in this commit exists.
200 * We have walked all the objects reachable from the refs
201 * and cache earlier. The commits reachable by this commit
202 * must meet SEEN commits -- and then we should mark them as
203 * SEEN as well.
205 if (!commit_is_complete(commit))
206 return 0;
207 *it = commit;
208 return 1;
211 static int expire_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
212 const char *email, unsigned long timestamp, int tz,
213 const char *message, void *cb_data)
215 struct expire_reflog_cb *cb = cb_data;
216 struct commit *old, *new;
218 if (timestamp < cb->cmd->expire_total)
219 goto prune;
221 if (cb->cmd->rewrite)
222 osha1 = cb->last_kept_sha1;
224 old = new = NULL;
225 if (cb->cmd->stalefix &&
226 (!keep_entry(&old, osha1) || !keep_entry(&new, nsha1)))
227 goto prune;
229 if (timestamp < cb->cmd->expire_unreachable) {
230 if (!cb->ref_commit)
231 goto prune;
232 if (!old && !is_null_sha1(osha1))
233 old = lookup_commit_reference_gently(osha1, 1);
234 if (!new && !is_null_sha1(nsha1))
235 new = lookup_commit_reference_gently(nsha1, 1);
236 if ((old && !in_merge_bases(old, &cb->ref_commit, 1)) ||
237 (new && !in_merge_bases(new, &cb->ref_commit, 1)))
238 goto prune;
241 if (cb->cmd->recno && --(cb->cmd->recno) == 0)
242 goto prune;
244 if (cb->newlog) {
245 char sign = (tz < 0) ? '-' : '+';
246 int zone = (tz < 0) ? (-tz) : tz;
247 fprintf(cb->newlog, "%s %s %s %lu %c%04d\t%s",
248 sha1_to_hex(osha1), sha1_to_hex(nsha1),
249 email, timestamp, sign, zone,
250 message);
251 hashcpy(cb->last_kept_sha1, nsha1);
253 if (cb->cmd->verbose)
254 printf("keep %s", message);
255 return 0;
256 prune:
257 if (!cb->newlog || cb->cmd->verbose)
258 printf("%sprune %s", cb->newlog ? "" : "would ", message);
259 return 0;
262 static int expire_reflog(const char *ref, const unsigned char *sha1, int unused, void *cb_data)
264 struct cmd_reflog_expire_cb *cmd = cb_data;
265 struct expire_reflog_cb cb;
266 struct ref_lock *lock;
267 char *log_file, *newlog_path = NULL;
268 int status = 0;
270 memset(&cb, 0, sizeof(cb));
271 /* we take the lock for the ref itself to prevent it from
272 * getting updated.
274 lock = lock_any_ref_for_update(ref, sha1, 0);
275 if (!lock)
276 return error("cannot lock ref '%s'", ref);
277 log_file = xstrdup(git_path("logs/%s", ref));
278 if (!file_exists(log_file))
279 goto finish;
280 if (!cmd->dry_run) {
281 newlog_path = xstrdup(git_path("logs/%s.lock", ref));
282 cb.newlog = fopen(newlog_path, "w");
285 cb.ref_commit = lookup_commit_reference_gently(sha1, 1);
286 cb.ref = ref;
287 cb.cmd = cmd;
288 for_each_reflog_ent(ref, expire_reflog_ent, &cb);
289 finish:
290 if (cb.newlog) {
291 if (fclose(cb.newlog)) {
292 status |= error("%s: %s", strerror(errno),
293 newlog_path);
294 unlink(newlog_path);
295 } else if (rename(newlog_path, log_file)) {
296 status |= error("cannot rename %s to %s",
297 newlog_path, log_file);
298 unlink(newlog_path);
301 free(newlog_path);
302 free(log_file);
303 unlock_ref(lock);
304 return status;
307 static int collect_reflog(const char *ref, const unsigned char *sha1, int unused, void *cb_data)
309 struct collected_reflog *e;
310 struct collect_reflog_cb *cb = cb_data;
311 size_t namelen = strlen(ref);
313 e = xmalloc(sizeof(*e) + namelen + 1);
314 hashcpy(e->sha1, sha1);
315 memcpy(e->reflog, ref, namelen + 1);
316 ALLOC_GROW(cb->e, cb->nr + 1, cb->alloc);
317 cb->e[cb->nr++] = e;
318 return 0;
321 static int reflog_expire_config(const char *var, const char *value)
323 if (!strcmp(var, "gc.reflogexpire")) {
324 if (!value)
325 config_error_nonbool(var);
326 default_reflog_expire = approxidate(value);
327 return 0;
329 if (!strcmp(var, "gc.reflogexpireunreachable")) {
330 if (!value)
331 config_error_nonbool(var);
332 default_reflog_expire_unreachable = approxidate(value);
333 return 0;
335 return git_default_config(var, value);
338 static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
340 struct cmd_reflog_expire_cb cb;
341 unsigned long now = time(NULL);
342 int i, status, do_all;
344 git_config(reflog_expire_config);
346 save_commit_buffer = 0;
347 do_all = status = 0;
348 memset(&cb, 0, sizeof(cb));
350 if (!default_reflog_expire_unreachable)
351 default_reflog_expire_unreachable = now - 30 * 24 * 3600;
352 if (!default_reflog_expire)
353 default_reflog_expire = now - 90 * 24 * 3600;
354 cb.expire_total = default_reflog_expire;
355 cb.expire_unreachable = default_reflog_expire_unreachable;
358 * We can trust the commits and objects reachable from refs
359 * even in older repository. We cannot trust what's reachable
360 * from reflog if the repository was pruned with older git.
363 for (i = 1; i < argc; i++) {
364 const char *arg = argv[i];
365 if (!strcmp(arg, "--dry-run") || !strcmp(arg, "-n"))
366 cb.dry_run = 1;
367 else if (!prefixcmp(arg, "--expire="))
368 cb.expire_total = approxidate(arg + 9);
369 else if (!prefixcmp(arg, "--expire-unreachable="))
370 cb.expire_unreachable = approxidate(arg + 21);
371 else if (!strcmp(arg, "--stale-fix"))
372 cb.stalefix = 1;
373 else if (!strcmp(arg, "--rewrite"))
374 cb.rewrite = 1;
375 else if (!strcmp(arg, "--all"))
376 do_all = 1;
377 else if (!strcmp(arg, "--verbose"))
378 cb.verbose = 1;
379 else if (!strcmp(arg, "--")) {
380 i++;
381 break;
383 else if (arg[0] == '-')
384 usage(reflog_expire_usage);
385 else
386 break;
388 if (cb.stalefix) {
389 init_revisions(&cb.revs, prefix);
390 if (cb.verbose)
391 printf("Marking reachable objects...");
392 mark_reachable_objects(&cb.revs, 0);
393 if (cb.verbose)
394 putchar('\n');
397 if (do_all) {
398 struct collect_reflog_cb collected;
399 int i;
401 memset(&collected, 0, sizeof(collected));
402 for_each_reflog(collect_reflog, &collected);
403 for (i = 0; i < collected.nr; i++) {
404 struct collected_reflog *e = collected.e[i];
405 status |= expire_reflog(e->reflog, e->sha1, 0, &cb);
406 free(e);
408 free(collected.e);
411 while (i < argc) {
412 const char *ref = argv[i++];
413 unsigned char sha1[20];
414 if (!resolve_ref(ref, sha1, 1, NULL)) {
415 status |= error("%s points nowhere!", ref);
416 continue;
418 status |= expire_reflog(ref, sha1, 0, &cb);
420 return status;
423 static int count_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
424 const char *email, unsigned long timestamp, int tz,
425 const char *message, void *cb_data)
427 struct cmd_reflog_expire_cb *cb = cb_data;
428 if (!cb->expire_total || timestamp < cb->expire_total)
429 cb->recno++;
430 return 0;
433 static int cmd_reflog_delete(int argc, const char **argv, const char *prefix)
435 struct cmd_reflog_expire_cb cb;
436 int i, status = 0;
438 memset(&cb, 0, sizeof(cb));
440 for (i = 1; i < argc; i++) {
441 const char *arg = argv[i];
442 if (!strcmp(arg, "--dry-run") || !strcmp(arg, "-n"))
443 cb.dry_run = 1;
444 else if (!strcmp(arg, "--rewrite"))
445 cb.rewrite = 1;
446 else if (!strcmp(arg, "--verbose"))
447 cb.verbose = 1;
448 else if (!strcmp(arg, "--")) {
449 i++;
450 break;
452 else if (arg[0] == '-')
453 usage(reflog_delete_usage);
454 else
455 break;
458 if (argc - i < 1)
459 return error("Nothing to delete?");
461 for ( ; i < argc; i++) {
462 const char *spec = strstr(argv[i], "@{");
463 unsigned char sha1[20];
464 char *ep, *ref;
465 int recno;
467 if (!spec) {
468 status |= error("Not a reflog: %s", argv[i]);
469 continue;
472 if (!dwim_ref(argv[i], spec - argv[i], sha1, &ref)) {
473 status |= error("%s points nowhere!", argv[i]);
474 continue;
477 recno = strtoul(spec + 2, &ep, 10);
478 if (*ep == '}') {
479 cb.recno = -recno;
480 for_each_reflog_ent(ref, count_reflog_ent, &cb);
481 } else {
482 cb.expire_total = approxidate(spec + 2);
483 for_each_reflog_ent(ref, count_reflog_ent, &cb);
484 cb.expire_total = 0;
487 status |= expire_reflog(ref, sha1, 0, &cb);
488 free(ref);
490 return status;
494 * main "reflog"
497 static const char reflog_usage[] =
498 "git-reflog (expire | ...)";
500 int cmd_reflog(int argc, const char **argv, const char *prefix)
502 /* With no command, we default to showing it. */
503 if (argc < 2 || *argv[1] == '-')
504 return cmd_log_reflog(argc, argv, prefix);
506 if (!strcmp(argv[1], "show"))
507 return cmd_log_reflog(argc - 1, argv + 1, prefix);
509 if (!strcmp(argv[1], "expire"))
510 return cmd_reflog_expire(argc - 1, argv + 1, prefix);
512 if (!strcmp(argv[1], "delete"))
513 return cmd_reflog_delete(argc - 1, argv + 1, prefix);
515 /* Not a recognized reflog command..*/
516 usage(reflog_usage);