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