Move newlog and last_kept_sha1 to "struct expire_reflog_cb"
[git/mingw/j6t.git] / builtin / reflog.c
blobd36c2c6f2f0c3c25de426b76ad1080aae00b7e8c
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 enum expire_reflog_flags {
24 EXPIRE_REFLOGS_DRY_RUN = 1 << 0,
25 EXPIRE_REFLOGS_UPDATE_REF = 1 << 1,
26 EXPIRE_REFLOGS_VERBOSE = 1 << 2,
27 EXPIRE_REFLOGS_REWRITE = 1 << 3
30 struct cmd_reflog_expire_cb {
31 struct rev_info revs;
32 int stalefix;
33 unsigned long expire_total;
34 unsigned long expire_unreachable;
35 int recno;
38 struct expire_reflog_policy_cb {
39 enum {
40 UE_NORMAL,
41 UE_ALWAYS,
42 UE_HEAD
43 } unreachable_expire_kind;
44 struct commit_list *mark_list;
45 unsigned long mark_limit;
46 struct cmd_reflog_expire_cb *cmd;
47 struct commit *tip_commit;
48 struct commit_list *tips;
51 struct expire_reflog_cb {
52 unsigned int flags;
53 void *policy_cb;
54 FILE *newlog;
55 unsigned char last_kept_sha1[20];
58 struct collected_reflog {
59 unsigned char sha1[20];
60 char reflog[FLEX_ARRAY];
63 struct collect_reflog_cb {
64 struct collected_reflog **e;
65 int alloc;
66 int nr;
69 #define INCOMPLETE (1u<<10)
70 #define STUDYING (1u<<11)
71 #define REACHABLE (1u<<12)
73 static int tree_is_complete(const unsigned char *sha1)
75 struct tree_desc desc;
76 struct name_entry entry;
77 int complete;
78 struct tree *tree;
80 tree = lookup_tree(sha1);
81 if (!tree)
82 return 0;
83 if (tree->object.flags & SEEN)
84 return 1;
85 if (tree->object.flags & INCOMPLETE)
86 return 0;
88 if (!tree->buffer) {
89 enum object_type type;
90 unsigned long size;
91 void *data = read_sha1_file(sha1, &type, &size);
92 if (!data) {
93 tree->object.flags |= INCOMPLETE;
94 return 0;
96 tree->buffer = data;
97 tree->size = size;
99 init_tree_desc(&desc, tree->buffer, tree->size);
100 complete = 1;
101 while (tree_entry(&desc, &entry)) {
102 if (!has_sha1_file(entry.sha1) ||
103 (S_ISDIR(entry.mode) && !tree_is_complete(entry.sha1))) {
104 tree->object.flags |= INCOMPLETE;
105 complete = 0;
108 free_tree_buffer(tree);
110 if (complete)
111 tree->object.flags |= SEEN;
112 return complete;
115 static int commit_is_complete(struct commit *commit)
117 struct object_array study;
118 struct object_array found;
119 int is_incomplete = 0;
120 int i;
122 /* early return */
123 if (commit->object.flags & SEEN)
124 return 1;
125 if (commit->object.flags & INCOMPLETE)
126 return 0;
128 * Find all commits that are reachable and are not marked as
129 * SEEN. Then make sure the trees and blobs contained are
130 * complete. After that, mark these commits also as SEEN.
131 * If some of the objects that are needed to complete this
132 * commit are missing, mark this commit as INCOMPLETE.
134 memset(&study, 0, sizeof(study));
135 memset(&found, 0, sizeof(found));
136 add_object_array(&commit->object, NULL, &study);
137 add_object_array(&commit->object, NULL, &found);
138 commit->object.flags |= STUDYING;
139 while (study.nr) {
140 struct commit *c;
141 struct commit_list *parent;
143 c = (struct commit *)study.objects[--study.nr].item;
144 if (!c->object.parsed && !parse_object(c->object.sha1))
145 c->object.flags |= INCOMPLETE;
147 if (c->object.flags & INCOMPLETE) {
148 is_incomplete = 1;
149 break;
151 else if (c->object.flags & SEEN)
152 continue;
153 for (parent = c->parents; parent; parent = parent->next) {
154 struct commit *p = parent->item;
155 if (p->object.flags & STUDYING)
156 continue;
157 p->object.flags |= STUDYING;
158 add_object_array(&p->object, NULL, &study);
159 add_object_array(&p->object, NULL, &found);
162 if (!is_incomplete) {
164 * make sure all commits in "found" array have all the
165 * necessary objects.
167 for (i = 0; i < found.nr; i++) {
168 struct commit *c =
169 (struct commit *)found.objects[i].item;
170 if (!tree_is_complete(c->tree->object.sha1)) {
171 is_incomplete = 1;
172 c->object.flags |= INCOMPLETE;
175 if (!is_incomplete) {
176 /* mark all found commits as complete, iow SEEN */
177 for (i = 0; i < found.nr; i++)
178 found.objects[i].item->flags |= SEEN;
181 /* clear flags from the objects we traversed */
182 for (i = 0; i < found.nr; i++)
183 found.objects[i].item->flags &= ~STUDYING;
184 if (is_incomplete)
185 commit->object.flags |= INCOMPLETE;
186 else {
188 * If we come here, we have (1) traversed the ancestry chain
189 * from the "commit" until we reach SEEN commits (which are
190 * known to be complete), and (2) made sure that the commits
191 * encountered during the above traversal refer to trees that
192 * are complete. Which means that we know *all* the commits
193 * we have seen during this process are complete.
195 for (i = 0; i < found.nr; i++)
196 found.objects[i].item->flags |= SEEN;
198 /* free object arrays */
199 free(study.objects);
200 free(found.objects);
201 return !is_incomplete;
204 static int keep_entry(struct commit **it, unsigned char *sha1)
206 struct commit *commit;
208 if (is_null_sha1(sha1))
209 return 1;
210 commit = lookup_commit_reference_gently(sha1, 1);
211 if (!commit)
212 return 0;
215 * Make sure everything in this commit exists.
217 * We have walked all the objects reachable from the refs
218 * and cache earlier. The commits reachable by this commit
219 * must meet SEEN commits -- and then we should mark them as
220 * SEEN as well.
222 if (!commit_is_complete(commit))
223 return 0;
224 *it = commit;
225 return 1;
229 * Starting from commits in the cb->mark_list, mark commits that are
230 * reachable from them. Stop the traversal at commits older than
231 * the expire_limit and queue them back, so that the caller can call
232 * us again to restart the traversal with longer expire_limit.
234 static void mark_reachable(struct expire_reflog_policy_cb *cb)
236 struct commit *commit;
237 struct commit_list *pending;
238 unsigned long expire_limit = cb->mark_limit;
239 struct commit_list *leftover = NULL;
241 for (pending = cb->mark_list; pending; pending = pending->next)
242 pending->item->object.flags &= ~REACHABLE;
244 pending = cb->mark_list;
245 while (pending) {
246 struct commit_list *entry = pending;
247 struct commit_list *parent;
248 pending = entry->next;
249 commit = entry->item;
250 free(entry);
251 if (commit->object.flags & REACHABLE)
252 continue;
253 if (parse_commit(commit))
254 continue;
255 commit->object.flags |= REACHABLE;
256 if (commit->date < expire_limit) {
257 commit_list_insert(commit, &leftover);
258 continue;
260 commit->object.flags |= REACHABLE;
261 parent = commit->parents;
262 while (parent) {
263 commit = parent->item;
264 parent = parent->next;
265 if (commit->object.flags & REACHABLE)
266 continue;
267 commit_list_insert(commit, &pending);
270 cb->mark_list = leftover;
273 static int unreachable(struct expire_reflog_policy_cb *cb, struct commit *commit, unsigned char *sha1)
276 * We may or may not have the commit yet - if not, look it
277 * up using the supplied sha1.
279 if (!commit) {
280 if (is_null_sha1(sha1))
281 return 0;
283 commit = lookup_commit_reference_gently(sha1, 1);
285 /* Not a commit -- keep it */
286 if (!commit)
287 return 0;
290 /* Reachable from the current ref? Don't prune. */
291 if (commit->object.flags & REACHABLE)
292 return 0;
294 if (cb->mark_list && cb->mark_limit) {
295 cb->mark_limit = 0; /* dig down to the root */
296 mark_reachable(cb);
299 return !(commit->object.flags & REACHABLE);
303 * Return true iff the specified reflog entry should be expired.
305 static int should_expire_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
306 const char *email, unsigned long timestamp, int tz,
307 const char *message, void *cb_data)
309 struct expire_reflog_policy_cb *cb = cb_data;
310 struct commit *old, *new;
312 if (timestamp < cb->cmd->expire_total)
313 return 1;
315 old = new = NULL;
316 if (cb->cmd->stalefix &&
317 (!keep_entry(&old, osha1) || !keep_entry(&new, nsha1)))
318 return 1;
320 if (timestamp < cb->cmd->expire_unreachable) {
321 if (cb->unreachable_expire_kind == UE_ALWAYS)
322 return 1;
323 if (unreachable(cb, old, osha1) || unreachable(cb, new, nsha1))
324 return 1;
327 if (cb->cmd->recno && --(cb->cmd->recno) == 0)
328 return 1;
330 return 0;
333 static int expire_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
334 const char *email, unsigned long timestamp, int tz,
335 const char *message, void *cb_data)
337 struct expire_reflog_cb *cb = cb_data;
338 struct expire_reflog_policy_cb *policy_cb = cb->policy_cb;
340 if (cb->flags & EXPIRE_REFLOGS_REWRITE)
341 osha1 = cb->last_kept_sha1;
343 if (should_expire_reflog_ent(osha1, nsha1, email, timestamp, tz,
344 message, policy_cb)) {
345 if (!cb->newlog)
346 printf("would prune %s", message);
347 else if (cb->flags & EXPIRE_REFLOGS_VERBOSE)
348 printf("prune %s", message);
349 } else {
350 if (cb->newlog) {
351 char sign = (tz < 0) ? '-' : '+';
352 int zone = (tz < 0) ? (-tz) : tz;
353 fprintf(cb->newlog, "%s %s %s %lu %c%04d\t%s",
354 sha1_to_hex(osha1), sha1_to_hex(nsha1),
355 email, timestamp, sign, zone,
356 message);
357 hashcpy(cb->last_kept_sha1, nsha1);
359 if (cb->flags & EXPIRE_REFLOGS_VERBOSE)
360 printf("keep %s", message);
362 return 0;
365 static int push_tip_to_list(const char *refname, const unsigned char *sha1,
366 int flags, void *cb_data)
368 struct commit_list **list = cb_data;
369 struct commit *tip_commit;
370 if (flags & REF_ISSYMREF)
371 return 0;
372 tip_commit = lookup_commit_reference_gently(sha1, 1);
373 if (!tip_commit)
374 return 0;
375 commit_list_insert(tip_commit, list);
376 return 0;
379 static void reflog_expiry_prepare(const char *refname,
380 const unsigned char *sha1,
381 struct expire_reflog_policy_cb *cb)
383 if (!cb->cmd->expire_unreachable || !strcmp(refname, "HEAD")) {
384 cb->tip_commit = NULL;
385 cb->unreachable_expire_kind = UE_HEAD;
386 } else {
387 cb->tip_commit = lookup_commit_reference_gently(sha1, 1);
388 if (!cb->tip_commit)
389 cb->unreachable_expire_kind = UE_ALWAYS;
390 else
391 cb->unreachable_expire_kind = UE_NORMAL;
394 if (cb->cmd->expire_unreachable <= cb->cmd->expire_total)
395 cb->unreachable_expire_kind = UE_ALWAYS;
397 cb->mark_list = NULL;
398 cb->tips = NULL;
399 if (cb->unreachable_expire_kind != UE_ALWAYS) {
400 if (cb->unreachable_expire_kind == UE_HEAD) {
401 struct commit_list *elem;
402 for_each_ref(push_tip_to_list, &cb->tips);
403 for (elem = cb->tips; elem; elem = elem->next)
404 commit_list_insert(elem->item, &cb->mark_list);
405 } else {
406 commit_list_insert(cb->tip_commit, &cb->mark_list);
408 cb->mark_limit = cb->cmd->expire_total;
409 mark_reachable(cb);
413 static void reflog_expiry_cleanup(struct expire_reflog_policy_cb *cb)
415 if (cb->unreachable_expire_kind != UE_ALWAYS) {
416 if (cb->unreachable_expire_kind == UE_HEAD) {
417 struct commit_list *elem;
418 for (elem = cb->tips; elem; elem = elem->next)
419 clear_commit_marks(elem->item, REACHABLE);
420 free_commit_list(cb->tips);
421 } else {
422 clear_commit_marks(cb->tip_commit, REACHABLE);
427 static int expire_reflog(const char *refname, const unsigned char *sha1,
428 unsigned int flags, struct cmd_reflog_expire_cb *cmd)
430 static struct lock_file reflog_lock;
431 struct expire_reflog_cb cb;
432 struct expire_reflog_policy_cb policy_cb;
433 struct ref_lock *lock;
434 char *log_file;
435 int status = 0;
437 memset(&cb, 0, sizeof(cb));
438 memset(&policy_cb, 0, sizeof(policy_cb));
439 cb.flags = flags;
440 cb.policy_cb = &policy_cb;
443 * The reflog file is locked by holding the lock on the
444 * reference itself, plus we might need to update the
445 * reference if --updateref was specified:
447 lock = lock_any_ref_for_update(refname, sha1, 0, NULL);
448 if (!lock)
449 return error("cannot lock ref '%s'", refname);
450 if (!reflog_exists(refname)) {
451 unlock_ref(lock);
452 return 0;
455 log_file = git_pathdup("logs/%s", refname);
456 if (!(flags & EXPIRE_REFLOGS_DRY_RUN)) {
458 * Even though holding $GIT_DIR/logs/$reflog.lock has
459 * no locking implications, we use the lock_file
460 * machinery here anyway because it does a lot of the
461 * work we need, including cleaning up if the program
462 * exits unexpectedly.
464 if (hold_lock_file_for_update(&reflog_lock, log_file, 0) < 0) {
465 struct strbuf err = STRBUF_INIT;
466 unable_to_lock_message(log_file, errno, &err);
467 error("%s", err.buf);
468 strbuf_release(&err);
469 goto failure;
471 cb.newlog = fdopen_lock_file(&reflog_lock, "w");
472 if (!cb.newlog) {
473 error("cannot fdopen %s (%s)",
474 reflog_lock.filename.buf, strerror(errno));
475 goto failure;
479 policy_cb.cmd = cmd;
481 reflog_expiry_prepare(refname, sha1, &policy_cb);
482 for_each_reflog_ent(refname, expire_reflog_ent, &cb);
483 reflog_expiry_cleanup(&policy_cb);
485 if (!(flags & EXPIRE_REFLOGS_DRY_RUN)) {
486 if (close_lock_file(&reflog_lock)) {
487 status |= error("couldn't write %s: %s", log_file,
488 strerror(errno));
489 } else if ((flags & EXPIRE_REFLOGS_UPDATE_REF) &&
490 (write_in_full(lock->lock_fd,
491 sha1_to_hex(cb.last_kept_sha1), 40) != 40 ||
492 write_str_in_full(lock->lock_fd, "\n") != 1 ||
493 close_ref(lock) < 0)) {
494 status |= error("couldn't write %s",
495 lock->lk->filename.buf);
496 rollback_lock_file(&reflog_lock);
497 } else if (commit_lock_file(&reflog_lock)) {
498 status |= error("unable to commit reflog '%s' (%s)",
499 log_file, strerror(errno));
500 } else if ((flags & EXPIRE_REFLOGS_UPDATE_REF) && commit_ref(lock)) {
501 status |= error("couldn't set %s", lock->ref_name);
504 free(log_file);
505 unlock_ref(lock);
506 return status;
508 failure:
509 rollback_lock_file(&reflog_lock);
510 free(log_file);
511 unlock_ref(lock);
512 return -1;
515 static int collect_reflog(const char *ref, const unsigned char *sha1, int unused, void *cb_data)
517 struct collected_reflog *e;
518 struct collect_reflog_cb *cb = cb_data;
519 size_t namelen = strlen(ref);
521 e = xmalloc(sizeof(*e) + namelen + 1);
522 hashcpy(e->sha1, sha1);
523 memcpy(e->reflog, ref, namelen + 1);
524 ALLOC_GROW(cb->e, cb->nr + 1, cb->alloc);
525 cb->e[cb->nr++] = e;
526 return 0;
529 static struct reflog_expire_cfg {
530 struct reflog_expire_cfg *next;
531 unsigned long expire_total;
532 unsigned long expire_unreachable;
533 size_t len;
534 char pattern[FLEX_ARRAY];
535 } *reflog_expire_cfg, **reflog_expire_cfg_tail;
537 static struct reflog_expire_cfg *find_cfg_ent(const char *pattern, size_t len)
539 struct reflog_expire_cfg *ent;
541 if (!reflog_expire_cfg_tail)
542 reflog_expire_cfg_tail = &reflog_expire_cfg;
544 for (ent = reflog_expire_cfg; ent; ent = ent->next)
545 if (ent->len == len &&
546 !memcmp(ent->pattern, pattern, len))
547 return ent;
549 ent = xcalloc(1, (sizeof(*ent) + len));
550 memcpy(ent->pattern, pattern, len);
551 ent->len = len;
552 *reflog_expire_cfg_tail = ent;
553 reflog_expire_cfg_tail = &(ent->next);
554 return ent;
557 static int parse_expire_cfg_value(const char *var, const char *value, unsigned long *expire)
559 if (!value)
560 return config_error_nonbool(var);
561 if (parse_expiry_date(value, expire))
562 return error(_("%s' for '%s' is not a valid timestamp"),
563 value, var);
564 return 0;
567 /* expiry timer slot */
568 #define EXPIRE_TOTAL 01
569 #define EXPIRE_UNREACH 02
571 static int reflog_expire_config(const char *var, const char *value, void *cb)
573 const char *pattern, *key;
574 int pattern_len;
575 unsigned long expire;
576 int slot;
577 struct reflog_expire_cfg *ent;
579 if (parse_config_key(var, "gc", &pattern, &pattern_len, &key) < 0)
580 return git_default_config(var, value, cb);
582 if (!strcmp(key, "reflogexpire")) {
583 slot = EXPIRE_TOTAL;
584 if (parse_expire_cfg_value(var, value, &expire))
585 return -1;
586 } else if (!strcmp(key, "reflogexpireunreachable")) {
587 slot = EXPIRE_UNREACH;
588 if (parse_expire_cfg_value(var, value, &expire))
589 return -1;
590 } else
591 return git_default_config(var, value, cb);
593 if (!pattern) {
594 switch (slot) {
595 case EXPIRE_TOTAL:
596 default_reflog_expire = expire;
597 break;
598 case EXPIRE_UNREACH:
599 default_reflog_expire_unreachable = expire;
600 break;
602 return 0;
605 ent = find_cfg_ent(pattern, pattern_len);
606 if (!ent)
607 return -1;
608 switch (slot) {
609 case EXPIRE_TOTAL:
610 ent->expire_total = expire;
611 break;
612 case EXPIRE_UNREACH:
613 ent->expire_unreachable = expire;
614 break;
616 return 0;
619 static void set_reflog_expiry_param(struct cmd_reflog_expire_cb *cb, int slot, const char *ref)
621 struct reflog_expire_cfg *ent;
623 if (slot == (EXPIRE_TOTAL|EXPIRE_UNREACH))
624 return; /* both given explicitly -- nothing to tweak */
626 for (ent = reflog_expire_cfg; ent; ent = ent->next) {
627 if (!wildmatch(ent->pattern, ref, 0, NULL)) {
628 if (!(slot & EXPIRE_TOTAL))
629 cb->expire_total = ent->expire_total;
630 if (!(slot & EXPIRE_UNREACH))
631 cb->expire_unreachable = ent->expire_unreachable;
632 return;
637 * If unconfigured, make stash never expire
639 if (!strcmp(ref, "refs/stash")) {
640 if (!(slot & EXPIRE_TOTAL))
641 cb->expire_total = 0;
642 if (!(slot & EXPIRE_UNREACH))
643 cb->expire_unreachable = 0;
644 return;
647 /* Nothing matched -- use the default value */
648 if (!(slot & EXPIRE_TOTAL))
649 cb->expire_total = default_reflog_expire;
650 if (!(slot & EXPIRE_UNREACH))
651 cb->expire_unreachable = default_reflog_expire_unreachable;
654 static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
656 struct cmd_reflog_expire_cb cb;
657 unsigned long now = time(NULL);
658 int i, status, do_all;
659 int explicit_expiry = 0;
660 unsigned int flags = 0;
662 default_reflog_expire_unreachable = now - 30 * 24 * 3600;
663 default_reflog_expire = now - 90 * 24 * 3600;
664 git_config(reflog_expire_config, NULL);
666 save_commit_buffer = 0;
667 do_all = status = 0;
668 memset(&cb, 0, sizeof(cb));
670 cb.expire_total = default_reflog_expire;
671 cb.expire_unreachable = default_reflog_expire_unreachable;
673 for (i = 1; i < argc; i++) {
674 const char *arg = argv[i];
675 if (!strcmp(arg, "--dry-run") || !strcmp(arg, "-n"))
676 flags |= EXPIRE_REFLOGS_DRY_RUN;
677 else if (starts_with(arg, "--expire=")) {
678 if (parse_expiry_date(arg + 9, &cb.expire_total))
679 die(_("'%s' is not a valid timestamp"), arg);
680 explicit_expiry |= EXPIRE_TOTAL;
682 else if (starts_with(arg, "--expire-unreachable=")) {
683 if (parse_expiry_date(arg + 21, &cb.expire_unreachable))
684 die(_("'%s' is not a valid timestamp"), arg);
685 explicit_expiry |= EXPIRE_UNREACH;
687 else if (!strcmp(arg, "--stale-fix"))
688 cb.stalefix = 1;
689 else if (!strcmp(arg, "--rewrite"))
690 flags |= EXPIRE_REFLOGS_REWRITE;
691 else if (!strcmp(arg, "--updateref"))
692 flags |= EXPIRE_REFLOGS_UPDATE_REF;
693 else if (!strcmp(arg, "--all"))
694 do_all = 1;
695 else if (!strcmp(arg, "--verbose"))
696 flags |= EXPIRE_REFLOGS_VERBOSE;
697 else if (!strcmp(arg, "--")) {
698 i++;
699 break;
701 else if (arg[0] == '-')
702 usage(reflog_expire_usage);
703 else
704 break;
708 * We can trust the commits and objects reachable from refs
709 * even in older repository. We cannot trust what's reachable
710 * from reflog if the repository was pruned with older git.
712 if (cb.stalefix) {
713 init_revisions(&cb.revs, prefix);
714 if (flags & EXPIRE_REFLOGS_VERBOSE)
715 printf("Marking reachable objects...");
716 mark_reachable_objects(&cb.revs, 0, 0, NULL);
717 if (flags & EXPIRE_REFLOGS_VERBOSE)
718 putchar('\n');
721 if (do_all) {
722 struct collect_reflog_cb collected;
723 int i;
725 memset(&collected, 0, sizeof(collected));
726 for_each_reflog(collect_reflog, &collected);
727 for (i = 0; i < collected.nr; i++) {
728 struct collected_reflog *e = collected.e[i];
729 set_reflog_expiry_param(&cb, explicit_expiry, e->reflog);
730 status |= expire_reflog(e->reflog, e->sha1, flags, &cb);
731 free(e);
733 free(collected.e);
736 for (; i < argc; i++) {
737 char *ref;
738 unsigned char sha1[20];
739 if (!dwim_log(argv[i], strlen(argv[i]), sha1, &ref)) {
740 status |= error("%s points nowhere!", argv[i]);
741 continue;
743 set_reflog_expiry_param(&cb, explicit_expiry, ref);
744 status |= expire_reflog(ref, sha1, flags, &cb);
746 return status;
749 static int count_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
750 const char *email, unsigned long timestamp, int tz,
751 const char *message, void *cb_data)
753 struct cmd_reflog_expire_cb *cb = cb_data;
754 if (!cb->expire_total || timestamp < cb->expire_total)
755 cb->recno++;
756 return 0;
759 static int cmd_reflog_delete(int argc, const char **argv, const char *prefix)
761 struct cmd_reflog_expire_cb cb;
762 int i, status = 0;
763 unsigned int flags = 0;
765 memset(&cb, 0, sizeof(cb));
767 for (i = 1; i < argc; i++) {
768 const char *arg = argv[i];
769 if (!strcmp(arg, "--dry-run") || !strcmp(arg, "-n"))
770 flags |= EXPIRE_REFLOGS_DRY_RUN;
771 else if (!strcmp(arg, "--rewrite"))
772 flags |= EXPIRE_REFLOGS_REWRITE;
773 else if (!strcmp(arg, "--updateref"))
774 flags |= EXPIRE_REFLOGS_UPDATE_REF;
775 else if (!strcmp(arg, "--verbose"))
776 flags |= EXPIRE_REFLOGS_VERBOSE;
777 else if (!strcmp(arg, "--")) {
778 i++;
779 break;
781 else if (arg[0] == '-')
782 usage(reflog_delete_usage);
783 else
784 break;
787 if (argc - i < 1)
788 return error("Nothing to delete?");
790 for ( ; i < argc; i++) {
791 const char *spec = strstr(argv[i], "@{");
792 unsigned char sha1[20];
793 char *ep, *ref;
794 int recno;
796 if (!spec) {
797 status |= error("Not a reflog: %s", argv[i]);
798 continue;
801 if (!dwim_log(argv[i], spec - argv[i], sha1, &ref)) {
802 status |= error("no reflog for '%s'", argv[i]);
803 continue;
806 recno = strtoul(spec + 2, &ep, 10);
807 if (*ep == '}') {
808 cb.recno = -recno;
809 for_each_reflog_ent(ref, count_reflog_ent, &cb);
810 } else {
811 cb.expire_total = approxidate(spec + 2);
812 for_each_reflog_ent(ref, count_reflog_ent, &cb);
813 cb.expire_total = 0;
816 status |= expire_reflog(ref, sha1, flags, &cb);
817 free(ref);
819 return status;
823 * main "reflog"
826 static const char reflog_usage[] =
827 "git reflog [ show | expire | delete ]";
829 int cmd_reflog(int argc, const char **argv, const char *prefix)
831 if (argc > 1 && !strcmp(argv[1], "-h"))
832 usage(reflog_usage);
834 /* With no command, we default to showing it. */
835 if (argc < 2 || *argv[1] == '-')
836 return cmd_log_reflog(argc, argv, prefix);
838 if (!strcmp(argv[1], "show"))
839 return cmd_log_reflog(argc - 1, argv + 1, prefix);
841 if (!strcmp(argv[1], "expire"))
842 return cmd_reflog_expire(argc - 1, argv + 1, prefix);
844 if (!strcmp(argv[1], "delete"))
845 return cmd_reflog_delete(argc - 1, argv + 1, prefix);
847 return cmd_log_reflog(argc, argv, prefix);