Extract function should_expire_reflog_ent()
[git/mingw.git] / builtin / reflog.c
blob06ce8b174c575da39e936f0598f5ee9f116cd76e
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];
48 struct collected_reflog {
49 unsigned char sha1[20];
50 char reflog[FLEX_ARRAY];
52 struct collect_reflog_cb {
53 struct collected_reflog **e;
54 int alloc;
55 int nr;
58 #define INCOMPLETE (1u<<10)
59 #define STUDYING (1u<<11)
60 #define REACHABLE (1u<<12)
62 static int tree_is_complete(const unsigned char *sha1)
64 struct tree_desc desc;
65 struct name_entry entry;
66 int complete;
67 struct tree *tree;
69 tree = lookup_tree(sha1);
70 if (!tree)
71 return 0;
72 if (tree->object.flags & SEEN)
73 return 1;
74 if (tree->object.flags & INCOMPLETE)
75 return 0;
77 if (!tree->buffer) {
78 enum object_type type;
79 unsigned long size;
80 void *data = read_sha1_file(sha1, &type, &size);
81 if (!data) {
82 tree->object.flags |= INCOMPLETE;
83 return 0;
85 tree->buffer = data;
86 tree->size = size;
88 init_tree_desc(&desc, tree->buffer, tree->size);
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;
97 free_tree_buffer(tree);
99 if (complete)
100 tree->object.flags |= SEEN;
101 return complete;
104 static int commit_is_complete(struct commit *commit)
106 struct object_array study;
107 struct object_array found;
108 int is_incomplete = 0;
109 int i;
111 /* early return */
112 if (commit->object.flags & SEEN)
113 return 1;
114 if (commit->object.flags & INCOMPLETE)
115 return 0;
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.
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;
132 c = (struct commit *)study.objects[--study.nr].item;
133 if (!c->object.parsed && !parse_object(c->object.sha1))
134 c->object.flags |= INCOMPLETE;
136 if (c->object.flags & INCOMPLETE) {
137 is_incomplete = 1;
138 break;
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);
151 if (!is_incomplete) {
153 * make sure all commits in "found" array have all the
154 * necessary objects.
156 for (i = 0; i < found.nr; i++) {
157 struct commit *c =
158 (struct commit *)found.objects[i].item;
159 if (!tree_is_complete(c->tree->object.sha1)) {
160 is_incomplete = 1;
161 c->object.flags |= INCOMPLETE;
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;
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;
175 else {
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.
184 for (i = 0; i < found.nr; i++)
185 found.objects[i].item->flags |= SEEN;
187 /* free object arrays */
188 free(study.objects);
189 free(found.objects);
190 return !is_incomplete;
193 static int keep_entry(struct commit **it, unsigned char *sha1)
195 struct commit *commit;
197 if (is_null_sha1(sha1))
198 return 1;
199 commit = lookup_commit_reference_gently(sha1, 1);
200 if (!commit)
201 return 0;
204 * Make sure everything in this commit exists.
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.
211 if (!commit_is_complete(commit))
212 return 0;
213 *it = commit;
214 return 1;
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.
223 static void mark_reachable(struct expire_reflog_cb *cb)
225 struct commit *commit;
226 struct commit_list *pending;
227 unsigned long expire_limit = cb->mark_limit;
228 struct commit_list *leftover = NULL;
230 for (pending = cb->mark_list; pending; pending = pending->next)
231 pending->item->object.flags &= ~REACHABLE;
233 pending = cb->mark_list;
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;
245 if (commit->date < expire_limit) {
246 commit_list_insert(commit, &leftover);
247 continue;
249 commit->object.flags |= REACHABLE;
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);
259 cb->mark_list = leftover;
262 static int unreachable(struct expire_reflog_cb *cb, struct commit *commit, unsigned char *sha1)
265 * We may or may not have the commit yet - if not, look it
266 * up using the supplied sha1.
268 if (!commit) {
269 if (is_null_sha1(sha1))
270 return 0;
272 commit = lookup_commit_reference_gently(sha1, 1);
274 /* Not a commit -- keep it */
275 if (!commit)
276 return 0;
279 /* Reachable from the current ref? Don't prune. */
280 if (commit->object.flags & REACHABLE)
281 return 0;
283 if (cb->mark_list && cb->mark_limit) {
284 cb->mark_limit = 0; /* dig down to the root */
285 mark_reachable(cb);
288 return !(commit->object.flags & REACHABLE);
292 * Return true iff the specified reflog entry should be expired.
294 static int should_expire_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
295 const char *email, unsigned long timestamp, int tz,
296 const char *message, void *cb_data)
298 struct expire_reflog_cb *cb = cb_data;
299 struct commit *old, *new;
301 if (timestamp < cb->cmd->expire_total)
302 return 1;
304 old = new = NULL;
305 if (cb->cmd->stalefix &&
306 (!keep_entry(&old, osha1) || !keep_entry(&new, nsha1)))
307 return 1;
309 if (timestamp < cb->cmd->expire_unreachable) {
310 if (cb->unreachable_expire_kind == UE_ALWAYS)
311 return 1;
312 if (unreachable(cb, old, osha1) || unreachable(cb, new, nsha1))
313 return 1;
316 if (cb->cmd->recno && --(cb->cmd->recno) == 0)
317 return 1;
319 return 0;
322 static int expire_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
323 const char *email, unsigned long timestamp, int tz,
324 const char *message, void *cb_data)
326 struct expire_reflog_cb *cb = cb_data;
328 if (cb->cmd->rewrite)
329 osha1 = cb->last_kept_sha1;
331 if (should_expire_reflog_ent(osha1, nsha1, email, timestamp, tz,
332 message, cb_data)) {
333 if (!cb->newlog)
334 printf("would prune %s", message);
335 else if (cb->cmd->verbose)
336 printf("prune %s", message);
337 } else {
338 if (cb->newlog) {
339 char sign = (tz < 0) ? '-' : '+';
340 int zone = (tz < 0) ? (-tz) : tz;
341 fprintf(cb->newlog, "%s %s %s %lu %c%04d\t%s",
342 sha1_to_hex(osha1), sha1_to_hex(nsha1),
343 email, timestamp, sign, zone,
344 message);
345 hashcpy(cb->last_kept_sha1, nsha1);
347 if (cb->cmd->verbose)
348 printf("keep %s", message);
350 return 0;
353 static int push_tip_to_list(const char *refname, const unsigned char *sha1, int flags, void *cb_data)
355 struct commit_list **list = cb_data;
356 struct commit *tip_commit;
357 if (flags & REF_ISSYMREF)
358 return 0;
359 tip_commit = lookup_commit_reference_gently(sha1, 1);
360 if (!tip_commit)
361 return 0;
362 commit_list_insert(tip_commit, list);
363 return 0;
366 static int expire_reflog(const char *refname, const unsigned char *sha1,
367 struct cmd_reflog_expire_cb *cmd)
369 static struct lock_file reflog_lock;
370 struct expire_reflog_cb cb;
371 struct ref_lock *lock;
372 char *log_file;
373 struct commit *tip_commit;
374 struct commit_list *tips;
375 int status = 0;
377 memset(&cb, 0, sizeof(cb));
380 * The reflog file is locked by holding the lock on the
381 * reference itself, plus we might need to update the
382 * reference if --updateref was specified:
384 lock = lock_any_ref_for_update(refname, sha1, 0, NULL);
385 if (!lock)
386 return error("cannot lock ref '%s'", refname);
387 if (!reflog_exists(refname)) {
388 unlock_ref(lock);
389 return 0;
392 log_file = git_pathdup("logs/%s", refname);
393 if (!cmd->dry_run) {
395 * Even though holding $GIT_DIR/logs/$reflog.lock has
396 * no locking implications, we use the lock_file
397 * machinery here anyway because it does a lot of the
398 * work we need, including cleaning up if the program
399 * exits unexpectedly.
401 if (hold_lock_file_for_update(&reflog_lock, log_file, 0) < 0) {
402 struct strbuf err = STRBUF_INIT;
403 unable_to_lock_message(log_file, errno, &err);
404 error("%s", err.buf);
405 strbuf_release(&err);
406 goto failure;
408 cb.newlog = fdopen_lock_file(&reflog_lock, "w");
409 if (!cb.newlog) {
410 error("cannot fdopen %s (%s)",
411 reflog_lock.filename.buf, strerror(errno));
412 goto failure;
416 cb.cmd = cmd;
418 if (!cmd->expire_unreachable || !strcmp(refname, "HEAD")) {
419 tip_commit = NULL;
420 cb.unreachable_expire_kind = UE_HEAD;
421 } else {
422 tip_commit = lookup_commit_reference_gently(sha1, 1);
423 if (!tip_commit)
424 cb.unreachable_expire_kind = UE_ALWAYS;
425 else
426 cb.unreachable_expire_kind = UE_NORMAL;
429 if (cmd->expire_unreachable <= cmd->expire_total)
430 cb.unreachable_expire_kind = UE_ALWAYS;
432 cb.mark_list = NULL;
433 tips = NULL;
434 if (cb.unreachable_expire_kind != UE_ALWAYS) {
435 if (cb.unreachable_expire_kind == UE_HEAD) {
436 struct commit_list *elem;
437 for_each_ref(push_tip_to_list, &tips);
438 for (elem = tips; elem; elem = elem->next)
439 commit_list_insert(elem->item, &cb.mark_list);
440 } else {
441 commit_list_insert(tip_commit, &cb.mark_list);
443 cb.mark_limit = cmd->expire_total;
444 mark_reachable(&cb);
447 for_each_reflog_ent(refname, expire_reflog_ent, &cb);
449 if (cb.unreachable_expire_kind != UE_ALWAYS) {
450 if (cb.unreachable_expire_kind == UE_HEAD) {
451 struct commit_list *elem;
452 for (elem = tips; elem; elem = elem->next)
453 clear_commit_marks(elem->item, REACHABLE);
454 free_commit_list(tips);
455 } else {
456 clear_commit_marks(tip_commit, REACHABLE);
460 if (cb.newlog) {
461 if (close_lock_file(&reflog_lock)) {
462 status |= error("couldn't write %s: %s", log_file,
463 strerror(errno));
464 } else if (cmd->updateref &&
465 (write_in_full(lock->lock_fd,
466 sha1_to_hex(cb.last_kept_sha1), 40) != 40 ||
467 write_str_in_full(lock->lock_fd, "\n") != 1 ||
468 close_ref(lock) < 0)) {
469 status |= error("couldn't write %s",
470 lock->lk->filename.buf);
471 rollback_lock_file(&reflog_lock);
472 } else if (commit_lock_file(&reflog_lock)) {
473 status |= error("unable to commit reflog '%s' (%s)",
474 log_file, strerror(errno));
475 } else if (cmd->updateref && commit_ref(lock)) {
476 status |= error("couldn't set %s", lock->ref_name);
479 free(log_file);
480 unlock_ref(lock);
481 return status;
483 failure:
484 rollback_lock_file(&reflog_lock);
485 free(log_file);
486 unlock_ref(lock);
487 return -1;
490 static int collect_reflog(const char *ref, const unsigned char *sha1, int unused, void *cb_data)
492 struct collected_reflog *e;
493 struct collect_reflog_cb *cb = cb_data;
494 size_t namelen = strlen(ref);
496 e = xmalloc(sizeof(*e) + namelen + 1);
497 hashcpy(e->sha1, sha1);
498 memcpy(e->reflog, ref, namelen + 1);
499 ALLOC_GROW(cb->e, cb->nr + 1, cb->alloc);
500 cb->e[cb->nr++] = e;
501 return 0;
504 static struct reflog_expire_cfg {
505 struct reflog_expire_cfg *next;
506 unsigned long expire_total;
507 unsigned long expire_unreachable;
508 size_t len;
509 char pattern[FLEX_ARRAY];
510 } *reflog_expire_cfg, **reflog_expire_cfg_tail;
512 static struct reflog_expire_cfg *find_cfg_ent(const char *pattern, size_t len)
514 struct reflog_expire_cfg *ent;
516 if (!reflog_expire_cfg_tail)
517 reflog_expire_cfg_tail = &reflog_expire_cfg;
519 for (ent = reflog_expire_cfg; ent; ent = ent->next)
520 if (ent->len == len &&
521 !memcmp(ent->pattern, pattern, len))
522 return ent;
524 ent = xcalloc(1, (sizeof(*ent) + len));
525 memcpy(ent->pattern, pattern, len);
526 ent->len = len;
527 *reflog_expire_cfg_tail = ent;
528 reflog_expire_cfg_tail = &(ent->next);
529 return ent;
532 static int parse_expire_cfg_value(const char *var, const char *value, unsigned long *expire)
534 if (!value)
535 return config_error_nonbool(var);
536 if (parse_expiry_date(value, expire))
537 return error(_("%s' for '%s' is not a valid timestamp"),
538 value, var);
539 return 0;
542 /* expiry timer slot */
543 #define EXPIRE_TOTAL 01
544 #define EXPIRE_UNREACH 02
546 static int reflog_expire_config(const char *var, const char *value, void *cb)
548 const char *pattern, *key;
549 int pattern_len;
550 unsigned long expire;
551 int slot;
552 struct reflog_expire_cfg *ent;
554 if (parse_config_key(var, "gc", &pattern, &pattern_len, &key) < 0)
555 return git_default_config(var, value, cb);
557 if (!strcmp(key, "reflogexpire")) {
558 slot = EXPIRE_TOTAL;
559 if (parse_expire_cfg_value(var, value, &expire))
560 return -1;
561 } else if (!strcmp(key, "reflogexpireunreachable")) {
562 slot = EXPIRE_UNREACH;
563 if (parse_expire_cfg_value(var, value, &expire))
564 return -1;
565 } else
566 return git_default_config(var, value, cb);
568 if (!pattern) {
569 switch (slot) {
570 case EXPIRE_TOTAL:
571 default_reflog_expire = expire;
572 break;
573 case EXPIRE_UNREACH:
574 default_reflog_expire_unreachable = expire;
575 break;
577 return 0;
580 ent = find_cfg_ent(pattern, pattern_len);
581 if (!ent)
582 return -1;
583 switch (slot) {
584 case EXPIRE_TOTAL:
585 ent->expire_total = expire;
586 break;
587 case EXPIRE_UNREACH:
588 ent->expire_unreachable = expire;
589 break;
591 return 0;
594 static void set_reflog_expiry_param(struct cmd_reflog_expire_cb *cb, int slot, const char *ref)
596 struct reflog_expire_cfg *ent;
598 if (slot == (EXPIRE_TOTAL|EXPIRE_UNREACH))
599 return; /* both given explicitly -- nothing to tweak */
601 for (ent = reflog_expire_cfg; ent; ent = ent->next) {
602 if (!wildmatch(ent->pattern, ref, 0, NULL)) {
603 if (!(slot & EXPIRE_TOTAL))
604 cb->expire_total = ent->expire_total;
605 if (!(slot & EXPIRE_UNREACH))
606 cb->expire_unreachable = ent->expire_unreachable;
607 return;
612 * If unconfigured, make stash never expire
614 if (!strcmp(ref, "refs/stash")) {
615 if (!(slot & EXPIRE_TOTAL))
616 cb->expire_total = 0;
617 if (!(slot & EXPIRE_UNREACH))
618 cb->expire_unreachable = 0;
619 return;
622 /* Nothing matched -- use the default value */
623 if (!(slot & EXPIRE_TOTAL))
624 cb->expire_total = default_reflog_expire;
625 if (!(slot & EXPIRE_UNREACH))
626 cb->expire_unreachable = default_reflog_expire_unreachable;
629 static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
631 struct cmd_reflog_expire_cb cb;
632 unsigned long now = time(NULL);
633 int i, status, do_all;
634 int explicit_expiry = 0;
636 default_reflog_expire_unreachable = now - 30 * 24 * 3600;
637 default_reflog_expire = now - 90 * 24 * 3600;
638 git_config(reflog_expire_config, NULL);
640 save_commit_buffer = 0;
641 do_all = status = 0;
642 memset(&cb, 0, sizeof(cb));
644 cb.expire_total = default_reflog_expire;
645 cb.expire_unreachable = default_reflog_expire_unreachable;
647 for (i = 1; i < argc; i++) {
648 const char *arg = argv[i];
649 if (!strcmp(arg, "--dry-run") || !strcmp(arg, "-n"))
650 cb.dry_run = 1;
651 else if (starts_with(arg, "--expire=")) {
652 if (parse_expiry_date(arg + 9, &cb.expire_total))
653 die(_("'%s' is not a valid timestamp"), arg);
654 explicit_expiry |= EXPIRE_TOTAL;
656 else if (starts_with(arg, "--expire-unreachable=")) {
657 if (parse_expiry_date(arg + 21, &cb.expire_unreachable))
658 die(_("'%s' is not a valid timestamp"), arg);
659 explicit_expiry |= EXPIRE_UNREACH;
661 else if (!strcmp(arg, "--stale-fix"))
662 cb.stalefix = 1;
663 else if (!strcmp(arg, "--rewrite"))
664 cb.rewrite = 1;
665 else if (!strcmp(arg, "--updateref"))
666 cb.updateref = 1;
667 else if (!strcmp(arg, "--all"))
668 do_all = 1;
669 else if (!strcmp(arg, "--verbose"))
670 cb.verbose = 1;
671 else if (!strcmp(arg, "--")) {
672 i++;
673 break;
675 else if (arg[0] == '-')
676 usage(reflog_expire_usage);
677 else
678 break;
682 * We can trust the commits and objects reachable from refs
683 * even in older repository. We cannot trust what's reachable
684 * from reflog if the repository was pruned with older git.
686 if (cb.stalefix) {
687 init_revisions(&cb.revs, prefix);
688 if (cb.verbose)
689 printf("Marking reachable objects...");
690 mark_reachable_objects(&cb.revs, 0, 0, NULL);
691 if (cb.verbose)
692 putchar('\n');
695 if (do_all) {
696 struct collect_reflog_cb collected;
697 int i;
699 memset(&collected, 0, sizeof(collected));
700 for_each_reflog(collect_reflog, &collected);
701 for (i = 0; i < collected.nr; i++) {
702 struct collected_reflog *e = collected.e[i];
703 set_reflog_expiry_param(&cb, explicit_expiry, e->reflog);
704 status |= expire_reflog(e->reflog, e->sha1, &cb);
705 free(e);
707 free(collected.e);
710 for (; i < argc; i++) {
711 char *ref;
712 unsigned char sha1[20];
713 if (!dwim_log(argv[i], strlen(argv[i]), sha1, &ref)) {
714 status |= error("%s points nowhere!", argv[i]);
715 continue;
717 set_reflog_expiry_param(&cb, explicit_expiry, ref);
718 status |= expire_reflog(ref, sha1, &cb);
720 return status;
723 static int count_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
724 const char *email, unsigned long timestamp, int tz,
725 const char *message, void *cb_data)
727 struct cmd_reflog_expire_cb *cb = cb_data;
728 if (!cb->expire_total || timestamp < cb->expire_total)
729 cb->recno++;
730 return 0;
733 static int cmd_reflog_delete(int argc, const char **argv, const char *prefix)
735 struct cmd_reflog_expire_cb cb;
736 int i, status = 0;
738 memset(&cb, 0, sizeof(cb));
740 for (i = 1; i < argc; i++) {
741 const char *arg = argv[i];
742 if (!strcmp(arg, "--dry-run") || !strcmp(arg, "-n"))
743 cb.dry_run = 1;
744 else if (!strcmp(arg, "--rewrite"))
745 cb.rewrite = 1;
746 else if (!strcmp(arg, "--updateref"))
747 cb.updateref = 1;
748 else if (!strcmp(arg, "--verbose"))
749 cb.verbose = 1;
750 else if (!strcmp(arg, "--")) {
751 i++;
752 break;
754 else if (arg[0] == '-')
755 usage(reflog_delete_usage);
756 else
757 break;
760 if (argc - i < 1)
761 return error("Nothing to delete?");
763 for ( ; i < argc; i++) {
764 const char *spec = strstr(argv[i], "@{");
765 unsigned char sha1[20];
766 char *ep, *ref;
767 int recno;
769 if (!spec) {
770 status |= error("Not a reflog: %s", argv[i]);
771 continue;
774 if (!dwim_log(argv[i], spec - argv[i], sha1, &ref)) {
775 status |= error("no reflog for '%s'", argv[i]);
776 continue;
779 recno = strtoul(spec + 2, &ep, 10);
780 if (*ep == '}') {
781 cb.recno = -recno;
782 for_each_reflog_ent(ref, count_reflog_ent, &cb);
783 } else {
784 cb.expire_total = approxidate(spec + 2);
785 for_each_reflog_ent(ref, count_reflog_ent, &cb);
786 cb.expire_total = 0;
789 status |= expire_reflog(ref, sha1, &cb);
790 free(ref);
792 return status;
796 * main "reflog"
799 static const char reflog_usage[] =
800 "git reflog [ show | expire | delete ]";
802 int cmd_reflog(int argc, const char **argv, const char *prefix)
804 if (argc > 1 && !strcmp(argv[1], "-h"))
805 usage(reflog_usage);
807 /* With no command, we default to showing it. */
808 if (argc < 2 || *argv[1] == '-')
809 return cmd_log_reflog(argc, argv, prefix);
811 if (!strcmp(argv[1], "show"))
812 return cmd_log_reflog(argc - 1, argv + 1, prefix);
814 if (!strcmp(argv[1], "expire"))
815 return cmd_reflog_expire(argc - 1, argv + 1, prefix);
817 if (!strcmp(argv[1], "delete"))
818 return cmd_reflog_delete(argc - 1, argv + 1, prefix);
820 return cmd_log_reflog(argc, argv, prefix);