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