object_array: add and use `object_array_pop()`
[git/git.git] / builtin / reflog.c
CommitLineData
4264dc15 1#include "builtin.h"
b2141fc1 2#include "config.h"
697cc8ef 3#include "lockfile.h"
4264dc15
JH
4#include "commit.h"
5#include "refs.h"
6#include "dir.h"
8d8b9f62 7#include "tree-walk.h"
1389d9dd
JH
8#include "diff.h"
9#include "revision.h"
10#include "reachable.h"
11
fe2a1816 12/* NEEDSWORK: switch to using parse_options */
1389d9dd 13static const char reflog_expire_usage[] =
fe2a1816 14"git reflog expire [--expire=<time>] [--expire-unreachable=<time>] [--rewrite] [--updateref] [--stale-fix] [--dry-run | -n] [--verbose] [--all] <refs>...";
3c386aa3 15static const char reflog_delete_usage[] =
fe2a1816 16"git reflog delete [--rewrite] [--updateref] [--dry-run | -n] [--verbose] <refs>...";
afcb2e7a
DT
17static const char reflog_exists_usage[] =
18"git reflog exists <ref>";
4264dc15 19
dddbad72
JS
20static timestamp_t default_reflog_expire;
21static timestamp_t default_reflog_expire_unreachable;
4aec56d1 22
1389d9dd
JH
23struct cmd_reflog_expire_cb {
24 struct rev_info revs;
1389d9dd 25 int stalefix;
dddbad72
JS
26 timestamp_t expire_total;
27 timestamp_t expire_unreachable;
552cecc2 28 int recno;
1389d9dd
JH
29};
30
ea7b4f6d 31struct expire_reflog_policy_cb {
03cb91b1
JH
32 enum {
33 UE_NORMAL,
34 UE_ALWAYS,
35 UE_HEAD
36 } unreachable_expire_kind;
b4ca1db9
JH
37 struct commit_list *mark_list;
38 unsigned long mark_limit;
b729effb 39 struct cmd_reflog_expire_cb cmd;
c48a1635
MH
40 struct commit *tip_commit;
41 struct commit_list *tips;
4264dc15
JH
42};
43
bda3a31c
JH
44struct collected_reflog {
45 unsigned char sha1[20];
46 char reflog[FLEX_ARRAY];
47};
ddd64c56 48
bda3a31c
JH
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
49a09e74 59static int tree_is_complete(const struct object_id *oid)
8d8b9f62
JH
60{
61 struct tree_desc desc;
cd1f9c36
JH
62 struct name_entry entry;
63 int complete;
64 struct tree *tree;
8d8b9f62 65
740ee055 66 tree = lookup_tree(oid);
cd1f9c36 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 76 unsigned long size;
49a09e74 77 void *data = read_sha1_file(oid->hash, &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)) {
7d924c91 88 if (!has_sha1_file(entry.oid->hash) ||
49a09e74 89 (S_ISDIR(entry.mode) && !tree_is_complete(entry.oid))) {
cd1f9c36
JH
90 tree->object.flags |= INCOMPLETE;
91 complete = 0;
92 }
93 }
6e454b9a 94 free_tree_buffer(tree);
8d8b9f62 95
cd1f9c36
JH
96 if (complete)
97 tree->object.flags |= SEEN;
98 return complete;
99}
1389d9dd
JH
100
101static int commit_is_complete(struct commit *commit)
102{
103 struct object_array study;
104 struct object_array found;
105 int is_incomplete = 0;
106 int i;
107
108 /* early return */
109 if (commit->object.flags & SEEN)
110 return 1;
111 if (commit->object.flags & INCOMPLETE)
112 return 0;
113 /*
114 * Find all commits that are reachable and are not marked as
115 * SEEN. Then make sure the trees and blobs contained are
116 * complete. After that, mark these commits also as SEEN.
117 * If some of the objects that are needed to complete this
118 * commit are missing, mark this commit as INCOMPLETE.
119 */
120 memset(&study, 0, sizeof(study));
121 memset(&found, 0, sizeof(found));
122 add_object_array(&commit->object, NULL, &study);
123 add_object_array(&commit->object, NULL, &found);
124 commit->object.flags |= STUDYING;
125 while (study.nr) {
126 struct commit *c;
127 struct commit_list *parent;
128
71992039 129 c = (struct commit *)object_array_pop(&study);
c251c83d 130 if (!c->object.parsed && !parse_object(&c->object.oid))
1389d9dd
JH
131 c->object.flags |= INCOMPLETE;
132
133 if (c->object.flags & INCOMPLETE) {
134 is_incomplete = 1;
135 break;
136 }
137 else if (c->object.flags & SEEN)
138 continue;
139 for (parent = c->parents; parent; parent = parent->next) {
140 struct commit *p = parent->item;
141 if (p->object.flags & STUDYING)
142 continue;
143 p->object.flags |= STUDYING;
144 add_object_array(&p->object, NULL, &study);
145 add_object_array(&p->object, NULL, &found);
146 }
147 }
148 if (!is_incomplete) {
cd1f9c36
JH
149 /*
150 * make sure all commits in "found" array have all the
1389d9dd
JH
151 * necessary objects.
152 */
cd1f9c36 153 for (i = 0; i < found.nr; i++) {
1389d9dd
JH
154 struct commit *c =
155 (struct commit *)found.objects[i].item;
49a09e74 156 if (!tree_is_complete(&c->tree->object.oid)) {
1389d9dd 157 is_incomplete = 1;
cd1f9c36
JH
158 c->object.flags |= INCOMPLETE;
159 }
1389d9dd
JH
160 }
161 if (!is_incomplete) {
162 /* mark all found commits as complete, iow SEEN */
163 for (i = 0; i < found.nr; i++)
164 found.objects[i].item->flags |= SEEN;
165 }
166 }
167 /* clear flags from the objects we traversed */
168 for (i = 0; i < found.nr; i++)
169 found.objects[i].item->flags &= ~STUDYING;
170 if (is_incomplete)
171 commit->object.flags |= INCOMPLETE;
cd1f9c36
JH
172 else {
173 /*
174 * If we come here, we have (1) traversed the ancestry chain
175 * from the "commit" until we reach SEEN commits (which are
176 * known to be complete), and (2) made sure that the commits
177 * encountered during the above traversal refer to trees that
178 * are complete. Which means that we know *all* the commits
179 * we have seen during this process are complete.
180 */
181 for (i = 0; i < found.nr; i++)
182 found.objects[i].item->flags |= SEEN;
183 }
1389d9dd 184 /* free object arrays */
dcb572ab
185 object_array_clear(&study);
186 object_array_clear(&found);
1389d9dd
JH
187 return !is_incomplete;
188}
189
4322478a 190static int keep_entry(struct commit **it, struct object_id *oid)
4264dc15 191{
8d8b9f62
JH
192 struct commit *commit;
193
4322478a 194 if (is_null_oid(oid))
4264dc15 195 return 1;
bc83266a 196 commit = lookup_commit_reference_gently(oid, 1);
8d8b9f62
JH
197 if (!commit)
198 return 0;
199
1389d9dd
JH
200 /*
201 * Make sure everything in this commit exists.
202 *
203 * We have walked all the objects reachable from the refs
204 * and cache earlier. The commits reachable by this commit
205 * must meet SEEN commits -- and then we should mark them as
206 * SEEN as well.
207 */
208 if (!commit_is_complete(commit))
8d8b9f62
JH
209 return 0;
210 *it = commit;
211 return 1;
4264dc15
JH
212}
213
b4ca1db9
JH
214/*
215 * Starting from commits in the cb->mark_list, mark commits that are
216 * reachable from them. Stop the traversal at commits older than
217 * the expire_limit and queue them back, so that the caller can call
218 * us again to restart the traversal with longer expire_limit.
219 */
ea7b4f6d 220static void mark_reachable(struct expire_reflog_policy_cb *cb)
666e07e6 221{
b4ca1db9 222 struct commit_list *pending;
dddbad72 223 timestamp_t expire_limit = cb->mark_limit;
b4ca1db9 224 struct commit_list *leftover = NULL;
666e07e6 225
b4ca1db9
JH
226 for (pending = cb->mark_list; pending; pending = pending->next)
227 pending->item->object.flags &= ~REACHABLE;
494fbfe8 228
b4ca1db9 229 pending = cb->mark_list;
494fbfe8 230 while (pending) {
494fbfe8 231 struct commit_list *parent;
e510ab89 232 struct commit *commit = pop_commit(&pending);
494fbfe8
JH
233 if (commit->object.flags & REACHABLE)
234 continue;
235 if (parse_commit(commit))
236 continue;
237 commit->object.flags |= REACHABLE;
b4ca1db9
JH
238 if (commit->date < expire_limit) {
239 commit_list_insert(commit, &leftover);
494fbfe8 240 continue;
b4ca1db9
JH
241 }
242 commit->object.flags |= REACHABLE;
494fbfe8
JH
243 parent = commit->parents;
244 while (parent) {
245 commit = parent->item;
246 parent = parent->next;
247 if (commit->object.flags & REACHABLE)
248 continue;
249 commit_list_insert(commit, &pending);
250 }
251 }
b4ca1db9
JH
252 cb->mark_list = leftover;
253}
254
4322478a 255static int unreachable(struct expire_reflog_policy_cb *cb, struct commit *commit, struct object_id *oid)
b4ca1db9
JH
256{
257 /*
258 * We may or may not have the commit yet - if not, look it
259 * up using the supplied sha1.
260 */
261 if (!commit) {
4322478a 262 if (is_null_oid(oid))
b4ca1db9
JH
263 return 0;
264
bc83266a 265 commit = lookup_commit_reference_gently(oid, 1);
b4ca1db9
JH
266
267 /* Not a commit -- keep it */
268 if (!commit)
269 return 0;
270 }
271
272 /* Reachable from the current ref? Don't prune. */
273 if (commit->object.flags & REACHABLE)
274 return 0;
275
276 if (cb->mark_list && cb->mark_limit) {
277 cb->mark_limit = 0; /* dig down to the root */
278 mark_reachable(cb);
279 }
280
281 return !(commit->object.flags & REACHABLE);
494fbfe8
JH
282}
283
60cc3c40
MH
284/*
285 * Return true iff the specified reflog entry should be expired.
286 */
4322478a 287static int should_expire_reflog_ent(struct object_id *ooid, struct object_id *noid,
dddbad72 288 const char *email, timestamp_t timestamp, int tz,
60cc3c40 289 const char *message, void *cb_data)
4264dc15 290{
ea7b4f6d 291 struct expire_reflog_policy_cb *cb = cb_data;
4264dc15
JH
292 struct commit *old, *new;
293
b729effb 294 if (timestamp < cb->cmd.expire_total)
60cc3c40 295 return 1;
2b81fab2 296
9bbaa6cc 297 old = new = NULL;
b729effb 298 if (cb->cmd.stalefix &&
4322478a 299 (!keep_entry(&old, ooid) || !keep_entry(&new, noid)))
60cc3c40 300 return 1;
4264dc15 301
b729effb 302 if (timestamp < cb->cmd.expire_unreachable) {
03cb91b1 303 if (cb->unreachable_expire_kind == UE_ALWAYS)
60cc3c40 304 return 1;
4322478a 305 if (unreachable(cb, old, ooid) || unreachable(cb, new, noid))
60cc3c40 306 return 1;
9bbaa6cc 307 }
4264dc15 308
b729effb 309 if (cb->cmd.recno && --(cb->cmd.recno) == 0)
60cc3c40
MH
310 return 1;
311
4264dc15 312 return 0;
60cc3c40
MH
313}
314
5bcad1bc 315static int push_tip_to_list(const char *refname, const struct object_id *oid,
ea7b4f6d 316 int flags, void *cb_data)
03cb91b1
JH
317{
318 struct commit_list **list = cb_data;
319 struct commit *tip_commit;
320 if (flags & REF_ISSYMREF)
321 return 0;
bc83266a 322 tip_commit = lookup_commit_reference_gently(oid, 1);
03cb91b1
JH
323 if (!tip_commit)
324 return 0;
325 commit_list_insert(tip_commit, list);
326 return 0;
327}
328
c48a1635 329static void reflog_expiry_prepare(const char *refname,
4322478a 330 const struct object_id *oid,
b729effb 331 void *cb_data)
c48a1635 332{
b729effb
MH
333 struct expire_reflog_policy_cb *cb = cb_data;
334
335 if (!cb->cmd.expire_unreachable || !strcmp(refname, "HEAD")) {
c48a1635
MH
336 cb->tip_commit = NULL;
337 cb->unreachable_expire_kind = UE_HEAD;
338 } else {
bc83266a 339 cb->tip_commit = lookup_commit_reference_gently(oid, 1);
c48a1635
MH
340 if (!cb->tip_commit)
341 cb->unreachable_expire_kind = UE_ALWAYS;
342 else
343 cb->unreachable_expire_kind = UE_NORMAL;
344 }
345
b729effb 346 if (cb->cmd.expire_unreachable <= cb->cmd.expire_total)
c48a1635
MH
347 cb->unreachable_expire_kind = UE_ALWAYS;
348
349 cb->mark_list = NULL;
350 cb->tips = NULL;
351 if (cb->unreachable_expire_kind != UE_ALWAYS) {
352 if (cb->unreachable_expire_kind == UE_HEAD) {
353 struct commit_list *elem;
2b2a5be3 354
5bcad1bc 355 for_each_ref(push_tip_to_list, &cb->tips);
c48a1635
MH
356 for (elem = cb->tips; elem; elem = elem->next)
357 commit_list_insert(elem->item, &cb->mark_list);
358 } else {
359 commit_list_insert(cb->tip_commit, &cb->mark_list);
360 }
b729effb 361 cb->mark_limit = cb->cmd.expire_total;
c48a1635
MH
362 mark_reachable(cb);
363 }
364}
365
b729effb 366static void reflog_expiry_cleanup(void *cb_data)
c48a1635 367{
b729effb
MH
368 struct expire_reflog_policy_cb *cb = cb_data;
369
c48a1635
MH
370 if (cb->unreachable_expire_kind != UE_ALWAYS) {
371 if (cb->unreachable_expire_kind == UE_HEAD) {
372 struct commit_list *elem;
373 for (elem = cb->tips; elem; elem = elem->next)
374 clear_commit_marks(elem->item, REACHABLE);
375 free_commit_list(cb->tips);
376 } else {
377 clear_commit_marks(cb->tip_commit, REACHABLE);
378 }
379 }
380}
381
5bcad1bc 382static int collect_reflog(const char *ref, const struct object_id *oid, int unused, void *cb_data)
bda3a31c
JH
383{
384 struct collected_reflog *e;
385 struct collect_reflog_cb *cb = cb_data;
bda3a31c 386
96ffc06f 387 FLEX_ALLOC_STR(e, reflog, ref);
5bcad1bc 388 hashcpy(e->sha1, oid->hash);
bda3a31c
JH
389 ALLOC_GROW(cb->e, cb->nr + 1, cb->alloc);
390 cb->e[cb->nr++] = e;
391 return 0;
392}
393
3cb22b8e
JH
394static struct reflog_expire_cfg {
395 struct reflog_expire_cfg *next;
dddbad72
JS
396 timestamp_t expire_total;
397 timestamp_t expire_unreachable;
3cb22b8e
JH
398 char pattern[FLEX_ARRAY];
399} *reflog_expire_cfg, **reflog_expire_cfg_tail;
400
401static struct reflog_expire_cfg *find_cfg_ent(const char *pattern, size_t len)
4aec56d1 402{
3cb22b8e
JH
403 struct reflog_expire_cfg *ent;
404
405 if (!reflog_expire_cfg_tail)
406 reflog_expire_cfg_tail = &reflog_expire_cfg;
407
408 for (ent = reflog_expire_cfg; ent; ent = ent->next)
c3a700fb
JK
409 if (!strncmp(ent->pattern, pattern, len) &&
410 ent->pattern[len] == '\0')
3cb22b8e
JH
411 return ent;
412
96ffc06f 413 FLEX_ALLOC_MEM(ent, pattern, pattern, len);
3cb22b8e
JH
414 *reflog_expire_cfg_tail = ent;
415 reflog_expire_cfg_tail = &(ent->next);
416 return ent;
417}
418
dddbad72 419static int parse_expire_cfg_value(const char *var, const char *value, timestamp_t *expire)
3cb22b8e
JH
420{
421 if (!value)
422 return config_error_nonbool(var);
3d27b9b0 423 if (parse_expiry_date(value, expire))
99885bc0 424 return error(_("'%s' for '%s' is not a valid timestamp"),
3d27b9b0 425 value, var);
3cb22b8e
JH
426 return 0;
427}
428
429/* expiry timer slot */
430#define EXPIRE_TOTAL 01
431#define EXPIRE_UNREACH 02
432
433static int reflog_expire_config(const char *var, const char *value, void *cb)
434{
b3873c33
JK
435 const char *pattern, *key;
436 int pattern_len;
dddbad72 437 timestamp_t expire;
3cb22b8e
JH
438 int slot;
439 struct reflog_expire_cfg *ent;
440
b3873c33 441 if (parse_config_key(var, "gc", &pattern, &pattern_len, &key) < 0)
3cb22b8e
JH
442 return git_default_config(var, value, cb);
443
b3873c33 444 if (!strcmp(key, "reflogexpire")) {
3cb22b8e
JH
445 slot = EXPIRE_TOTAL;
446 if (parse_expire_cfg_value(var, value, &expire))
447 return -1;
b3873c33 448 } else if (!strcmp(key, "reflogexpireunreachable")) {
3cb22b8e
JH
449 slot = EXPIRE_UNREACH;
450 if (parse_expire_cfg_value(var, value, &expire))
451 return -1;
452 } else
453 return git_default_config(var, value, cb);
454
b3873c33 455 if (!pattern) {
3cb22b8e
JH
456 switch (slot) {
457 case EXPIRE_TOTAL:
458 default_reflog_expire = expire;
459 break;
460 case EXPIRE_UNREACH:
461 default_reflog_expire_unreachable = expire;
462 break;
463 }
4f342b96
JH
464 return 0;
465 }
3cb22b8e 466
b3873c33 467 ent = find_cfg_ent(pattern, pattern_len);
3cb22b8e
JH
468 if (!ent)
469 return -1;
470 switch (slot) {
471 case EXPIRE_TOTAL:
472 ent->expire_total = expire;
473 break;
474 case EXPIRE_UNREACH:
475 ent->expire_unreachable = expire;
476 break;
477 }
478 return 0;
479}
480
481static void set_reflog_expiry_param(struct cmd_reflog_expire_cb *cb, int slot, const char *ref)
482{
483 struct reflog_expire_cfg *ent;
484
485 if (slot == (EXPIRE_TOTAL|EXPIRE_UNREACH))
486 return; /* both given explicitly -- nothing to tweak */
487
488 for (ent = reflog_expire_cfg; ent; ent = ent->next) {
55d34269 489 if (!wildmatch(ent->pattern, ref, 0)) {
3cb22b8e
JH
490 if (!(slot & EXPIRE_TOTAL))
491 cb->expire_total = ent->expire_total;
492 if (!(slot & EXPIRE_UNREACH))
493 cb->expire_unreachable = ent->expire_unreachable;
494 return;
495 }
496 }
497
60bce2bb
JH
498 /*
499 * If unconfigured, make stash never expire
500 */
501 if (!strcmp(ref, "refs/stash")) {
502 if (!(slot & EXPIRE_TOTAL))
503 cb->expire_total = 0;
504 if (!(slot & EXPIRE_UNREACH))
505 cb->expire_unreachable = 0;
506 return;
507 }
508
3cb22b8e
JH
509 /* Nothing matched -- use the default value */
510 if (!(slot & EXPIRE_TOTAL))
511 cb->expire_total = default_reflog_expire;
512 if (!(slot & EXPIRE_UNREACH))
513 cb->expire_unreachable = default_reflog_expire_unreachable;
4aec56d1
JH
514}
515
4264dc15
JH
516static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
517{
b729effb 518 struct expire_reflog_policy_cb cb;
dddbad72 519 timestamp_t now = time(NULL);
4264dc15 520 int i, status, do_all;
3cb22b8e 521 int explicit_expiry = 0;
aba56c89 522 unsigned int flags = 0;
4264dc15 523
4a9f4394
AS
524 default_reflog_expire_unreachable = now - 30 * 24 * 3600;
525 default_reflog_expire = now - 90 * 24 * 3600;
ef90d6d4 526 git_config(reflog_expire_config, NULL);
4aec56d1 527
4264dc15
JH
528 save_commit_buffer = 0;
529 do_all = status = 0;
530 memset(&cb, 0, sizeof(cb));
4aec56d1 531
b729effb
MH
532 cb.cmd.expire_total = default_reflog_expire;
533 cb.cmd.expire_unreachable = default_reflog_expire_unreachable;
4264dc15
JH
534
535 for (i = 1; i < argc; i++) {
536 const char *arg = argv[i];
537 if (!strcmp(arg, "--dry-run") || !strcmp(arg, "-n"))
98f31d85 538 flags |= EXPIRE_REFLOGS_DRY_RUN;
59556548 539 else if (starts_with(arg, "--expire=")) {
b729effb 540 if (parse_expiry_date(arg + 9, &cb.cmd.expire_total))
3d27b9b0 541 die(_("'%s' is not a valid timestamp"), arg);
3cb22b8e
JH
542 explicit_expiry |= EXPIRE_TOTAL;
543 }
59556548 544 else if (starts_with(arg, "--expire-unreachable=")) {
b729effb 545 if (parse_expiry_date(arg + 21, &cb.cmd.expire_unreachable))
3d27b9b0 546 die(_("'%s' is not a valid timestamp"), arg);
3cb22b8e
JH
547 explicit_expiry |= EXPIRE_UNREACH;
548 }
1389d9dd 549 else if (!strcmp(arg, "--stale-fix"))
b729effb 550 cb.cmd.stalefix = 1;
2b81fab2 551 else if (!strcmp(arg, "--rewrite"))
553daf13 552 flags |= EXPIRE_REFLOGS_REWRITE;
55f10565 553 else if (!strcmp(arg, "--updateref"))
c4c4fbf8 554 flags |= EXPIRE_REFLOGS_UPDATE_REF;
4264dc15
JH
555 else if (!strcmp(arg, "--all"))
556 do_all = 1;
1389d9dd 557 else if (!strcmp(arg, "--verbose"))
bc11155c 558 flags |= EXPIRE_REFLOGS_VERBOSE;
4264dc15
JH
559 else if (!strcmp(arg, "--")) {
560 i++;
561 break;
562 }
563 else if (arg[0] == '-')
564 usage(reflog_expire_usage);
565 else
566 break;
567 }
3cb22b8e
JH
568
569 /*
570 * We can trust the commits and objects reachable from refs
571 * even in older repository. We cannot trust what's reachable
572 * from reflog if the repository was pruned with older git.
573 */
b729effb
MH
574 if (cb.cmd.stalefix) {
575 init_revisions(&cb.cmd.revs, prefix);
bc11155c 576 if (flags & EXPIRE_REFLOGS_VERBOSE)
1389d9dd 577 printf("Marking reachable objects...");
b729effb 578 mark_reachable_objects(&cb.cmd.revs, 0, 0, NULL);
bc11155c 579 if (flags & EXPIRE_REFLOGS_VERBOSE)
1389d9dd
JH
580 putchar('\n');
581 }
582
bda3a31c
JH
583 if (do_all) {
584 struct collect_reflog_cb collected;
585 int i;
586
587 memset(&collected, 0, sizeof(collected));
5bcad1bc 588 for_each_reflog(collect_reflog, &collected);
bda3a31c
JH
589 for (i = 0; i < collected.nr; i++) {
590 struct collected_reflog *e = collected.e[i];
b729effb 591 set_reflog_expiry_param(&cb.cmd, explicit_expiry, e->reflog);
fa5b1830
MH
592 status |= reflog_expire(e->reflog, e->sha1, flags,
593 reflog_expiry_prepare,
594 should_expire_reflog_ent,
595 reflog_expiry_cleanup,
596 &cb);
bda3a31c
JH
597 free(e);
598 }
599 free(collected.e);
600 }
601
90fb46ec
PB
602 for (; i < argc; i++) {
603 char *ref;
4264dc15 604 unsigned char sha1[20];
90fb46ec
PB
605 if (!dwim_log(argv[i], strlen(argv[i]), sha1, &ref)) {
606 status |= error("%s points nowhere!", argv[i]);
4264dc15
JH
607 continue;
608 }
b729effb 609 set_reflog_expiry_param(&cb.cmd, explicit_expiry, ref);
fa5b1830
MH
610 status |= reflog_expire(ref, sha1, flags,
611 reflog_expiry_prepare,
612 should_expire_reflog_ent,
613 reflog_expiry_cleanup,
614 &cb);
4264dc15
JH
615 }
616 return status;
617}
618
9461d272 619static int count_reflog_ent(struct object_id *ooid, struct object_id *noid,
dddbad72 620 const char *email, timestamp_t timestamp, int tz,
552cecc2
JS
621 const char *message, void *cb_data)
622{
b729effb
MH
623 struct expire_reflog_policy_cb *cb = cb_data;
624 if (!cb->cmd.expire_total || timestamp < cb->cmd.expire_total)
625 cb->cmd.recno++;
552cecc2
JS
626 return 0;
627}
628
629static int cmd_reflog_delete(int argc, const char **argv, const char *prefix)
630{
b729effb 631 struct expire_reflog_policy_cb cb;
552cecc2 632 int i, status = 0;
aba56c89 633 unsigned int flags = 0;
552cecc2 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"))
98f31d85 640 flags |= EXPIRE_REFLOGS_DRY_RUN;
2b81fab2 641 else if (!strcmp(arg, "--rewrite"))
553daf13 642 flags |= EXPIRE_REFLOGS_REWRITE;
55f10565 643 else if (!strcmp(arg, "--updateref"))
c4c4fbf8 644 flags |= EXPIRE_REFLOGS_UPDATE_REF;
3c386aa3 645 else if (!strcmp(arg, "--verbose"))
bc11155c 646 flags |= EXPIRE_REFLOGS_VERBOSE;
3c386aa3
BC
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 == '}') {
b729effb 678 cb.cmd.recno = -recno;
552cecc2
JS
679 for_each_reflog_ent(ref, count_reflog_ent, &cb);
680 } else {
b729effb 681 cb.cmd.expire_total = approxidate(spec + 2);
552cecc2 682 for_each_reflog_ent(ref, count_reflog_ent, &cb);
b729effb 683 cb.cmd.expire_total = 0;
552cecc2
JS
684 }
685
fa5b1830
MH
686 status |= reflog_expire(ref, sha1, flags,
687 reflog_expiry_prepare,
688 should_expire_reflog_ent,
689 reflog_expiry_cleanup,
690 &cb);
552cecc2
JS
691 free(ref);
692 }
693 return status;
694}
695
afcb2e7a
DT
696static int cmd_reflog_exists(int argc, const char **argv, const char *prefix)
697{
698 int i, start = 0;
699
700 for (i = 1; i < argc; i++) {
701 const char *arg = argv[i];
702 if (!strcmp(arg, "--")) {
703 i++;
704 break;
705 }
706 else if (arg[0] == '-')
707 usage(reflog_exists_usage);
708 else
709 break;
710 }
711
712 start = i;
713
714 if (argc - start != 1)
715 usage(reflog_exists_usage);
716
717 if (check_refname_format(argv[start], REFNAME_ALLOW_ONELEVEL))
718 die("invalid ref format: %s", argv[start]);
719 return !reflog_exists(argv[start]);
720}
721
1389d9dd
JH
722/*
723 * main "reflog"
724 */
725
4264dc15 726static const char reflog_usage[] =
afcb2e7a 727"git reflog [ show | expire | delete | exists ]";
4264dc15
JH
728
729int cmd_reflog(int argc, const char **argv, const char *prefix)
730{
99caeed0
JN
731 if (argc > 1 && !strcmp(argv[1], "-h"))
732 usage(reflog_usage);
733
cf39f54e
LT
734 /* With no command, we default to showing it. */
735 if (argc < 2 || *argv[1] == '-')
736 return cmd_log_reflog(argc, argv, prefix);
737
738 if (!strcmp(argv[1], "show"))
739 return cmd_log_reflog(argc - 1, argv + 1, prefix);
740
741 if (!strcmp(argv[1], "expire"))
4264dc15 742 return cmd_reflog_expire(argc - 1, argv + 1, prefix);
cf39f54e 743
552cecc2
JS
744 if (!strcmp(argv[1], "delete"))
745 return cmd_reflog_delete(argc - 1, argv + 1, prefix);
746
afcb2e7a
DT
747 if (!strcmp(argv[1], "exists"))
748 return cmd_reflog_exists(argc - 1, argv + 1, prefix);
749
bf01d4a3 750 return cmd_log_reflog(argc, argv, prefix);
4264dc15 751}