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