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 {
29 unsigned long expire_total;
30 unsigned long expire_unreachable;
34 struct expire_reflog_cb {
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;
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;
62 tree = lookup_tree(sha1);
65 if (tree->object.flags & SEEN)
67 if (tree->object.flags & INCOMPLETE)
71 enum object_type type;
73 void *data = read_sha1_file(sha1, &type, &size);
75 tree->object.flags |= INCOMPLETE;
81 init_tree_desc(&desc, tree->buffer, tree->size);
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;
94 tree->object.flags |= SEEN;
98 static int commit_is_complete(struct commit *commit)
100 struct object_array study;
101 struct object_array found;
102 int is_incomplete = 0;
106 if (commit->object.flags & SEEN)
108 if (commit->object.flags & INCOMPLETE)
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;
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) {
134 else if (c->object.flags & SEEN)
136 for (parent = c->parents; parent; parent = parent->next) {
137 struct commit *p = parent->item;
138 if (p->object.flags & STUDYING)
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
150 for (i = 0; i < found.nr; i++) {
152 (struct commit *)found.objects[i].item;
153 if (!tree_is_complete(c->tree->object.sha1)) {
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;
168 commit->object.flags |= INCOMPLETE;
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 */
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))
193 commit = lookup_commit_reference_gently(sha1, 1);
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
205 if (!commit_is_complete(commit))
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)
221 if (cb->cmd->rewrite)
222 osha1 = cb->last_kept_sha1;
225 if (cb->cmd->stalefix &&
226 (!keep_entry(&old, osha1) || !keep_entry(&new, nsha1)))
229 if (timestamp < cb->cmd->expire_unreachable) {
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)))
241 if (cb->cmd->recno && --(cb->cmd->recno) == 0)
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,
251 hashcpy(cb->last_kept_sha1, nsha1);
253 if (cb->cmd->verbose)
254 printf("keep %s", message);
257 if (!cb->newlog || cb->cmd->verbose)
258 printf("%sprune %s", cb->newlog ? "" : "would ", message);
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;
270 memset(&cb, 0, sizeof(cb));
271 /* we take the lock for the ref itself to prevent it from
274 lock = lock_any_ref_for_update(ref, sha1, 0);
276 return error("cannot lock ref '%s'", ref);
277 log_file = xstrdup(git_path("logs/%s", ref));
278 if (!file_exists(log_file))
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);
288 for_each_reflog_ent(ref, expire_reflog_ent, &cb);
291 if (fclose(cb.newlog)) {
292 status |= error("%s: %s", strerror(errno),
295 } else if (rename(newlog_path, log_file)) {
296 status |= error("cannot rename %s to %s",
297 newlog_path, log_file);
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);
321 static int reflog_expire_config(const char *var, const char *value)
323 if (!strcmp(var, "gc.reflogexpire")) {
325 config_error_nonbool(var);
326 default_reflog_expire = approxidate(value);
329 if (!strcmp(var, "gc.reflogexpireunreachable")) {
331 config_error_nonbool(var);
332 default_reflog_expire_unreachable = approxidate(value);
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;
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"))
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"))
373 else if (!strcmp(arg, "--rewrite"))
375 else if (!strcmp(arg, "--all"))
377 else if (!strcmp(arg, "--verbose"))
379 else if (!strcmp(arg, "--")) {
383 else if (arg[0] == '-')
384 usage(reflog_expire_usage);
389 init_revisions(&cb.revs, prefix);
391 printf("Marking reachable objects...");
392 mark_reachable_objects(&cb.revs, 0);
398 struct collect_reflog_cb collected;
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);
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);
418 status |= expire_reflog(ref, sha1, 0, &cb);
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)
433 static int cmd_reflog_delete(int argc, const char **argv, const char *prefix)
435 struct cmd_reflog_expire_cb cb;
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"))
444 else if (!strcmp(arg, "--rewrite"))
446 else if (!strcmp(arg, "--verbose"))
448 else if (!strcmp(arg, "--")) {
452 else if (arg[0] == '-')
453 usage(reflog_delete_usage);
459 return error("Nothing to delete?");
461 for ( ; i < argc; i++) {
462 const char *spec = strstr(argv[i], "@{");
463 unsigned char sha1[20];
468 status |= error("Not a reflog: %s", argv[i]);
472 if (!dwim_ref(argv[i], spec - argv[i], sha1, &ref)) {
473 status |= error("%s points nowhere!", argv[i]);
477 recno = strtoul(spec + 2, &ep, 10);
480 for_each_reflog_ent(ref, count_reflog_ent, &cb);
482 cb.expire_total = approxidate(spec + 2);
483 for_each_reflog_ent(ref, count_reflog_ent, &cb);
487 status |= expire_reflog(ref, sha1, 0, &cb);
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..*/