]>
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; | |
28 | }; | |
29 | ||
4264dc15 JH |
30 | struct expire_reflog_cb { |
31 | FILE *newlog; | |
32 | const char *ref; | |
33 | struct commit *ref_commit; | |
1389d9dd | 34 | struct cmd_reflog_expire_cb *cmd; |
4264dc15 JH |
35 | }; |
36 | ||
bda3a31c JH |
37 | struct collected_reflog { |
38 | unsigned char sha1[20]; | |
39 | char reflog[FLEX_ARRAY]; | |
40 | }; | |
41 | struct collect_reflog_cb { | |
42 | struct collected_reflog **e; | |
43 | int alloc; | |
44 | int nr; | |
45 | }; | |
46 | ||
cd1f9c36 JH |
47 | #define INCOMPLETE (1u<<10) |
48 | #define STUDYING (1u<<11) | |
49 | ||
8d8b9f62 JH |
50 | static int tree_is_complete(const unsigned char *sha1) |
51 | { | |
52 | struct tree_desc desc; | |
cd1f9c36 JH |
53 | struct name_entry entry; |
54 | int complete; | |
55 | struct tree *tree; | |
8d8b9f62 | 56 | |
cd1f9c36 JH |
57 | tree = lookup_tree(sha1); |
58 | if (!tree) | |
8d8b9f62 | 59 | return 0; |
cd1f9c36 JH |
60 | if (tree->object.flags & SEEN) |
61 | return 1; | |
62 | if (tree->object.flags & INCOMPLETE) | |
63 | return 0; | |
64 | ||
6fda5e51 | 65 | if (!tree->buffer) { |
21666f1a | 66 | enum object_type type; |
6fda5e51 LT |
67 | unsigned long size; |
68 | void *data = read_sha1_file(sha1, &type, &size); | |
cd1f9c36 JH |
69 | if (!data) { |
70 | tree->object.flags |= INCOMPLETE; | |
8d8b9f62 JH |
71 | return 0; |
72 | } | |
cd1f9c36 | 73 | tree->buffer = data; |
6fda5e51 | 74 | tree->size = size; |
8d8b9f62 | 75 | } |
6fda5e51 | 76 | init_tree_desc(&desc, tree->buffer, tree->size); |
cd1f9c36 JH |
77 | complete = 1; |
78 | while (tree_entry(&desc, &entry)) { | |
79 | if (!has_sha1_file(entry.sha1) || | |
80 | (S_ISDIR(entry.mode) && !tree_is_complete(entry.sha1))) { | |
81 | tree->object.flags |= INCOMPLETE; | |
82 | complete = 0; | |
83 | } | |
84 | } | |
85 | free(tree->buffer); | |
86 | tree->buffer = NULL; | |
8d8b9f62 | 87 | |
cd1f9c36 JH |
88 | if (complete) |
89 | tree->object.flags |= SEEN; | |
90 | return complete; | |
91 | } | |
1389d9dd JH |
92 | |
93 | static int commit_is_complete(struct commit *commit) | |
94 | { | |
95 | struct object_array study; | |
96 | struct object_array found; | |
97 | int is_incomplete = 0; | |
98 | int i; | |
99 | ||
100 | /* early return */ | |
101 | if (commit->object.flags & SEEN) | |
102 | return 1; | |
103 | if (commit->object.flags & INCOMPLETE) | |
104 | return 0; | |
105 | /* | |
106 | * Find all commits that are reachable and are not marked as | |
107 | * SEEN. Then make sure the trees and blobs contained are | |
108 | * complete. After that, mark these commits also as SEEN. | |
109 | * If some of the objects that are needed to complete this | |
110 | * commit are missing, mark this commit as INCOMPLETE. | |
111 | */ | |
112 | memset(&study, 0, sizeof(study)); | |
113 | memset(&found, 0, sizeof(found)); | |
114 | add_object_array(&commit->object, NULL, &study); | |
115 | add_object_array(&commit->object, NULL, &found); | |
116 | commit->object.flags |= STUDYING; | |
117 | while (study.nr) { | |
118 | struct commit *c; | |
119 | struct commit_list *parent; | |
120 | ||
121 | c = (struct commit *)study.objects[--study.nr].item; | |
122 | if (!c->object.parsed && !parse_object(c->object.sha1)) | |
123 | c->object.flags |= INCOMPLETE; | |
124 | ||
125 | if (c->object.flags & INCOMPLETE) { | |
126 | is_incomplete = 1; | |
127 | break; | |
128 | } | |
129 | else if (c->object.flags & SEEN) | |
130 | continue; | |
131 | for (parent = c->parents; parent; parent = parent->next) { | |
132 | struct commit *p = parent->item; | |
133 | if (p->object.flags & STUDYING) | |
134 | continue; | |
135 | p->object.flags |= STUDYING; | |
136 | add_object_array(&p->object, NULL, &study); | |
137 | add_object_array(&p->object, NULL, &found); | |
138 | } | |
139 | } | |
140 | if (!is_incomplete) { | |
cd1f9c36 JH |
141 | /* |
142 | * make sure all commits in "found" array have all the | |
1389d9dd JH |
143 | * necessary objects. |
144 | */ | |
cd1f9c36 | 145 | for (i = 0; i < found.nr; i++) { |
1389d9dd JH |
146 | struct commit *c = |
147 | (struct commit *)found.objects[i].item; | |
cd1f9c36 | 148 | if (!tree_is_complete(c->tree->object.sha1)) { |
1389d9dd | 149 | is_incomplete = 1; |
cd1f9c36 JH |
150 | c->object.flags |= INCOMPLETE; |
151 | } | |
1389d9dd JH |
152 | } |
153 | if (!is_incomplete) { | |
154 | /* mark all found commits as complete, iow SEEN */ | |
155 | for (i = 0; i < found.nr; i++) | |
156 | found.objects[i].item->flags |= SEEN; | |
157 | } | |
158 | } | |
159 | /* clear flags from the objects we traversed */ | |
160 | for (i = 0; i < found.nr; i++) | |
161 | found.objects[i].item->flags &= ~STUDYING; | |
162 | if (is_incomplete) | |
163 | commit->object.flags |= INCOMPLETE; | |
cd1f9c36 JH |
164 | else { |
165 | /* | |
166 | * If we come here, we have (1) traversed the ancestry chain | |
167 | * from the "commit" until we reach SEEN commits (which are | |
168 | * known to be complete), and (2) made sure that the commits | |
169 | * encountered during the above traversal refer to trees that | |
170 | * are complete. Which means that we know *all* the commits | |
171 | * we have seen during this process are complete. | |
172 | */ | |
173 | for (i = 0; i < found.nr; i++) | |
174 | found.objects[i].item->flags |= SEEN; | |
175 | } | |
1389d9dd JH |
176 | /* free object arrays */ |
177 | free(study.objects); | |
178 | free(found.objects); | |
179 | return !is_incomplete; | |
180 | } | |
181 | ||
4264dc15 JH |
182 | static int keep_entry(struct commit **it, unsigned char *sha1) |
183 | { | |
8d8b9f62 JH |
184 | struct commit *commit; |
185 | ||
4264dc15 JH |
186 | if (is_null_sha1(sha1)) |
187 | return 1; | |
8d8b9f62 JH |
188 | commit = lookup_commit_reference_gently(sha1, 1); |
189 | if (!commit) | |
190 | return 0; | |
191 | ||
1389d9dd JH |
192 | /* |
193 | * Make sure everything in this commit exists. | |
194 | * | |
195 | * We have walked all the objects reachable from the refs | |
196 | * and cache earlier. The commits reachable by this commit | |
197 | * must meet SEEN commits -- and then we should mark them as | |
198 | * SEEN as well. | |
199 | */ | |
200 | if (!commit_is_complete(commit)) | |
8d8b9f62 JH |
201 | return 0; |
202 | *it = commit; | |
203 | return 1; | |
4264dc15 JH |
204 | } |
205 | ||
206 | static int expire_reflog_ent(unsigned char *osha1, unsigned char *nsha1, | |
883d60fa JS |
207 | const char *email, unsigned long timestamp, int tz, |
208 | const char *message, void *cb_data) | |
4264dc15 JH |
209 | { |
210 | struct expire_reflog_cb *cb = cb_data; | |
4264dc15 JH |
211 | struct commit *old, *new; |
212 | ||
1389d9dd | 213 | if (timestamp < cb->cmd->expire_total) |
4264dc15 JH |
214 | goto prune; |
215 | ||
9bbaa6cc | 216 | old = new = NULL; |
1389d9dd JH |
217 | if (cb->cmd->stalefix && |
218 | (!keep_entry(&old, osha1) || !keep_entry(&new, nsha1))) | |
4264dc15 JH |
219 | goto prune; |
220 | ||
9bbaa6cc JH |
221 | if (timestamp < cb->cmd->expire_unreachable) { |
222 | if (!cb->ref_commit) | |
223 | goto prune; | |
224 | if (!old && !is_null_sha1(osha1)) | |
225 | old = lookup_commit_reference_gently(osha1, 1); | |
226 | if (!new && !is_null_sha1(nsha1)) | |
227 | new = lookup_commit_reference_gently(nsha1, 1); | |
4a164d48 JH |
228 | if ((old && !in_merge_bases(old, &cb->ref_commit, 1)) || |
229 | (new && !in_merge_bases(new, &cb->ref_commit, 1))) | |
9bbaa6cc JH |
230 | goto prune; |
231 | } | |
4264dc15 | 232 | |
883d60fa JS |
233 | if (cb->newlog) { |
234 | char sign = (tz < 0) ? '-' : '+'; | |
235 | int zone = (tz < 0) ? (-tz) : tz; | |
236 | fprintf(cb->newlog, "%s %s %s %lu %c%04d\t%s", | |
237 | sha1_to_hex(osha1), sha1_to_hex(nsha1), | |
238 | email, timestamp, sign, zone, | |
239 | message); | |
240 | } | |
1389d9dd | 241 | if (cb->cmd->verbose) |
883d60fa | 242 | printf("keep %s", message); |
4264dc15 JH |
243 | return 0; |
244 | prune: | |
1389d9dd | 245 | if (!cb->newlog || cb->cmd->verbose) |
883d60fa | 246 | printf("%sprune %s", cb->newlog ? "" : "would ", message); |
4264dc15 JH |
247 | return 0; |
248 | } | |
249 | ||
4264dc15 JH |
250 | static int expire_reflog(const char *ref, const unsigned char *sha1, int unused, void *cb_data) |
251 | { | |
252 | struct cmd_reflog_expire_cb *cmd = cb_data; | |
253 | struct expire_reflog_cb cb; | |
254 | struct ref_lock *lock; | |
9a13f0b7 | 255 | char *log_file, *newlog_path = NULL; |
4264dc15 JH |
256 | int status = 0; |
257 | ||
4264dc15 JH |
258 | memset(&cb, 0, sizeof(cb)); |
259 | /* we take the lock for the ref itself to prevent it from | |
260 | * getting updated. | |
261 | */ | |
68db31cc | 262 | lock = lock_any_ref_for_update(ref, sha1, 0); |
4264dc15 JH |
263 | if (!lock) |
264 | return error("cannot lock ref '%s'", ref); | |
9a13f0b7 NP |
265 | log_file = xstrdup(git_path("logs/%s", ref)); |
266 | if (!file_exists(log_file)) | |
4264dc15 JH |
267 | goto finish; |
268 | if (!cmd->dry_run) { | |
269 | newlog_path = xstrdup(git_path("logs/%s.lock", ref)); | |
270 | cb.newlog = fopen(newlog_path, "w"); | |
271 | } | |
272 | ||
273 | cb.ref_commit = lookup_commit_reference_gently(sha1, 1); | |
4264dc15 | 274 | cb.ref = ref; |
1389d9dd | 275 | cb.cmd = cmd; |
4264dc15 JH |
276 | for_each_reflog_ent(ref, expire_reflog_ent, &cb); |
277 | finish: | |
278 | if (cb.newlog) { | |
279 | if (fclose(cb.newlog)) | |
280 | status |= error("%s: %s", strerror(errno), | |
281 | newlog_path); | |
9a13f0b7 | 282 | if (rename(newlog_path, log_file)) { |
4264dc15 | 283 | status |= error("cannot rename %s to %s", |
9a13f0b7 | 284 | newlog_path, log_file); |
4264dc15 JH |
285 | unlink(newlog_path); |
286 | } | |
287 | } | |
288 | free(newlog_path); | |
9a13f0b7 | 289 | free(log_file); |
4264dc15 JH |
290 | unlock_ref(lock); |
291 | return status; | |
292 | } | |
293 | ||
bda3a31c JH |
294 | static int collect_reflog(const char *ref, const unsigned char *sha1, int unused, void *cb_data) |
295 | { | |
296 | struct collected_reflog *e; | |
297 | struct collect_reflog_cb *cb = cb_data; | |
298 | size_t namelen = strlen(ref); | |
299 | ||
300 | e = xmalloc(sizeof(*e) + namelen + 1); | |
301 | hashcpy(e->sha1, sha1); | |
302 | memcpy(e->reflog, ref, namelen + 1); | |
303 | ALLOC_GROW(cb->e, cb->nr + 1, cb->alloc); | |
304 | cb->e[cb->nr++] = e; | |
305 | return 0; | |
306 | } | |
307 | ||
4aec56d1 JH |
308 | static int reflog_expire_config(const char *var, const char *value) |
309 | { | |
4f342b96 JH |
310 | if (!strcmp(var, "gc.reflogexpire")) { |
311 | if (!value) | |
312 | config_error_nonbool(var); | |
4aec56d1 | 313 | default_reflog_expire = approxidate(value); |
4f342b96 JH |
314 | return 0; |
315 | } | |
316 | if (!strcmp(var, "gc.reflogexpireunreachable")) { | |
317 | if (!value) | |
318 | config_error_nonbool(var); | |
4aec56d1 | 319 | default_reflog_expire_unreachable = approxidate(value); |
4f342b96 JH |
320 | return 0; |
321 | } | |
322 | return git_default_config(var, value); | |
4aec56d1 JH |
323 | } |
324 | ||
4264dc15 JH |
325 | static int cmd_reflog_expire(int argc, const char **argv, const char *prefix) |
326 | { | |
327 | struct cmd_reflog_expire_cb cb; | |
328 | unsigned long now = time(NULL); | |
329 | int i, status, do_all; | |
330 | ||
4aec56d1 JH |
331 | git_config(reflog_expire_config); |
332 | ||
4264dc15 JH |
333 | save_commit_buffer = 0; |
334 | do_all = status = 0; | |
335 | memset(&cb, 0, sizeof(cb)); | |
4aec56d1 JH |
336 | |
337 | if (!default_reflog_expire_unreachable) | |
338 | default_reflog_expire_unreachable = now - 30 * 24 * 3600; | |
339 | if (!default_reflog_expire) | |
340 | default_reflog_expire = now - 90 * 24 * 3600; | |
341 | cb.expire_total = default_reflog_expire; | |
342 | cb.expire_unreachable = default_reflog_expire_unreachable; | |
4264dc15 | 343 | |
1389d9dd JH |
344 | /* |
345 | * We can trust the commits and objects reachable from refs | |
346 | * even in older repository. We cannot trust what's reachable | |
347 | * from reflog if the repository was pruned with older git. | |
348 | */ | |
349 | ||
4264dc15 JH |
350 | for (i = 1; i < argc; i++) { |
351 | const char *arg = argv[i]; | |
352 | if (!strcmp(arg, "--dry-run") || !strcmp(arg, "-n")) | |
353 | cb.dry_run = 1; | |
cc44c765 | 354 | else if (!prefixcmp(arg, "--expire=")) |
4264dc15 | 355 | cb.expire_total = approxidate(arg + 9); |
cc44c765 | 356 | else if (!prefixcmp(arg, "--expire-unreachable=")) |
4264dc15 | 357 | cb.expire_unreachable = approxidate(arg + 21); |
1389d9dd JH |
358 | else if (!strcmp(arg, "--stale-fix")) |
359 | cb.stalefix = 1; | |
4264dc15 JH |
360 | else if (!strcmp(arg, "--all")) |
361 | do_all = 1; | |
1389d9dd JH |
362 | else if (!strcmp(arg, "--verbose")) |
363 | cb.verbose = 1; | |
4264dc15 JH |
364 | else if (!strcmp(arg, "--")) { |
365 | i++; | |
366 | break; | |
367 | } | |
368 | else if (arg[0] == '-') | |
369 | usage(reflog_expire_usage); | |
370 | else | |
371 | break; | |
372 | } | |
1389d9dd JH |
373 | if (cb.stalefix) { |
374 | init_revisions(&cb.revs, prefix); | |
375 | if (cb.verbose) | |
376 | printf("Marking reachable objects..."); | |
377 | mark_reachable_objects(&cb.revs, 0); | |
378 | if (cb.verbose) | |
379 | putchar('\n'); | |
380 | } | |
381 | ||
bda3a31c JH |
382 | if (do_all) { |
383 | struct collect_reflog_cb collected; | |
384 | int i; | |
385 | ||
386 | memset(&collected, 0, sizeof(collected)); | |
387 | for_each_reflog(collect_reflog, &collected); | |
388 | for (i = 0; i < collected.nr; i++) { | |
389 | struct collected_reflog *e = collected.e[i]; | |
390 | status |= expire_reflog(e->reflog, e->sha1, 0, &cb); | |
391 | free(e); | |
392 | } | |
393 | free(collected.e); | |
394 | } | |
395 | ||
4264dc15 JH |
396 | while (i < argc) { |
397 | const char *ref = argv[i++]; | |
398 | unsigned char sha1[20]; | |
399 | if (!resolve_ref(ref, sha1, 1, NULL)) { | |
400 | status |= error("%s points nowhere!", ref); | |
401 | continue; | |
402 | } | |
403 | status |= expire_reflog(ref, sha1, 0, &cb); | |
404 | } | |
405 | return status; | |
406 | } | |
407 | ||
1389d9dd JH |
408 | /* |
409 | * main "reflog" | |
410 | */ | |
411 | ||
4264dc15 JH |
412 | static const char reflog_usage[] = |
413 | "git-reflog (expire | ...)"; | |
414 | ||
415 | int cmd_reflog(int argc, const char **argv, const char *prefix) | |
416 | { | |
cf39f54e LT |
417 | /* With no command, we default to showing it. */ |
418 | if (argc < 2 || *argv[1] == '-') | |
419 | return cmd_log_reflog(argc, argv, prefix); | |
420 | ||
421 | if (!strcmp(argv[1], "show")) | |
422 | return cmd_log_reflog(argc - 1, argv + 1, prefix); | |
423 | ||
424 | if (!strcmp(argv[1], "expire")) | |
4264dc15 | 425 | return cmd_reflog_expire(argc - 1, argv + 1, prefix); |
cf39f54e LT |
426 | |
427 | /* Not a recognized reflog command..*/ | |
428 | usage(reflog_usage); | |
4264dc15 | 429 | } |