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