Skip to content

Commit 2b81fab

Browse files
drafnelgitster
authored andcommitted
git-reflog: add option --rewrite to update reflog entries while expiring
Certain sanity checks on the reflog assume that each entry will contain a reference to the previous entry. i.e. that the "old" sha1 field of a reflog entry will be equal to the "new" sha1 field of the previous entry. When reflog entries are deleted, this assumption may not hold. This patch adds a new option to git-reflog which causes the subcommands "expire" and "delete" to rewrite the "old" sha1 field of each reflog entry so that it points to the previous reflog entry. Signed-off-by: Brandon Casey <casey@nrlssc.navy.mil> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 3c386aa commit 2b81fab

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

builtin-reflog.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
static const char reflog_expire_usage[] =
1616
"git-reflog (show|expire) [--verbose] [--dry-run] [--stale-fix] [--expire=<time>] [--expire-unreachable=<time>] [--all] <refs>...";
1717
static const char reflog_delete_usage[] =
18-
"git-reflog delete [--verbose] [--dry-run] <refs>...";
18+
"git-reflog delete [--verbose] [--dry-run] [--rewrite] <refs>...";
1919

2020
static unsigned long default_reflog_expire;
2121
static unsigned long default_reflog_expire_unreachable;
@@ -24,6 +24,7 @@ struct cmd_reflog_expire_cb {
2424
struct rev_info revs;
2525
int dry_run;
2626
int stalefix;
27+
int rewrite;
2728
int verbose;
2829
unsigned long expire_total;
2930
unsigned long expire_unreachable;
@@ -35,6 +36,7 @@ struct expire_reflog_cb {
3536
const char *ref;
3637
struct commit *ref_commit;
3738
struct cmd_reflog_expire_cb *cmd;
39+
unsigned char last_kept_sha1[20];
3840
};
3941

4042
struct collected_reflog {
@@ -216,6 +218,9 @@ static int expire_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
216218
if (timestamp < cb->cmd->expire_total)
217219
goto prune;
218220

221+
if (cb->cmd->rewrite)
222+
osha1 = cb->last_kept_sha1;
223+
219224
old = new = NULL;
220225
if (cb->cmd->stalefix &&
221226
(!keep_entry(&old, osha1) || !keep_entry(&new, nsha1)))
@@ -243,6 +248,7 @@ static int expire_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
243248
sha1_to_hex(osha1), sha1_to_hex(nsha1),
244249
email, timestamp, sign, zone,
245250
message);
251+
hashcpy(cb->last_kept_sha1, nsha1);
246252
}
247253
if (cb->cmd->verbose)
248254
printf("keep %s", message);
@@ -364,6 +370,8 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
364370
cb.expire_unreachable = approxidate(arg + 21);
365371
else if (!strcmp(arg, "--stale-fix"))
366372
cb.stalefix = 1;
373+
else if (!strcmp(arg, "--rewrite"))
374+
cb.rewrite = 1;
367375
else if (!strcmp(arg, "--all"))
368376
do_all = 1;
369377
else if (!strcmp(arg, "--verbose"))
@@ -433,6 +441,8 @@ static int cmd_reflog_delete(int argc, const char **argv, const char *prefix)
433441
const char *arg = argv[i];
434442
if (!strcmp(arg, "--dry-run") || !strcmp(arg, "-n"))
435443
cb.dry_run = 1;
444+
else if (!strcmp(arg, "--rewrite"))
445+
cb.rewrite = 1;
436446
else if (!strcmp(arg, "--verbose"))
437447
cb.verbose = 1;
438448
else if (!strcmp(arg, "--")) {

0 commit comments

Comments
 (0)