Merge branch 'bc/reflog-fix' into js/reflog-delete
[git/haiku.git] / builtin-reflog.c
blob8af75bc3e120a390c11c0054b80e6bc852451ac3
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>...";
18 static unsigned long default_reflog_expire;
19 static unsigned long default_reflog_expire_unreachable;
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;
31 struct expire_reflog_cb {
32 FILE *newlog;
33 const char *ref;
34 struct commit *ref_commit;
35 struct cmd_reflog_expire_cb *cmd;
38 struct collected_reflog {
39 unsigned char sha1[20];
40 char reflog[FLEX_ARRAY];
42 struct collect_reflog_cb {
43 struct collected_reflog **e;
44 int alloc;
45 int nr;
48 #define INCOMPLETE (1u<<10)
49 #define STUDYING (1u<<11)
51 static int tree_is_complete(const unsigned char *sha1)
53 struct tree_desc desc;
54 struct name_entry entry;
55 int complete;
56 struct tree *tree;
58 tree = lookup_tree(sha1);
59 if (!tree)
60 return 0;
61 if (tree->object.flags & SEEN)
62 return 1;
63 if (tree->object.flags & INCOMPLETE)
64 return 0;
66 if (!tree->buffer) {
67 enum object_type type;
68 unsigned long size;
69 void *data = read_sha1_file(sha1, &type, &size);
70 if (!data) {
71 tree->object.flags |= INCOMPLETE;
72 return 0;
74 tree->buffer = data;
75 tree->size = size;
77 init_tree_desc(&desc, tree->buffer, tree->size);
78 complete = 1;
79 while (tree_entry(&desc, &entry)) {
80 if (!has_sha1_file(entry.sha1) ||
81 (S_ISDIR(entry.mode) && !tree_is_complete(entry.sha1))) {
82 tree->object.flags |= INCOMPLETE;
83 complete = 0;
86 free(tree->buffer);
87 tree->buffer = NULL;
89 if (complete)
90 tree->object.flags |= SEEN;
91 return complete;
94 static int commit_is_complete(struct commit *commit)
96 struct object_array study;
97 struct object_array found;
98 int is_incomplete = 0;
99 int i;
101 /* early return */
102 if (commit->object.flags & SEEN)
103 return 1;
104 if (commit->object.flags & INCOMPLETE)
105 return 0;
107 * Find all commits that are reachable and are not marked as
108 * SEEN. Then make sure the trees and blobs contained are
109 * complete. After that, mark these commits also as SEEN.
110 * If some of the objects that are needed to complete this
111 * commit are missing, mark this commit as INCOMPLETE.
113 memset(&study, 0, sizeof(study));
114 memset(&found, 0, sizeof(found));
115 add_object_array(&commit->object, NULL, &study);
116 add_object_array(&commit->object, NULL, &found);
117 commit->object.flags |= STUDYING;
118 while (study.nr) {
119 struct commit *c;
120 struct commit_list *parent;
122 c = (struct commit *)study.objects[--study.nr].item;
123 if (!c->object.parsed && !parse_object(c->object.sha1))
124 c->object.flags |= INCOMPLETE;
126 if (c->object.flags & INCOMPLETE) {
127 is_incomplete = 1;
128 break;
130 else if (c->object.flags & SEEN)
131 continue;
132 for (parent = c->parents; parent; parent = parent->next) {
133 struct commit *p = parent->item;
134 if (p->object.flags & STUDYING)
135 continue;
136 p->object.flags |= STUDYING;
137 add_object_array(&p->object, NULL, &study);
138 add_object_array(&p->object, NULL, &found);
141 if (!is_incomplete) {
143 * make sure all commits in "found" array have all the
144 * necessary objects.
146 for (i = 0; i < found.nr; i++) {
147 struct commit *c =
148 (struct commit *)found.objects[i].item;
149 if (!tree_is_complete(c->tree->object.sha1)) {
150 is_incomplete = 1;
151 c->object.flags |= INCOMPLETE;
154 if (!is_incomplete) {
155 /* mark all found commits as complete, iow SEEN */
156 for (i = 0; i < found.nr; i++)
157 found.objects[i].item->flags |= SEEN;
160 /* clear flags from the objects we traversed */
161 for (i = 0; i < found.nr; i++)
162 found.objects[i].item->flags &= ~STUDYING;
163 if (is_incomplete)
164 commit->object.flags |= INCOMPLETE;
165 else {
167 * If we come here, we have (1) traversed the ancestry chain
168 * from the "commit" until we reach SEEN commits (which are
169 * known to be complete), and (2) made sure that the commits
170 * encountered during the above traversal refer to trees that
171 * are complete. Which means that we know *all* the commits
172 * we have seen during this process are complete.
174 for (i = 0; i < found.nr; i++)
175 found.objects[i].item->flags |= SEEN;
177 /* free object arrays */
178 free(study.objects);
179 free(found.objects);
180 return !is_incomplete;
183 static int keep_entry(struct commit **it, unsigned char *sha1)
185 struct commit *commit;
187 if (is_null_sha1(sha1))
188 return 1;
189 commit = lookup_commit_reference_gently(sha1, 1);
190 if (!commit)
191 return 0;
194 * Make sure everything in this commit exists.
196 * We have walked all the objects reachable from the refs
197 * and cache earlier. The commits reachable by this commit
198 * must meet SEEN commits -- and then we should mark them as
199 * SEEN as well.
201 if (!commit_is_complete(commit))
202 return 0;
203 *it = commit;
204 return 1;
207 static int expire_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
208 const char *email, unsigned long timestamp, int tz,
209 const char *message, void *cb_data)
211 struct expire_reflog_cb *cb = cb_data;
212 struct commit *old, *new;
214 if (timestamp < cb->cmd->expire_total)
215 goto prune;
217 old = new = NULL;
218 if (cb->cmd->stalefix &&
219 (!keep_entry(&old, osha1) || !keep_entry(&new, nsha1)))
220 goto prune;
222 if (timestamp < cb->cmd->expire_unreachable) {
223 if (!cb->ref_commit)
224 goto prune;
225 if (!old && !is_null_sha1(osha1))
226 old = lookup_commit_reference_gently(osha1, 1);
227 if (!new && !is_null_sha1(nsha1))
228 new = lookup_commit_reference_gently(nsha1, 1);
229 if ((old && !in_merge_bases(old, &cb->ref_commit, 1)) ||
230 (new && !in_merge_bases(new, &cb->ref_commit, 1)))
231 goto prune;
234 if (cb->cmd->recno && --(cb->cmd->recno) == 0)
235 goto prune;
237 if (cb->newlog) {
238 char sign = (tz < 0) ? '-' : '+';
239 int zone = (tz < 0) ? (-tz) : tz;
240 fprintf(cb->newlog, "%s %s %s %lu %c%04d\t%s",
241 sha1_to_hex(osha1), sha1_to_hex(nsha1),
242 email, timestamp, sign, zone,
243 message);
245 if (cb->cmd->verbose)
246 printf("keep %s", message);
247 return 0;
248 prune:
249 if (!cb->newlog || cb->cmd->verbose)
250 printf("%sprune %s", cb->newlog ? "" : "would ", message);
251 return 0;
254 static int expire_reflog(const char *ref, const unsigned char *sha1, int unused, void *cb_data)
256 struct cmd_reflog_expire_cb *cmd = cb_data;
257 struct expire_reflog_cb cb;
258 struct ref_lock *lock;
259 char *log_file, *newlog_path = NULL;
260 int status = 0;
262 memset(&cb, 0, sizeof(cb));
263 /* we take the lock for the ref itself to prevent it from
264 * getting updated.
266 lock = lock_any_ref_for_update(ref, sha1, 0);
267 if (!lock)
268 return error("cannot lock ref '%s'", ref);
269 log_file = xstrdup(git_path("logs/%s", ref));
270 if (!file_exists(log_file))
271 goto finish;
272 if (!cmd->dry_run) {
273 newlog_path = xstrdup(git_path("logs/%s.lock", ref));
274 cb.newlog = fopen(newlog_path, "w");
277 cb.ref_commit = lookup_commit_reference_gently(sha1, 1);
278 cb.ref = ref;
279 cb.cmd = cmd;
280 for_each_reflog_ent(ref, expire_reflog_ent, &cb);
281 finish:
282 if (cb.newlog) {
283 if (fclose(cb.newlog)) {
284 status |= error("%s: %s", strerror(errno),
285 newlog_path);
286 unlink(newlog_path);
287 } else if (rename(newlog_path, log_file)) {
288 status |= error("cannot rename %s to %s",
289 newlog_path, log_file);
290 unlink(newlog_path);
293 free(newlog_path);
294 free(log_file);
295 unlock_ref(lock);
296 return status;
299 static int collect_reflog(const char *ref, const unsigned char *sha1, int unused, void *cb_data)
301 struct collected_reflog *e;
302 struct collect_reflog_cb *cb = cb_data;
303 size_t namelen = strlen(ref);
305 e = xmalloc(sizeof(*e) + namelen + 1);
306 hashcpy(e->sha1, sha1);
307 memcpy(e->reflog, ref, namelen + 1);
308 ALLOC_GROW(cb->e, cb->nr + 1, cb->alloc);
309 cb->e[cb->nr++] = e;
310 return 0;
313 static int reflog_expire_config(const char *var, const char *value)
315 if (!strcmp(var, "gc.reflogexpire")) {
316 if (!value)
317 config_error_nonbool(var);
318 default_reflog_expire = approxidate(value);
319 return 0;
321 if (!strcmp(var, "gc.reflogexpireunreachable")) {
322 if (!value)
323 config_error_nonbool(var);
324 default_reflog_expire_unreachable = approxidate(value);
325 return 0;
327 return git_default_config(var, value);
330 static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
332 struct cmd_reflog_expire_cb cb;
333 unsigned long now = time(NULL);
334 int i, status, do_all;
336 git_config(reflog_expire_config);
338 save_commit_buffer = 0;
339 do_all = status = 0;
340 memset(&cb, 0, sizeof(cb));
342 if (!default_reflog_expire_unreachable)
343 default_reflog_expire_unreachable = now - 30 * 24 * 3600;
344 if (!default_reflog_expire)
345 default_reflog_expire = now - 90 * 24 * 3600;
346 cb.expire_total = default_reflog_expire;
347 cb.expire_unreachable = default_reflog_expire_unreachable;
350 * We can trust the commits and objects reachable from refs
351 * even in older repository. We cannot trust what's reachable
352 * from reflog if the repository was pruned with older git.
355 for (i = 1; i < argc; i++) {
356 const char *arg = argv[i];
357 if (!strcmp(arg, "--dry-run") || !strcmp(arg, "-n"))
358 cb.dry_run = 1;
359 else if (!prefixcmp(arg, "--expire="))
360 cb.expire_total = approxidate(arg + 9);
361 else if (!prefixcmp(arg, "--expire-unreachable="))
362 cb.expire_unreachable = approxidate(arg + 21);
363 else if (!strcmp(arg, "--stale-fix"))
364 cb.stalefix = 1;
365 else if (!strcmp(arg, "--all"))
366 do_all = 1;
367 else if (!strcmp(arg, "--verbose"))
368 cb.verbose = 1;
369 else if (!strcmp(arg, "--")) {
370 i++;
371 break;
373 else if (arg[0] == '-')
374 usage(reflog_expire_usage);
375 else
376 break;
378 if (cb.stalefix) {
379 init_revisions(&cb.revs, prefix);
380 if (cb.verbose)
381 printf("Marking reachable objects...");
382 mark_reachable_objects(&cb.revs, 0);
383 if (cb.verbose)
384 putchar('\n');
387 if (do_all) {
388 struct collect_reflog_cb collected;
389 int i;
391 memset(&collected, 0, sizeof(collected));
392 for_each_reflog(collect_reflog, &collected);
393 for (i = 0; i < collected.nr; i++) {
394 struct collected_reflog *e = collected.e[i];
395 status |= expire_reflog(e->reflog, e->sha1, 0, &cb);
396 free(e);
398 free(collected.e);
401 while (i < argc) {
402 const char *ref = argv[i++];
403 unsigned char sha1[20];
404 if (!resolve_ref(ref, sha1, 1, NULL)) {
405 status |= error("%s points nowhere!", ref);
406 continue;
408 status |= expire_reflog(ref, sha1, 0, &cb);
410 return status;
413 static int count_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
414 const char *email, unsigned long timestamp, int tz,
415 const char *message, void *cb_data)
417 struct cmd_reflog_expire_cb *cb = cb_data;
418 if (!cb->expire_total || timestamp < cb->expire_total)
419 cb->recno++;
420 return 0;
423 static int cmd_reflog_delete(int argc, const char **argv, const char *prefix)
425 struct cmd_reflog_expire_cb cb;
426 int i, status = 0;
428 if (argc < 2)
429 return error("Nothing to delete?");
431 memset(&cb, 0, sizeof(cb));
433 for (i = 1; i < argc; i++) {
434 const char *spec = strstr(argv[i], "@{");
435 unsigned char sha1[20];
436 char *ep, *ref;
437 int recno;
439 if (!spec) {
440 status |= error("Not a reflog: %s", argv[i]);
441 continue;
444 if (!dwim_ref(argv[i], spec - argv[i], sha1, &ref)) {
445 status |= error("%s points nowhere!", argv[i]);
446 continue;
449 recno = strtoul(spec + 2, &ep, 10);
450 if (*ep == '}') {
451 cb.recno = -recno;
452 for_each_reflog_ent(ref, count_reflog_ent, &cb);
453 } else {
454 cb.expire_total = approxidate(spec + 2);
455 for_each_reflog_ent(ref, count_reflog_ent, &cb);
456 cb.expire_total = 0;
459 status |= expire_reflog(ref, sha1, 0, &cb);
460 free(ref);
462 return status;
466 * main "reflog"
469 static const char reflog_usage[] =
470 "git-reflog (expire | ...)";
472 int cmd_reflog(int argc, const char **argv, const char *prefix)
474 /* With no command, we default to showing it. */
475 if (argc < 2 || *argv[1] == '-')
476 return cmd_log_reflog(argc, argv, prefix);
478 if (!strcmp(argv[1], "show"))
479 return cmd_log_reflog(argc - 1, argv + 1, prefix);
481 if (!strcmp(argv[1], "expire"))
482 return cmd_reflog_expire(argc - 1, argv + 1, prefix);
484 if (!strcmp(argv[1], "delete"))
485 return cmd_reflog_delete(argc - 1, argv + 1, prefix);
487 /* Not a recognized reflog command..*/
488 usage(reflog_usage);