]>
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[] = | |
1b1dd23f | 16 | "git reflog (show|expire) [--verbose] [--dry-run] [--stale-fix] [--expire=<time>] [--expire-unreachable=<time>] [--all] <refs>..."; |
3c386aa3 | 17 | static const char reflog_delete_usage[] = |
1b1dd23f | 18 | "git reflog delete [--verbose] [--dry-run] [--rewrite] [--updateref] <refs>..."; |
4264dc15 | 19 | |
4aec56d1 JH |
20 | static unsigned long default_reflog_expire; |
21 | static unsigned long default_reflog_expire_unreachable; | |
22 | ||
1389d9dd JH |
23 | struct cmd_reflog_expire_cb { |
24 | struct rev_info revs; | |
25 | int dry_run; | |
26 | int stalefix; | |
2b81fab2 | 27 | int rewrite; |
55f10565 | 28 | int updateref; |
1389d9dd JH |
29 | int verbose; |
30 | unsigned long expire_total; | |
31 | unsigned long expire_unreachable; | |
552cecc2 | 32 | int recno; |
1389d9dd JH |
33 | }; |
34 | ||
4264dc15 JH |
35 | struct expire_reflog_cb { |
36 | FILE *newlog; | |
37 | const char *ref; | |
38 | struct commit *ref_commit; | |
1389d9dd | 39 | struct cmd_reflog_expire_cb *cmd; |
2b81fab2 | 40 | unsigned char last_kept_sha1[20]; |
4264dc15 JH |
41 | }; |
42 | ||
bda3a31c JH |
43 | struct collected_reflog { |
44 | unsigned char sha1[20]; | |
45 | char reflog[FLEX_ARRAY]; | |
46 | }; | |
47 | struct collect_reflog_cb { | |
48 | struct collected_reflog **e; | |
49 | int alloc; | |
50 | int nr; | |
51 | }; | |
52 | ||
cd1f9c36 JH |
53 | #define INCOMPLETE (1u<<10) |
54 | #define STUDYING (1u<<11) | |
55 | ||
8d8b9f62 JH |
56 | static int tree_is_complete(const unsigned char *sha1) |
57 | { | |
58 | struct tree_desc desc; | |
cd1f9c36 JH |
59 | struct name_entry entry; |
60 | int complete; | |
61 | struct tree *tree; | |
8d8b9f62 | 62 | |
cd1f9c36 JH |
63 | tree = lookup_tree(sha1); |
64 | if (!tree) | |
8d8b9f62 | 65 | return 0; |
cd1f9c36 JH |
66 | if (tree->object.flags & SEEN) |
67 | return 1; | |
68 | if (tree->object.flags & INCOMPLETE) | |
69 | return 0; | |
70 | ||
6fda5e51 | 71 | if (!tree->buffer) { |
21666f1a | 72 | enum object_type type; |
6fda5e51 LT |
73 | unsigned long size; |
74 | void *data = read_sha1_file(sha1, &type, &size); | |
cd1f9c36 JH |
75 | if (!data) { |
76 | tree->object.flags |= INCOMPLETE; | |
8d8b9f62 JH |
77 | return 0; |
78 | } | |
cd1f9c36 | 79 | tree->buffer = data; |
6fda5e51 | 80 | tree->size = size; |
8d8b9f62 | 81 | } |
6fda5e51 | 82 | init_tree_desc(&desc, tree->buffer, tree->size); |
cd1f9c36 JH |
83 | complete = 1; |
84 | while (tree_entry(&desc, &entry)) { | |
85 | if (!has_sha1_file(entry.sha1) || | |
86 | (S_ISDIR(entry.mode) && !tree_is_complete(entry.sha1))) { | |
87 | tree->object.flags |= INCOMPLETE; | |
88 | complete = 0; | |
89 | } | |
90 | } | |
91 | free(tree->buffer); | |
92 | tree->buffer = NULL; | |
8d8b9f62 | 93 | |
cd1f9c36 JH |
94 | if (complete) |
95 | tree->object.flags |= SEEN; | |
96 | return complete; | |
97 | } | |
1389d9dd JH |
98 | |
99 | static int commit_is_complete(struct commit *commit) | |
100 | { | |
101 | struct object_array study; | |
102 | struct object_array found; | |
103 | int is_incomplete = 0; | |
104 | int i; | |
105 | ||
106 | /* early return */ | |
107 | if (commit->object.flags & SEEN) | |
108 | return 1; | |
109 | if (commit->object.flags & INCOMPLETE) | |
110 | return 0; | |
111 | /* | |
112 | * Find all commits that are reachable and are not marked as | |
113 | * SEEN. Then make sure the trees and blobs contained are | |
114 | * complete. After that, mark these commits also as SEEN. | |
115 | * If some of the objects that are needed to complete this | |
116 | * commit are missing, mark this commit as INCOMPLETE. | |
117 | */ | |
118 | memset(&study, 0, sizeof(study)); | |
119 | memset(&found, 0, sizeof(found)); | |
120 | add_object_array(&commit->object, NULL, &study); | |
121 | add_object_array(&commit->object, NULL, &found); | |
122 | commit->object.flags |= STUDYING; | |
123 | while (study.nr) { | |
124 | struct commit *c; | |
125 | struct commit_list *parent; | |
126 | ||
127 | c = (struct commit *)study.objects[--study.nr].item; | |
128 | if (!c->object.parsed && !parse_object(c->object.sha1)) | |
129 | c->object.flags |= INCOMPLETE; | |
130 | ||
131 | if (c->object.flags & INCOMPLETE) { | |
132 | is_incomplete = 1; | |
133 | break; | |
134 | } | |
135 | else if (c->object.flags & SEEN) | |
136 | continue; | |
137 | for (parent = c->parents; parent; parent = parent->next) { | |
138 | struct commit *p = parent->item; | |
139 | if (p->object.flags & STUDYING) | |
140 | continue; | |
141 | p->object.flags |= STUDYING; | |
142 | add_object_array(&p->object, NULL, &study); | |
143 | add_object_array(&p->object, NULL, &found); | |
144 | } | |
145 | } | |
146 | if (!is_incomplete) { | |
cd1f9c36 JH |
147 | /* |
148 | * make sure all commits in "found" array have all the | |
1389d9dd JH |
149 | * necessary objects. |
150 | */ | |
cd1f9c36 | 151 | for (i = 0; i < found.nr; i++) { |
1389d9dd JH |
152 | struct commit *c = |
153 | (struct commit *)found.objects[i].item; | |
cd1f9c36 | 154 | if (!tree_is_complete(c->tree->object.sha1)) { |
1389d9dd | 155 | is_incomplete = 1; |
cd1f9c36 JH |
156 | c->object.flags |= INCOMPLETE; |
157 | } | |
1389d9dd JH |
158 | } |
159 | if (!is_incomplete) { | |
160 | /* mark all found commits as complete, iow SEEN */ | |
161 | for (i = 0; i < found.nr; i++) | |
162 | found.objects[i].item->flags |= SEEN; | |
163 | } | |
164 | } | |
165 | /* clear flags from the objects we traversed */ | |
166 | for (i = 0; i < found.nr; i++) | |
167 | found.objects[i].item->flags &= ~STUDYING; | |
168 | if (is_incomplete) | |
169 | commit->object.flags |= INCOMPLETE; | |
cd1f9c36 JH |
170 | else { |
171 | /* | |
172 | * If we come here, we have (1) traversed the ancestry chain | |
173 | * from the "commit" until we reach SEEN commits (which are | |
174 | * known to be complete), and (2) made sure that the commits | |
175 | * encountered during the above traversal refer to trees that | |
176 | * are complete. Which means that we know *all* the commits | |
177 | * we have seen during this process are complete. | |
178 | */ | |
179 | for (i = 0; i < found.nr; i++) | |
180 | found.objects[i].item->flags |= SEEN; | |
181 | } | |
1389d9dd JH |
182 | /* free object arrays */ |
183 | free(study.objects); | |
184 | free(found.objects); | |
185 | return !is_incomplete; | |
186 | } | |
187 | ||
4264dc15 JH |
188 | static int keep_entry(struct commit **it, unsigned char *sha1) |
189 | { | |
8d8b9f62 JH |
190 | struct commit *commit; |
191 | ||
4264dc15 JH |
192 | if (is_null_sha1(sha1)) |
193 | return 1; | |
8d8b9f62 JH |
194 | commit = lookup_commit_reference_gently(sha1, 1); |
195 | if (!commit) | |
196 | return 0; | |
197 | ||
1389d9dd JH |
198 | /* |
199 | * Make sure everything in this commit exists. | |
200 | * | |
201 | * We have walked all the objects reachable from the refs | |
202 | * and cache earlier. The commits reachable by this commit | |
203 | * must meet SEEN commits -- and then we should mark them as | |
204 | * SEEN as well. | |
205 | */ | |
206 | if (!commit_is_complete(commit)) | |
8d8b9f62 JH |
207 | return 0; |
208 | *it = commit; | |
209 | return 1; | |
4264dc15 JH |
210 | } |
211 | ||
666e07e6 LT |
212 | static int unreachable(struct expire_reflog_cb *cb, struct commit *commit, unsigned char *sha1) |
213 | { | |
214 | /* | |
215 | * We may or may not have the commit yet - if not, look it | |
216 | * up using the supplied sha1. | |
217 | */ | |
218 | if (!commit) { | |
219 | if (is_null_sha1(sha1)) | |
220 | return 0; | |
221 | ||
222 | commit = lookup_commit_reference_gently(sha1, 1); | |
223 | ||
224 | /* Not a commit -- keep it */ | |
225 | if (!commit) | |
226 | return 0; | |
227 | } | |
228 | ||
229 | /* Reachable from the current ref? Don't prune. */ | |
230 | if (in_merge_bases(commit, &cb->ref_commit, 1)) | |
231 | return 0; | |
232 | ||
233 | /* We can't reach it - prune it. */ | |
234 | return 1; | |
235 | } | |
236 | ||
4264dc15 | 237 | static int expire_reflog_ent(unsigned char *osha1, unsigned char *nsha1, |
883d60fa JS |
238 | const char *email, unsigned long timestamp, int tz, |
239 | const char *message, void *cb_data) | |
4264dc15 JH |
240 | { |
241 | struct expire_reflog_cb *cb = cb_data; | |
4264dc15 JH |
242 | struct commit *old, *new; |
243 | ||
1389d9dd | 244 | if (timestamp < cb->cmd->expire_total) |
4264dc15 JH |
245 | goto prune; |
246 | ||
2b81fab2 BC |
247 | if (cb->cmd->rewrite) |
248 | osha1 = cb->last_kept_sha1; | |
249 | ||
9bbaa6cc | 250 | old = new = NULL; |
1389d9dd JH |
251 | if (cb->cmd->stalefix && |
252 | (!keep_entry(&old, osha1) || !keep_entry(&new, nsha1))) | |
4264dc15 JH |
253 | goto prune; |
254 | ||
9bbaa6cc JH |
255 | if (timestamp < cb->cmd->expire_unreachable) { |
256 | if (!cb->ref_commit) | |
257 | goto prune; | |
666e07e6 | 258 | if (unreachable(cb, old, osha1) || unreachable(cb, new, nsha1)) |
9bbaa6cc JH |
259 | goto prune; |
260 | } | |
4264dc15 | 261 | |
552cecc2 JS |
262 | if (cb->cmd->recno && --(cb->cmd->recno) == 0) |
263 | goto prune; | |
264 | ||
883d60fa JS |
265 | if (cb->newlog) { |
266 | char sign = (tz < 0) ? '-' : '+'; | |
267 | int zone = (tz < 0) ? (-tz) : tz; | |
268 | fprintf(cb->newlog, "%s %s %s %lu %c%04d\t%s", | |
269 | sha1_to_hex(osha1), sha1_to_hex(nsha1), | |
270 | email, timestamp, sign, zone, | |
271 | message); | |
2b81fab2 | 272 | hashcpy(cb->last_kept_sha1, nsha1); |
883d60fa | 273 | } |
1389d9dd | 274 | if (cb->cmd->verbose) |
883d60fa | 275 | printf("keep %s", message); |
4264dc15 JH |
276 | return 0; |
277 | prune: | |
1389d9dd | 278 | if (!cb->newlog || cb->cmd->verbose) |
883d60fa | 279 | printf("%sprune %s", cb->newlog ? "" : "would ", message); |
4264dc15 JH |
280 | return 0; |
281 | } | |
282 | ||
4264dc15 JH |
283 | static int expire_reflog(const char *ref, const unsigned char *sha1, int unused, void *cb_data) |
284 | { | |
285 | struct cmd_reflog_expire_cb *cmd = cb_data; | |
286 | struct expire_reflog_cb cb; | |
287 | struct ref_lock *lock; | |
9a13f0b7 | 288 | char *log_file, *newlog_path = NULL; |
4264dc15 JH |
289 | int status = 0; |
290 | ||
4264dc15 | 291 | memset(&cb, 0, sizeof(cb)); |
3cb22b8e JH |
292 | |
293 | /* | |
294 | * we take the lock for the ref itself to prevent it from | |
4264dc15 JH |
295 | * getting updated. |
296 | */ | |
68db31cc | 297 | lock = lock_any_ref_for_update(ref, sha1, 0); |
4264dc15 JH |
298 | if (!lock) |
299 | return error("cannot lock ref '%s'", ref); | |
a4f34cbb | 300 | log_file = git_pathdup("logs/%s", ref); |
9a13f0b7 | 301 | if (!file_exists(log_file)) |
4264dc15 JH |
302 | goto finish; |
303 | if (!cmd->dry_run) { | |
a4f34cbb | 304 | newlog_path = git_pathdup("logs/%s.lock", ref); |
4264dc15 JH |
305 | cb.newlog = fopen(newlog_path, "w"); |
306 | } | |
307 | ||
308 | cb.ref_commit = lookup_commit_reference_gently(sha1, 1); | |
4264dc15 | 309 | cb.ref = ref; |
1389d9dd | 310 | cb.cmd = cmd; |
4264dc15 JH |
311 | for_each_reflog_ent(ref, expire_reflog_ent, &cb); |
312 | finish: | |
313 | if (cb.newlog) { | |
4cd883d7 | 314 | if (fclose(cb.newlog)) { |
4264dc15 JH |
315 | status |= error("%s: %s", strerror(errno), |
316 | newlog_path); | |
4cd883d7 | 317 | unlink(newlog_path); |
55f10565 BC |
318 | } else if (cmd->updateref && |
319 | (write_in_full(lock->lock_fd, | |
320 | sha1_to_hex(cb.last_kept_sha1), 40) != 40 || | |
321 | write_in_full(lock->lock_fd, "\n", 1) != 1 || | |
322 | close_ref(lock) < 0)) { | |
323 | status |= error("Couldn't write %s", | |
324 | lock->lk->filename); | |
325 | unlink(newlog_path); | |
4cd883d7 | 326 | } else if (rename(newlog_path, log_file)) { |
4264dc15 | 327 | status |= error("cannot rename %s to %s", |
9a13f0b7 | 328 | newlog_path, log_file); |
4264dc15 | 329 | unlink(newlog_path); |
55f10565 BC |
330 | } else if (cmd->updateref && commit_ref(lock)) { |
331 | status |= error("Couldn't set %s", lock->ref_name); | |
336d09da PH |
332 | } else { |
333 | adjust_shared_perm(log_file); | |
4264dc15 JH |
334 | } |
335 | } | |
336 | free(newlog_path); | |
9a13f0b7 | 337 | free(log_file); |
4264dc15 JH |
338 | unlock_ref(lock); |
339 | return status; | |
340 | } | |
341 | ||
bda3a31c JH |
342 | static int collect_reflog(const char *ref, const unsigned char *sha1, int unused, void *cb_data) |
343 | { | |
344 | struct collected_reflog *e; | |
345 | struct collect_reflog_cb *cb = cb_data; | |
346 | size_t namelen = strlen(ref); | |
347 | ||
348 | e = xmalloc(sizeof(*e) + namelen + 1); | |
349 | hashcpy(e->sha1, sha1); | |
350 | memcpy(e->reflog, ref, namelen + 1); | |
351 | ALLOC_GROW(cb->e, cb->nr + 1, cb->alloc); | |
352 | cb->e[cb->nr++] = e; | |
353 | return 0; | |
354 | } | |
355 | ||
3cb22b8e JH |
356 | static struct reflog_expire_cfg { |
357 | struct reflog_expire_cfg *next; | |
358 | unsigned long expire_total; | |
359 | unsigned long expire_unreachable; | |
360 | size_t len; | |
361 | char pattern[FLEX_ARRAY]; | |
362 | } *reflog_expire_cfg, **reflog_expire_cfg_tail; | |
363 | ||
364 | static struct reflog_expire_cfg *find_cfg_ent(const char *pattern, size_t len) | |
4aec56d1 | 365 | { |
3cb22b8e JH |
366 | struct reflog_expire_cfg *ent; |
367 | ||
368 | if (!reflog_expire_cfg_tail) | |
369 | reflog_expire_cfg_tail = &reflog_expire_cfg; | |
370 | ||
371 | for (ent = reflog_expire_cfg; ent; ent = ent->next) | |
372 | if (ent->len == len && | |
373 | !memcmp(ent->pattern, pattern, len)) | |
374 | return ent; | |
375 | ||
376 | ent = xcalloc(1, (sizeof(*ent) + len)); | |
377 | memcpy(ent->pattern, pattern, len); | |
378 | ent->len = len; | |
379 | *reflog_expire_cfg_tail = ent; | |
380 | reflog_expire_cfg_tail = &(ent->next); | |
381 | return ent; | |
382 | } | |
383 | ||
384 | static int parse_expire_cfg_value(const char *var, const char *value, unsigned long *expire) | |
385 | { | |
386 | if (!value) | |
387 | return config_error_nonbool(var); | |
388 | if (!strcmp(value, "never") || !strcmp(value, "false")) { | |
389 | *expire = 0; | |
4f342b96 JH |
390 | return 0; |
391 | } | |
3cb22b8e JH |
392 | *expire = approxidate(value); |
393 | return 0; | |
394 | } | |
395 | ||
396 | /* expiry timer slot */ | |
397 | #define EXPIRE_TOTAL 01 | |
398 | #define EXPIRE_UNREACH 02 | |
399 | ||
400 | static int reflog_expire_config(const char *var, const char *value, void *cb) | |
401 | { | |
402 | const char *lastdot = strrchr(var, '.'); | |
403 | unsigned long expire; | |
404 | int slot; | |
405 | struct reflog_expire_cfg *ent; | |
406 | ||
407 | if (!lastdot || prefixcmp(var, "gc.")) | |
408 | return git_default_config(var, value, cb); | |
409 | ||
410 | if (!strcmp(lastdot, ".reflogexpire")) { | |
411 | slot = EXPIRE_TOTAL; | |
412 | if (parse_expire_cfg_value(var, value, &expire)) | |
413 | return -1; | |
414 | } else if (!strcmp(lastdot, ".reflogexpireunreachable")) { | |
415 | slot = EXPIRE_UNREACH; | |
416 | if (parse_expire_cfg_value(var, value, &expire)) | |
417 | return -1; | |
418 | } else | |
419 | return git_default_config(var, value, cb); | |
420 | ||
421 | if (lastdot == var + 2) { | |
422 | switch (slot) { | |
423 | case EXPIRE_TOTAL: | |
424 | default_reflog_expire = expire; | |
425 | break; | |
426 | case EXPIRE_UNREACH: | |
427 | default_reflog_expire_unreachable = expire; | |
428 | break; | |
429 | } | |
4f342b96 JH |
430 | return 0; |
431 | } | |
3cb22b8e JH |
432 | |
433 | ent = find_cfg_ent(var + 3, lastdot - (var+3)); | |
434 | if (!ent) | |
435 | return -1; | |
436 | switch (slot) { | |
437 | case EXPIRE_TOTAL: | |
438 | ent->expire_total = expire; | |
439 | break; | |
440 | case EXPIRE_UNREACH: | |
441 | ent->expire_unreachable = expire; | |
442 | break; | |
443 | } | |
444 | return 0; | |
445 | } | |
446 | ||
447 | static void set_reflog_expiry_param(struct cmd_reflog_expire_cb *cb, int slot, const char *ref) | |
448 | { | |
449 | struct reflog_expire_cfg *ent; | |
450 | ||
451 | if (slot == (EXPIRE_TOTAL|EXPIRE_UNREACH)) | |
452 | return; /* both given explicitly -- nothing to tweak */ | |
453 | ||
454 | for (ent = reflog_expire_cfg; ent; ent = ent->next) { | |
455 | if (!fnmatch(ent->pattern, ref, 0)) { | |
456 | if (!(slot & EXPIRE_TOTAL)) | |
457 | cb->expire_total = ent->expire_total; | |
458 | if (!(slot & EXPIRE_UNREACH)) | |
459 | cb->expire_unreachable = ent->expire_unreachable; | |
460 | return; | |
461 | } | |
462 | } | |
463 | ||
60bce2bb JH |
464 | /* |
465 | * If unconfigured, make stash never expire | |
466 | */ | |
467 | if (!strcmp(ref, "refs/stash")) { | |
468 | if (!(slot & EXPIRE_TOTAL)) | |
469 | cb->expire_total = 0; | |
470 | if (!(slot & EXPIRE_UNREACH)) | |
471 | cb->expire_unreachable = 0; | |
472 | return; | |
473 | } | |
474 | ||
3cb22b8e JH |
475 | /* Nothing matched -- use the default value */ |
476 | if (!(slot & EXPIRE_TOTAL)) | |
477 | cb->expire_total = default_reflog_expire; | |
478 | if (!(slot & EXPIRE_UNREACH)) | |
479 | cb->expire_unreachable = default_reflog_expire_unreachable; | |
4aec56d1 JH |
480 | } |
481 | ||
4264dc15 JH |
482 | static int cmd_reflog_expire(int argc, const char **argv, const char *prefix) |
483 | { | |
484 | struct cmd_reflog_expire_cb cb; | |
485 | unsigned long now = time(NULL); | |
486 | int i, status, do_all; | |
3cb22b8e | 487 | int explicit_expiry = 0; |
4264dc15 | 488 | |
ef90d6d4 | 489 | git_config(reflog_expire_config, NULL); |
4aec56d1 | 490 | |
4264dc15 JH |
491 | save_commit_buffer = 0; |
492 | do_all = status = 0; | |
493 | memset(&cb, 0, sizeof(cb)); | |
4aec56d1 JH |
494 | |
495 | if (!default_reflog_expire_unreachable) | |
496 | default_reflog_expire_unreachable = now - 30 * 24 * 3600; | |
497 | if (!default_reflog_expire) | |
498 | default_reflog_expire = now - 90 * 24 * 3600; | |
499 | cb.expire_total = default_reflog_expire; | |
500 | cb.expire_unreachable = default_reflog_expire_unreachable; | |
4264dc15 JH |
501 | |
502 | for (i = 1; i < argc; i++) { | |
503 | const char *arg = argv[i]; | |
504 | if (!strcmp(arg, "--dry-run") || !strcmp(arg, "-n")) | |
505 | cb.dry_run = 1; | |
3cb22b8e | 506 | else if (!prefixcmp(arg, "--expire=")) { |
4264dc15 | 507 | cb.expire_total = approxidate(arg + 9); |
3cb22b8e JH |
508 | explicit_expiry |= EXPIRE_TOTAL; |
509 | } | |
510 | else if (!prefixcmp(arg, "--expire-unreachable=")) { | |
4264dc15 | 511 | cb.expire_unreachable = approxidate(arg + 21); |
3cb22b8e JH |
512 | explicit_expiry |= EXPIRE_UNREACH; |
513 | } | |
1389d9dd JH |
514 | else if (!strcmp(arg, "--stale-fix")) |
515 | cb.stalefix = 1; | |
2b81fab2 BC |
516 | else if (!strcmp(arg, "--rewrite")) |
517 | cb.rewrite = 1; | |
55f10565 BC |
518 | else if (!strcmp(arg, "--updateref")) |
519 | cb.updateref = 1; | |
4264dc15 JH |
520 | else if (!strcmp(arg, "--all")) |
521 | do_all = 1; | |
1389d9dd JH |
522 | else if (!strcmp(arg, "--verbose")) |
523 | cb.verbose = 1; | |
4264dc15 JH |
524 | else if (!strcmp(arg, "--")) { |
525 | i++; | |
526 | break; | |
527 | } | |
528 | else if (arg[0] == '-') | |
529 | usage(reflog_expire_usage); | |
530 | else | |
531 | break; | |
532 | } | |
3cb22b8e JH |
533 | |
534 | /* | |
535 | * We can trust the commits and objects reachable from refs | |
536 | * even in older repository. We cannot trust what's reachable | |
537 | * from reflog if the repository was pruned with older git. | |
538 | */ | |
1389d9dd JH |
539 | if (cb.stalefix) { |
540 | init_revisions(&cb.revs, prefix); | |
541 | if (cb.verbose) | |
542 | printf("Marking reachable objects..."); | |
543 | mark_reachable_objects(&cb.revs, 0); | |
544 | if (cb.verbose) | |
545 | putchar('\n'); | |
546 | } | |
547 | ||
bda3a31c JH |
548 | if (do_all) { |
549 | struct collect_reflog_cb collected; | |
550 | int i; | |
551 | ||
552 | memset(&collected, 0, sizeof(collected)); | |
553 | for_each_reflog(collect_reflog, &collected); | |
554 | for (i = 0; i < collected.nr; i++) { | |
555 | struct collected_reflog *e = collected.e[i]; | |
3cb22b8e | 556 | set_reflog_expiry_param(&cb, explicit_expiry, e->reflog); |
bda3a31c JH |
557 | status |= expire_reflog(e->reflog, e->sha1, 0, &cb); |
558 | free(e); | |
559 | } | |
560 | free(collected.e); | |
561 | } | |
562 | ||
90fb46ec PB |
563 | for (; i < argc; i++) { |
564 | char *ref; | |
4264dc15 | 565 | unsigned char sha1[20]; |
90fb46ec PB |
566 | if (!dwim_log(argv[i], strlen(argv[i]), sha1, &ref)) { |
567 | status |= error("%s points nowhere!", argv[i]); | |
4264dc15 JH |
568 | continue; |
569 | } | |
3cb22b8e | 570 | set_reflog_expiry_param(&cb, explicit_expiry, ref); |
4264dc15 JH |
571 | status |= expire_reflog(ref, sha1, 0, &cb); |
572 | } | |
573 | return status; | |
574 | } | |
575 | ||
552cecc2 JS |
576 | static int count_reflog_ent(unsigned char *osha1, unsigned char *nsha1, |
577 | const char *email, unsigned long timestamp, int tz, | |
578 | const char *message, void *cb_data) | |
579 | { | |
580 | struct cmd_reflog_expire_cb *cb = cb_data; | |
581 | if (!cb->expire_total || timestamp < cb->expire_total) | |
582 | cb->recno++; | |
583 | return 0; | |
584 | } | |
585 | ||
586 | static int cmd_reflog_delete(int argc, const char **argv, const char *prefix) | |
587 | { | |
588 | struct cmd_reflog_expire_cb cb; | |
589 | int i, status = 0; | |
590 | ||
552cecc2 JS |
591 | memset(&cb, 0, sizeof(cb)); |
592 | ||
593 | for (i = 1; i < argc; i++) { | |
3c386aa3 BC |
594 | const char *arg = argv[i]; |
595 | if (!strcmp(arg, "--dry-run") || !strcmp(arg, "-n")) | |
596 | cb.dry_run = 1; | |
2b81fab2 BC |
597 | else if (!strcmp(arg, "--rewrite")) |
598 | cb.rewrite = 1; | |
55f10565 BC |
599 | else if (!strcmp(arg, "--updateref")) |
600 | cb.updateref = 1; | |
3c386aa3 BC |
601 | else if (!strcmp(arg, "--verbose")) |
602 | cb.verbose = 1; | |
603 | else if (!strcmp(arg, "--")) { | |
604 | i++; | |
605 | break; | |
606 | } | |
607 | else if (arg[0] == '-') | |
608 | usage(reflog_delete_usage); | |
609 | else | |
610 | break; | |
611 | } | |
612 | ||
613 | if (argc - i < 1) | |
614 | return error("Nothing to delete?"); | |
615 | ||
616 | for ( ; i < argc; i++) { | |
552cecc2 JS |
617 | const char *spec = strstr(argv[i], "@{"); |
618 | unsigned char sha1[20]; | |
619 | char *ep, *ref; | |
620 | int recno; | |
621 | ||
622 | if (!spec) { | |
cb97cc9f | 623 | status |= error("Not a reflog: %s", argv[i]); |
552cecc2 JS |
624 | continue; |
625 | } | |
626 | ||
55beff4f JH |
627 | if (!dwim_log(argv[i], spec - argv[i], sha1, &ref)) { |
628 | status |= error("no reflog for '%s'", argv[i]); | |
552cecc2 JS |
629 | continue; |
630 | } | |
631 | ||
632 | recno = strtoul(spec + 2, &ep, 10); | |
633 | if (*ep == '}') { | |
634 | cb.recno = -recno; | |
635 | for_each_reflog_ent(ref, count_reflog_ent, &cb); | |
636 | } else { | |
637 | cb.expire_total = approxidate(spec + 2); | |
638 | for_each_reflog_ent(ref, count_reflog_ent, &cb); | |
639 | cb.expire_total = 0; | |
640 | } | |
641 | ||
642 | status |= expire_reflog(ref, sha1, 0, &cb); | |
643 | free(ref); | |
644 | } | |
645 | return status; | |
646 | } | |
647 | ||
1389d9dd JH |
648 | /* |
649 | * main "reflog" | |
650 | */ | |
651 | ||
4264dc15 | 652 | static const char reflog_usage[] = |
1b1dd23f | 653 | "git reflog (expire | ...)"; |
4264dc15 JH |
654 | |
655 | int cmd_reflog(int argc, const char **argv, const char *prefix) | |
656 | { | |
cf39f54e LT |
657 | /* With no command, we default to showing it. */ |
658 | if (argc < 2 || *argv[1] == '-') | |
659 | return cmd_log_reflog(argc, argv, prefix); | |
660 | ||
661 | if (!strcmp(argv[1], "show")) | |
662 | return cmd_log_reflog(argc - 1, argv + 1, prefix); | |
663 | ||
664 | if (!strcmp(argv[1], "expire")) | |
4264dc15 | 665 | return cmd_reflog_expire(argc - 1, argv + 1, prefix); |
cf39f54e | 666 | |
552cecc2 JS |
667 | if (!strcmp(argv[1], "delete")) |
668 | return cmd_reflog_delete(argc - 1, argv + 1, prefix); | |
669 | ||
cf39f54e LT |
670 | /* Not a recognized reflog command..*/ |
671 | usage(reflog_usage); | |
4264dc15 | 672 | } |