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