expire_reflog(): extract two policy-related functions
[git/mingw/j6t.git] / builtin / reflog.c
blob8db52d7e50b4fa03ccbbfd77e107eb0e7185f8c2
1 #include "builtin.h"
2 #include "lockfile.h"
3 #include "commit.h"
4 #include "refs.h"
5 #include "dir.h"
6 #include "tree-walk.h"
7 #include "diff.h"
8 #include "revision.h"
9 #include "reachable.h"
12 * reflog expire
15 static const char reflog_expire_usage[] =
16 "git reflog expire [--verbose] [--dry-run] [--stale-fix] [--expire=<time>] [--expire-unreachable=<time>] [--all] <refs>...";
17 static const char reflog_delete_usage[] =
18 "git reflog delete [--verbose] [--dry-run] [--rewrite] [--updateref] <refs>...";
20 static unsigned long default_reflog_expire;
21 static unsigned long default_reflog_expire_unreachable;
23 struct cmd_reflog_expire_cb {
24 struct rev_info revs;
25 int dry_run;
26 int stalefix;
27 int rewrite;
28 int updateref;
29 int verbose;
30 unsigned long expire_total;
31 unsigned long expire_unreachable;
32 int recno;
35 struct expire_reflog_cb {
36 FILE *newlog;
37 enum {
38 UE_NORMAL,
39 UE_ALWAYS,
40 UE_HEAD
41 } unreachable_expire_kind;
42 struct commit_list *mark_list;
43 unsigned long mark_limit;
44 struct cmd_reflog_expire_cb *cmd;
45 unsigned char last_kept_sha1[20];
46 struct commit *tip_commit;
47 struct commit_list *tips;
50 struct collected_reflog {
51 unsigned char sha1[20];
52 char reflog[FLEX_ARRAY];
54 struct collect_reflog_cb {
55 struct collected_reflog **e;
56 int alloc;
57 int nr;
60 #define INCOMPLETE (1u<<10)
61 #define STUDYING (1u<<11)
62 #define REACHABLE (1u<<12)
64 static int tree_is_complete(const unsigned char *sha1)
66 struct tree_desc desc;
67 struct name_entry entry;
68 int complete;
69 struct tree *tree;
71 tree = lookup_tree(sha1);
72 if (!tree)
73 return 0;
74 if (tree->object.flags & SEEN)
75 return 1;
76 if (tree->object.flags & INCOMPLETE)
77 return 0;
79 if (!tree->buffer) {
80 enum object_type type;
81 unsigned long size;
82 void *data = read_sha1_file(sha1, &type, &size);
83 if (!data) {
84 tree->object.flags |= INCOMPLETE;
85 return 0;
87 tree->buffer = data;
88 tree->size = size;
90 init_tree_desc(&desc, tree->buffer, tree->size);
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;
99 free_tree_buffer(tree);
101 if (complete)
102 tree->object.flags |= SEEN;
103 return complete;
106 static int commit_is_complete(struct commit *commit)
108 struct object_array study;
109 struct object_array found;
110 int is_incomplete = 0;
111 int i;
113 /* early return */
114 if (commit->object.flags & SEEN)
115 return 1;
116 if (commit->object.flags & INCOMPLETE)
117 return 0;
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.
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;
134 c = (struct commit *)study.objects[--study.nr].item;
135 if (!c->object.parsed && !parse_object(c->object.sha1))
136 c->object.flags |= INCOMPLETE;
138 if (c->object.flags & INCOMPLETE) {
139 is_incomplete = 1;
140 break;
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);
153 if (!is_incomplete) {
155 * make sure all commits in "found" array have all the
156 * necessary objects.
158 for (i = 0; i < found.nr; i++) {
159 struct commit *c =
160 (struct commit *)found.objects[i].item;
161 if (!tree_is_complete(c->tree->object.sha1)) {
162 is_incomplete = 1;
163 c->object.flags |= INCOMPLETE;
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;
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;
177 else {
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.
186 for (i = 0; i < found.nr; i++)
187 found.objects[i].item->flags |= SEEN;
189 /* free object arrays */
190 free(study.objects);
191 free(found.objects);
192 return !is_incomplete;
195 static int keep_entry(struct commit **it, unsigned char *sha1)
197 struct commit *commit;
199 if (is_null_sha1(sha1))
200 return 1;
201 commit = lookup_commit_reference_gently(sha1, 1);
202 if (!commit)
203 return 0;
206 * Make sure everything in this commit exists.
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.
213 if (!commit_is_complete(commit))
214 return 0;
215 *it = commit;
216 return 1;
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.
225 static void mark_reachable(struct expire_reflog_cb *cb)
227 struct commit *commit;
228 struct commit_list *pending;
229 unsigned long expire_limit = cb->mark_limit;
230 struct commit_list *leftover = NULL;
232 for (pending = cb->mark_list; pending; pending = pending->next)
233 pending->item->object.flags &= ~REACHABLE;
235 pending = cb->mark_list;
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;
247 if (commit->date < expire_limit) {
248 commit_list_insert(commit, &leftover);
249 continue;
251 commit->object.flags |= REACHABLE;
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);
261 cb->mark_list = leftover;
264 static int unreachable(struct expire_reflog_cb *cb, struct commit *commit, unsigned char *sha1)
267 * We may or may not have the commit yet - if not, look it
268 * up using the supplied sha1.
270 if (!commit) {
271 if (is_null_sha1(sha1))
272 return 0;
274 commit = lookup_commit_reference_gently(sha1, 1);
276 /* Not a commit -- keep it */
277 if (!commit)
278 return 0;
281 /* Reachable from the current ref? Don't prune. */
282 if (commit->object.flags & REACHABLE)
283 return 0;
285 if (cb->mark_list && cb->mark_limit) {
286 cb->mark_limit = 0; /* dig down to the root */
287 mark_reachable(cb);
290 return !(commit->object.flags & REACHABLE);
294 * Return true iff the specified reflog entry should be expired.
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)
300 struct expire_reflog_cb *cb = cb_data;
301 struct commit *old, *new;
303 if (timestamp < cb->cmd->expire_total)
304 return 1;
306 old = new = NULL;
307 if (cb->cmd->stalefix &&
308 (!keep_entry(&old, osha1) || !keep_entry(&new, nsha1)))
309 return 1;
311 if (timestamp < cb->cmd->expire_unreachable) {
312 if (cb->unreachable_expire_kind == UE_ALWAYS)
313 return 1;
314 if (unreachable(cb, old, osha1) || unreachable(cb, new, nsha1))
315 return 1;
318 if (cb->cmd->recno && --(cb->cmd->recno) == 0)
319 return 1;
321 return 0;
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)
328 struct expire_reflog_cb *cb = cb_data;
330 if (cb->cmd->rewrite)
331 osha1 = cb->last_kept_sha1;
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);
349 if (cb->cmd->verbose)
350 printf("keep %s", message);
352 return 0;
355 static int push_tip_to_list(const char *refname, const unsigned char *sha1, int flags, void *cb_data)
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;
368 static void reflog_expiry_prepare(const char *refname,
369 const unsigned char *sha1,
370 struct expire_reflog_cb *cb)
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;
383 if (cb->cmd->expire_unreachable <= cb->cmd->expire_total)
384 cb->unreachable_expire_kind = UE_ALWAYS;
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);
397 cb->mark_limit = cb->cmd->expire_total;
398 mark_reachable(cb);
402 static void reflog_expiry_cleanup(struct expire_reflog_cb *cb)
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);
416 static int expire_reflog(const char *refname, const unsigned char *sha1,
417 struct cmd_reflog_expire_cb *cmd)
419 static struct lock_file reflog_lock;
420 struct expire_reflog_cb cb;
421 struct ref_lock *lock;
422 char *log_file;
423 int status = 0;
425 memset(&cb, 0, sizeof(cb));
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:
432 lock = lock_any_ref_for_update(refname, sha1, 0, NULL);
433 if (!lock)
434 return error("cannot lock ref '%s'", refname);
435 if (!reflog_exists(refname)) {
436 unlock_ref(lock);
437 return 0;
440 log_file = git_pathdup("logs/%s", refname);
441 if (!cmd->dry_run) {
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.
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;
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;
464 cb.cmd = cmd;
466 reflog_expiry_prepare(refname, sha1, &cb);
467 for_each_reflog_ent(refname, expire_reflog_ent, &cb);
468 reflog_expiry_cleanup(&cb);
470 if (cb.newlog) {
471 if (close_lock_file(&reflog_lock)) {
472 status |= error("couldn't write %s: %s", log_file,
473 strerror(errno));
474 } else if (cmd->updateref &&
475 (write_in_full(lock->lock_fd,
476 sha1_to_hex(cb.last_kept_sha1), 40) != 40 ||
477 write_str_in_full(lock->lock_fd, "\n") != 1 ||
478 close_ref(lock) < 0)) {
479 status |= error("couldn't write %s",
480 lock->lk->filename.buf);
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));
485 } else if (cmd->updateref && commit_ref(lock)) {
486 status |= error("couldn't set %s", lock->ref_name);
489 free(log_file);
490 unlock_ref(lock);
491 return status;
493 failure:
494 rollback_lock_file(&reflog_lock);
495 free(log_file);
496 unlock_ref(lock);
497 return -1;
500 static int collect_reflog(const char *ref, const unsigned char *sha1, int unused, void *cb_data)
502 struct collected_reflog *e;
503 struct collect_reflog_cb *cb = cb_data;
504 size_t namelen = strlen(ref);
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;
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;
522 static struct reflog_expire_cfg *find_cfg_ent(const char *pattern, size_t len)
524 struct reflog_expire_cfg *ent;
526 if (!reflog_expire_cfg_tail)
527 reflog_expire_cfg_tail = &reflog_expire_cfg;
529 for (ent = reflog_expire_cfg; ent; ent = ent->next)
530 if (ent->len == len &&
531 !memcmp(ent->pattern, pattern, len))
532 return ent;
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;
542 static int parse_expire_cfg_value(const char *var, const char *value, unsigned long *expire)
544 if (!value)
545 return config_error_nonbool(var);
546 if (parse_expiry_date(value, expire))
547 return error(_("%s' for '%s' is not a valid timestamp"),
548 value, var);
549 return 0;
552 /* expiry timer slot */
553 #define EXPIRE_TOTAL 01
554 #define EXPIRE_UNREACH 02
556 static int reflog_expire_config(const char *var, const char *value, void *cb)
558 const char *pattern, *key;
559 int pattern_len;
560 unsigned long expire;
561 int slot;
562 struct reflog_expire_cfg *ent;
564 if (parse_config_key(var, "gc", &pattern, &pattern_len, &key) < 0)
565 return git_default_config(var, value, cb);
567 if (!strcmp(key, "reflogexpire")) {
568 slot = EXPIRE_TOTAL;
569 if (parse_expire_cfg_value(var, value, &expire))
570 return -1;
571 } else if (!strcmp(key, "reflogexpireunreachable")) {
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);
578 if (!pattern) {
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;
587 return 0;
590 ent = find_cfg_ent(pattern, pattern_len);
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;
601 return 0;
604 static void set_reflog_expiry_param(struct cmd_reflog_expire_cb *cb, int slot, const char *ref)
606 struct reflog_expire_cfg *ent;
608 if (slot == (EXPIRE_TOTAL|EXPIRE_UNREACH))
609 return; /* both given explicitly -- nothing to tweak */
611 for (ent = reflog_expire_cfg; ent; ent = ent->next) {
612 if (!wildmatch(ent->pattern, ref, 0, NULL)) {
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;
622 * If unconfigured, make stash never expire
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;
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;
639 static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
641 struct cmd_reflog_expire_cb cb;
642 unsigned long now = time(NULL);
643 int i, status, do_all;
644 int explicit_expiry = 0;
646 default_reflog_expire_unreachable = now - 30 * 24 * 3600;
647 default_reflog_expire = now - 90 * 24 * 3600;
648 git_config(reflog_expire_config, NULL);
650 save_commit_buffer = 0;
651 do_all = status = 0;
652 memset(&cb, 0, sizeof(cb));
654 cb.expire_total = default_reflog_expire;
655 cb.expire_unreachable = default_reflog_expire_unreachable;
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;
661 else if (starts_with(arg, "--expire=")) {
662 if (parse_expiry_date(arg + 9, &cb.expire_total))
663 die(_("'%s' is not a valid timestamp"), arg);
664 explicit_expiry |= EXPIRE_TOTAL;
666 else if (starts_with(arg, "--expire-unreachable=")) {
667 if (parse_expiry_date(arg + 21, &cb.expire_unreachable))
668 die(_("'%s' is not a valid timestamp"), arg);
669 explicit_expiry |= EXPIRE_UNREACH;
671 else if (!strcmp(arg, "--stale-fix"))
672 cb.stalefix = 1;
673 else if (!strcmp(arg, "--rewrite"))
674 cb.rewrite = 1;
675 else if (!strcmp(arg, "--updateref"))
676 cb.updateref = 1;
677 else if (!strcmp(arg, "--all"))
678 do_all = 1;
679 else if (!strcmp(arg, "--verbose"))
680 cb.verbose = 1;
681 else if (!strcmp(arg, "--")) {
682 i++;
683 break;
685 else if (arg[0] == '-')
686 usage(reflog_expire_usage);
687 else
688 break;
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.
696 if (cb.stalefix) {
697 init_revisions(&cb.revs, prefix);
698 if (cb.verbose)
699 printf("Marking reachable objects...");
700 mark_reachable_objects(&cb.revs, 0, 0, NULL);
701 if (cb.verbose)
702 putchar('\n');
705 if (do_all) {
706 struct collect_reflog_cb collected;
707 int i;
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];
713 set_reflog_expiry_param(&cb, explicit_expiry, e->reflog);
714 status |= expire_reflog(e->reflog, e->sha1, &cb);
715 free(e);
717 free(collected.e);
720 for (; i < argc; i++) {
721 char *ref;
722 unsigned char sha1[20];
723 if (!dwim_log(argv[i], strlen(argv[i]), sha1, &ref)) {
724 status |= error("%s points nowhere!", argv[i]);
725 continue;
727 set_reflog_expiry_param(&cb, explicit_expiry, ref);
728 status |= expire_reflog(ref, sha1, &cb);
730 return status;
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)
737 struct cmd_reflog_expire_cb *cb = cb_data;
738 if (!cb->expire_total || timestamp < cb->expire_total)
739 cb->recno++;
740 return 0;
743 static int cmd_reflog_delete(int argc, const char **argv, const char *prefix)
745 struct cmd_reflog_expire_cb cb;
746 int i, status = 0;
748 memset(&cb, 0, sizeof(cb));
750 for (i = 1; i < argc; i++) {
751 const char *arg = argv[i];
752 if (!strcmp(arg, "--dry-run") || !strcmp(arg, "-n"))
753 cb.dry_run = 1;
754 else if (!strcmp(arg, "--rewrite"))
755 cb.rewrite = 1;
756 else if (!strcmp(arg, "--updateref"))
757 cb.updateref = 1;
758 else if (!strcmp(arg, "--verbose"))
759 cb.verbose = 1;
760 else if (!strcmp(arg, "--")) {
761 i++;
762 break;
764 else if (arg[0] == '-')
765 usage(reflog_delete_usage);
766 else
767 break;
770 if (argc - i < 1)
771 return error("Nothing to delete?");
773 for ( ; i < argc; i++) {
774 const char *spec = strstr(argv[i], "@{");
775 unsigned char sha1[20];
776 char *ep, *ref;
777 int recno;
779 if (!spec) {
780 status |= error("Not a reflog: %s", argv[i]);
781 continue;
784 if (!dwim_log(argv[i], spec - argv[i], sha1, &ref)) {
785 status |= error("no reflog for '%s'", argv[i]);
786 continue;
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;
799 status |= expire_reflog(ref, sha1, &cb);
800 free(ref);
802 return status;
806 * main "reflog"
809 static const char reflog_usage[] =
810 "git reflog [ show | expire | delete ]";
812 int cmd_reflog(int argc, const char **argv, const char *prefix)
814 if (argc > 1 && !strcmp(argv[1], "-h"))
815 usage(reflog_usage);
817 /* With no command, we default to showing it. */
818 if (argc < 2 || *argv[1] == '-')
819 return cmd_log_reflog(argc, argv, prefix);
821 if (!strcmp(argv[1], "show"))
822 return cmd_log_reflog(argc - 1, argv + 1, prefix);
824 if (!strcmp(argv[1], "expire"))
825 return cmd_reflog_expire(argc - 1, argv + 1, prefix);
827 if (!strcmp(argv[1], "delete"))
828 return cmd_reflog_delete(argc - 1, argv + 1, prefix);
830 return cmd_log_reflog(argc, argv, prefix);