15 static const char reflog_expire_usage[] =
16 "git-reflog (show|expire) [--verbose] [--dry-run] [--stale-fix] [--expire=<time>] [--expire-unreachable=<time>] [--all] <refs>...";
18 static unsigned long default_reflog_expire;
19 static unsigned long default_reflog_expire_unreachable;
21 struct cmd_reflog_expire_cb {
26 unsigned long expire_total;
27 unsigned long expire_unreachable;
31 struct expire_reflog_cb {
34 struct commit *ref_commit;
35 struct cmd_reflog_expire_cb *cmd;
38 #define INCOMPLETE (1u<<10)
39 #define STUDYING (1u<<11)
41 static int tree_is_complete(const unsigned char *sha1)
43 struct tree_desc desc;
44 struct name_entry entry;
48 tree = lookup_tree(sha1);
51 if (tree->object.flags & SEEN)
53 if (tree->object.flags & INCOMPLETE)
57 enum object_type type;
59 void *data = read_sha1_file(sha1, &type, &size);
61 tree->object.flags |= INCOMPLETE;
67 init_tree_desc(&desc, tree->buffer, tree->size);
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;
80 tree->object.flags |= SEEN;
84 static int commit_is_complete(struct commit *commit)
86 struct object_array study;
87 struct object_array found;
88 int is_incomplete = 0;
92 if (commit->object.flags & SEEN)
94 if (commit->object.flags & INCOMPLETE)
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.
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;
110 struct commit_list *parent;
112 c = (struct commit *)study.objects[--study.nr].item;
113 if (!c->object.parsed && !parse_object(c->object.sha1))
114 c->object.flags |= INCOMPLETE;
116 if (c->object.flags & INCOMPLETE) {
120 else if (c->object.flags & SEEN)
122 for (parent = c->parents; parent; parent = parent->next) {
123 struct commit *p = parent->item;
124 if (p->object.flags & STUDYING)
126 p->object.flags |= STUDYING;
127 add_object_array(&p->object, NULL, &study);
128 add_object_array(&p->object, NULL, &found);
131 if (!is_incomplete) {
133 * make sure all commits in "found" array have all the
136 for (i = 0; i < found.nr; i++) {
138 (struct commit *)found.objects[i].item;
139 if (!tree_is_complete(c->tree->object.sha1)) {
141 c->object.flags |= INCOMPLETE;
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;
150 /* clear flags from the objects we traversed */
151 for (i = 0; i < found.nr; i++)
152 found.objects[i].item->flags &= ~STUDYING;
154 commit->object.flags |= INCOMPLETE;
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.
164 for (i = 0; i < found.nr; i++)
165 found.objects[i].item->flags |= SEEN;
167 /* free object arrays */
170 return !is_incomplete;
173 static int keep_entry(struct commit **it, unsigned char *sha1)
175 struct commit *commit;
177 if (is_null_sha1(sha1))
179 commit = lookup_commit_reference_gently(sha1, 1);
184 * Make sure everything in this commit exists.
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
191 if (!commit_is_complete(commit))
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)
201 struct expire_reflog_cb *cb = cb_data;
202 struct commit *old, *new;
204 if (timestamp < cb->cmd->expire_total)
208 if (cb->cmd->stalefix &&
209 (!keep_entry(&old, osha1) || !keep_entry(&new, nsha1)))
212 if (timestamp < cb->cmd->expire_unreachable) {
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)))
224 if (cb->cmd->recno && --(cb->cmd->recno) == 0)
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,
235 if (cb->cmd->verbose)
236 printf("keep %s", message);
239 if (!cb->newlog || cb->cmd->verbose)
240 printf("%sprune %s", cb->newlog ? "" : "would ", message);
244 static int expire_reflog(const char *ref, const unsigned char *sha1, int unused, void *cb_data)
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;
252 memset(&cb, 0, sizeof(cb));
253 /* we take the lock for the ref itself to prevent it from
256 lock = lock_any_ref_for_update(ref, sha1, 0);
258 return error("cannot lock ref '%s'", ref);
259 log_file = xstrdup(git_path("logs/%s", ref));
260 if (!file_exists(log_file))
263 newlog_path = xstrdup(git_path("logs/%s.lock", ref));
264 cb.newlog = fopen(newlog_path, "w");
267 cb.ref_commit = lookup_commit_reference_gently(sha1, 1);
270 for_each_reflog_ent(ref, expire_reflog_ent, &cb);
273 if (fclose(cb.newlog))
274 status |= error("%s: %s", strerror(errno),
276 if (rename(newlog_path, log_file)) {
277 status |= error("cannot rename %s to %s",
278 newlog_path, log_file);
288 static int reflog_expire_config(const char *var, const char *value)
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);
295 return git_default_config(var, value);
299 static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
301 struct cmd_reflog_expire_cb cb;
302 unsigned long now = time(NULL);
303 int i, status, do_all;
305 git_config(reflog_expire_config);
307 save_commit_buffer = 0;
309 memset(&cb, 0, sizeof(cb));
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;
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.
324 for (i = 1; i < argc; i++) {
325 const char *arg = argv[i];
326 if (!strcmp(arg, "--dry-run") || !strcmp(arg, "-n"))
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"))
334 else if (!strcmp(arg, "--all"))
336 else if (!strcmp(arg, "--verbose"))
338 else if (!strcmp(arg, "--")) {
342 else if (arg[0] == '-')
343 usage(reflog_expire_usage);
348 init_revisions(&cb.revs, prefix);
350 printf("Marking reachable objects...");
351 mark_reachable_objects(&cb.revs, 0);
357 status |= for_each_reflog(expire_reflog, &cb);
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);
365 status |= expire_reflog(ref, sha1, 0, &cb);
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)
374 struct cmd_reflog_expire_cb *cb = cb_data;
375 if (!cb->expire_total || timestamp < cb->expire_total)
380 static int cmd_reflog_delete(int argc, const char **argv, const char *prefix)
382 struct cmd_reflog_expire_cb cb;
386 return error("Nothing to delete?");
388 memset(&cb, 0, sizeof(cb));
390 for (i = 1; i < argc; i++) {
391 const char *spec = strstr(argv[i], "@{");
392 unsigned char sha1[20];
397 status |= error("Not a reflog: %s", ref);
401 if (!dwim_ref(argv[i], spec - argv[i], sha1, &ref)) {
402 status |= error("%s points nowhere!", argv[i]);
406 recno = strtoul(spec + 2, &ep, 10);
409 for_each_reflog_ent(ref, count_reflog_ent, &cb);
411 cb.expire_total = approxidate(spec + 2);
412 for_each_reflog_ent(ref, count_reflog_ent, &cb);
416 status |= expire_reflog(ref, sha1, 0, &cb);
426 static const char reflog_usage[] =
427 "git-reflog (expire | ...)";
429 int cmd_reflog(int argc, const char **argv, const char *prefix)
431 /* With no command, we default to showing it. */
432 if (argc < 2 || *argv[1] == '-')
433 return cmd_log_reflog(argc, argv, prefix);
435 if (!strcmp(argv[1], "show"))
436 return cmd_log_reflog(argc - 1, argv + 1, prefix);
438 if (!strcmp(argv[1], "expire"))
439 return cmd_reflog_expire(argc - 1, argv + 1, prefix);
441 if (!strcmp(argv[1], "delete"))
442 return cmd_reflog_delete(argc - 1, argv + 1, prefix);
444 /* Not a recognized reflog command..*/