@matheusbsbs / matheus-bagliano / commits / 2b81fab288
Fork 0

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>

Brandon Casey committed Feb 22, 2008 at 12:56 UTC 2b81fab2885256501d0bba6e16e516addb667bf3
1 file changed +11 -1
builtin-reflog.c
+11 -1
@@ -15,7 +15,7 @@
15 static const char reflog_expire_usage[] =
16 "git-reflog (show|expire) [--verbose] [--dry-run] [--stale-fix] [--expire=<time>] [--expire-unreachable=<time>] [--all] <refs>...";
17 static const char reflog_delete_usage[] =
18 -"git-reflog delete [--verbose] [--dry-run] <refs>...";
18 +"git-reflog delete [--verbose] [--dry-run] [--rewrite] <refs>...";
19
20 static unsigned long default_reflog_expire;
21 static unsigned long default_reflog_expire_unreachable;
@@ -24,6 +24,7 @@ struct cmd_reflog_expire_cb {
24 struct rev_info revs;
25 int dry_run;
26 int stalefix;
27 + int rewrite;
28 int verbose;
29 unsigned long expire_total;
30 unsigned long expire_unreachable;
@@ -35,6 +36,7 @@ struct expire_reflog_cb {
36 const char *ref;
37 struct commit *ref_commit;
38 struct cmd_reflog_expire_cb *cmd;
39 + unsigned char last_kept_sha1[20];
40 };
41
42 struct collected_reflog {
@@ -216,6 +218,9 @@ static int expire_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
218 if (timestamp < cb->cmd->expire_total)
219 goto prune;
220
221 + if (cb->cmd->rewrite)
222 + osha1 = cb->last_kept_sha1;
223 +
224 old = new = NULL;
225 if (cb->cmd->stalefix &&
226 (!keep_entry(&old, osha1) || !keep_entry(&new, nsha1)))
@@ -243,6 +248,7 @@ static int expire_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
248 sha1_to_hex(osha1), sha1_to_hex(nsha1),
249 email, timestamp, sign, zone,
250 message);
251 + hashcpy(cb->last_kept_sha1, nsha1);
252 }
253 if (cb->cmd->verbose)
254 printf("keep %s", message);
@@ -364,6 +370,8 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
370 cb.expire_unreachable = approxidate(arg + 21);
371 else if (!strcmp(arg, "--stale-fix"))
372 cb.stalefix = 1;
373 + else if (!strcmp(arg, "--rewrite"))
374 + cb.rewrite = 1;
375 else if (!strcmp(arg, "--all"))
376 do_all = 1;
377 else if (!strcmp(arg, "--verbose"))
@@ -433,6 +441,8 @@ static int cmd_reflog_delete(int argc, const char **argv, const char *prefix)
441 const char *arg = argv[i];
442 if (!strcmp(arg, "--dry-run") || !strcmp(arg, "-n"))
443 cb.dry_run = 1;
444 + else if (!strcmp(arg, "--rewrite"))
445 + cb.rewrite = 1;
446 else if (!strcmp(arg, "--verbose"))
447 cb.verbose = 1;
448 else if (!strcmp(arg, "--")) {