git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 3/4] git-reflog: add option --updateref to write the last reflog sha1 into the ref

From
Brandon Casey <casey@nrlssc.navy.mil>
Date
Feb 22, 2008, 19:04 UTC
Message-ID
<47BF1CAC.5000009@nrlssc.navy.mil>
In-Reply-To
<1203706541-21719-2-git-send-email-casey@nrlssc.navy.mil>

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>
---

This sucks in close_ref() and commit_ref() from refs.c. The changes to expire_reflog() implement some of what write_ref_sha1() does. I guess a new function in refs.c could have been made which instead of a logmsg takes an updated reflog filename, but that seemed too contrived. We're already modifying the reflog directly anyway, so maybe it's ok to modify the ref directly too.

-brandon
 builtin-reflog.c |   17 ++++++++++++++++-
 1 files changed, 16 insertions(+), 1 deletions(-)
diff --git a/builtin-reflog.c b/builtin-reflog.c
index f685d76..280e24e 100644
--- a/builtin-reflog.c
+++ b/builtin-reflog.c
@@ -15,7 +15,7 @@
 static const char reflog_expire_usage[] =
 "git-reflog (show|expire) [--verbose] [--dry-run] [--stale-fix] [--expire=<time>] [--expire-unreachable=<time>] [--all] <refs>...";
 static const char reflog_delete_usage[] =
-"git-reflog delete [--verbose] [--dry-run] [--rewrite] <refs>...";
+"git-reflog delete [--verbose] [--dry-run] [--rewrite] [--updateref] <refs>...";
 
 static unsigned long default_reflog_expire;
 static unsigned long default_reflog_expire_unreachable;
@@ -25,6 +25,7 @@ struct cmd_reflog_expire_cb {
 	int dry_run;
 	int stalefix;
 	int rewrite;
+	int updateref;
 	int verbose;
 	unsigned long expire_total;
 	unsigned long expire_unreachable;
@@ -292,10 +293,20 @@ static int expire_reflog(const char *ref, const unsigned char *sha1, int unused,
 			status |= error("%s: %s", strerror(errno),
 					newlog_path);
 			unlink(newlog_path);
+		} else if (cmd->updateref &&
+			(write_in_full(lock->lock_fd,
+				sha1_to_hex(cb.last_kept_sha1), 40) != 40 ||
+			 write_in_full(lock->lock_fd, "\n", 1) != 1 ||
+			 close_ref(lock) < 0)) {
+			status |= error("Couldn't write %s",
+				lock->lk->filename);
+			unlink(newlog_path);
 		} else if (rename(newlog_path, log_file)) {
 			status |= error("cannot rename %s to %s",
 					newlog_path, log_file);
 			unlink(newlog_path);
+		} else if (cmd->updateref && commit_ref(lock)) {
+			status |= error("Couldn't set %s", lock->ref_name);
 		}
 	}
 	free(newlog_path);
@@ -372,6 +383,8 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
 			cb.stalefix = 1;
 		else if (!strcmp(arg, "--rewrite"))
 			cb.rewrite = 1;
+		else if (!strcmp(arg, "--updateref"))
+			cb.updateref = 1;
 		else if (!strcmp(arg, "--all"))
 			do_all = 1;
 		else if (!strcmp(arg, "--verbose"))
@@ -443,6 +456,8 @@ static int cmd_reflog_delete(int argc, const char **argv, const char *prefix)
 			cb.dry_run = 1;
 		else if (!strcmp(arg, "--rewrite"))
 			cb.rewrite = 1;
+		else if (!strcmp(arg, "--updateref"))
+			cb.updateref = 1;
 		else if (!strcmp(arg, "--verbose"))
 			cb.verbose = 1;
 		else if (!strcmp(arg, "--")) {
-- 
1.5.4.2.267.g38ea
Previous: Brandon CaseyNext: Brandon Casey
Message 48 of 50 in “What's cooking in git.git (topics)”
  1. Junio C HamanoFeb 3, 2008
  2. Johannes SchindelinFeb 3, 2008
  3. Junio C HamanoFeb 5, 2008
  4. Jakub NarebskiFeb 5, 2008
  5. Junio C HamanoFeb 6, 2008
  6. Junio C HamanoFeb 7, 2008
  7. Jeff KingFeb 7, 2008
  8. Lars HjemliFeb 7, 2008
  9. Jakub NarebskiFeb 7, 2008
  10. Junio C HamanoFeb 10, 2008
  11. Jakub NarebskiFeb 10, 2008
  12. Johannes SchindelinFeb 10, 2008
  13. Junio C HamanoFeb 10, 2008
  14. Junio C HamanoFeb 10, 2008
  15. Junio C HamanoFeb 12, 2008
  16. reflog-delete, was Re: What's cooking in git.git (topics)Johannes Schindelin, Feb 12, 2008
  17. Junio C HamanoFeb 17, 2008
  18. Jeff KingFeb 17, 2008
  19. Jakub NarebskiFeb 17, 2008
  20. Junio C HamanoFeb 17, 2008
  21. Jakub NarebskiFeb 17, 2008
  22. Junio C HamanoFeb 18, 2008
  23. Jakub NarebskiFeb 18, 2008
  24. Matthias KestenholzFeb 17, 2008
  25. Junio C HamanoFeb 17, 2008
  26. Jeff KingFeb 17, 2008
  27. [Announce] 'next' rewound and rebasedJunio C Hamano, Feb 17, 2008
  28. Junio C HamanoFeb 21, 2008
  29. Johannes SchindelinFeb 21, 2008
  30. Junio C HamanoFeb 21, 2008
  31. Brandon CaseyFeb 22, 2008
  32. 1/4 git-reflog: add option --rewrite to update reflog entries while expiringBrandon Casey, Feb 22, 2008
  33. reflog-delete: parse standard reflog optionsBrandon Casey, Feb 22, 2008
  34. Junio C HamanoFeb 22, 2008
  35. Brandon CaseyFeb 23, 2008
  36. Junio C HamanoFeb 23, 2008
  37. Junio C HamanoFeb 23, 2008
  38. Brandon CaseyFeb 23, 2008
  39. Junio C HamanoFeb 25, 2008
  40. Junio C HamanoFeb 28, 2008
  41. Junio C HamanoMar 1, 2008
  42. Shawn O. PearceMar 2, 2008
  43. Junio C HamanoMar 3, 2008
  44. Junio C HamanoMar 6, 2008
  45. Johannes SchindelinMar 6, 2008
  46. Junio C HamanoMar 8, 2008
  47. 2/4 refs.c: make close_ref() and commit_ref() non-staticBrandon Casey, Feb 22, 2008
  48. 3/4 git-reflog: add option --updateref to write the last reflog sha1 into the refBrandon Casey, Feb 22, 2008
  49. 4/4 git-stash: add new 'drop' subcommandBrandon Casey, Feb 22, 2008
  50. git-stash: add new 'pop' subcommandBrandon Casey, Feb 22, 2008

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.