@matheusbsbs / matheus-bagliano / commits / 55f1056537
Fork 0

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>

Brandon Casey committed Feb 22, 2008 at 13:04 UTC 55f105653715356fe0b2392445a6f394b891402c
1 file changed +16 -1
builtin-reflog.c
+16 -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] [--rewrite] <refs>...";
18 +"git-reflog delete [--verbose] [--dry-run] [--rewrite] [--updateref] <refs>...";
19
20 static unsigned long default_reflog_expire;
21 static unsigned long default_reflog_expire_unreachable;
@@ -25,6 +25,7 @@ struct cmd_reflog_expire_cb {
25 int dry_run;
26 int stalefix;
27 int rewrite;
28 + int updateref;
29 int verbose;
30 unsigned long expire_total;
31 unsigned long expire_unreachable;
@@ -292,10 +293,20 @@ static int expire_reflog(const char *ref, const unsigned char *sha1, int unused,
293 status |= error("%s: %s", strerror(errno),
294 newlog_path);
295 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);
304 } else if (rename(newlog_path, log_file)) {
305 status |= error("cannot rename %s to %s",
306 newlog_path, log_file);
307 unlink(newlog_path);
308 + } else if (cmd->updateref && commit_ref(lock)) {
309 + status |= error("Couldn't set %s", lock->ref_name);
310 }
311 }
312 free(newlog_path);
@@ -372,6 +383,8 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
383 cb.stalefix = 1;
384 else if (!strcmp(arg, "--rewrite"))
385 cb.rewrite = 1;
386 + else if (!strcmp(arg, "--updateref"))
387 + cb.updateref = 1;
388 else if (!strcmp(arg, "--all"))
389 do_all = 1;
390 else if (!strcmp(arg, "--verbose"))
@@ -443,6 +456,8 @@ static int cmd_reflog_delete(int argc, const char **argv, const char *prefix)
456 cb.dry_run = 1;
457 else if (!strcmp(arg, "--rewrite"))
458 cb.rewrite = 1;
459 + else if (!strcmp(arg, "--updateref"))
460 + cb.updateref = 1;
461 else if (!strcmp(arg, "--verbose"))
462 cb.verbose = 1;
463 else if (!strcmp(arg, "--")) {