Skip to contentSkip to conversation

refs.c: add err arguments to reflog functions

PATCH Superseded David Turner 4 participants 30 messages Jun 15, 2016 Documentation

v7 of 10 Superseded Latest is v10 →

lore
Share & tools
lore
#0 lore raw

PATCH v7 1/8 refs.c: add err arguments to reflog functions

Add an err argument to log_ref_setup that can explain the reason for a failure. This then eliminates the need to manage errno through this function since we can just add strerror(errno) to the err string when meaningful. No callers relied on errno from this function for anything else than the error message.

Also add err arguments to private functions write_ref_to_lockfile, log_ref_write_1, commit_ref_update. This again eliminates the need to manage errno in these functions.

Some error messages change slightly. For instance, we sometimes lose "cannot update ref" and instead only show the specific cause of ref update failure.

Update of a patch by Ronnie Sahlberg.

---
 builtin/checkout.c |   8 ++--
 refs.c             | 130 +++++++++++++++++++++++++++++------------------------
 refs.h             |   4 +-
 3 files changed, 79 insertions(+), 63 deletions(-)
builtin/checkout.c +5 -3
index c018ab3..93f63d3 100644
@@ -624,16 +624,18 @@ static void update_refs_for_switch(const struct checkout_opts *opts,
 				struct strbuf log_file = STRBUF_INIT;
 				int ret;
 				const char *ref_name;
+				struct strbuf err = STRBUF_INIT;
 
 				ref_name = mkpath("refs/heads/%s", opts->new_orphan_branch);
 				temp = log_all_ref_updates;
 				log_all_ref_updates = 1;
-				ret = log_ref_setup(ref_name, &log_file);
+				ret = log_ref_setup(ref_name, &log_file, &err);
 				log_all_ref_updates = temp;
 				strbuf_release(&log_file);
 				if (ret) {
-					fprintf(stderr, _("Can not do reflog for '%s'\n"),
-					    opts->new_orphan_branch);
+					fprintf(stderr, _("Can not do reflog for '%s'. %s\n"),
+						opts->new_orphan_branch, err.buf);
+					strbuf_release(&err);
 					return;
 				}
 			}
refs.c +72 -58

280 lines not on this page · raw message

refs.h +2 -2
index e82fca5..debdefc 100644
@@ -226,9 +226,9 @@ int pack_refs(unsigned int flags);
 #define REF_NODEREF	0x01
 
 /*
- * Setup reflog before using. Set errno to something meaningful on failure.
+ * Setup reflog before using. Fill in err and return -1 on failure.
  */
