]>
Commit | Line | Data |
---|---|---|
4264dc15 | 1 | #include "builtin.h" |
697cc8ef | 2 | #include "lockfile.h" |
4264dc15 JH |
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 | ||
fe2a1816 | 11 | /* NEEDSWORK: switch to using parse_options */ |
1389d9dd | 12 | static const char reflog_expire_usage[] = |
fe2a1816 | 13 | "git reflog expire [--expire=<time>] [--expire-unreachable=<time>] [--rewrite] [--updateref] [--stale-fix] [--dry-run | -n] [--verbose] [--all] <refs>..."; |
3c386aa3 | 14 | static const char reflog_delete_usage[] = |
fe2a1816 | 15 | "git reflog delete [--rewrite] [--updateref] [--dry-run | -n] [--verbose] <refs>..."; |
afcb2e7a DT |
16 | static const char reflog_exists_usage[] = |
17 | "git reflog exists <ref>"; | |
4264dc15 | 18 | |
4aec56d1 JH |
19 | static unsigned long default_reflog_expire; |
20 | static unsigned long default_reflog_expire_unreachable; | |
21 | ||
1389d9dd JH |
22 | struct cmd_reflog_expire_cb { |
23 | struct rev_info revs; | |
1389d9dd | 24 | int stalefix; |
1389d9dd JH |
25 | unsigned long expire_total; |
26 | unsigned long expire_unreachable; | |
552cecc2 | 27 | int recno; |
1389d9dd JH |
28 | }; |
29 | ||
ea7b4f6d | 30 | struct expire_reflog_policy_cb { |
03cb91b1 JH |
31 | enum { |
32 | UE_NORMAL, | |
33 | UE_ALWAYS, | |
34 | UE_HEAD | |
35 | } unreachable_expire_kind; | |
b4ca1db9 JH |
36 | struct commit_list *mark_list; |
37 | unsigned long mark_limit; | |
b729effb | 38 | struct cmd_reflog_expire_cb cmd; |
c48a1635 MH |
39 | struct commit *tip_commit; |
40 | struct commit_list *tips; | |
4264dc15 JH |
41 | }; |
42 | ||
bda3a31c JH |
43 | struct collected_reflog { |
44 | unsigned char sha1[20]; | |
45 | char reflog[FLEX_ARRAY]; | |
46 | }; | |
ddd64c56 | 47 | |
bda3a31c JH |
48 | struct collect_reflog_cb { |
49 | struct collected_reflog **e; | |
50 | int alloc; | |
51 | int nr; | |
52 | }; | |
53 | ||
cd1f9c36 JH |
54 | #define INCOMPLETE (1u<<10) |
55 | #define STUDYING (1u<<11) | |
494fbfe8 | 56 | #define REACHABLE (1u<<12) |
cd1f9c36 | 57 | |
8d8b9f62 JH |
58 | static int tree_is_complete(const unsigned char *sha1) |
59 | { | |
60 | struct tree_desc desc; | |
cd1f9c36 JH |
61 | struct name_entry entry; |
62 | int complete; | |
63 | struct tree *tree; | |
8d8b9f62 | 64 | |
cd1f9c36 JH |
65 | tree = lookup_tree(sha1); |
66 | if (!tree) | |
8d8b9f62 | 67 | return 0; |
cd1f9c36 JH |
68 | if (tree->object.flags & SEEN) |
69 | return 1; | |
70 | if (tree->object.flags & INCOMPLETE) | |
71 | return 0; | |
72 | ||
6fda5e51 | 73 | if (!tree->buffer) { |
21666f1a | 74 | enum object_type type; |
6fda5e51 LT |
75 | unsigned long size; |
76 | void *data = read_sha1_file(sha1, &type, &size); | |
cd1f9c36 JH |
77 | if (!data) { |
78 | tree->object.flags |= INCOMPLETE; | |
8d8b9f62 JH |
79 | return 0; |
80 | } | |
cd1f9c36 | 81 | tree->buffer = data; |
6fda5e51 | 82 | tree->size = size; |
8d8b9f62 | 83 | } |
6fda5e51 | 84 | init_tree_desc(&desc, tree->buffer, tree->size); |
cd1f9c36 JH |
85 | complete = 1; |
86 | while (tree_entry(&desc, &entry)) { | |
87 | if (!has_sha1_file(entry.sha1) || | |
88 | (S_ISDIR(entry.mode) && !tree_is_complete(entry.sha1))) { | |
89 | tree->object.flags |= INCOMPLETE; | |
90 | complete = 0; | |
91 | } | |
92 | } | |
6e454b9a | 93 | free_tree_buffer(tree); |
8d8b9f62 | 94 | |
cd1f9c36 JH |
95 | if (complete) |
96 | tree->object.flags |= SEEN; | |
97 | return complete; | |
98 | } | |
1389d9dd JH |
99 | |
100 | static int commit_is_complete(struct commit *commit) | |
101 | { | |
102 | struct object_array study; | |
103 | struct object_array found; | |
104 | int is_incomplete = 0; | |
105 | int i; | |
106 | ||
107 | /* early return */ | |
108 | if (commit->object.flags & SEEN) | |
109 | return 1; | |
110 | if (commit->object.flags & INCOMPLETE) | |
111 | return 0; | |
112 | /* | |
113 | * Find all commits that are reachable and are not marked as | |
114 | * SEEN. Then make sure the trees and blobs contained are | |
115 | * complete. After that, mark these commits also as SEEN. | |
116 | * If some of the objects that are needed to complete this | |
117 | * commit are missing, mark this commit as INCOMPLETE. | |
118 | */ | |
119 | memset(&study, 0, sizeof(study)); | |
120 | memset(&found, 0, sizeof(found)); | |
121 | add_object_array(&commit->object, NULL, &study); | |
122 | add_object_array(&commit->object, NULL, &found); | |
123 | commit->object.flags |= STUDYING; | |
124 | while (study.nr) { | |
125 | struct commit *c; | |
126 | struct commit_list *parent; | |
127 | ||
128 | c = (struct commit *)study.objects[--study.nr].item; | |
ed1c9977 | 129 | if (!c->object.parsed && !parse_object(c->object.oid.hash)) |
1389d9dd JH |
130 | c->object.flags |= INCOMPLETE; |
131 | ||
132 | if (c->object.flags & INCOMPLETE) { | |
133 | is_incomplete = 1; | |
134 | break; | |
135 | } | |
136 | else if (c->object.flags & SEEN) | |
137 | continue; | |
138 | for (parent = c->parents; parent; parent = parent->next) { | |
139 | struct commit *p = parent->item; | |
140 | if (p->object.flags & STUDYING) | |
141 | continue; | |
142 | p->object.flags |= STUDYING; | |
143 | add_object_array(&p->object, NULL, &study); | |
144 | add_object_array(&p->object, NULL, &found); | |
145 | } | |
146 | } | |
147 | if (!is_incomplete) { | |
cd1f9c36 JH |
148 | /* |
149 | * make sure all commits in "found" array have all the | |
1389d9dd JH |
150 | * necessary objects. |
151 | */ | |
cd1f9c36 | 152 | for (i = 0; i < found.nr; i++) { |
1389d9dd JH |
153 | struct commit *c = |
154 | (struct commit *)found.objects[i].item; | |
ed1c9977 | 155 | if (!tree_is_complete(c->tree->object.oid.hash)) { |
1389d9dd | 156 | is_incomplete = 1; |
cd1f9c36 JH |
157 | c->object.flags |= INCOMPLETE; |
158 | } | |
1389d9dd JH |
159 | } |
160 | if (!is_incomplete) { | |
161 | /* mark all found commits as complete, iow SEEN */ | |
162 | for (i = 0; i < found.nr; i++) | |
163 | found.objects[i].item->flags |= SEEN; | |
164 | } | |
165 | } | |
166 | /* clear flags from the objects we traversed */ | |
167 | for (i = 0; i < found.nr; i++) | |
168 | found.objects[i].item->flags &= ~STUDYING; | |
169 | if (is_incomplete) | |
170 | commit->object.flags |= INCOMPLETE; | |
cd1f9c36 JH |
171 | else { |
172 | /* | |
173 | * If we come here, we have (1) traversed the ancestry chain | |
174 | * from the "commit" until we reach SEEN commits (which are | |
175 | * known to be complete), and (2) made sure that the commits | |
176 | * encountered during the above traversal refer to trees that | |
177 | * are complete. Which means that we know *all* the commits | |
178 | * we have seen during this process are complete. | |
179 | */ | |
180 | for (i = 0; i < found.nr; i++) | |
181 | found.objects[i].item->flags |= SEEN; | |
182 | } | |
1389d9dd JH |
183 | /* free object arrays */ |
184 | free(study.objects); | |
185 | free(found.objects); | |
186 | return !is_incomplete; | |
187 | } | |
188 | ||
4264dc15 JH |
189 | static int keep_entry(struct commit **it, unsigned char *sha1) |
190 | { | |
8d8b9f62 JH |
191 | struct commit *commit; |
192 | ||
4264dc15 JH |
193 | if (is_null_sha1(sha1)) |
194 | return 1; | |
8d8b9f62 JH |
195 | commit = lookup_commit_reference_gently(sha1, 1); |
196 | if (!commit) | |
197 | return 0; | |
198 | ||
1389d9dd JH |
199 | /* |
200 | * Make sure everything in this commit exists. | |
201 | * | |
202 | * We have walked all the objects reachable from the refs | |
203 | * and cache earlier. The commits reachable by this commit | |
204 | * must meet SEEN commits -- and then we should mark them as | |
205 | * SEEN as well. | |
206 | */ | |
207 | if (!commit_is_complete(commit)) | |
8d8b9f62 JH |
208 | return 0; |
209 | *it = commit; | |
210 | return 1; | |
4264dc15 JH |
211 | } |
212 | ||
b4ca1db9 JH |
213 | /* |
214 | * Starting from commits in the cb->mark_list, mark commits that are | |
215 | * reachable from them. Stop the traversal at commits older than | |
216 | * the expire_limit and queue them back, so that the caller can call | |
217 | * us again to restart the traversal with longer expire_limit. | |
218 | */ | |
ea7b4f6d | 219 | static void mark_reachable(struct expire_reflog_policy_cb *cb) |
666e07e6 | 220 | { |
b4ca1db9 JH |
221 | struct commit_list *pending; |
222 | unsigned long expire_limit = cb->mark_limit; | |
223 | struct commit_list *leftover = NULL; | |
666e07e6 | 224 | |
b4ca1db9 JH |
225 | for (pending = cb->mark_list; pending; pending = pending->next) |
226 | pending->item->object.flags &= ~REACHABLE; | |
494fbfe8 | 227 | |
b4ca1db9 | 228 | pending = cb->mark_list; |
494fbfe8 | 229 | while (pending) { |
494fbfe8 | 230 | struct commit_list *parent; |
e510ab89 | 231 | struct commit *commit = pop_commit(&pending); |
494fbfe8 JH |
232 | if (commit->object.flags & REACHABLE) |
233 | continue; | |
234 | if (parse_commit(commit)) | |
235 | continue; | |
236 | commit->object.flags |= REACHABLE; | |
b4ca1db9 JH |
237 | if (commit->date < expire_limit) { |
238 | commit_list_insert(commit, &leftover); | |
494fbfe8 | 239 | continue; |
b4ca1db9 JH |
240 | } |
241 | commit->object.flags |= REACHABLE; | |
494fbfe8 JH |
242 | parent = commit->parents; |
243 | while (parent) { | |
244 | commit = parent->item; | |
245 | parent = parent->next; | |
246 | if (commit->object.flags & REACHABLE) | |
247 | continue; | |
248 | commit_list_insert(commit, &pending); | |
249 | } | |
250 | } | |
b4ca1db9 JH |
251 | cb->mark_list = leftover; |
252 | } | |
253 | ||
ea7b4f6d | 254 | static int unreachable(struct expire_reflog_policy_cb *cb, struct commit *commit, unsigned char *sha1) |
b4ca1db9 JH |
255 | { |
256 | /* | |
257 | * We may or may not have the commit yet - if not, look it | |
258 | * up using the supplied sha1. | |
259 | */ | |
260 | if (!commit) { | |
261 | if (is_null_sha1(sha1)) | |
262 | return 0; | |
263 | ||
264 | commit = lookup_commit_reference_gently(sha1, 1); | |
265 | ||
266 | /* Not a commit -- keep it */ | |
267 | if (!commit) | |
268 | return 0; | |
269 | } | |
270 | ||
271 | /* Reachable from the current ref? Don't prune. */ | |
272 | if (commit->object.flags & REACHABLE) | |
273 | return 0; | |
274 | ||
275 | if (cb->mark_list && cb->mark_limit) { | |
276 | cb->mark_limit = 0; /* dig down to the root */ | |
277 | mark_reachable(cb); | |
278 | } | |
279 | ||
280 | return !(commit->object.flags & REACHABLE); | |
494fbfe8 JH |
281 | } |
282 | ||
60cc3c40 MH |
283 | /* |
284 | * Return true iff the specified reflog entry should be expired. | |
285 | */ | |
286 | static int should_expire_reflog_ent(unsigned char *osha1, unsigned char *nsha1, | |
287 | const char *email, unsigned long timestamp, int tz, | |
288 | const char *message, void *cb_data) | |
4264dc15 | 289 | { |
ea7b4f6d | 290 | struct expire_reflog_policy_cb *cb = cb_data; |
4264dc15 JH |
291 | struct commit *old, *new; |
292 | ||
b729effb | 293 | if (timestamp < cb->cmd.expire_total) |
60cc3c40 | 294 | return 1; |
2b81fab2 | 295 | |
9bbaa6cc | 296 | old = new = NULL; |
b729effb | 297 | if (cb->cmd.stalefix && |
1389d9dd | 298 | (!keep_entry(&old, osha1) || !keep_entry(&new, nsha1))) |
60cc3c40 | 299 | return 1; |
4264dc15 | 300 | |
b729effb | 301 | if (timestamp < cb->cmd.expire_unreachable) { |
03cb91b1 | 302 | if (cb->unreachable_expire_kind == UE_ALWAYS) |
60cc3c40 | 303 | return 1; |
666e07e6 | 304 | if (unreachable(cb, old, osha1) || unreachable(cb, new, nsha1)) |
60cc3c40 | 305 | return 1; |
9bbaa6cc | 306 | } |
4264dc15 | 307 | |
b729effb | 308 | if (cb->cmd.recno && --(cb->cmd.recno) == 0) |
60cc3c40 MH |
309 | return 1; |
310 | ||
4264dc15 | 311 | return 0; |
60cc3c40 MH |
312 | } |
313 | ||
5bcad1bc | 314 | static int push_tip_to_list(const char *refname, const struct object_id *oid, |
ea7b4f6d | 315 | int flags, void *cb_data) |
03cb91b1 JH |
316 | { |
317 | struct commit_list **list = cb_data; | |
318 | struct commit *tip_commit; | |
319 | if (flags & REF_ISSYMREF) | |
320 | return 0; | |
5bcad1bc | 321 | tip_commit = lookup_commit_reference_gently(oid->hash, 1); |
03cb91b1 JH |
322 | if (!tip_commit) |
323 | return 0; | |
324 | commit_list_insert(tip_commit, list); | |
325 | return 0; | |
326 | } | |
327 | ||
c48a1635 MH |
328 | static void reflog_expiry_prepare(const char *refname, |
329 | const unsigned char *sha1, | |
b729effb | 330 | void *cb_data) |
c48a1635 | 331 | { |
b729effb MH |
332 | struct expire_reflog_policy_cb *cb = cb_data; |
333 | ||
334 | if (!cb->cmd.expire_unreachable || !strcmp(refname, "HEAD")) { | |
c48a1635 MH |
335 | cb->tip_commit = NULL; |
336 | cb->unreachable_expire_kind = UE_HEAD; | |
337 | } else { | |
338 | cb->tip_commit = lookup_commit_reference_gently(sha1, 1); | |
339 | if (!cb->tip_commit) | |
340 | cb->unreachable_expire_kind = UE_ALWAYS; | |
341 | else | |
342 | cb->unreachable_expire_kind = UE_NORMAL; | |
343 | } | |
344 | ||
b729effb | 345 | if (cb->cmd.expire_unreachable <= cb->cmd.expire_total) |
c48a1635 MH |
346 | cb->unreachable_expire_kind = UE_ALWAYS; |
347 | ||
348 | cb->mark_list = NULL; | |
349 | cb->tips = NULL; | |
350 | if (cb->unreachable_expire_kind != UE_ALWAYS) { | |
351 | if (cb->unreachable_expire_kind == UE_HEAD) { | |
352 | struct commit_list *elem; | |
2b2a5be3 | 353 | |
5bcad1bc | 354 | for_each_ref(push_tip_to_list, &cb->tips); |
c48a1635 MH |
355 | for (elem = cb->tips; elem; elem = elem->next) |
356 | commit_list_insert(elem->item, &cb->mark_list); | |
357 | } else { | |
358 | commit_list_insert(cb->tip_commit, &cb->mark_list); | |
359 | } | |
b729effb | 360 | cb->mark_limit = cb->cmd.expire_total; |
c48a1635 MH |
361 | mark_reachable(cb); |
362 | } | |
363 | } | |
364 | ||
b729effb | 365 | static void reflog_expiry_cleanup(void *cb_data) |
c48a1635 | 366 | { |
b729effb MH |
367 | struct expire_reflog_policy_cb *cb = cb_data; |
368 | ||
c48a1635 MH |
369 | if (cb->unreachable_expire_kind != UE_ALWAYS) { |
370 | if (cb->unreachable_expire_kind == UE_HEAD) { | |
371 | struct commit_list *elem; | |
372 | for (elem = cb->tips; elem; elem = elem->next) | |
373 | clear_commit_marks(elem->item, REACHABLE); | |
374 | free_commit_list(cb->tips); | |
375 | } else { | |
376 | clear_commit_marks(cb->tip_commit, REACHABLE); | |
377 | } | |
378 | } | |
379 | } | |
380 | ||
5bcad1bc | 381 | static int collect_reflog(const char *ref, const struct object_id *oid, int unused, void *cb_data) |
bda3a31c JH |
382 | { |
383 | struct collected_reflog *e; | |
384 | struct collect_reflog_cb *cb = cb_data; | |
bda3a31c | 385 | |
96ffc06f | 386 | FLEX_ALLOC_STR(e, reflog, ref); |
5bcad1bc | 387 | hashcpy(e->sha1, oid->hash); |
bda3a31c JH |
388 | ALLOC_GROW(cb->e, cb->nr + 1, cb->alloc); |
389 | cb->e[cb->nr++] = e; | |
390 | return 0; | |
391 | } | |
392 | ||
3cb22b8e JH |
393 | static struct reflog_expire_cfg { |
394 | struct reflog_expire_cfg *next; | |
395 | unsigned long expire_total; | |
396 | unsigned long expire_unreachable; | |
3cb22b8e JH |
397 | char pattern[FLEX_ARRAY]; |
398 | } *reflog_expire_cfg, **reflog_expire_cfg_tail; | |
399 | ||
400 | static struct reflog_expire_cfg *find_cfg_ent(const char *pattern, size_t len) | |
4aec56d1 | 401 | { |
3cb22b8e JH |
402 | struct reflog_expire_cfg *ent; |
403 | ||
404 | if (!reflog_expire_cfg_tail) | |
405 | reflog_expire_cfg_tail = &reflog_expire_cfg; | |
406 | ||
407 | for (ent = reflog_expire_cfg; ent; ent = ent->next) | |
c3a700fb JK |
408 | if (!strncmp(ent->pattern, pattern, len) && |
409 | ent->pattern[len] == '\0') | |
3cb22b8e JH |
410 | return ent; |
411 | ||
96ffc06f | 412 | FLEX_ALLOC_MEM(ent, pattern, pattern, len); |
3cb22b8e JH |
413 | *reflog_expire_cfg_tail = ent; |
414 | reflog_expire_cfg_tail = &(ent->next); | |
415 | return ent; | |
416 | } | |
417 | ||
418 | static int parse_expire_cfg_value(const char *var, const char *value, unsigned long *expire) | |
419 | { | |
420 | if (!value) | |
421 | return config_error_nonbool(var); | |
3d27b9b0 | 422 | if (parse_expiry_date(value, expire)) |
99885bc0 | 423 | return error(_("'%s' for '%s' is not a valid timestamp"), |
3d27b9b0 | 424 | value, var); |
3cb22b8e JH |
425 | return 0; |
426 | } | |
427 | ||
428 | /* expiry timer slot */ | |
429 | #define EXPIRE_TOTAL 01 | |
430 | #define EXPIRE_UNREACH 02 | |
431 | ||
432 | static int reflog_expire_config(const char *var, const char *value, void *cb) | |
433 | { | |
b3873c33 JK |
434 | const char *pattern, *key; |
435 | int pattern_len; | |
3cb22b8e JH |
436 | unsigned long expire; |
437 | int slot; | |
438 | struct reflog_expire_cfg *ent; | |
439 | ||
b3873c33 | 440 | if (parse_config_key(var, "gc", &pattern, &pattern_len, &key) < 0) |
3cb22b8e JH |
441 | return git_default_config(var, value, cb); |
442 | ||
b3873c33 | 443 | if (!strcmp(key, "reflogexpire")) { |
3cb22b8e JH |
444 | slot = EXPIRE_TOTAL; |
445 | if (parse_expire_cfg_value(var, value, &expire)) | |
446 | return -1; | |
b3873c33 | 447 | } else if (!strcmp(key, "reflogexpireunreachable")) { |
3cb22b8e JH |
448 | slot = EXPIRE_UNREACH; |
449 | if (parse_expire_cfg_value(var, value, &expire)) | |
450 | return -1; | |
451 | } else | |
452 | return git_default_config(var, value, cb); | |
453 | ||
b3873c33 | 454 | if (!pattern) { |
3cb22b8e JH |
455 | switch (slot) { |
456 | case EXPIRE_TOTAL: | |
457 | default_reflog_expire = expire; | |
458 | break; | |
459 | case EXPIRE_UNREACH: | |
460 | default_reflog_expire_unreachable = expire; | |
461 | break; | |
462 | } | |
4f342b96 JH |
463 | return 0; |
464 | } | |
3cb22b8e | 465 | |
b3873c33 | 466 | ent = find_cfg_ent(pattern, pattern_len); |
3cb22b8e JH |
467 | if (!ent) |
468 | return -1; | |
469 | switch (slot) { | |
470 | case EXPIRE_TOTAL: | |
471 | ent->expire_total = expire; | |
472 | break; | |
473 | case EXPIRE_UNREACH: | |
474 | ent->expire_unreachable = expire; | |
475 | break; | |
476 | } | |
477 | return 0; | |
478 | } | |
479 | ||
480 | static void set_reflog_expiry_param(struct cmd_reflog_expire_cb *cb, int slot, const char *ref) | |
481 | { | |
482 | struct reflog_expire_cfg *ent; | |
483 | ||
484 | if (slot == (EXPIRE_TOTAL|EXPIRE_UNREACH)) | |
485 | return; /* both given explicitly -- nothing to tweak */ | |
486 | ||
487 | for (ent = reflog_expire_cfg; ent; ent = ent->next) { | |
eb07894f | 488 | if (!wildmatch(ent->pattern, ref, 0, NULL)) { |
3cb22b8e JH |
489 | if (!(slot & EXPIRE_TOTAL)) |
490 | cb->expire_total = ent->expire_total; | |
491 | if (!(slot & EXPIRE_UNREACH)) | |
492 | cb->expire_unreachable = ent->expire_unreachable; | |
493 | return; | |
494 | } | |
495 | } | |
496 | ||
60bce2bb JH |
497 | /* |
498 | * If unconfigured, make stash never expire | |
499 | */ | |
500 | if (!strcmp(ref, "refs/stash")) { | |
501 | if (!(slot & EXPIRE_TOTAL)) | |
502 | cb->expire_total = 0; | |
503 | if (!(slot & EXPIRE_UNREACH)) | |
504 | cb->expire_unreachable = 0; | |
505 | return; | |
506 | } | |
507 | ||
3cb22b8e JH |
508 | /* Nothing matched -- use the default value */ |
509 | if (!(slot & EXPIRE_TOTAL)) | |
510 | cb->expire_total = default_reflog_expire; | |
511 | if (!(slot & EXPIRE_UNREACH)) | |
512 | cb->expire_unreachable = default_reflog_expire_unreachable; | |
4aec56d1 JH |
513 | } |
514 | ||
4264dc15 JH |
515 | static int cmd_reflog_expire(int argc, const char **argv, const char *prefix) |
516 | { | |
b729effb | 517 | struct expire_reflog_policy_cb cb; |
4264dc15 JH |
518 | unsigned long now = time(NULL); |
519 | int i, status, do_all; | |
3cb22b8e | 520 | int explicit_expiry = 0; |
aba56c89 | 521 | unsigned int flags = 0; |
4264dc15 | 522 | |
4a9f4394 AS |
523 | default_reflog_expire_unreachable = now - 30 * 24 * 3600; |
524 | default_reflog_expire = now - 90 * 24 * 3600; | |
ef90d6d4 | 525 | git_config(reflog_expire_config, NULL); |
4aec56d1 | 526 | |
4264dc15 JH |
527 | save_commit_buffer = 0; |
528 | do_all = status = 0; | |
529 | memset(&cb, 0, sizeof(cb)); | |
4aec56d1 | 530 | |
b729effb MH |
531 | cb.cmd.expire_total = default_reflog_expire; |
532 | cb.cmd.expire_unreachable = default_reflog_expire_unreachable; | |
4264dc15 JH |
533 | |
534 | for (i = 1; i < argc; i++) { | |
535 | const char *arg = argv[i]; | |
536 | if (!strcmp(arg, "--dry-run") || !strcmp(arg, "-n")) | |
98f31d85 | 537 | flags |= EXPIRE_REFLOGS_DRY_RUN; |
59556548 | 538 | else if (starts_with(arg, "--expire=")) { |
b729effb | 539 | if (parse_expiry_date(arg + 9, &cb.cmd.expire_total)) |
3d27b9b0 | 540 | die(_("'%s' is not a valid timestamp"), arg); |
3cb22b8e JH |
541 | explicit_expiry |= EXPIRE_TOTAL; |
542 | } | |
59556548 | 543 | else if (starts_with(arg, "--expire-unreachable=")) { |
b729effb | 544 | if (parse_expiry_date(arg + 21, &cb.cmd.expire_unreachable)) |
3d27b9b0 | 545 | die(_("'%s' is not a valid timestamp"), arg); |
3cb22b8e JH |
546 | explicit_expiry |= EXPIRE_UNREACH; |
547 | } | |
1389d9dd | 548 | else if (!strcmp(arg, "--stale-fix")) |
b729effb | 549 | cb.cmd.stalefix = 1; |
2b81fab2 | 550 | else if (!strcmp(arg, "--rewrite")) |
553daf13 | 551 | flags |= EXPIRE_REFLOGS_REWRITE; |
55f10565 | 552 | else if (!strcmp(arg, "--updateref")) |
c4c4fbf8 | 553 | flags |= EXPIRE_REFLOGS_UPDATE_REF; |
4264dc15 JH |
554 | else if (!strcmp(arg, "--all")) |
555 | do_all = 1; | |
1389d9dd | 556 | else if (!strcmp(arg, "--verbose")) |
bc11155c | 557 | flags |= EXPIRE_REFLOGS_VERBOSE; |
4264dc15 JH |
558 | else if (!strcmp(arg, "--")) { |
559 | i++; | |
560 | break; | |
561 | } | |
562 | else if (arg[0] == '-') | |
563 | usage(reflog_expire_usage); | |
564 | else | |
565 | break; | |
566 | } | |
3cb22b8e JH |
567 | |
568 | /* | |
569 | * We can trust the commits and objects reachable from refs | |
570 | * even in older repository. We cannot trust what's reachable | |
571 | * from reflog if the repository was pruned with older git. | |
572 | */ | |
b729effb MH |
573 | if (cb.cmd.stalefix) { |
574 | init_revisions(&cb.cmd.revs, prefix); | |
bc11155c | 575 | if (flags & EXPIRE_REFLOGS_VERBOSE) |
1389d9dd | 576 | printf("Marking reachable objects..."); |
b729effb | 577 | mark_reachable_objects(&cb.cmd.revs, 0, 0, NULL); |
bc11155c | 578 | if (flags & EXPIRE_REFLOGS_VERBOSE) |
1389d9dd JH |
579 | putchar('\n'); |
580 | } | |
581 | ||
bda3a31c JH |
582 | if (do_all) { |
583 | struct collect_reflog_cb collected; | |
584 | int i; | |
585 | ||
586 | memset(&collected, 0, sizeof(collected)); | |
5bcad1bc | 587 | for_each_reflog(collect_reflog, &collected); |
bda3a31c JH |
588 | for (i = 0; i < collected.nr; i++) { |
589 | struct collected_reflog *e = collected.e[i]; | |
b729effb | 590 | set_reflog_expiry_param(&cb.cmd, explicit_expiry, e->reflog); |
fa5b1830 MH |
591 | status |= reflog_expire(e->reflog, e->sha1, flags, |
592 | reflog_expiry_prepare, | |
593 | should_expire_reflog_ent, | |
594 | reflog_expiry_cleanup, | |
595 | &cb); | |
bda3a31c JH |
596 | free(e); |
597 | } | |
598 | free(collected.e); | |
599 | } | |
600 | ||
90fb46ec PB |
601 | for (; i < argc; i++) { |
602 | char *ref; | |
4264dc15 | 603 | unsigned char sha1[20]; |
90fb46ec PB |
604 | if (!dwim_log(argv[i], strlen(argv[i]), sha1, &ref)) { |
605 | status |= error("%s points nowhere!", argv[i]); | |
4264dc15 JH |
606 | continue; |
607 | } | |
b729effb | 608 | set_reflog_expiry_param(&cb.cmd, explicit_expiry, ref); |
fa5b1830 MH |
609 | status |= reflog_expire(ref, sha1, flags, |
610 | reflog_expiry_prepare, | |
611 | should_expire_reflog_ent, | |
612 | reflog_expiry_cleanup, | |
613 | &cb); | |
4264dc15 JH |
614 | } |
615 | return status; | |
616 | } | |
617 | ||
552cecc2 JS |
618 | static int count_reflog_ent(unsigned char *osha1, unsigned char *nsha1, |
619 | const char *email, unsigned long timestamp, int tz, | |
620 | const char *message, void *cb_data) | |
621 | { | |
b729effb MH |
622 | struct expire_reflog_policy_cb *cb = cb_data; |
623 | if (!cb->cmd.expire_total || timestamp < cb->cmd.expire_total) | |
624 | cb->cmd.recno++; | |
552cecc2 JS |
625 | return 0; |
626 | } | |
627 | ||
628 | static int cmd_reflog_delete(int argc, const char **argv, const char *prefix) | |
629 | { | |
b729effb | 630 | struct expire_reflog_policy_cb cb; |
552cecc2 | 631 | int i, status = 0; |
aba56c89 | 632 | unsigned int flags = 0; |
552cecc2 | 633 | |
552cecc2 JS |
634 | memset(&cb, 0, sizeof(cb)); |
635 | ||
636 | for (i = 1; i < argc; i++) { | |
3c386aa3 BC |
637 | const char *arg = argv[i]; |
638 | if (!strcmp(arg, "--dry-run") || !strcmp(arg, "-n")) | |
98f31d85 | 639 | flags |= EXPIRE_REFLOGS_DRY_RUN; |
2b81fab2 | 640 | else if (!strcmp(arg, "--rewrite")) |
553daf13 | 641 | flags |= EXPIRE_REFLOGS_REWRITE; |
55f10565 | 642 | else if (!strcmp(arg, "--updateref")) |
c4c4fbf8 | 643 | flags |= EXPIRE_REFLOGS_UPDATE_REF; |
3c386aa3 | 644 | else if (!strcmp(arg, "--verbose")) |
bc11155c | 645 | flags |= EXPIRE_REFLOGS_VERBOSE; |
3c386aa3 BC |
646 | else if (!strcmp(arg, "--")) { |
647 | i++; | |
648 | break; | |
649 | } | |
650 | else if (arg[0] == '-') | |
651 | usage(reflog_delete_usage); | |
652 | else | |
653 | break; | |
654 | } | |
655 | ||
656 | if (argc - i < 1) | |
657 | return error("Nothing to delete?"); | |
658 | ||
659 | for ( ; i < argc; i++) { | |
552cecc2 JS |
660 | const char *spec = strstr(argv[i], "@{"); |
661 | unsigned char sha1[20]; | |
662 | char *ep, *ref; | |
663 | int recno; | |
664 | ||
665 | if (!spec) { | |
cb97cc9f | 666 | status |= error("Not a reflog: %s", argv[i]); |
552cecc2 JS |
667 | continue; |
668 | } | |
669 | ||
55beff4f JH |
670 | if (!dwim_log(argv[i], spec - argv[i], sha1, &ref)) { |
671 | status |= error("no reflog for '%s'", argv[i]); | |
552cecc2 JS |
672 | continue; |
673 | } | |
674 | ||
675 | recno = strtoul(spec + 2, &ep, 10); | |
676 | if (*ep == '}') { | |
b729effb | 677 | cb.cmd.recno = -recno; |
552cecc2 JS |
678 | for_each_reflog_ent(ref, count_reflog_ent, &cb); |
679 | } else { | |
b729effb | 680 | cb.cmd.expire_total = approxidate(spec + 2); |
552cecc2 | 681 | for_each_reflog_ent(ref, count_reflog_ent, &cb); |
b729effb | 682 | cb.cmd.expire_total = 0; |
552cecc2 JS |
683 | } |
684 | ||
fa5b1830 MH |
685 | status |= reflog_expire(ref, sha1, flags, |
686 | reflog_expiry_prepare, | |
687 | should_expire_reflog_ent, | |
688 | reflog_expiry_cleanup, | |
689 | &cb); | |
552cecc2 JS |
690 | free(ref); |
691 | } | |
692 | return status; | |
693 | } | |
694 | ||
afcb2e7a DT |
695 | static int cmd_reflog_exists(int argc, const char **argv, const char *prefix) |
696 | { | |
697 | int i, start = 0; | |
698 | ||
699 | for (i = 1; i < argc; i++) { | |
700 | const char *arg = argv[i]; | |
701 | if (!strcmp(arg, "--")) { | |
702 | i++; | |
703 | break; | |
704 | } | |
705 | else if (arg[0] == '-') | |
706 | usage(reflog_exists_usage); | |
707 | else | |
708 | break; | |
709 | } | |
710 | ||
711 | start = i; | |
712 | ||
713 | if (argc - start != 1) | |
714 | usage(reflog_exists_usage); | |
715 | ||
716 | if (check_refname_format(argv[start], REFNAME_ALLOW_ONELEVEL)) | |
717 | die("invalid ref format: %s", argv[start]); | |
718 | return !reflog_exists(argv[start]); | |
719 | } | |
720 | ||
1389d9dd JH |
721 | /* |
722 | * main "reflog" | |
723 | */ | |
724 | ||
4264dc15 | 725 | static const char reflog_usage[] = |
afcb2e7a | 726 | "git reflog [ show | expire | delete | exists ]"; |
4264dc15 JH |
727 | |
728 | int cmd_reflog(int argc, const char **argv, const char *prefix) | |
729 | { | |
99caeed0 JN |
730 | if (argc > 1 && !strcmp(argv[1], "-h")) |
731 | usage(reflog_usage); | |
732 | ||
cf39f54e LT |
733 | /* With no command, we default to showing it. */ |
734 | if (argc < 2 || *argv[1] == '-') | |
735 | return cmd_log_reflog(argc, argv, prefix); | |
736 | ||
737 | if (!strcmp(argv[1], "show")) | |
738 | return cmd_log_reflog(argc - 1, argv + 1, prefix); | |
739 | ||
740 | if (!strcmp(argv[1], "expire")) | |
4264dc15 | 741 | return cmd_reflog_expire(argc - 1, argv + 1, prefix); |
cf39f54e | 742 | |
552cecc2 JS |
743 | if (!strcmp(argv[1], "delete")) |
744 | return cmd_reflog_delete(argc - 1, argv + 1, prefix); | |
745 | ||
afcb2e7a DT |
746 | if (!strcmp(argv[1], "exists")) |
747 | return cmd_reflog_exists(argc - 1, argv + 1, prefix); | |
748 | ||
bf01d4a3 | 749 | return cmd_log_reflog(argc, argv, prefix); |
4264dc15 | 750 | } |