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