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