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