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