]>
Commit | Line | Data |
---|---|---|
4264dc15 JH |
1 | #include "cache.h" |
2 | #include "builtin.h" | |
3 | #include "commit.h" | |
4 | #include "refs.h" | |
5 | #include "dir.h" | |
8d8b9f62 | 6 | #include "tree-walk.h" |
1389d9dd JH |
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[] = | |
cf39f54e | 16 | "git-reflog (show|expire) [--verbose] [--dry-run] [--stale-fix] [--expire=<time>] [--expire-unreachable=<time>] [--all] <refs>..."; |
4264dc15 | 17 | |
4aec56d1 JH |
18 | static unsigned long default_reflog_expire; |
19 | static unsigned long default_reflog_expire_unreachable; | |
20 | ||
1389d9dd JH |
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; | |
552cecc2 | 28 | int recno; |
1389d9dd JH |
29 | }; |
30 | ||
4264dc15 JH |
31 | struct expire_reflog_cb { |
32 | FILE *newlog; | |
33 | const char *ref; | |
34 | struct commit *ref_commit; | |
1389d9dd | 35 | struct cmd_reflog_expire_cb *cmd; |
4264dc15 JH |
36 | }; |
37 | ||
bda3a31c JH |
38 | struct collected_reflog { |
39 | unsigned char sha1[20]; | |
40 | char reflog[FLEX_ARRAY]; | |
41 | }; | |
42 | struct collect_reflog_cb { | |
43 | struct collected_reflog **e; | |
44 | int alloc; | |
45 | int nr; | |
46 | }; | |
47 | ||
cd1f9c36 JH |
48 | #define INCOMPLETE (1u<<10) |
49 | #define STUDYING (1u<<11) | |
50 | ||
8d8b9f62 JH |
51 | static int tree_is_complete(const unsigned char *sha1) |
52 | { | |
53 | struct tree_desc desc; | |
cd1f9c36 JH |
54 | struct name_entry entry; |
55 | int complete; | |
56 | struct tree *tree; | |
8d8b9f62 | 57 | |
cd1f9c36 JH |
58 | tree = lookup_tree(sha1); |
59 | if (!tree) | |
8d8b9f62 | 60 | return 0; |
cd1f9c36 JH |
61 | if (tree->object.flags & SEEN) |
62 | return 1; | |
63 | if (tree->object.flags & INCOMPLETE) | |
64 | return 0; | |
65 | ||
6fda5e51 | 66 | if (!tree->buffer) { |
21666f1a | 67 | enum object_type type; |
6fda5e51 LT |
68 | unsigned long size; |
69 | void *data = read_sha1_file(sha1, &type, &size); | |
cd1f9c36 JH |
70 | if (!data) { |
71 | tree->object.flags |= INCOMPLETE; | |
8d8b9f62 JH |
72 | return 0; |
73 | } | |
cd1f9c36 | 74 | tree->buffer = data; |
6fda5e51 | 75 | tree->size = size; |
8d8b9f62 | 76 | } |
6fda5e51 | 77 | init_tree_desc(&desc, tree->buffer, tree->size); |
cd1f9c36 JH |
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; | |
84 | } | |
85 | } | |
86 | free(tree->buffer); | |
87 | tree->buffer = NULL; | |
8d8b9f62 | 88 | |
cd1f9c36 JH |
89 | if (complete) |
90 | tree->object.flags |= SEEN; | |
91 | return complete; | |
92 | } | |
1389d9dd JH |
93 | |
94 | static int commit_is_complete(struct commit *commit) | |
95 | { | |
96 | struct object_array study; | |
97 | struct object_array found; | |
98 | int is_incomplete = 0; | |
99 | int i; | |
100 | ||
101 | /* early return */ | |
102 | if (commit->object.flags & SEEN) | |
103 | return 1; | |
104 | if (commit->object.flags & INCOMPLETE) | |
105 | return 0; | |
106 | /* | |
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. | |
112 | */ | |
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; | |
121 | ||
122 | c = (struct commit *)study.objects[--study.nr].item; | |
123 | if (!c->object.parsed && !parse_object(c->object.sha1)) | |
124 | c->object.flags |= INCOMPLETE; | |
125 | ||
126 | if (c->object.flags & INCOMPLETE) { | |
127 | is_incomplete = 1; | |
128 | break; | |
129 | } | |
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); | |
139 | } | |
140 | } | |
141 | if (!is_incomplete) { | |
cd1f9c36 JH |
142 | /* |
143 | * make sure all commits in "found" array have all the | |
1389d9dd JH |
144 | * necessary objects. |
145 | */ | |
cd1f9c36 | 146 | for (i = 0; i < found.nr; i++) { |
1389d9dd JH |
147 | struct commit *c = |
148 | (struct commit *)found.objects[i].item; | |
cd1f9c36 | 149 | if (!tree_is_complete(c->tree->object.sha1)) { |
1389d9dd | 150 | is_incomplete = 1; |
cd1f9c36 JH |
151 | c->object.flags |= INCOMPLETE; |
152 | } | |
1389d9dd JH |
153 | } |
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; | |
158 | } | |
159 | } | |
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; | |
cd1f9c36 JH |
165 | else { |
166 | /* | |
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. | |
173 | */ | |
174 | for (i = 0; i < found.nr; i++) | |
175 | found.objects[i].item->flags |= SEEN; | |
176 | } | |
1389d9dd JH |
177 | /* free object arrays */ |
178 | free(study.objects); | |
179 | free(found.objects); | |
180 | return !is_incomplete; | |
181 | } | |
182 | ||
4264dc15 JH |
183 | static int keep_entry(struct commit **it, unsigned char *sha1) |
184 | { | |
8d8b9f62 JH |
185 | struct commit *commit; |
186 | ||
4264dc15 JH |
187 | if (is_null_sha1(sha1)) |
188 | return 1; | |
8d8b9f62 JH |
189 | commit = lookup_commit_reference_gently(sha1, 1); |
190 | if (!commit) | |
191 | return 0; | |
192 | ||
1389d9dd JH |
193 | /* |
194 | * Make sure everything in this commit exists. | |
195 | * | |
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. | |
200 | */ | |
201 | if (!commit_is_complete(commit)) | |
8d8b9f62 JH |
202 | return 0; |
203 | *it = commit; | |
204 | return 1; | |
4264dc15 JH |
205 | } |
206 | ||
207 | static int expire_reflog_ent(unsigned char *osha1, unsigned char *nsha1, | |
883d60fa JS |
208 | const char *email, unsigned long timestamp, int tz, |
209 | const char *message, void *cb_data) | |
4264dc15 JH |
210 | { |
211 | struct expire_reflog_cb *cb = cb_data; | |
4264dc15 JH |
212 | struct commit *old, *new; |
213 | ||
1389d9dd | 214 | if (timestamp < cb->cmd->expire_total) |
4264dc15 JH |
215 | goto prune; |
216 | ||
9bbaa6cc | 217 | old = new = NULL; |
1389d9dd JH |
218 | if (cb->cmd->stalefix && |
219 | (!keep_entry(&old, osha1) || !keep_entry(&new, nsha1))) | |
4264dc15 JH |
220 | goto prune; |
221 | ||
9bbaa6cc JH |
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); | |
4a164d48 JH |
229 | if ((old && !in_merge_bases(old, &cb->ref_commit, 1)) || |
230 | (new && !in_merge_bases(new, &cb->ref_commit, 1))) | |
9bbaa6cc JH |
231 | goto prune; |
232 | } | |
4264dc15 | 233 | |
552cecc2 JS |
234 | if (cb->cmd->recno && --(cb->cmd->recno) == 0) |
235 | goto prune; | |
236 | ||
883d60fa JS |
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); | |
244 | } | |
1389d9dd | 245 | if (cb->cmd->verbose) |
883d60fa | 246 | printf("keep %s", message); |
4264dc15 JH |
247 | return 0; |
248 | prune: | |
1389d9dd | 249 | if (!cb->newlog || cb->cmd->verbose) |
883d60fa | 250 | printf("%sprune %s", cb->newlog ? "" : "would ", message); |
4264dc15 JH |
251 | return 0; |
252 | } | |
253 | ||
4264dc15 JH |
254 | static int expire_reflog(const char *ref, const unsigned char *sha1, int unused, void *cb_data) |
255 | { | |
256 | struct cmd_reflog_expire_cb *cmd = cb_data; | |
257 | struct expire_reflog_cb cb; | |
258 | struct ref_lock *lock; | |
9a13f0b7 | 259 | char *log_file, *newlog_path = NULL; |
4264dc15 JH |
260 | int status = 0; |
261 | ||
4264dc15 JH |
262 | memset(&cb, 0, sizeof(cb)); |
263 | /* we take the lock for the ref itself to prevent it from | |
264 | * getting updated. | |
265 | */ | |
68db31cc | 266 | lock = lock_any_ref_for_update(ref, sha1, 0); |
4264dc15 JH |
267 | if (!lock) |
268 | return error("cannot lock ref '%s'", ref); | |
9a13f0b7 NP |
269 | log_file = xstrdup(git_path("logs/%s", ref)); |
270 | if (!file_exists(log_file)) | |
4264dc15 JH |
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"); | |
275 | } | |
276 | ||
277 | cb.ref_commit = lookup_commit_reference_gently(sha1, 1); | |
4264dc15 | 278 | cb.ref = ref; |
1389d9dd | 279 | cb.cmd = cmd; |
4264dc15 JH |
280 | for_each_reflog_ent(ref, expire_reflog_ent, &cb); |
281 | finish: | |
282 | if (cb.newlog) { | |
4cd883d7 | 283 | if (fclose(cb.newlog)) { |
4264dc15 JH |
284 | status |= error("%s: %s", strerror(errno), |
285 | newlog_path); | |
4cd883d7 BC |
286 | unlink(newlog_path); |
287 | } else if (rename(newlog_path, log_file)) { | |
4264dc15 | 288 | status |= error("cannot rename %s to %s", |
9a13f0b7 | 289 | newlog_path, log_file); |
4264dc15 JH |
290 | unlink(newlog_path); |
291 | } | |
292 | } | |
293 | free(newlog_path); | |
9a13f0b7 | 294 | free(log_file); |
4264dc15 JH |
295 | unlock_ref(lock); |
296 | return status; | |
297 | } | |
298 | ||
bda3a31c JH |
299 | static int collect_reflog(const char *ref, const unsigned char *sha1, int unused, void *cb_data) |
300 | { | |
301 | struct collected_reflog *e; | |
302 | struct collect_reflog_cb *cb = cb_data; | |
303 | size_t namelen = strlen(ref); | |
304 | ||
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; | |
311 | } | |
312 | ||
4aec56d1 JH |
313 | static int reflog_expire_config(const char *var, const char *value) |
314 | { | |
4f342b96 JH |
315 | if (!strcmp(var, "gc.reflogexpire")) { |
316 | if (!value) | |
317 | config_error_nonbool(var); | |
4aec56d1 | 318 | default_reflog_expire = approxidate(value); |
4f342b96 JH |
319 | return 0; |
320 | } | |
321 | if (!strcmp(var, "gc.reflogexpireunreachable")) { | |
322 | if (!value) | |
323 | config_error_nonbool(var); | |
4aec56d1 | 324 | default_reflog_expire_unreachable = approxidate(value); |
4f342b96 JH |
325 | return 0; |
326 | } | |
327 | return git_default_config(var, value); | |
4aec56d1 JH |
328 | } |
329 | ||
4264dc15 JH |
330 | static int cmd_reflog_expire(int argc, const char **argv, const char *prefix) |
331 | { | |
332 | struct cmd_reflog_expire_cb cb; | |
333 | unsigned long now = time(NULL); | |
334 | int i, status, do_all; | |
335 | ||
4aec56d1 JH |
336 | git_config(reflog_expire_config); |
337 | ||
4264dc15 JH |
338 | save_commit_buffer = 0; |
339 | do_all = status = 0; | |
340 | memset(&cb, 0, sizeof(cb)); | |
4aec56d1 JH |
341 | |
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; | |
4264dc15 | 348 | |
1389d9dd JH |
349 | /* |
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. | |
353 | */ | |
354 | ||
4264dc15 JH |
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; | |
cc44c765 | 359 | else if (!prefixcmp(arg, "--expire=")) |
4264dc15 | 360 | cb.expire_total = approxidate(arg + 9); |
cc44c765 | 361 | else if (!prefixcmp(arg, "--expire-unreachable=")) |
4264dc15 | 362 | cb.expire_unreachable = approxidate(arg + 21); |
1389d9dd JH |
363 | else if (!strcmp(arg, "--stale-fix")) |
364 | cb.stalefix = 1; | |
4264dc15 JH |
365 | else if (!strcmp(arg, "--all")) |
366 | do_all = 1; | |
1389d9dd JH |
367 | else if (!strcmp(arg, "--verbose")) |
368 | cb.verbose = 1; | |
4264dc15 JH |
369 | else if (!strcmp(arg, "--")) { |
370 | i++; | |
371 | break; | |
372 | } | |
373 | else if (arg[0] == '-') | |
374 | usage(reflog_expire_usage); | |
375 | else | |
376 | break; | |
377 | } | |
1389d9dd JH |
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'); | |
385 | } | |
386 | ||
bda3a31c JH |
387 | if (do_all) { |
388 | struct collect_reflog_cb collected; | |
389 | int i; | |
390 | ||
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); | |
397 | } | |
398 | free(collected.e); | |
399 | } | |
400 | ||
4264dc15 JH |
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; | |
407 | } | |
408 | status |= expire_reflog(ref, sha1, 0, &cb); | |
409 | } | |
410 | return status; | |
411 | } | |
412 | ||
552cecc2 JS |
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) | |
416 | { | |
417 | struct cmd_reflog_expire_cb *cb = cb_data; | |
418 | if (!cb->expire_total || timestamp < cb->expire_total) | |
419 | cb->recno++; | |
420 | return 0; | |
421 | } | |
422 | ||
423 | static int cmd_reflog_delete(int argc, const char **argv, const char *prefix) | |
424 | { | |
425 | struct cmd_reflog_expire_cb cb; | |
426 | int i, status = 0; | |
427 | ||
428 | if (argc < 2) | |
429 | return error("Nothing to delete?"); | |
430 | ||
431 | memset(&cb, 0, sizeof(cb)); | |
432 | ||
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; | |
438 | ||
439 | if (!spec) { | |
cb97cc9f | 440 | status |= error("Not a reflog: %s", argv[i]); |
552cecc2 JS |
441 | continue; |
442 | } | |
443 | ||
444 | if (!dwim_ref(argv[i], spec - argv[i], sha1, &ref)) { | |
445 | status |= error("%s points nowhere!", argv[i]); | |
446 | continue; | |
447 | } | |
448 | ||
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; | |
457 | } | |
458 | ||
459 | status |= expire_reflog(ref, sha1, 0, &cb); | |
460 | free(ref); | |
461 | } | |
462 | return status; | |
463 | } | |
464 | ||
1389d9dd JH |
465 | /* |
466 | * main "reflog" | |
467 | */ | |
468 | ||
4264dc15 JH |
469 | static const char reflog_usage[] = |
470 | "git-reflog (expire | ...)"; | |
471 | ||
472 | int cmd_reflog(int argc, const char **argv, const char *prefix) | |
473 | { | |
cf39f54e LT |
474 | /* With no command, we default to showing it. */ |
475 | if (argc < 2 || *argv[1] == '-') | |
476 | return cmd_log_reflog(argc, argv, prefix); | |
477 | ||
478 | if (!strcmp(argv[1], "show")) | |
479 | return cmd_log_reflog(argc - 1, argv + 1, prefix); | |
480 | ||
481 | if (!strcmp(argv[1], "expire")) | |
4264dc15 | 482 | return cmd_reflog_expire(argc - 1, argv + 1, prefix); |
cf39f54e | 483 | |
552cecc2 JS |
484 | if (!strcmp(argv[1], "delete")) |
485 | return cmd_reflog_delete(argc - 1, argv + 1, prefix); | |
486 | ||
cf39f54e LT |
487 | /* Not a recognized reflog command..*/ |
488 | usage(reflog_usage); | |
4264dc15 | 489 | } |