-int log_ref_setup(const char *refname, struct strbuf *logfile);
+int log_ref_setup(const char *refname, struct strbuf *logfile, struct strbuf *err);
 
 /** Reads log for the value of ref during at_time. **/
 extern int read_ref_at(const char *refname, unsigned int flags,
Signature · 1 line
2.0.5.499.g01f6352.dirty-twtrsrc
#1 lore raw

PATCH v7 2/8 cherry-pick: treat CHERRY_PICK_HEAD and REVERT_HEAD as refs

Instead of directly writing to and reading from files in $GIT_DIR, use ref API to interact with CHERRY_PICK_HEAD and REVERT_HEAD.

---
 branch.c                         |  4 ++--
 builtin/commit.c                 |  6 +++---
 builtin/merge.c                  |  2 +-
 contrib/completion/git-prompt.sh |  4 ++--
 git-gui/lib/commit.tcl           |  2 +-
 sequencer.c                      | 31 ++++++++++---------------------
 t/t7509-commit.sh                |  4 ++--
 wt-status.c                      |  6 ++----
 8 files changed, 23 insertions(+), 36 deletions(-)
branch.c +2 -2
index b002435..ec598aa 100644
@@ -302,8 +302,8 @@ void create_branch(const char *head,
 
 void remove_branch_state(void)
 {
-	unlink(git_path("CHERRY_PICK_HEAD"));
-	unlink(git_path("REVERT_HEAD"));
+	delete_ref("CHERRY_PICK_HEAD", NULL, REF_NODEREF);
+	delete_ref("REVERT_HEAD", NULL, REF_NODEREF);
 	unlink(git_path("MERGE_HEAD"));
 	unlink(git_path("MERGE_RR"));
 	unlink(git_path("MERGE_MSG"));
builtin/commit.c +3 -3
index b5b1158..53c7e90 100644
@@ -168,7 +168,7 @@ static void determine_whence(struct wt_status *s)
 {
 	if (file_exists(git_path("MERGE_HEAD")))
 		whence = FROM_MERGE;
-	else if (file_exists(git_path("CHERRY_PICK_HEAD"))) {
+	else if (ref_exists("CHERRY_PICK_HEAD")) {
 		whence = FROM_CHERRY_PICK;
 		if (file_exists(git_path(SEQ_DIR)))
 			sequencer_in_use = 1;
@@ -1777,8 +1777,8 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
 	}
 	ref_transaction_free(transaction);
 
-	unlink(git_path("CHERRY_PICK_HEAD"));
-	unlink(git_path("REVERT_HEAD"));
+	delete_ref("CHERRY_PICK_HEAD", NULL, REF_NODEREF);
+	delete_ref("REVERT_HEAD", NULL, REF_NODEREF);
 	unlink(git_path("MERGE_HEAD"));
 	unlink(git_path("MERGE_MSG"));
 	unlink(git_path("MERGE_MODE"));
builtin/merge.c +1 -1
index 46aacd6..3e2ae2f 100644
@@ -1206,7 +1206,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
 		else
 			die(_("You have not concluded your merge (MERGE_HEAD exists)."));
 	}
-	if (file_exists(git_path("CHERRY_PICK_HEAD"))) {
+	if (ref_exists("CHERRY_PICK_HEAD")) {
 		if (advice_resolve_conflict)
 			die(_("You have not concluded your cherry-pick (CHERRY_PICK_HEAD exists).\n"
 			    "Please, commit your changes before you merge."));
contrib/completion/git-prompt.sh +2 -2
index 366f0bc..e2c5583 100644
@@ -415,9 +415,9 @@ __git_ps1 ()
 			fi
 		elif [ -f "$g/MERGE_HEAD" ]; then
 			r="|MERGING"
-		elif [ -f "$g/CHERRY_PICK_HEAD" ]; then
+		elif git rev-parse --quiet --verify "CHERRY_PICK_HEAD" >/dev/null; then
 			r="|CHERRY-PICKING"
-		elif [ -f "$g/REVERT_HEAD" ]; then
+		elif git rev-parse --quiet --verify "REVERT_HEAD" >/dev/null; then
 			r="|REVERTING"
 		elif [ -f "$g/BISECT_LOG" ]; then
 			r="|BISECTING"
git-gui/lib/commit.tcl +1 -1
index 864b687..2b08b13 100644
@@ -409,7 +409,7 @@ A rescan will be automatically started now.
 	catch {file delete [gitdir MERGE_MSG]}
 	catch {file delete [gitdir SQUASH_MSG]}
 	catch {file delete [gitdir GITGUI_MSG]}
-	catch {file delete [gitdir CHERRY_PICK_HEAD]}
+	catch {git update-ref -d --no-deref CHERRY_PICK_HEAD}
 
 	# -- Let rerere do its thing.
 	#
sequencer.c +10 -21
index f8421a8..90396ba 100644
@@ -158,21 +158,10 @@ static void free_message(struct commit *commit, struct commit_message *msg)
 	unuse_commit_buffer(commit, msg->message);
 }
 
-static void write_cherry_pick_head(struct commit *commit, const char *pseudoref)
+static void write_cherry_pick_head(struct commit *commit, const char *ref)
 {
-	const char *filename;
-	int fd;
-	struct strbuf buf = STRBUF_INIT;
-
-	strbuf_addf(&buf, "%s\n", sha1_to_hex(commit->object.sha1));
-
-	filename = git_path("%s", pseudoref);
-	fd = open(filename, O_WRONLY | O_CREAT, 0666);
-	if (fd < 0)
-		die_errno(_("Could not open '%s' for writing"), filename);
-	if (write_in_full(fd, buf.buf, buf.len) != buf.len || close(fd))
-		die_errno(_("Could not write to '%s'"), filename);
-	strbuf_release(&buf);
+	update_ref(NULL, ref, commit->object.sha1, NULL, REF_NODEREF,
+		   UPDATE_REFS_DIE_ON_ERR);
 }
 
 static void print_advice(int show_hint, struct replay_opts *opts)
@@ -186,7 +175,7 @@ static void print_advice(int show_hint, struct replay_opts *opts)
 		 * (typically rebase --interactive) wants to take care
 		 * of the commit itself so remove CHERRY_PICK_HEAD
 		 */
-		unlink(git_path("CHERRY_PICK_HEAD"));
+		delete_ref("CHERRY_PICK_HEAD", NULL, REF_NODEREF);
 		return;
 	}
 
@@ -878,8 +867,8 @@ static int rollback_single_pick(void)
 {
 	unsigned char head_sha1[20];
 
-	if (!file_exists(git_path("CHERRY_PICK_HEAD")) &&
-	    !file_exists(git_path("REVERT_HEAD")))
+	if (!ref_exists("CHERRY_PICK_HEAD") &&
+	    !ref_exists("REVERT_HEAD"))
 		return error(_("no cherry-pick or revert in progress"));
 	if (read_ref_full("HEAD", 0, head_sha1, NULL))
 		return error(_("cannot resolve HEAD"));
@@ -1014,8 +1003,8 @@ static int continue_single_pick(void)
 {
 	const char *argv[] = { "commit", NULL };
 
-	if (!file_exists(git_path("CHERRY_PICK_HEAD")) &&
-	    !file_exists(git_path("REVERT_HEAD")))
+	if (!ref_exists("CHERRY_PICK_HEAD") &&
+	    !ref_exists("REVERT_HEAD"))
 		return error(_("no cherry-pick or revert in progress"));
 	return run_command_v_opt(argv, RUN_GIT_CMD);
 }
@@ -1030,8 +1019,8 @@ static int sequencer_continue(struct replay_opts *opts)
 	read_populate_todo(&todo_list, opts);
 
 	/* Verify that the conflict has been resolved */
-	if (file_exists(git_path("CHERRY_PICK_HEAD")) ||
-	    file_exists(git_path("REVERT_HEAD"))) {
+	if (ref_exists("CHERRY_PICK_HEAD") ||
+	    ref_exists("REVERT_HEAD")) {
 		int ret = continue_single_pick();
 		if (ret)
 			return ret;
t/t7509-commit.sh +2 -2
index 9ac7940..f7fd62c 100755
@@ -163,7 +163,7 @@ test_expect_success 'commit respects CHERRY_PICK_HEAD and MERGE_MSG' '
 	test_tick &&
 	git commit -am "cherry-pick 1" --author="Cherry <[email protected]>" &&
 	git tag cherry-pick-head &&
-	git rev-parse cherry-pick-head >.git/CHERRY_PICK_HEAD &&
+	git update-ref CHERRY_PICK_HEAD $(git rev-parse cherry-pick-head) &&
 	echo "This is a MERGE_MSG" >.git/MERGE_MSG &&
 	echo "cherry-pick 1b" >>foo &&
 	test_tick &&
@@ -178,7 +178,7 @@ test_expect_success 'commit respects CHERRY_PICK_HEAD and MERGE_MSG' '
 '
 
 test_expect_success '--reset-author with CHERRY_PICK_HEAD' '
-	git rev-parse cherry-pick-head >.git/CHERRY_PICK_HEAD &&
+	git update-ref CHERRY_PICK_HEAD $(git rev-parse cherry-pick-head) &&
 	echo "cherry-pick 2" >>foo &&
 	test_tick &&
 	git commit -am "cherry-pick 2" --reset-author &&
wt-status.c +2 -4
index 9c686e6..661c1fb 100644
@@ -1336,8 +1336,7 @@ void wt_status_get_state(struct wt_status_state *state,
 			state->rebase_in_progress = 1;
 		state->branch = read_and_strip_branch("rebase-merge/head-name");
 		state->onto = read_and_strip_branch("rebase-merge/onto");
-	} else if (!stat(git_path("CHERRY_PICK_HEAD"), &st) &&
-			!get_sha1("CHERRY_PICK_HEAD", sha1)) {
+	} else if (!read_ref("CHERRY_PICK_HEAD", sha1)) {
 		state->cherry_pick_in_progress = 1;
 		hashcpy(state->cherry_pick_head_sha1, sha1);
 	}
@@ -1345,8 +1344,7 @@ void wt_status_get_state(struct wt_status_state *state,
 		state->bisect_in_progress = 1;
 		state->branch = read_and_strip_branch("BISECT_START");
 	}
-	if (!stat(git_path("REVERT_HEAD"), &st) &&
-	    !get_sha1("REVERT_HEAD", sha1)) {
+	if (!read_ref("REVERT_HEAD", sha1)) {
 		state->revert_in_progress = 1;
 		hashcpy(state->revert_head_sha1, sha1);
 	}
Signature · 1 line
2.0.5.499.g01f6352.dirty-twtrsrc
#2 lore raw

Re: [PATCH v7 2/8] cherry-pick: treat CHERRY_PICK_HEAD and REVERT_HEAD as refs

Am 08.07.2015 um 02:55 schrieb David Turner:

Quoted 6 lines

Instead of directly writing to and reading from files in $GIT_DIR, use ref API to interact with CHERRY_PICK_HEAD and REVERT_HEAD.

---

...

Quoted 6 lines
contrib/completion/git-prompt.sh +0 -0
index 366f0bc..e2c5583 100644
@@ -415,9 +415,9 @@ __git_ps1 ()
  			fi

contrib/completion/git-prompt.sh __git_ps1 () · line 423

Quoted 10 lines
contrib/completion/git-prompt.sh +2 -2
  		elif [ -f "$g/MERGE_HEAD" ]; then
  			r="|MERGING"
-		elif [ -f "$g/CHERRY_PICK_HEAD" ]; then
+		elif git rev-parse --quiet --verify "CHERRY_PICK_HEAD" >/dev/null; then
  			r="|CHERRY-PICKING"
-		elif [ -f "$g/REVERT_HEAD" ]; then
+		elif git rev-parse --quiet --verify "REVERT_HEAD" >/dev/null; then
  			r="|REVERTING"
  		elif [ -f "$g/BISECT_LOG" ]; then
  			r="|BISECTING"

We are trying very hard not to spawn any new processes in __git_ps1(). So, I raise a moderate veto against this hunk.

-- Hannes

#16 lore raw

PATCH v7 3/8 bisect: treat BISECT_HEAD as a ref

In reply to David Turner

Instead of directly writing to and reading from files in $GIT_DIR, use ref API to interact with BISECT_HEAD.

---
 git-bisect.sh | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
git-bisect.sh +7 -3
index ae3fec2..2fd8ea6 100755
@@ -35,7 +35,7 @@ _x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
 
 bisect_head()
 {
-	if test -f "$GIT_DIR/BISECT_HEAD"
+	if bisect_head_exists
 	then
 		echo BISECT_HEAD
 	else
@@ -209,6 +209,10 @@ check_expected_revs() {
 	done
 }
 
+bisect_head_exists() {
+	git rev-parse --quiet --verify "BISECT_HEAD" >/dev/null
+}
+
 bisect_skip() {
 	all=''
 	for arg in "$@"
@@ -310,7 +314,7 @@ bisect_next() {
 	bisect_next_check good
 
 	# Perform all bisection computation, display and checkout
-	git bisect--helper --next-all $(test -f "$GIT_DIR/BISECT_HEAD" && echo --no-checkout)
+	git bisect--helper --next-all $(bisect_head_exists && echo --no-checkout)
 	res=$?
 
 	# Check if we should exit because bisection is finished
@@ -377,7 +381,7 @@ bisect_reset() {
 		usage ;;
 	esac
 
-	if ! test -f "$GIT_DIR/BISECT_HEAD" && ! git checkout "$branch" --
+	if ! bisect_head_exists && ! git checkout "$branch" --
 	then
 		die "$(eval_gettext "Could not check out original HEAD '\$branch'.
 Try 'git bisect reset <commit>'.")"
Signature · 1 line
2.0.5.499.g01f6352.dirty-twtrsrc
#17 lore raw

PATCH v7 4/8 refs: Break out check for reflog autocreation

In reply to David Turner

This is just for clarity.

---
 refs.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
refs.c +11 -5
index e891bed..e694359 100644
@@ -3118,6 +3118,16 @@ static int copy_msg(char *buf, const char *msg)
 	return cp - buf;
 }
 
+static int should_autocreate_reflog(const char *refname)
+{
+	if (!log_all_ref_updates)
+		return 0;
+	return starts_with(refname, "refs/heads/") ||
+		starts_with(refname, "refs/remotes/") ||
+		starts_with(refname, "refs/notes/") ||
+		!strcmp(refname, "HEAD");
+}
+
 /* This function will fill in *err and return -1 on failure */
 int log_ref_setup(const char *refname, struct strbuf *sb_logfile, struct strbuf *err)
 {
@@ -3128,11 +3138,7 @@ int log_ref_setup(const char *refname, struct strbuf *sb_logfile, struct strbuf
 	logfile = sb_logfile->buf;
 	/* make sure the rest of the function can't change "logfile" */
 	sb_logfile = NULL;
-	if (log_all_ref_updates &&
-	    (starts_with(refname, "refs/heads/") ||
-	     starts_with(refname, "refs/remotes/") ||
-	     starts_with(refname, "refs/notes/") ||
-	     !strcmp(refname, "HEAD"))) {
+	if (should_autocreate_reflog(refname)) {
 		if (safe_create_leading_directories(logfile) < 0) {
 			strbuf_addf(err, "unable to create directory for %s. "
 				    "%s", logfile, strerror(errno));
Signature · 1 line
2.0.5.499.g01f6352.dirty-twtrsrc
#18 lore raw

PATCH v7 5/8 refs: new public ref function: safe_create_reflog

In reply to David Turner

The safe_create_reflog function creates a reflog, if it does not already exist.

The log_ref_setup function becomes private and gains a force_create parameter to force the creation of a reflog even if log_all_ref_updates is false or the refname is not one of the special refnames.

The new parameter also reduces the need to store, modify, and restore the log_all_ref_updates global before reflog creation.

In a moment, we will use this to add reflog creation commands to git-reflog.

---
 builtin/checkout.c | 16 +++++-----------
 refs.c             | 25 +++++++++++++++++++++----
 refs.h             |  2 +-
 3 files changed, 27 insertions(+), 16 deletions(-)
builtin/checkout.c +5 -11
index 93f63d3..c840f33 100644
@@ -620,24 +620,18 @@ static void update_refs_for_switch(const struct checkout_opts *opts,
 	if (opts->new_branch) {
 		if (opts->new_orphan_branch) {
 			if (opts->new_branch_log && !log_all_ref_updates) {
-				int temp;
-				struct strbuf log_file = STRBUF_INIT;
-				int ret;
-				const char *ref_name;
+				char *refname;
 				struct strbuf err = STRBUF_INIT;
 
-				ref_name = mkpath("refs/heads/%s", opts->new_orphan_branch);
-				temp = log_all_ref_updates;
-				log_all_ref_updates = 1;
-				ret = log_ref_setup(ref_name, &log_file, &err);
-				log_all_ref_updates = temp;
-				strbuf_release(&log_file);
-				if (ret) {
+				refname = mkpathdup("refs/heads/%s", opts->new_orphan_branch);
+				if (safe_create_reflog(refname, &err, 1)) {
+					free(refname);
 					fprintf(stderr, _("Can not do reflog for '%s'. %s\n"),
 						opts->new_orphan_branch, err.buf);
 					strbuf_release(&err);
 					return;
 				}
+				free(refname);
 			}
 		}
 		else
refs.c +21 -4
index e694359..01b0af5 100644
@@ -3128,8 +3128,14 @@ static int should_autocreate_reflog(const char *refname)
 		!strcmp(refname, "HEAD");
 }
 
-/* This function will fill in *err and return -1 on failure */
-int log_ref_setup(const char *refname, struct strbuf *sb_logfile, struct strbuf *err)
+/*
+ * Create a reflog for a ref.  If force_create = 0, the reflog will
+ * only be created for certain refs (those for which
+ * should_autocreate_reflog returns non-zero.  Otherwise, it will be
+ * created regardless of the ref name.  This function will fill in
+ * *err and return -1 on failure
+ */
+static int log_ref_setup(const char *refname, struct strbuf *sb_logfile, struct strbuf *err, int force_create)
 {
 	int logfd, oflags = O_APPEND | O_WRONLY;
 	char *logfile;
@@ -3138,7 +3144,7 @@ int log_ref_setup(const char *refname, struct strbuf *sb_logfile, struct strbuf
 	logfile = sb_logfile->buf;
 	/* make sure the rest of the function can't change "logfile" */
 	sb_logfile = NULL;
-	if (should_autocreate_reflog(refname)) {
+	if (force_create || should_autocreate_reflog(refname)) {
 		if (safe_create_leading_directories(logfile) < 0) {
 			strbuf_addf(err, "unable to create directory for %s. "
 				    "%s", logfile, strerror(errno));
@@ -3173,6 +3179,17 @@ int log_ref_setup(const char *refname, struct strbuf *sb_logfile, struct strbuf
 	return 0;
 }
 
+
+int safe_create_reflog(const char *refname, struct strbuf *err, int force_create)
+{
+	int ret;
+	struct strbuf sb = STRBUF_INIT;
+
+	ret = log_ref_setup(refname, &sb, err, force_create);
+	strbuf_release(&sb);
+	return ret;
+}
+
 static int log_ref_write_fd(int fd, const unsigned char *old_sha1,
 			    const unsigned char *new_sha1,
 			    const char *committer, const char *msg)
@@ -3209,7 +3226,7 @@ static int log_ref_write_1(const char *refname, const unsigned char *old_sha1,
 	if (log_all_ref_updates < 0)
 		log_all_ref_updates = !is_bare_repository();
 
-	result = log_ref_setup(refname, sb_log_file, err);
+	result = log_ref_setup(refname, sb_log_file, err, 0);
 
 	if (result)
 		return result;
refs.h +1 -1
index debdefc..3b90e16 100644
@@ -228,7 +228,7 @@ int pack_refs(unsigned int flags);
 /*
  * Setup reflog before using. Fill in err and return -1 on failure.
  */
-int log_ref_setup(const char *refname, struct strbuf *logfile, struct strbuf *err);
+int safe_create_reflog(const char *refname, struct strbuf *err, int force_create);
 
 /** Reads log for the value of ref during at_time. **/
 extern int read_ref_at(const char *refname, unsigned int flags,
Signature · 1 line
2.0.5.499.g01f6352.dirty-twtrsrc
#19 lore raw

Re: [PATCH v7 5/8] refs: new public ref function: safe_create_reflog

Quoted 32 lines · David Turner

The safe_create_reflog function creates a reflog, if it does not already exist.

The log_ref_setup function becomes private and gains a force_create parameter to force the creation of a reflog even if log_all_ref_updates is false or the refname is not one of the special refnames.

The new parameter also reduces the need to store, modify, and restore the log_all_ref_updates global before reflog creation.

In a moment, we will use this to add reflog creation commands to git-reflog.

---
 builtin/checkout.c | 16 +++++-----------
 refs.c             | 25 +++++++++++++++++++++----
 refs.h             |  2 +-
 3 files changed, 27 insertions(+), 16 deletions(-)

[...]

refs.c +1 -2
index e694359..01b0af5 100644
@@ -3128,8 +3128,14 @@ static int should_autocreate_reflog(const char *refname)
 		!strcmp(refname, "HEAD");
 }
 
-/* This function will fill in *err and return -1 on failure */
-int log_ref_setup(const char *refname, struct strbuf *sb_logfile, struct strbuf *err)
+/*

refs.c static int should_autocreate_reflog(const char *refname) · line 3141

Quoted 10 lines
refs.c +7 -0
+ * Create a reflog for a ref.  If force_create = 0, the reflog will
+ * only be created for certain refs (those for which
+ * should_autocreate_reflog returns non-zero.  Otherwise, it will be
+ * created regardless of the ref name.  This function will fill in
+ * *err and return -1 on failure
+ */
+static int log_ref_setup(const char *refname, struct strbuf *sb_logfile, struct strbuf *err, int force_create)
 {
 	int logfd, oflags = O_APPEND | O_WRONLY;
 	char *logfile;

Sorry for being unclear in the last round. I meant that the whole docstring should be in the imperative voice, like

/*
* Create a reflog for a ref. If force_create = 0, only create the
* reflog for certain refs (those for which should_autocreate_reflog
* returns non-zero). Otherwise, create it regardless of the reference
* name. Fill in *err and return -1 on failure.
*/

Otherwise, this patch looks good.

Michael

Signature · 2 lines
Michael Haggerty
[email protected]
#20 lore raw

PATCH v7 6/8 git-reflog: add exists command

In reply to David Turner

Theis are necessary because alternate ref backends might store reflogs somewhere other than .git/logs. Code that now directly manipulates .git/logs should instead go through git-reflog.

---
 Documentation/git-reflog.txt |  4 ++++
 builtin/reflog.c             | 33 ++++++++++++++++++++++++++++++++-
 t/t1411-reflog-show.sh       |  5 +++++
 3 files changed, 41 insertions(+), 1 deletion(-)
Documentation/git-reflog.txt +4 -0
index 5e7908e..4b08fc7 100644
@@ -23,6 +23,7 @@ depending on the subcommand:
 	[--dry-run] [--verbose] [--all | <refs>...]
 'git reflog delete' [--rewrite] [--updateref]
 	[--dry-run] [--verbose] ref@\{specifier\}...
+'git reflog exists' <ref>
 
 Reference logs, or "reflogs", record when the tips of branches and
 other references were updated in the local repository. Reflogs are
@@ -52,6 +53,9 @@ argument must be an _exact_ entry (e.g. "`git reflog delete
 master@{2}`"). This subcommand is also typically not used directly by
 end users.
 
+The "exists" subcommand checks whether a ref has a reflog.  It exists
+with zero status if the reflog exists, and non-zero status if it does
+not.
 
 OPTIONS
 -------
builtin/reflog.c +32 -1
index c2eb8ff..7ed0e85 100644
@@ -13,6 +13,8 @@ static const char reflog_expire_usage[] =
 "git reflog expire [--expire=<time>] [--expire-unreachable=<time>] [--rewrite] [--updateref] [--stale-fix] [--dry-run | -n] [--verbose] [--all] <refs>...";
 static const char reflog_delete_usage[] =
 "git reflog delete [--rewrite] [--updateref] [--dry-run | -n] [--verbose] <refs>...";
+static const char reflog_exists_usage[] =
+"git reflog exists <ref>";
 
 static unsigned long default_reflog_expire;
 static unsigned long default_reflog_expire_unreachable;
@@ -699,12 +701,38 @@ static int cmd_reflog_delete(int argc, const char **argv, const char *prefix)
 	return status;
 }
 
+static int cmd_reflog_exists(int argc, const char **argv, const char *prefix)
+{
+	int i, start = 0;
+
+	for (i = 1; i < argc; i++) {
+		const char *arg = argv[i];
+		if (!strcmp(arg, "--")) {
+			i++;
+			break;
+		}
+		else if (arg[0] == '-')
+			usage(reflog_exists_usage);
+		else
+			break;
+	}
+
+	start = i;
+
+	if (argc - start != 1)
+		usage(reflog_exists_usage);
+
+	if (check_refname_format(argv[start], REFNAME_ALLOW_ONELEVEL))
+		die("invalid ref format: %s", argv[start]);
+	return !reflog_exists(argv[start]);
+}
+
 /*
  * main "reflog"
  */
 
 static const char reflog_usage[] =
-"git reflog [ show | expire | delete ]";
+"git reflog [ show | expire | delete | exists ]";
 
 int cmd_reflog(int argc, const char **argv, const char *prefix)
 {
@@ -724,5 +752,8 @@ int cmd_reflog(int argc, const char **argv, const char *prefix)
 	if (!strcmp(argv[1], "delete"))
 		return cmd_reflog_delete(argc - 1, argv + 1, prefix);
 
+	if (!strcmp(argv[1], "exists"))
+		return cmd_reflog_exists(argc - 1, argv + 1, prefix);
+
 	return cmd_log_reflog(argc, argv, prefix);
 }
t/t1411-reflog-show.sh +5 -0
index 6f47c0d..3eb4f10 100755
@@ -166,4 +166,9 @@ test_expect_success 'git log -g -p shows diffs vs. parents' '
 	test_cmp expect actual
 '
 
+test_expect_success 'reflog exists works' '
+	git reflog exists refs/heads/master &&
+	! git reflog exists refs/heads/nonexistent
+'
+
 test_done
Signature · 1 line
2.0.5.499.g01f6352.dirty-twtrsrc
#21 lore raw

PATCH v7 7/8 update-ref and tag: add --create-reflog arg

In reply to David Turner

Allow the creation of a ref (e.g. stash) with a reflog already in place. For most refs (e.g. those under refs/heads), this happens automatically, but for others, we need this option.

Currently, git does this by pre-creating the reflog, but alternate ref backends might store reflogs somewhere other than .git/logs. Code that now directly manipulates .git/logs should instead use git plumbing commands.

I also added --create-reflog to git tag, just for completeness.

In a moment, we will use this argument to make git stash work with alternate ref backends.

---
 Documentation/git-tag.txt        |  5 ++++-
 Documentation/git-update-ref.txt |  5 ++++-
 builtin/tag.c                    |  5 +++++
 builtin/update-ref.c             | 17 +++++++++++++++--
 t/t1400-update-ref.sh            | 38 ++++++++++++++++++++++++++++++++++++++
 t/t7004-tag.sh                   |  9 ++++++++-
 6 files changed, 74 insertions(+), 5 deletions(-)
Documentation/git-tag.txt +4 -1
index 034d10d..2312980 100644
@@ -13,7 +13,7 @@ SYNOPSIS
 	<tagname> [<commit> | <object>]
 'git tag' -d <tagname>...
 'git tag' [-n[<num>]] -l [--contains <commit>] [--points-at <object>]
-	[--column[=<options>] | --no-column] [<pattern>...]
+	[--column[=<options>] | --no-column] [--create-reflog] [<pattern>...]
 	[<pattern>...]
 'git tag' -v <tagname>...
 
@@ -143,6 +143,9 @@ This option is only applicable when listing tags without annotation lines.
 	all, 'whitespace' removes just leading/trailing whitespace lines and
 	'strip' removes both whitespace and commentary.
 
+--create-reflog::
+	Create a reflog for the tag.
+
 <tagname>::
 	The name of the tag to create, delete, or describe.
 	The new tag name must pass all checks defined by
Documentation/git-update-ref.txt +4 -1
index c8f5ae5..969bfab 100644
@@ -8,7 +8,7 @@ git-update-ref - Update the object name stored in a ref safely
 SYNOPSIS
 --------
 [verse]
-'git update-ref' [-m <reason>] (-d <ref> [<oldvalue>] | [--no-deref] <ref> <newvalue> [<oldvalue>] | --stdin [-z])
+'git update-ref' [-m <reason>] (-d <ref> [<oldvalue>] | [--no-deref] [--create-reflog] <ref> <newvalue> [<oldvalue>] | --stdin [-z])
 
 DESCRIPTION
 -----------
@@ -67,6 +67,9 @@ performs all modifications together.  Specify commands of the form:
 	verify SP <ref> [SP <oldvalue>] LF
 	option SP <opt> LF
 
+With `--create-reflog`, update-ref will create a reflog for each ref
+even if one would not ordinarily be created.
+
 Quote fields containing whitespace as if they were strings in C source
 code; i.e., surrounded by double-quotes and with backslash escapes.
 Use 40 "0" characters or the empty string to specify a zero value.  To
builtin/tag.c +5 -0
index 5f6cdc5..896f434 100644
@@ -579,6 +579,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
 	struct create_tag_options opt;
 	char *cleanup_arg = NULL;
 	int annotate = 0, force = 0, lines = -1;
+	int create_reflog = 0;
 	int cmdmode = 0;
 	const char *msgfile = NULL, *keyid = NULL;
 	struct msg_arg msg = { 0, STRBUF_INIT };
@@ -605,6 +606,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
 		OPT_STRING('u', "local-user", &keyid, N_("key-id"),
 					N_("use another key to sign the tag")),
 		OPT__FORCE(&force, N_("replace the tag if exists")),
+		OPT_BOOL(0, "create-reflog", &create_reflog, N_("create_reflog")),
 
 		OPT_GROUP(N_("Tag listing options")),
 		OPT_COLUMN(0, "column", &colopts, N_("show tag list in columns")),
@@ -730,6 +732,9 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
 	if (annotate)
 		create_tag(object, tag, &buf, &opt, prev, object);
 
+	if (create_reflog && safe_create_reflog(ref.buf, &err, 1))
+		die("failed to create reflog for %s: %s", ref.buf, err.buf);
+
 	transaction = ref_transaction_begin(&err);
 	if (!transaction ||
 	    ref_transaction_update(transaction, ref.buf, object, prev,
builtin/update-ref.c +15 -2
index 6763cf1..d570e5e 100644
@@ -14,6 +14,7 @@ static const char * const git_update_ref_usage[] = {
 
 static char line_termination = '\n';
 static int update_flags;
+int create_reflog;
 static const char *msg;
 
 /*
@@ -198,6 +199,9 @@ static const char *parse_cmd_update(struct ref_transaction *transaction,
 	if (*next != line_termination)
 		die("update %s: extra input: %s", refname, next);
 
+	if (create_reflog && safe_create_reflog(refname, &err, 1))
+		die("failed to create reflog for %s: %s", refname, err.buf);
+
 	if (ref_transaction_update(transaction, refname,
 				   new_sha1, have_old ? old_sha1 : NULL,
 				   update_flags, msg, &err))
@@ -230,6 +234,9 @@ static const char *parse_cmd_create(struct ref_transaction *transaction,
 	if (*next != line_termination)
 		die("create %s: extra input: %s", refname, next);
 
+	if (create_reflog && safe_create_reflog(refname, &err, 1))
+		die("failed to create reflog for %s: %s", refname, err.buf);
+
 	if (ref_transaction_create(transaction, refname, new_sha1,
 				   update_flags, msg, &err))
 		die("%s", err.buf);
@@ -361,6 +368,7 @@ int cmd_update_ref(int argc, const char **argv, const char *prefix)
 					N_("update <refname> not the one it points to")),
 		OPT_BOOL('z', NULL, &end_null, N_("stdin has NUL-terminated arguments")),
 		OPT_BOOL( 0 , "stdin", &read_stdin, N_("read updates from stdin")),
+		OPT_BOOL( 0 , "create-reflog", &create_reflog, N_("create_reflog")),
 		OPT_END(),
 	};
 
@@ -421,7 +429,7 @@ int cmd_update_ref(int argc, const char **argv, const char *prefix)
 
 	if (no_deref)
 		flags = REF_NODEREF;
-	if (delete)
+	if (delete) {
 		/*
 		 * For purposes of backwards compatibility, we treat
 		 * NULL_SHA1 as "don't care" here:
@@ -429,7 +437,12 @@ int cmd_update_ref(int argc, const char **argv, const char *prefix)
 		return delete_ref(refname,
 				  (oldval && !is_null_sha1(oldsha1)) ? oldsha1 : NULL,
 				  flags);
-	else
+	} else {
+		struct strbuf err = STRBUF_INIT;
+		if (create_reflog && safe_create_reflog(refname, &err, 1))
+			die("failed to create reflog for %s: %s", refname, err.buf);
+
 		return update_ref(msg, refname, sha1, oldval ? oldsha1 : NULL,
 				  flags, UPDATE_REFS_DIE_ON_ERR);
+	}
 }
t/t1400-update-ref.sh +38 -0
index d787bf5..9d21c19 100755
@@ -23,6 +23,7 @@ test_expect_success setup '
 m=refs/heads/master
 n_dir=refs/heads/gu
 n=$n_dir/fixes
+outside=foo
 
 test_expect_success \
 	"create $m" \
@@ -74,6 +75,24 @@ test_expect_success "delete $m (by HEAD)" '
 '
 rm -f .git/$m
 
+test_expect_success 'update-ref does not create reflogs by default' '
+	test_when_finished "git update-ref -d $outside" &&
+	git update-ref $outside $A &&
+	git rev-parse $A >expect &&
+	git rev-parse $outside >actual &&
+	test_cmp expect actual &&
+	test_must_fail git reflog exists $outside
+'
+
+test_expect_success 'update-ref creates reflogs with --create-reflog' '
+	test_when_finished "git update-ref -d $outside" &&
+	git update-ref --create-reflog $outside $A &&
+	git rev-parse $A >expect &&
+	git rev-parse $outside >actual &&
+	test_cmp expect actual &&
+	git reflog exists $outside
+'
+
 test_expect_success \
 	"create $m (by HEAD)" \
 	"git update-ref HEAD $A &&
@@ -472,6 +491,25 @@ test_expect_success 'stdin create ref works' '
 	test_cmp expect actual
 '
 
+test_expect_success 'stdin does not create reflogs by default' '
+	test_when_finished "git update-ref -d $outside" &&
+	echo "create $outside $m" >stdin &&
+	git update-ref --stdin <stdin &&
+	git rev-parse $m >expect &&
+	git rev-parse $outside >actual &&
+	test_cmp expect actual &&
+	test_must_fail git reflog exists $outside
+'
+
+test_expect_success 'stdin creates reflogs with --create-reflog' '
+	echo "create $outside $m" >stdin &&
+	git update-ref --create-reflog --stdin <stdin &&
+	git rev-parse $m >expect &&
+	git rev-parse $outside >actual &&
+	test_cmp expect actual &&
+	git reflog exists $outside
+'
+
 test_expect_success 'stdin succeeds with quoted argument' '
 	git update-ref -d $a &&
 	echo "create $a \"$m\"" >stdin &&
t/t7004-tag.sh +8 -1
index d1ff5c9..426d285 100755
@@ -51,7 +51,14 @@ test_expect_success 'creating a tag using default HEAD should succeed' '
 	echo foo >foo &&
 	git add foo &&
 	git commit -m Foo &&
-	git tag mytag
+	git tag mytag &&
+	test_must_fail git reflog exists refs/tags/mytag
+'
+
+test_expect_success 'creating a tag with --create-reflog should create reflog' '
+	test_when_finished "git tag -d tag_with_reflog" &&
+	git tag --create-reflog tag_with_reflog &&
+	git reflog exists refs/tags/tag_with_reflog
 '
 
 test_expect_success 'listing all tags if one exists should succeed' '
Signature · 1 line
2.0.5.499.g01f6352.dirty-twtrsrc
#22 lore raw

Re: [PATCH v7 7/8] update-ref and tag: add --create-reflog arg

Please see my email about v6 [*] for an argument for why, at the API level, the create-reflog functionality for new references needs to be implemented within the ref_transaction API.

[*] http://article.gmane.org/gmane.comp.version-control.git/273682

Quoted 1 line · David Turner

update-ref and tag: add --create-reflog arg

--create-reflog is an "option", not an "arg".

Quoted 3 lines

Allow the creation of a ref (e.g. stash) with a reflog already in place. For most refs (e.g. those under refs/heads), this happens automatically, but for others, we need this option.

As I mentioned, this fact is only true if core.logAllRefUpdates is turned on (or by default in non-bare repos).

Quoted 9 lines

Currently, git does this by pre-creating the reflog, but alternate ref backends might store reflogs somewhere other than .git/logs. Code that now directly manipulates .git/logs should instead use git plumbing commands.

I also added --create-reflog to git tag, just for completeness.

In a moment, we will use this argument to make git stash work with alternate ref backends.

For a moment I thought that there should be a corresponding --no-create-reflog option. But it would be pretty pointless, because if this call to update-ref would want to create a reflog, then so would *every* subsequent update to the reference. So the effect of --no-create-reflog could at best be temporary.

Quoted 77 lines
---
 Documentation/git-tag.txt        |  5 ++++-
 Documentation/git-update-ref.txt |  5 ++++-
 builtin/tag.c                    |  5 +++++
 builtin/update-ref.c             | 17 +++++++++++++++--
 t/t1400-update-ref.sh            | 38 ++++++++++++++++++++++++++++++++++++++
 t/t7004-tag.sh                   |  9 ++++++++-
 6 files changed, 74 insertions(+), 5 deletions(-)
Documentation/git-tag.txt +4 -1
index 034d10d..2312980 100644
@@ -13,7 +13,7 @@ SYNOPSIS
 	<tagname> [<commit> | <object>]
 'git tag' -d <tagname>...
 'git tag' [-n[<num>]] -l [--contains <commit>] [--points-at <object>]
-	[--column[=<options>] | --no-column] [<pattern>...]
+	[--column[=<options>] | --no-column] [--create-reflog] [<pattern>...]
 	[<pattern>...]
 'git tag' -v <tagname>...
 
@@ -143,6 +143,9 @@ This option is only applicable when listing tags without annotation lines.
 	all, 'whitespace' removes just leading/trailing whitespace lines and
 	'strip' removes both whitespace and commentary.
 
+--create-reflog::
+	Create a reflog for the tag.
+
 <tagname>::
 	The name of the tag to create, delete, or describe.
 	The new tag name must pass all checks defined by
Documentation/git-update-ref.txt +4 -1
index c8f5ae5..969bfab 100644
@@ -8,7 +8,7 @@ git-update-ref - Update the object name stored in a ref safely
 SYNOPSIS
 --------
 [verse]
-'git update-ref' [-m <reason>] (-d <ref> [<oldvalue>] | [--no-deref] <ref> <newvalue> [<oldvalue>] | --stdin [-z])
+'git update-ref' [-m <reason>] (-d <ref> [<oldvalue>] | [--no-deref] [--create-reflog] <ref> <newvalue> [<oldvalue>] | --stdin [-z])
 
 DESCRIPTION
 -----------
@@ -67,6 +67,9 @@ performs all modifications together.  Specify commands of the form:
 	verify SP <ref> [SP <oldvalue>] LF
 	option SP <opt> LF
 
+With `--create-reflog`, update-ref will create a reflog for each ref
+even if one would not ordinarily be created.
+
 Quote fields containing whitespace as if they were strings in C source
 code; i.e., surrounded by double-quotes and with backslash escapes.
 Use 40 "0" characters or the empty string to specify a zero value.  To
builtin/tag.c +2 -0
index 5f6cdc5..896f434 100644
@@ -579,6 +579,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
 	struct create_tag_options opt;
 	char *cleanup_arg = NULL;
 	int annotate = 0, force = 0, lines = -1;
+	int create_reflog = 0;
 	int cmdmode = 0;
 	const char *msgfile = NULL, *keyid = NULL;
 	struct msg_arg msg = { 0, STRBUF_INIT };
@@ -605,6 +606,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
 		OPT_STRING('u', "local-user", &keyid, N_("key-id"),
 					N_("use another key to sign the tag")),
 		OPT__FORCE(&force, N_("replace the tag if exists")),
+		OPT_BOOL(0, "create-reflog", &create_reflog, N_("create_reflog")),
 
 		OPT_GROUP(N_("Tag listing options")),
 		OPT_COLUMN(0, "column", &colopts, N_("show tag list in columns")),

builtin/tag.c int cmd_tag(int argc, const char **argv, const char *prefix) · line 737

Quoted 6 lines
builtin/tag.c +3 -0
@@ -730,6 +732,9 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
 	if (annotate)
 		create_tag(object, tag, &buf, &opt, prev, object);
 
+	if (create_reflog && safe_create_reflog(ref.buf, &err, 1))
+		die("failed to create reflog for %s: %s", ref.buf, err.buf);
+

This reflog should not be left around if the tag creation ultimately fails for any reason.

Quoted 16 lines
builtin/tag.c +0 -0
 	transaction = ref_transaction_begin(&err);
 	if (!transaction ||
 	    ref_transaction_update(transaction, ref.buf, object, prev,
builtin/update-ref.c +1 -0
index 6763cf1..d570e5e 100644
@@ -14,6 +14,7 @@ static const char * const git_update_ref_usage[] = {
 
 static char line_termination = '\n';
 static int update_flags;
+int create_reflog;
 static const char *msg;
 
 /*

builtin/update-ref.c static const char *parse_cmd_update(struct ref_transaction *transaction, · line 204

Quoted 6 lines
builtin/update-ref.c +3 -0
@@ -198,6 +199,9 @@ static const char *parse_cmd_update(struct ref_transaction *transaction,
 	if (*next != line_termination)
 		die("update %s: extra input: %s", refname, next);
 
+	if (create_reflog && safe_create_reflog(refname, &err, 1))
+		die("failed to create reflog for %s: %s", refname, err.buf);
+

It is valid to pass the empty string or 0{40} to the update command as the "new" value, in which case the reference will be deleted. In that case, no reflog should be created.

builtin/update-ref.c static const char *parse_cmd_create(struct ref_transaction *transaction, · line 242

Quoted 13 lines
builtin/update-ref.c +3 -0
 	if (ref_transaction_update(transaction, refname,
 				   new_sha1, have_old ? old_sha1 : NULL,
 				   update_flags, msg, &err))
@@ -230,6 +234,9 @@ static const char *parse_cmd_create(struct ref_transaction *transaction,
 	if (*next != line_termination)
 		die("create %s: extra input: %s", refname, next);
 
+	if (create_reflog && safe_create_reflog(refname, &err, 1))
+		die("failed to create reflog for %s: %s", refname, err.buf);
+
 	if (ref_transaction_create(transaction, refname, new_sha1,
 				   update_flags, msg, &err))
 		die("%s", err.buf);

Should the "verify" command also create a reflog, at least if the reference is not being verified to be missing?

Quoted 18 lines
builtin/update-ref.c +2 -1
@@ -361,6 +368,7 @@ int cmd_update_ref(int argc, const char **argv, const char *prefix)
 					N_("update <refname> not the one it points to")),
 		OPT_BOOL('z', NULL, &end_null, N_("stdin has NUL-terminated arguments")),
 		OPT_BOOL( 0 , "stdin", &read_stdin, N_("read updates from stdin")),
+		OPT_BOOL( 0 , "create-reflog", &create_reflog, N_("create_reflog")),
 		OPT_END(),
 	};
 
@@ -421,7 +429,7 @@ int cmd_update_ref(int argc, const char **argv, const char *prefix)
 
 	if (no_deref)
 		flags = REF_NODEREF;
-	if (delete)
+	if (delete) {
 		/*
 		 * For purposes of backwards compatibility, we treat
 		 * NULL_SHA1 as "don't care" here:

builtin/update-ref.c int cmd_update_ref(int argc, const char **argv, const char *prefix) · line 443

Quoted 8 lines
builtin/update-ref.c +4 -1
@@ -429,7 +437,12 @@ int cmd_update_ref(int argc, const char **argv, const char *prefix)
 		return delete_ref(refname,
 				  (oldval && !is_null_sha1(oldsha1)) ? oldsha1 : NULL,
 				  flags);
-	else
+	} else {
+		struct strbuf err = STRBUF_INIT;
+		if (create_reflog && safe_create_reflog(refname, &err, 1))
+			die("failed to create reflog for %s: %s", refname, err.buf);

This reflog also should not be left around if the reference does not exist at the end of this command.

t/t1400-update-ref.sh

Quoted 10 lines
builtin/update-ref.c +2 -0
+
 		return update_ref(msg, refname, sha1, oldval ? oldsha1 : NULL,
 				  flags, UPDATE_REFS_DIE_ON_ERR);
+	}
 }
t/t1400-update-ref.sh +0 -0
index d787bf5..9d21c19 100755
[...]

Michael

Signature · 2 lines
Michael Haggerty
[email protected]
#24 lore raw

PATCH v7 8/8 git-stash: use update-ref --create-reflog instead of creating files

In reply to David Turner

This is in support of alternate ref backends which don't necessarily store reflogs as files.

---
 git-stash.sh | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
git-stash.sh +2 -4
index 8e9e2cd..1d5ba7a 100755
@@ -183,9 +183,7 @@ store_stash () {
 		stash_msg="Created via \"git stash store\"."
 	fi
 
-	# Make sure the reflog for stash is kept.
-	: >>"$(git rev-parse --git-path logs/$ref_stash)"
-	git update-ref -m "$stash_msg" $ref_stash $w_commit
+	git update-ref --create-reflog -m "$stash_msg" $ref_stash $w_commit
 	ret=$?
 	test $ret != 0 && test -z $quiet &&
 	die "$(eval_gettext "Cannot update \$ref_stash with \$w_commit")"
@@ -262,7 +260,7 @@ save_stash () {
 		say "$(gettext "No local changes to save")"
 		exit 0
 	fi
-	test -f "$(git rev-parse --git-path logs/$ref_stash)" ||
+	git reflog exists $ref_stash ||
 		clear_stash || die "$(gettext "Cannot initialize stash")"
 
 	create_stash "$stash_msg" $untracked
Signature · 1 line
2.0.5.499.g01f6352.dirty-twtrsrc
#25 lore raw

Re: [PATCH v7 8/8] git-stash: use update-ref --create-reflog instead of creating files

I apologize for getting involved so late in the evolution of this patch series. Thanks for being so patient with the back-and-forth!

I'm really glad that you are working on this. It is important that this area gets cleaned up if we are ever to support alternate reference backends. And I'm happy that we will be getting new commands and command options to deal with reflogs, because that was one case were users previously had to muck about within the .git directory, which is a very bad thing. We need to train users never to look behind the curtain!

Michael

Signature · 2 lines
Michael Haggerty
[email protected]
#26 lore raw

Re: [PATCH v7 8/8] git-stash: use update-ref --create-reflog instead of creating files

In reply to David Turner

By the way, a series with 8 patches, especially that needed more than a couple of rerolls, deserve a separate cover-letter, if only to describe what was changed since the previous rounds and pointers to them to help reviewers.

Thanks.

#27 lore raw

Re: [PATCH v7 8/8] git-stash: use update-ref --create-reflog instead of creating files

In reply to David Turner
Quoted 5 lines · David Turner

This is in support of alternate ref backends which don't necessarily store reflogs as files.

---

Thanks. The last round was already a pleasant read, and I didn't spot anything questionable in the interdiff between the last one and this round (although I admit that I maybe a lot less careful at this time of the day ;-).

Michael?

#28 lore raw
In reply to David Turner
Quoted 6 lines · David Turner

Add an err argument to log_ref_setup that can explain the reason for a failure. This then eliminates the need to manage errno through this function since we can just add strerror(errno) to the err string when meaningful. No callers relied on errno from this function for anything else than the error message.

Quoted 7 lines

Also add err arguments to private functions write_ref_to_lockfile, log_ref_write_1, commit_ref_update. This again eliminates the need to manage errno in these functions.

Some error messages change slightly. For instance, we sometimes lose "cannot update ref" and instead only show the specific cause of ref update failure.

Did you check that the new error messages are at least as clear to the user as the old ones? (Sometimes errors from deep in the call stack provide details but don't make it clear how the details connect back to the action that the user was trying to do.)

Quoted 17 lines

Update of a patch by Ronnie Sahlberg.

---
 builtin/checkout.c |   8 ++--
 refs.c             | 130 +++++++++++++++++++++++++++++------------------------
 refs.h             |   4 +-
 3 files changed, 79 insertions(+), 63 deletions(-)

[...]

refs.c +0 -0
index fb568d7..e891bed 100644
[...]

refs.c static int write_ref_to_lockfile(struct ref_lock *lock, · line 3296

Quoted 8 lines
refs.c +3 -1
@@ -3288,14 +3290,20 @@ static int write_ref_to_lockfile(struct ref_lock *lock,
  * necessary, using the specified lockmsg (which can be NULL).
  */
 static int commit_ref_update(struct ref_lock *lock,
-			     const unsigned char *sha1, const char *logmsg)
+			     const unsigned char *sha1, const char *logmsg,
+			     struct strbuf *err)
 {
+	int result = 0;

Please put a blank line between local variable definitions and the start of code.

Quoted 5 lines
refs.c +1 -2
 	clear_loose_ref_cache(&ref_cache);
-	if (log_ref_write(lock->ref_name, lock->old_oid.hash, sha1, logmsg) < 0 ||
+	if (log_ref_write(lock->ref_name, lock->old_oid.hash, sha1, logmsg, err) < 0 ||
 	    (strcmp(lock->ref_name, lock->orig_ref_name) &&
-	     log_ref_write(lock->orig_ref_name, lock->old_oid.hash, sha1, logmsg) < 0)) {

refs.c static int write_ref_to_lockfile(struct ref_lock *lock, · line 3307

Quoted 10 lines
refs.c +7 -2
-		unlock_ref(lock);
-		return -1;
+	     log_ref_write(lock->orig_ref_name, lock->old_oid.hash, sha1, logmsg, err) < 0)) {
+		char *old_msg = strbuf_detach(err, NULL);
+		strbuf_addf(err, "Cannot update the ref '%s': '%s'",
+			    lock->ref_name, old_msg);
+		free(old_msg);
+		result = -1;
+		goto done;
 	}

I see this code already did what I complained about in my earlier email [*]: fail a reference transaction after it is already partly committed. In my opinion this is incorrect for the reasons stated there. But you don't have to consider it to be in the scope of your patch series.

[*] http://article.gmane.org/gmane.comp.version-control.git/273668

refs.c static int write_ref_to_lockfile(struct ref_lock *lock, · line 3309

Quoted 3 lines
refs.c +0 -0
 	if (strcmp(lock->orig_ref_name, "HEAD") != 0) {
 		/*
[...]

Michael

Signature · 2 lines
Michael Haggerty
[email protected]
#3 lore raw

Re: [PATCH v7 2/8] cherry-pick: treat CHERRY_PICK_HEAD and REVERT_HEAD as refs

In reply to Johannes Sixt
Quoted 19 lines · Johannes Sixt

Am 08.07.2015 um 02:55 schrieb David Turner:

Quoted 6 lines

Instead of directly writing to and reading from files in $GIT_DIR, use ref API to interact with CHERRY_PICK_HEAD and REVERT_HEAD.

---

...

Quoted 11 lines
contrib/completion/git-prompt.sh +1 -1
index 366f0bc..e2c5583 100644
@@ -415,9 +415,9 @@ __git_ps1 ()
  			fi
  		elif [ -f "$g/MERGE_HEAD" ]; then
  			r="|MERGING"
-		elif [ -f "$g/CHERRY_PICK_HEAD" ]; then
+		elif git rev-parse --quiet --verify "CHERRY_PICK_HEAD" >/dev/null; then
  			r="|CHERRY-PICKING"
Quoted 8 lines

contrib/completion/git-prompt.sh __git_ps1 () · line 423

Quoted 5 lines
contrib/completion/git-prompt.sh +1 -1
-		elif [ -f "$g/REVERT_HEAD" ]; then
+		elif git rev-parse --quiet --verify "REVERT_HEAD" >/dev/null; then
  			r="|REVERTING"
  		elif [ -f "$g/BISECT_LOG" ]; then
  			r="|BISECTING"

We are trying very hard not to spawn any new processes in __git_ps1(). So, I raise a moderate veto against this hunk.

Do you have an alternate suggestion about how to accomplish the same thing? Here are my ideas:

We could special-case CHERRY_PICK_HEAD and REVERT_HEAD to be files independent of the ref backend, but that tends to complicate the backends. I think this is a mistake.

We could reduce the number from two to one by providing a new git-am-status command which outputs one of "CHERRY-PICKING", "REVERTING", or "" (or maybe it would also handle rebase and am). We could also generalize it to "git-prompt-helper" or something by moving that entire bunch of if statements inside. This would replace calls to "git describe".

But you probably have a better idea.

#4 lore raw

Re: [PATCH v7 2/8] cherry-pick: treat CHERRY_PICK_HEAD and REVERT_HEAD as refs

Am 08.07.2015 um 21:16 schrieb David Turner:

Quoted 36 lines · Johannes Sixt
Quoted 19 lines

Am 08.07.2015 um 02:55 schrieb David Turner:

Quoted 6 lines

Instead of directly writing to and reading from files in $GIT_DIR, use ref API to interact with CHERRY_PICK_HEAD and REVERT_HEAD.

---

...

Quoted 11 lines
contrib/completion/git-prompt.sh +1 -1
index 366f0bc..e2c5583 100644
@@ -415,9 +415,9 @@ __git_ps1 ()
   			fi
   		elif [ -f "$g/MERGE_HEAD" ]; then
   			r="|MERGING"
-		elif [ -f "$g/CHERRY_PICK_HEAD" ]; then
+		elif git rev-parse --quiet --verify "CHERRY_PICK_HEAD" >/dev/null; then
   			r="|CHERRY-PICKING"
Quoted 8 lines

contrib/completion/git-prompt.sh __git_ps1 () · line 423

Quoted 5 lines
contrib/completion/git-prompt.sh +1 -1
-		elif [ -f "$g/REVERT_HEAD" ]; then
+		elif git rev-parse --quiet --verify "REVERT_HEAD" >/dev/null; then
   			r="|REVERTING"
   		elif [ -f "$g/BISECT_LOG" ]; then
   			r="|BISECTING"

We are trying very hard not to spawn any new processes in __git_ps1(). So, I raise a moderate veto against this hunk.

Do you have an alternate suggestion about how to accomplish the same thing? Here are my ideas:

We could special-case CHERRY_PICK_HEAD and REVERT_HEAD to be files independent of the ref backend, but that tends to complicate the backends. I think this is a mistake.

Quoted 8 lines

We could reduce the number from two to one by providing a new git-am-status command which outputs one of "CHERRY-PICKING", "REVERTING", or "" (or maybe it would also handle rebase and am). We could also generalize it to "git-prompt-helper" or something by moving that entire bunch of if statements inside. This would replace calls to "git describe".

But you probably have a better idea.

Isn't it mere coincidence that the content of these two files looks like a non-packed ref? Wouldn't it be better to consider the two akin to MERGE_HEAD (which is not a ref because it records more than just a commit name)?

-- Hannes

#5 lore raw

Re: [PATCH v7 2/8] cherry-pick: treat CHERRY_PICK_HEAD and REVERT_HEAD as refs

Quoted 44 lines · Johannes Sixt

Am 08.07.2015 um 21:16 schrieb David Turner:

Quoted 43 lines · Johannes Sixt
Quoted 19 lines

Am 08.07.2015 um 02:55 schrieb David Turner:

Quoted 6 lines

Instead of directly writing to and reading from files in $GIT_DIR, use ref API to interact with CHERRY_PICK_HEAD and REVERT_HEAD.

---

...

Quoted 11 lines
contrib/completion/git-prompt.sh +1 -1
index 366f0bc..e2c5583 100644
@@ -415,9 +415,9 @@ __git_ps1 ()
   			fi
   		elif [ -f "$g/MERGE_HEAD" ]; then
   			r="|MERGING"
-		elif [ -f "$g/CHERRY_PICK_HEAD" ]; then
+		elif git rev-parse --quiet --verify "CHERRY_PICK_HEAD" >/dev/null; then
   			r="|CHERRY-PICKING"
Quoted 8 lines

contrib/completion/git-prompt.sh __git_ps1 () · line 423

Quoted 5 lines
contrib/completion/git-prompt.sh +1 -1
-		elif [ -f "$g/REVERT_HEAD" ]; then
+		elif git rev-parse --quiet --verify "REVERT_HEAD" >/dev/null; then
   			r="|REVERTING"
   		elif [ -f "$g/BISECT_LOG" ]; then
   			r="|BISECTING"

We are trying very hard not to spawn any new processes in __git_ps1(). So, I raise a moderate veto against this hunk.

Do you have an alternate suggestion about how to accomplish the same thing? Here are my ideas:

We could special-case CHERRY_PICK_HEAD and REVERT_HEAD to be files independent of the ref backend, but that tends to complicate the backends. I think this is a mistake.

We could reduce the number from two to one by providing a new git-am-status command which outputs one of "CHERRY-PICKING", "REVERTING", or "" (or maybe it would also handle rebase and am). We could also generalize it to "git-prompt-helper" or something by moving that entire bunch of if statements inside. This would replace calls to "git describe".

Quoted 6 lines
Quoted 1 line

But you probably have a better idea.

Isn't it mere coincidence that the content of these two files looks like a non-packed ref? Wouldn't it be better to consider the two akin to MERGE_HEAD (which is not a ref because it records more than just a commit name)?

There appears to be a bit of code that assumes they can be read as refs, and the tests test this functionality. Indeed, I wrote most of this code by replacing the backend and fixing failing tests.

At least some people seem to use the rev-ness of CHERRY_PICK_HEAD:
https://mnaoumov.wordpress.com/2014/12/03/bulk-cherry-picking-process-and-magic-ref-cherry_pick_head/

So I think it's best to keep the feature.

#6 lore raw

Re: [PATCH v7 2/8] cherry-pick: treat CHERRY_PICK_HEAD and REVERT_HEAD as refs

In reply to Johannes Sixt
Quoted 7 lines · Johannes Sixt
Quoted 7 lines

We could reduce the number from two to one by providing a new git-am-status command which outputs one of "CHERRY-PICKING", "REVERTING", or "" (or maybe it would also handle rebase and am). We could also generalize it to "git-prompt-helper" or something by moving that entire bunch of if statements inside. This would replace calls to "git describe".

Quoted 6 lines
Quoted 1 line

But you probably have a better idea.

Isn't it mere coincidence that the content of these two files looks like a non-packed ref? Wouldn't it be better to consider the two akin to MERGE_HEAD (which is not a ref because it records more than just a commit name)?

That is an excellent thought that seems to have escaped everybody involved in the review.

These things do not behave like refs. They do not want reflogs (and even if they had, the log would not mean much), and if we want to add more information on the cherry-pick and revert in progress, they are the most natural place to do so.

Another thing that makes me vote for treating them just like FETCH_HEAD, MERGE_HEAD and other ALL_CAPS files like COMMIT_MSG is what should happen in a repository with more than one working trees. You do not want to share what "CHERRY_PICK_HEAD" means across them only because they happen to record an object name.

Thanks for a bit of sanity.

#7 lore raw

Re: [PATCH v7 2/8] cherry-pick: treat CHERRY_PICK_HEAD and REVERT_HEAD as refs

Quoted 24 lines · Junio C Hamano
Quoted 7 lines · Johannes Sixt
Quoted 7 lines

We could reduce the number from two to one by providing a new git-am-status command which outputs one of "CHERRY-PICKING", "REVERTING", or "" (or maybe it would also handle rebase and am). We could also generalize it to "git-prompt-helper" or something by moving that entire bunch of if statements inside. This would replace calls to "git describe".

Quoted 6 lines
Quoted 1 line

But you probably have a better idea.

Isn't it mere coincidence that the content of these two files looks like a non-packed ref? Wouldn't it be better to consider the two akin to MERGE_HEAD (which is not a ref because it records more than just a commit name)?

That is an excellent thought that seems to have escaped everybody involved in the review.

These things do not behave like refs. They do not want reflogs (and even if they had, the log would not mean much), and if we want to add more information on the cherry-pick and revert in progress, they are the most natural place to do so.

Quoted 5 lines

Another thing that makes me vote for treating them just like FETCH_HEAD, MERGE_HEAD and other ALL_CAPS files like COMMIT_MSG is what should happen in a repository with more than one working trees. You do not want to share what "CHERRY_PICK_HEAD" means across them only because they happen to record an object name.

I didn't see this until after I had sent my previous message. I think the "multiple working trees" argument is strong enough that I will change the code (and tests).

#8 lore raw

Re: [PATCH v7 2/8] cherry-pick: treat CHERRY_PICK_HEAD and REVERT_HEAD as refs

Quoted 3 lines · David Turner

I didn't see this until after I had sent my previous message. I think the "multiple working trees" argument is strong enough that I will change the code (and tests).

Not just code, but we probably should step back a bit and clearly define what we are trying to achieve. I _think_ what we want are:

- Everything under refs/* and their associated logs would be handed off to the "pluggable ref backend" when one is in use.

- All ref-like things with one-level ALL_CAPS names are per working tree.

- They do not participate in "prunable?" reachability computation.
- They (typically) do not want "logs".
- Each may have extra information specific to it.
- You can however read an object name off of them.

One possible and straight-forward implementation to achieve "ref-like things with one-level ALL_CAPS names are per working tree" is to declare that they will not be handed off to the backend, but will always be implemented as files immediately under $GIT_DIR/.

But note that there is no fundamental reason we have to do it that way; an alternative would be to allow backends to store these things per working tree, but then the interface to drive backends need to tell them which working tree we are working from.

Unlike branches, HEAD must be per working tree; the "pluggable ref backend" needs to be able handle HEAD when you introduce it. So from that point of view, "multiple working tree" is *not* a valid argument why they *have* to be implemented as files under $GIT_DIR/. If you plan to let the pluggable ref backend to handle HEAD, you must have a solution for per working tree ref-like things anyway.

As to J6t's "no excessive plumbing invocations", I think the right approach is to have a single "git prompt--helper" command that does what the current script does to compute $r. "we want to keep peeking into files under $GIT_DIR/" alone is not a valid enough reason to constrain us (I am fine if the solution we find appropriate for the 'multiple working trees' and other issues ends up being "we solve it by having them as files in $GIT_DIR" for other reasons, though).

#9 lore raw

Re: [PATCH v7 2/8] cherry-pick: treat CHERRY_PICK_HEAD and REVERT_HEAD as refs

Quoted 12 lines · Junio C Hamano
Quoted 3 lines · David Turner

I didn't see this until after I had sent my previous message. I think the "multiple working trees" argument is strong enough that I will change the code (and tests).

Not just code, but we probably should step back a bit and clearly define what we are trying to achieve. I _think_ what we want are:

- Everything under refs/* and their associated logs would be handed off to the "pluggable ref backend" when one is in use.

Quoted 7 lines

- All ref-like things with one-level ALL_CAPS names are per working tree.

- They do not participate in "prunable?" reachability computation.

- They (typically) do not want "logs".

Except HEAD definitely does.

Quoted 8 lines

- Each may have extra information specific to it.
- You can however read an object name off of them.

One possible and straight-forward implementation to achieve "ref-like things with one-level ALL_CAPS names are per working tree" is to declare that they will not be handed off to the backend, but will always be implemented as files immediately under $GIT_DIR/.

Quoted 7 lines

But note that there is no fundamental reason we have to do it that way; an alternative would be to allow backends to store these things per working tree, but then the interface to drive backends need to tell them which working tree we are working from.

Unlike branches, HEAD must be per working tree; the "pluggable ref backend" needs to be able handle HEAD when you introduce it.

I actually punted on this in my implementation, because at the time, git-new-workdir was only in contrib. But since the new worktree stuff, multiple worktrees have first-class support, so I'll have to update the code to handle it.

Quoted 5 lines

So
from that point of view, "multiple working tree" is *not* a valid argument why they *have* to be implemented as files under $GIT_DIR/. If you plan to let the pluggable ref backend to handle HEAD, you must have a solution for per working tree ref-like things anyway.

OK, here's my current best idea:

1. A "pseudoref" is an all-caps file in $GIT_DIR/ that always contains at least a SHA1. CHERRY_PICK_HEAD and REVERT_HEAD are examples. Because HEAD might be a symbolic ref, it is not a pseudoref.

Refs backends do not manage pseudorefs. Instead, when a pseudoref (an all-caps ref containing no slashes) is requested (e.g. git rev-parse FETCH_HEAD) the generic refs code checks for the existence of that file and if it exists, returns immediately without hitting the backend. The generic code will refuse to allow updates to pseudorefs.

2. The pluggable refs backend manages all refs other than HEAD.

3. The "files" backend always manages HEAD. This allows for a reflog and for HEAD to be a symbolic ref.

The major complication here is ref transactions -- what if there's a transaction that wants to update e.g. both HEAD and refs/heads/master?

It may be the case that this never happens; I have not actually audited the code to figure it out. If someone knows for sure that it does not happen, please say so. But assuming it does happen, here's my idea:

If the refs backend is the files backend, we can simply treat HEAD like any other ref.

If the refs backend is different, then the refs code needs to hold a files-backend transaction for HEAD, which it will commit immediately after the other transaction succeeds. We can stick a pointer to the extra transaction in the generic struct ref_transaction, which (as Michael Haggerty suggests) specific backends will extend.

A failure to commit either transaction will be reported as a failure, and we'll give an additional inconsistent state warning if the main transaction succeeds but the HEAD transaction fails.

I don't love this idea -- it seems like kind of a hack. But it's the best I can come up with.

What do other folks think?

Quoted 8 lines

As to J6t's "no excessive plumbing invocations", I think the right approach is to have a single "git prompt--helper" command that does what the current script does to compute $r. "we want to keep peeking into files under $GIT_DIR/" alone is not a valid enough reason to constrain us (I am fine if the solution we find appropriate for the 'multiple working trees' and other issues ends up being "we solve it by having them as files in $GIT_DIR" for other reasons, though).

Agree.

#10 lore raw

Re: [PATCH v7 2/8] cherry-pick: treat CHERRY_PICK_HEAD and REVERT_HEAD as refs

Quoted 12 lines · David Turner

OK, here's my current best idea:

1. A "pseudoref" is an all-caps file in $GIT_DIR/ that always contains at least a SHA1. CHERRY_PICK_HEAD and REVERT_HEAD are examples. Because HEAD might be a symbolic ref, it is not a pseudoref.

Refs backends do not manage pseudorefs. Instead, when a pseudoref (an all-caps ref containing no slashes) is requested (e.g. git rev-parse FETCH_HEAD) the generic refs code checks for the existence of that file and if it exists, returns immediately without hitting the backend. The generic code will refuse to allow updates to pseudorefs.

Quoted 7 lines

2. The pluggable refs backend manages all refs other than HEAD.

3. The "files" backend always manages HEAD. This allows for a reflog and for HEAD to be a symbolic ref.

The major complication here is ref transactions -- what if there's a transaction that wants to update e.g. both HEAD and refs/heads/master?

An update to the current branch (e.g. "git commit") does involve at least update to the reflog of HEAD, the current branch somewhere in refs/heads/ and its log, so it is not "what if" but is a norm [*1*].

Quoted 14 lines

It may be the case that this never happens; I have not actually audited the code to figure it out. If someone knows for sure that it does not happen, please say so. But assuming it does happen, here's my idea:

If the refs backend is the files backend, we can simply treat HEAD like any other ref.

If the refs backend is different, then the refs code needs to hold a files-backend transaction for HEAD, which it will commit immediately after the other transaction succeeds. We can stick a pointer to the extra transaction in the generic struct ref_transaction, which (as Michael Haggerty suggests) specific backends will extend.

Quoted 3 lines

A failure to commit either transaction will be reported as a failure, and we'll give an additional inconsistent state warning if the main transaction succeeds but the HEAD transaction fails.

Yeah, I was thinking along those lines, too. Thanks for clearly writing it down.

Quoted 1 line

What do other folks think?

Me too ;-)

[Footnote]

*1* But that is not a complaint; I do not have a better idea myself either.

#11 lore raw

Re: [PATCH v7 2/8] cherry-pick: treat CHERRY_PICK_HEAD and REVERT_HEAD as refs

Quoted 19 lines · Junio C Hamano
Quoted 17 lines · David Turner

OK, here's my current best idea:

1. A "pseudoref" is an all-caps file in $GIT_DIR/ that always contains at least a SHA1. CHERRY_PICK_HEAD and REVERT_HEAD are examples. Because HEAD might be a symbolic ref, it is not a pseudoref.

Refs backends do not manage pseudorefs. Instead, when a pseudoref (an all-caps ref containing no slashes) is requested (e.g. git rev-parse FETCH_HEAD) the generic refs code checks for the existence of that file and if it exists, returns immediately without hitting the backend. The generic code will refuse to allow updates to pseudorefs.

2. The pluggable refs backend manages all refs other than HEAD.

3. The "files" backend always manages HEAD. This allows for a reflog and for HEAD to be a symbolic ref.

Quoted 6 lines
Quoted 2 lines

The major complication here is ref transactions -- what if there's a transaction that wants to update e.g. both HEAD and refs/heads/master?

An update to the current branch (e.g. "git commit") does involve at least update to the reflog of HEAD, the current branch somewhere in refs/heads/ and its log, so it is not "what if" but is a norm [*1*].

The updating of symlink reflogs in general, and particularly that of HEAD, is not done very cleanly. You can see the code in commit_ref_update() (some of it helpfully commented to be a "Special hack"):

* If a reference is modified through a symlink, the symlink is locked rather than the reference itself.
* If a reference is modified directly, and HEAD points at it, then the HEAD reflog is amended without locking HEAD.

Aside from the lack of proper locking, which could result in races with other processes, we also have the problem that the same reference that is being changed via one of these implicit updates could *also* be being changed directly in the same transaction. Such an update would evade the ref_update_reject_duplicates() check.

Previously my thinking was that the locking should be done differently:
when the transaction is being processed, extra ref_update records could be created for the extra reference(s) that have to be modified, then these could be handled more straightforwardly. So supposing that HEAD points at refs/heads/master,

* An update of HEAD would be turned into a reflog update and also add a synthetic update to refs/heads/master.
* An update of refs/heads/master would add a synthetic update to the HEAD reflog

The first point would obviously apply to any updates via symbolic refs. The second one should too, thought this is a case that we currently punt on to avoid the need to do reverse symbolic ref lookups.

Quoted 17 lines
Quoted 13 lines

It may be the case that this never happens; I have not actually audited the code to figure it out. If someone knows for sure that it does not happen, please say so. But assuming it does happen, here's my idea:

If the refs backend is the files backend, we can simply treat HEAD like any other ref.

If the refs backend is different, then the refs code needs to hold a files-backend transaction for HEAD, which it will commit immediately after the other transaction succeeds. We can stick a pointer to the extra transaction in the generic struct ref_transaction, which (as Michael Haggerty suggests) specific backends will extend.

Quoted 3 lines

A failure to commit either transaction will be reported as a failure, and we'll give an additional inconsistent state warning if the main transaction succeeds but the HEAD transaction fails.

Quoted 6 lines

Yeah, I was thinking along those lines, too. Thanks for clearly writing it down.

Quoted 1 line

What do other folks think?

Me too ;-)

I don't have an answer right now, and I have to get on an airplane in a few hours so I can't think hard about it at the moment. But let me also braindump another vague plan that I have had for a long time:
overlayable reference storage schemes. Think of the way that loose refs are currently overlaid on top of packed refs. I think it might be useful to support overlaying more generally.

In this particular case there could be a workspace-local reference storage that only handles HEAD and perhaps some of the other pseudoreferences. That could be overlaid onto loose reference storage (which would then only concern itself with references under "refs/"), which would in turn be overlaid onto packed refs. The workspace-local reference storage layer would have evil special-cased code for dealing with the references that live outside of "refs/".

A ref_transaction_commit() would be broken into phases: first each of the stacked backends would be asked to verify that the transaction is possible and acquire any necessary locks, then each backend would get the final "commit" command.

This construct would make it easy for different backends to share the same implementation for HEAD (and potentially other workspace-local) references, by simply layering that one storage mechanism on top of their own.

That would probably be overengineering if it were only used to deal with HEAD, but I think it is a nice general mechanism that could have other applications.

Michael

Signature · 2 lines
Michael Haggerty
[email protected]
#23 lore raw

Re: [PATCH v7 7/8] update-ref and tag: add --create-reflog arg

In reply to Michael Haggerty

On Wed, 2015-07-08 at 15:44 +0200, Michael Haggerty wrote:
<snip>

Quoted 2 lines

Should the "verify" command also create a reflog, at least if the reference is not being verified to be missing?

I don't see why it should. "verify" does not sound like a command that should change anything.

#29 lore raw
In reply to Michael Haggerty
Quoted 11 lines · Michael Haggerty
Quoted 10 lines · David Turner

Add an err argument to log_ref_setup that can explain the reason for a failure. This then eliminates the need to manage errno through this function since we can just add strerror(errno) to the err string when meaningful. No callers relied on errno from this function for anything else than the error message.

Also add err arguments to private functions write_ref_to_lockfile, log_ref_write_1, commit_ref_update. This again eliminates the need to manage errno in these functions.

Quoted 8 lines
Quoted 3 lines

Some error messages change slightly. For instance, we sometimes lose "cannot update ref" and instead only show the specific cause of ref update failure.

Did you check that the new error messages are at least as clear to the user as the old ones? (Sometimes errors from deep in the call stack provide details but don't make it clear how the details connect back to the action that the user was trying to do.)

They seem clear enough to me, but I guess there's no reason not to keep the "cannot update ref" bit. Will fix, along with the rest.

#12 lore raw

Re: [PATCH v7 2/8] cherry-pick: treat CHERRY_PICK_HEAD and REVERT_HEAD as refs

In reply to Michael Haggerty
Quoted 49 lines · Michael Haggerty
Quoted 19 lines · Junio C Hamano
Quoted 17 lines · David Turner

OK, here's my current best idea:

1. A "pseudoref" is an all-caps file in $GIT_DIR/ that always contains at least a SHA1. CHERRY_PICK_HEAD and REVERT_HEAD are examples. Because HEAD might be a symbolic ref, it is not a pseudoref.

Refs backends do not manage pseudorefs. Instead, when a pseudoref (an all-caps ref containing no slashes) is requested (e.g. git rev-parse FETCH_HEAD) the generic refs code checks for the existence of that file and if it exists, returns immediately without hitting the backend. The generic code will refuse to allow updates to pseudorefs.

2. The pluggable refs backend manages all refs other than HEAD.

3. The "files" backend always manages HEAD. This allows for a reflog and for HEAD to be a symbolic ref.

Quoted 6 lines
Quoted 2 lines

The major complication here is ref transactions -- what if there's a transaction that wants to update e.g. both HEAD and refs/heads/master?

An update to the current branch (e.g. "git commit") does involve at least update to the reflog of HEAD, the current branch somewhere in refs/heads/ and its log, so it is not "what if" but is a norm [*1*].

The updating of symlink reflogs in general, and particularly that of HEAD, is not done very cleanly. You can see the code in commit_ref_update() (some of it helpfully commented to be a "Special hack"):

* If a reference is modified through a symlink, the symlink is locked rather than the reference itself.
* If a reference is modified directly, and HEAD points at it, then the HEAD reflog is amended without locking HEAD.

Aside from the lack of proper locking, which could result in races with other processes, we also have the problem that the same reference that is being changed via one of these implicit updates could *also* be being changed directly in the same transaction. Such an update would evade the ref_update_reject_duplicates() check.

Previously my thinking was that the locking should be done differently:
when the transaction is being processed, extra ref_update records could be created for the extra reference(s) that have to be modified, then these could be handled more straightforwardly. So supposing that HEAD points at refs/heads/master,

Quoted 8 lines

* An update of HEAD would be turned into a reflog update and also add a synthetic update to refs/heads/master.
* An update of refs/heads/master would add a synthetic update to the HEAD reflog

The first point would obviously apply to any updates via symbolic refs. The second one should too, thought this is a case that we currently punt on to avoid the need to do reverse symbolic ref lookups.

All of this is worth fixing, but I don't know that it needs to be fixed before ref backends hit. What do you think?

Quoted 44 lines
Quoted 17 lines
Quoted 13 lines

It may be the case that this never happens; I have not actually audited the code to figure it out. If someone knows for sure that it does not happen, please say so. But assuming it does happen, here's my idea:

If the refs backend is the files backend, we can simply treat HEAD like any other ref.

If the refs backend is different, then the refs code needs to hold a files-backend transaction for HEAD, which it will commit immediately after the other transaction succeeds. We can stick a pointer to the extra transaction in the generic struct ref_transaction, which (as Michael Haggerty suggests) specific backends will extend.

Quoted 3 lines

A failure to commit either transaction will be reported as a failure, and we'll give an additional inconsistent state warning if the main transaction succeeds but the HEAD transaction fails.

Quoted 6 lines

Yeah, I was thinking along those lines, too. Thanks for clearly writing it down.

Quoted 1 line

What do other folks think?

Me too ;-)

I don't have an answer right now, and I have to get on an airplane in a few hours so I can't think hard about it at the moment. But let me also braindump another vague plan that I have had for a long time:
overlayable reference storage schemes. Think of the way that loose refs are currently overlaid on top of packed refs. I think it might be useful to support overlaying more generally.

In this particular case there could be a workspace-local reference storage that only handles HEAD and perhaps some of the other pseudoreferences. That could be overlaid onto loose reference storage (which would then only concern itself with references under "refs/"), which would in turn be overlaid onto packed refs. The workspace-local reference storage layer would have evil special-cased code for dealing with the references that live outside of "refs/".

A ref_transaction_commit() would be broken into phases: first each of the stacked backends would be asked to verify that the transaction is possible and acquire any necessary locks, then each backend would get the final "commit" command.

Quoted 8 lines

This construct would make it easy for different backends to share the same implementation for HEAD (and potentially other workspace-local) references, by simply layering that one storage mechanism on top of their own.

That would probably be overengineering if it were only used to deal with HEAD, but I think it is a nice general mechanism that could have other applications.

Interesting concept. I think the semantics could get rather complicated, but maybe it's worth thinking about.

But for now, I think it would be better to special-case pseudorefs, with the option to expand that to full layering later if we see a need.

#13 lore raw

Re: [PATCH v7 2/8] cherry-pick: treat CHERRY_PICK_HEAD and REVERT_HEAD as refs

In reply to Michael Haggerty

[j6t to bcc as it looks like his concerns have been addressed]

Quoted 37 lines · Michael Haggerty
Quoted 19 lines · Junio C Hamano
Quoted 17 lines · David Turner

OK, here's my current best idea:

1. A "pseudoref" is an all-caps file in $GIT_DIR/ that always contains at least a SHA1. CHERRY_PICK_HEAD and REVERT_HEAD are examples. Because HEAD might be a symbolic ref, it is not a pseudoref.

Refs backends do not manage pseudorefs. Instead, when a pseudoref (an all-caps ref containing no slashes) is requested (e.g. git rev-parse FETCH_HEAD) the generic refs code checks for the existence of that file and if it exists, returns immediately without hitting the backend. The generic code will refuse to allow updates to pseudorefs.

2. The pluggable refs backend manages all refs other than HEAD.

3. The "files" backend always manages HEAD. This allows for a reflog and for HEAD to be a symbolic ref.

Quoted 6 lines
Quoted 2 lines

The major complication here is ref transactions -- what if there's a transaction that wants to update e.g. both HEAD and refs/heads/master?

An update to the current branch (e.g. "git commit") does involve at least update to the reflog of HEAD, the current branch somewhere in refs/heads/ and its log, so it is not "what if" but is a norm [*1*].

The updating of symlink reflogs in general, and particularly that of HEAD, is not done very cleanly. You can see the code in commit_ref_update() (some of it helpfully commented to be a "Special hack"):

* If a reference is modified through a symlink, the symlink is locked rather than the reference itself.
* If a reference is modified directly, and HEAD points at it, then the HEAD reflog is amended without locking HEAD.

Quoted 5 lines

Aside from the lack of proper locking, which could result in races with other processes, we also have the problem that the same reference that is being changed via one of these implicit updates could *also* be being changed directly in the same transaction. Such an update would evade the ref_update_reject_duplicates() check.

On reflection, I'm not sure my approach makes sense. The problem is that refs backends internally manage recursive updates to symbolic refs, so it is not easy to send update for HEAD to one backend while sending the corresponding refs/heads/master updates to a different one.

So I am thinking instead that backends should be required to manage updates to HEAD themselves, and that some functions from refs-be-files would be made generic to help with this.

For the LMDB backend, I could put most of that code at the LMDB access layer (which presently simply converts all LMDB errors other than NOT_FOUND to calls to die()). I would intercept reads and writes to HEAD and logs/HEAD and replace them with calls to the appropriate functions. So, for instance, the LMDB backend would still decide whether to write HEAD, but it would delegate to the files backend code to actually write it. Reflog updates are a bit more complicated, because we might end up using a different reflog format for LMDB vs for the files backend, but since all reflog updates are controlled by the backend, it should still be possible to handle this cleanly.

On reflection, I think that this would also be a reasonable approach to take for stash, which does not fall into any of the listed categories. It's not a pseudoref because it's not all-caps and it starts with refs/. Unlike other pseudorefs, it needs a reflog. But like HEAD and unlike other refs, it (and its reflog) wants to be per-worktree. Are there other ref-like-things in this category (which I'll call "per-worktree refs")? I guess one set of these is submodules' HEADs. I have been planning on punting on these because it looks to me like that's what the files backend does. So, let's leave those aside. I don't think of any others but maybe I'm missing something.

I'm also not sure how worktrees handle HEAD's reflog -- there doesn't seem to be anything worktree-specific in refs.c. But perhaps I'm just not understanding that bit of the code.

#14 lore raw

Re: [PATCH v7 2/8] cherry-pick: treat CHERRY_PICK_HEAD and REVERT_HEAD as refs

Quoted 7 lines · David Turner

So I am thinking instead that backends should be required to manage updates to HEAD themselves, and that some functions from refs-be-files would be made generic to help with this.
...
For the LMDB backend, I could put most of that code at the LMDB access
...
backend, it should still be possible to handle this cleanly.

Sounds sensible.

Quoted 5 lines

On reflection, I think that this would also be a reasonable approach to take for stash, which does not fall into any of the listed categories. It's not a pseudoref because it's not all-caps and it starts with refs/. Unlike other pseudorefs, it needs a reflog. But like HEAD and unlike other refs, it (and its reflog) wants to be per-worktree.

I think we want stash, unlike HEAD, to be shared across worktrees, and contrib/workdir gets this right.

So there is nothing that makes refs/stash like CHERRY_PICK_HEAD at all.

Quoted 3 lines

Are there
other ref-like-things in this category (which I'll call "per-worktree refs")? I guess one set of these is submodules' HEADs.

I've been assuming that a secondary worktree of superproject will get its own & corresponding secondary worktree of a submodule, which would automatically give per-worktree "submodule's HEADs" and everything else that has to be per-worktree. Am I missing something or are there any more need for underlying machinery than what we currently have for secondary worktrees for a single project tree?

#15 lore raw

Re: [PATCH v7 2/8] cherry-pick: treat CHERRY_PICK_HEAD and REVERT_HEAD as refs

Quoted 11 lines · Junio C Hamano
Quoted 5 lines

On reflection, I think that this would also be a reasonable approach to take for stash, which does not fall into any of the listed categories. It's not a pseudoref because it's not all-caps and it starts with refs/. Unlike other pseudorefs, it needs a reflog. But like HEAD and unlike other refs, it (and its reflog) wants to be per-worktree.

I think we want stash, unlike HEAD, to be shared across worktrees, and contrib/workdir gets this right.

So there is nothing that makes refs/stash like CHERRY_PICK_HEAD at all.

I just did a brief survey here and folks here agree with you. So, I'll adjust my patches accordingly.

Quoted 10 lines
Quoted 3 lines

Are there
other ref-like-things in this category (which I'll call "per-worktree refs")? I guess one set of these is submodules' HEADs.

I've been assuming that a secondary worktree of superproject will get its own & corresponding secondary worktree of a submodule, which would automatically give per-worktree "submodule's HEADs" and everything else that has to be per-worktree. Am I missing something or are there any more need for underlying machinery than what we currently have for secondary worktrees for a single project tree?

That sounds good to me, but judging by the docs, that's not yet implemented. So I'm going to worry about that when it is implemented.