Skip to content

Commit 55f1056

Browse files
drafnelgitster
authored andcommitted
git-reflog: add option --updateref to write the last reflog sha1 into the ref
Certain sanity checks on the reflog assume that the sha1 of the top reflog entry will be equal to the sha1 stored in the ref. 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 update the ref with the sha1 of the top-most reflog entry. Signed-off-by: Brandon Casey <casey@nrlssc.navy.mil> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 435fc85 commit 55f1056

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

builtin-reflog.c

Lines changed: 16 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] [--rewrite] <refs>...";
18+
"git-reflog delete [--verbose] [--dry-run] [--rewrite] [--updateref] <refs>...";
1919

2020
static unsigned long default_reflog_expire;
2121
static unsigned long default_reflog_expire_unreachable;
@@ -25,6 +25,7 @@ struct cmd_reflog_expire_cb {
2525
int dry_run;
2626
int stalefix;
2727
int rewrite;
28+
int updateref;
2829
int verbose;
2930
unsigned long expire_total;
3031
unsigned long expire_unreachable;
@@ -292,10 +293,20 @@ static int expire_reflog(const char *ref, const unsigned char *sha1, int unused,
292293
status |= error("%s: %s", strerror(errno),
293294
newlog_path);
294295
unlink(newlog_path);
296+
} else if (cmd->updateref &&
297+
(write_in_full(lock->lock_fd,
298+
sha1_to_hex(cb.last_kept_sha1), 40) != 40 ||
299+
write_in_full(lock->lock_fd, "\n", 1) != 1 ||
300+
close_ref(lock) < 0)) {
301+
status |= error("Couldn't write %s",
302+
lock->lk->filename);
303+
unlink(newlog_path);
295304
} else if (rename(newlog_path, log_file)) {
296305
status |= error("cannot rename %s to %s",
297306
newlog_path, log_file);
298307
unlink(newlog_path);
308+
} else if (cmd->updateref && commit_ref(lock)) {
309+
status |= error("Couldn't set %s", lock->ref_name);
299310
}
300311
}
301312
free(newlog_path);
@@ -372,6 +383,8 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
372383
cb.stalefix = 1;
373384
else if (!strcmp(arg, "--rewrite"))
374385
cb.rewrite = 1;
386+
else if (!strcmp(arg, "--updateref"))
387+
cb.updateref = 1;
375388
else if (!strcmp(arg, "--all"))
376389
do_all = 1;
377390
else if (!strcmp(arg, "--verbose"))
@@ -443,6 +456,8 @@ static int cmd_reflog_delete(int argc, const char **argv, const char *prefix)
443456
cb.dry_run = 1;
444457
else if (!strcmp(arg, "--rewrite"))
445458
cb.rewrite = 1;
459+
else if (!strcmp(arg, "--updateref"))
460+
cb.updateref = 1;
446461
else if (!strcmp(arg, "--verbose"))
447462
cb.verbose = 1;
448463
else if (!strcmp(arg, "--")) {

0 commit comments

Comments
 (0)