threads / patch / 39808

v7, 8 partsrefs.c: add err arguments to reflog functions

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

## tl;dr

30 messages between Jul 8, 2015 and Jul 15, 2015. Diffs are folded; open one to read it.

replies: 29people: 4as markdown or json

David Turner· Jul 8, 2015, 00:55 UTC · lore

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.
Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>
Signed-off-by: David Turner <dturner@twopensource.com>
---
 builtin/checkout.c |   8 ++--
 refs.c             | 130 +++++++++++++++++++++++++++++------------------------
 refs.h             |   4 +-
 3 files changed, 79 insertions(+), 63 deletions(-)
Show changes to 3 files +79 −63

builtin/checkout.c, refs.c, refs.h

diff --git a/builtin/checkout.c b/builtin/checkout.c
index c018ab3..93f63d3 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -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;
 				}
 			}
diff --git a/refs.c b/refs.c
index fb568d7..e891bed 100644
--- a/refs.c
+++ b/refs.c
@@ -2975,9 +2975,11 @@ static int rename_ref_available(const char *oldname, const char *newname)
 	return ret;
 }
 
-static int write_ref_to_lockfile(struct ref_lock *lock, const unsigned char *sha1);
+static int write_ref_to_lockfile(struct ref_lock *lock,
+				 const unsigned char *sha1, struct strbuf *err);
 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 rename_ref(const char *oldrefname, const char *newrefname, const char *logmsg)
 {
@@ -3038,9 +3040,10 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms
 	}
 	hashcpy(lock->old_oid.hash, orig_sha1);
 
-	if (write_ref_to_lockfile(lock, orig_sha1) ||
-	    commit_ref_update(lock, orig_sha1, logmsg)) {
-		error("unable to write current sha1 into %s", newrefname);
+	if (write_ref_to_lockfile(lock, orig_sha1, &err) ||
+	    commit_ref_update(lock, orig_sha1, logmsg, &err)) {
+		error("unable to write current sha1 into %s: %s", newrefname, err.buf);
+		strbuf_release(&err);
 		goto rollback;
 	}
 
@@ -3056,9 +3059,11 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms
 
 	flag = log_all_ref_updates;
 	log_all_ref_updates = 0;
-	if (write_ref_to_lockfile(lock, orig_sha1) ||
-	    commit_ref_update(lock, orig_sha1, NULL))
-		error("unable to write current sha1 into %s", oldrefname);
+	if (write_ref_to_lockfile(lock, orig_sha1, &err) ||
+	    commit_ref_update(lock, orig_sha1, NULL, &err)) {
+		error("unable to write current sha1 into %s: %s", oldrefname, err.buf);
+		strbuf_release(&err);
+	}
 	log_all_ref_updates = flag;
 
  rollbacklog:
@@ -3113,8 +3118,8 @@ static int copy_msg(char *buf, const char *msg)
 	return cp - buf;
 }
 
-/* This function must set a meaningful errno on failure */
-int log_ref_setup(const char *refname, struct strbuf *sb_logfile)
+/* 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)
 {
 	int logfd, oflags = O_APPEND | O_WRONLY;
 	char *logfile;
@@ -3129,9 +3134,8 @@ int log_ref_setup(const char *refname, struct strbuf *sb_logfile)
 	     starts_with(refname, "refs/notes/") ||
 	     !strcmp(refname, "HEAD"))) {
 		if (safe_create_leading_directories(logfile) < 0) {
-			int save_errno = errno;
-			error("unable to create directory for %s", logfile);
-			errno = save_errno;
+			strbuf_addf(err, "unable to create directory for %s. "
+				    "%s", logfile, strerror(errno));
 			return -1;
 		}
 		oflags |= O_CREAT;
@@ -3144,20 +3148,16 @@ int log_ref_setup(const char *refname, struct strbuf *sb_logfile)
 
 		if (errno == EISDIR) {
 			if (remove_empty_directories(logfile)) {
-				int save_errno = errno;
-				error("There are still logs under '%s'",
-				      logfile);
-				errno = save_errno;
+				strbuf_addf(err, "There are still logs under "
+					    "'%s'", logfile);
 				return -1;
 			}
 			logfd = open(logfile, oflags, 0666);
 		}
 
 		if (logfd < 0) {
-			int save_errno = errno;
-			error("Unable to append to %s: %s", logfile,
-			      strerror(errno));
-			errno = save_errno;
+			strbuf_addf(err, "Unable to append to %s: %s",
+				    logfile, strerror(errno));
 			return -1;
 		}
 	}
@@ -3195,7 +3195,7 @@ static int log_ref_write_fd(int fd, const unsigned char *old_sha1,
 
 static int log_ref_write_1(const char *refname, const unsigned char *old_sha1,
 			   const unsigned char *new_sha1, const char *msg,
-			   struct strbuf *sb_log_file)
+			   struct strbuf *sb_log_file, struct strbuf *err)
 {
 	int logfd, result, oflags = O_APPEND | O_WRONLY;
 	char *log_file;
@@ -3203,7 +3203,8 @@ 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);
+	result = log_ref_setup(refname, sb_log_file, err);
+
 	if (result)
 		return result;
 	log_file = sb_log_file->buf;
@@ -3216,26 +3217,25 @@ static int log_ref_write_1(const char *refname, const unsigned char *old_sha1,
 	result = log_ref_write_fd(logfd, old_sha1, new_sha1,
 				  git_committer_info(0), msg);
 	if (result) {
-		int save_errno = errno;
+		strbuf_addf(err, "Unable to append to %s. %s", log_file,
+			    strerror(errno));
 		close(logfd);
-		error("Unable to append to %s", log_file);
-		errno = save_errno;
 		return -1;
 	}
 	if (close(logfd)) {
-		int save_errno = errno;
-		error("Unable to append to %s", log_file);
-		errno = save_errno;
+		strbuf_addf(err, "Unable to append to %s. %s", log_file,
+			    strerror(errno));
 		return -1;
 	}
 	return 0;
 }
 
 static int log_ref_write(const char *refname, const unsigned char *old_sha1,
-			 const unsigned char *new_sha1, const char *msg)
+			 const unsigned char *new_sha1, const char *msg,
+			 struct strbuf *err)
 {
 	struct strbuf sb = STRBUF_INIT;
-	int ret = log_ref_write_1(refname, old_sha1, new_sha1, msg, &sb);
+	int ret = log_ref_write_1(refname, old_sha1, new_sha1, msg, &sb, err);
 	strbuf_release(&sb);
 	return ret;
 }
@@ -3247,25 +3247,28 @@ int is_branch(const char *refname)
 
 /*
  * Write sha1 into the open lockfile, then close the lockfile. On
- * errors, rollback the lockfile and set errno to reflect the problem.
+ * errors, rollback the lockfile, fill in *err and
+ * return -1.
  */
 static int write_ref_to_lockfile(struct ref_lock *lock,
-				 const unsigned char *sha1)
+				 const unsigned char *sha1, struct strbuf *err)
 {
 	static char term = '\n';
 	struct object *o;
 
 	o = parse_object(sha1);
 	if (!o) {
-		error("Trying to write ref %s with nonexistent object %s",
-			lock->ref_name, sha1_to_hex(sha1));
+		strbuf_addf(err,
+			    "Trying to write ref %s with nonexistent object %s",
+			    lock->ref_name, sha1_to_hex(sha1));
 		unlock_ref(lock);
 		errno = EINVAL;
 		return -1;
 	}
 	if (o->type != OBJ_COMMIT && is_branch(lock->ref_name)) {
-		error("Trying to write non-commit object %s to branch %s",
-			sha1_to_hex(sha1), lock->ref_name);
+		strbuf_addf(err,
+			    "Trying to write non-commit object %s to branch %s",
+			    sha1_to_hex(sha1), lock->ref_name);
 		unlock_ref(lock);
 		errno = EINVAL;
 		return -1;
@@ -3273,10 +3276,9 @@ static int write_ref_to_lockfile(struct ref_lock *lock,
 	if (write_in_full(lock->lk->fd, sha1_to_hex(sha1), 40) != 40 ||
 	    write_in_full(lock->lk->fd, &term, 1) != 1 ||
 	    close_ref(lock) < 0) {
-		int save_errno = errno;
-		error("Couldn't write %s", lock->lk->filename.buf);
+		strbuf_addf(err,
+			    "Couldn't write %s", lock->lk->filename.buf);
 		unlock_ref(lock);
-		errno = save_errno;
 		return -1;
 	}
 	return 0;
@@ -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;
 	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)) {
-		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;
 	}
 	if (strcmp(lock->orig_ref_name, "HEAD") != 0) {
 		/*
@@ -3316,16 +3324,21 @@ static int commit_ref_update(struct ref_lock *lock,
 		head_ref = resolve_ref_unsafe("HEAD", RESOLVE_REF_READING,
 					      head_sha1, &head_flag);
 		if (head_ref && (head_flag & REF_ISSYMREF) &&
-		    !strcmp(head_ref, lock->ref_name))
-			log_ref_write("HEAD", lock->old_oid.hash, sha1, logmsg);
+		    !strcmp(head_ref, lock->ref_name)) {
+			if (log_ref_write("HEAD", lock->old_oid.hash, sha1,
+					  logmsg, err)) {
+				result = -1;
+				goto done;
+			}
+		}
 	}
 	if (commit_ref(lock)) {
 		error("Couldn't set %s", lock->ref_name);
-		unlock_ref(lock);
-		return -1;
+		result = -1;
 	}
+done:
 	unlock_ref(lock);
-	return 0;
+	return result;
 }
 
 int create_symref(const char *ref_target, const char *refs_heads_master,
@@ -3336,6 +3349,7 @@ int create_symref(const char *ref_target, const char *refs_heads_master,
 	int fd, len, written;
 	char *git_HEAD = git_pathdup("%s", ref_target);
 	unsigned char old_sha1[20], new_sha1[20];
+	struct strbuf err = STRBUF_INIT;
 
 	if (logmsg && read_ref(ref_target, old_sha1))
 		hashclr(old_sha1);
@@ -3384,8 +3398,11 @@ int create_symref(const char *ref_target, const char *refs_heads_master,
 #ifndef NO_SYMLINK_HEAD
 	done:
 #endif
-	if (logmsg && !read_ref(refs_heads_master, new_sha1))
-		log_ref_write(ref_target, old_sha1, new_sha1, logmsg);
+	if (logmsg && !read_ref(refs_heads_master, new_sha1) &&
+		log_ref_write(ref_target, old_sha1, new_sha1, logmsg, &err)) {
+		error("%s", err.buf);
+		strbuf_release(&err);
+	}
 
 	free(git_HEAD);
 	return 0;
@@ -4021,14 +4038,13 @@ int ref_transaction_commit(struct ref_transaction *transaction,
 				 * value, so we don't need to write it.
 				 */
 			} else if (write_ref_to_lockfile(update->lock,
-							 update->new_sha1)) {
+							 update->new_sha1,
+							 err)) {
 				/*
 				 * The lock was freed upon failure of
 				 * write_ref_to_lockfile():
 				 */
 				update->lock = NULL;
-				strbuf_addf(err, "cannot update the ref '%s'.",
-					    update->refname);
 				ret = TRANSACTION_GENERIC_ERROR;
 				goto cleanup;
 			} else {
@@ -4054,11 +4070,9 @@ int ref_transaction_commit(struct ref_transaction *transaction,
 
 		if (update->flags & REF_NEEDS_COMMIT) {
 			if (commit_ref_update(update->lock,
-					      update->new_sha1, update->msg)) {
+					      update->new_sha1, update->msg, err)) {
 				/* freed by commit_ref_update(): */
 				update->lock = NULL;
-				strbuf_addf(err, "Cannot update the ref '%s'.",
-					    update->refname);
 				ret = TRANSACTION_GENERIC_ERROR;
 				goto cleanup;
 			} else {
diff --git a/refs.h b/refs.h
index e82fca5..debdefc 100644
--- a/refs.h
+++ b/refs.h
@@ -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,
-- 
2.0.5.499.g01f6352.dirty-twtrsrc
David Turner· Jul 8, 2015, 00:55 UTC · re: David Turner · lore

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

Signed-off-by: David Turner <dturner@twopensource.com>
---
 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(-)
Show changes to 8 files +23 −36

branch.c, builtin/commit.c, builtin/merge.c, contrib/completion/git-prompt.sh, git-gui/lib/commit.tcl, sequencer.c, t/t7509-commit.sh, wt-status.c

diff --git a/branch.c b/branch.c
index b002435..ec598aa 100644
--- a/branch.c
+++ b/branch.c
@@ -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"));
diff --git a/builtin/commit.c b/builtin/commit.c
index b5b1158..53c7e90 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -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"));
diff --git a/builtin/merge.c b/builtin/merge.c
index 46aacd6..3e2ae2f 100644
--- a/builtin/merge.c
+++ b/builtin/merge.c
@@ -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."));
diff --git a/contrib/completion/git-prompt.sh b/contrib/completion/git-prompt.sh
index 366f0bc..e2c5583 100644
--- a/contrib/completion/git-prompt.sh
+++ b/contrib/completion/git-prompt.sh
@@ -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"
diff --git a/git-gui/lib/commit.tcl b/git-gui/lib/commit.tcl
index 864b687..2b08b13 100644
--- a/git-gui/lib/commit.tcl
+++ b/git-gui/lib/commit.tcl
@@ -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.
 	#
diff --git a/sequencer.c b/sequencer.c
index f8421a8..90396ba 100644
--- a/sequencer.c
+++ b/sequencer.c
@@ -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;
diff --git a/t/t7509-commit.sh b/t/t7509-commit.sh
index 9ac7940..f7fd62c 100755
--- a/t/t7509-commit.sh
+++ b/t/t7509-commit.sh
@@ -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 <cherry@pick.er>" &&
 	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 &&
diff --git a/wt-status.c b/wt-status.c
index 9c686e6..661c1fb 100644
--- a/wt-status.c
+++ b/wt-status.c
@@ -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);
 	}
-- 
2.0.5.499.g01f6352.dirty-twtrsrc
Johannes Sixt· Jul 8, 2015, 17:46 UTC · re: David Turner · lore

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:
Show 6 quoted 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.
>
> Signed-off-by: David Turner <dturner@twopensource.com>
> ---
...
Show 16 quoted lines
> diff --git a/contrib/completion/git-prompt.sh b/contrib/completion/git-prompt.sh
> index 366f0bc..e2c5583 100644
> --- a/contrib/completion/git-prompt.sh
> +++ b/contrib/completion/git-prompt.sh
> @@ -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"

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

-- Hannes
David Turner· Jul 8, 2015, 19:16 UTC · re: Johannes Sixt · lore

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

On Wed, 2015-07-08 at 19:46 +0200, Johannes Sixt wrote:
Show 27 quoted lines
> Am 08.07.2015 um 02:55 schrieb David Turner:
> > Instead of directly writing to and reading from files in
> > $GIT_DIR, use ref API to interact with CHERRY_PICK_HEAD
> > and REVERT_HEAD.
> >
> > Signed-off-by: David Turner <dturner@twopensource.com>
> > ---
> ...
> > diff --git a/contrib/completion/git-prompt.sh b/contrib/completion/git-prompt.sh
> > index 366f0bc..e2c5583 100644
> > --- a/contrib/completion/git-prompt.sh
> > +++ b/contrib/completion/git-prompt.sh
> > @@ -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"
> 
> 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.
Johannes Sixt· Jul 8, 2015, 21:14 UTC · re: David Turner · lore

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:
Show 44 quoted lines
> On Wed, 2015-07-08 at 19:46 +0200, Johannes Sixt wrote:
>> Am 08.07.2015 um 02:55 schrieb David Turner:
>>> Instead of directly writing to and reading from files in
>>> $GIT_DIR, use ref API to interact with CHERRY_PICK_HEAD
>>> and REVERT_HEAD.
>>>
>>> Signed-off-by: David Turner <dturner@twopensource.com>
>>> ---
>> ...
>>> diff --git a/contrib/completion/git-prompt.sh b/contrib/completion/git-prompt.sh
>>> index 366f0bc..e2c5583 100644
>>> --- a/contrib/completion/git-prompt.sh
>>> +++ b/contrib/completion/git-prompt.sh
>>> @@ -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"
>>
>> 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.

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
Junio C Hamano· Jul 8, 2015, 23:23 UTC · re: Johannes Sixt · lore

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

Johannes Sixt <j6t@kdbg.org> writes:
Show 13 quoted 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)?

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.
David Turner· Jul 8, 2015, 23:44 UTC · re: Junio C Hamano · lore

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

On Wed, 2015-07-08 at 16:23 -0700, Junio C Hamano wrote:
Show 29 quoted lines
> Johannes Sixt <j6t@kdbg.org> writes:
> 
> >> 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)?
> 
> 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.

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

Junio C Hamano· Jul 9, 2015, 05:55 UTC · re: David Turner · lore

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

David Turner <dturner@twopensource.com> writes:
> 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).

David Turner· Jul 9, 2015, 21:53 UTC · re: Junio C Hamano · lore

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

On Wed, 2015-07-08 at 22:55 -0700, Junio C Hamano wrote:
Show 19 quoted lines
> David Turner <dturner@twopensource.com> writes:
> 
> > 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".
Except HEAD definitely does. 
Show 15 quoted 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/.
> 
> 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.

Show 5 quoted 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?
Show 8 quoted 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.
Junio C Hamano· Jul 9, 2015, 22:06 UTC · re: David Turner · lore

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

David Turner <dturner@twopensource.com> writes:
Show 19 quoted lines
> 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?

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*].

Show 17 quoted 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.
>
> 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.

> What do other folks think?
Me too ;-)
[Footnote]

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

Michael Haggerty· Jul 10, 2015, 04:30 UTC · re: Junio C Hamano · lore

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

On 07/10/2015 12:06 AM, Junio C Hamano wrote:
Show 25 quoted lines
> David Turner <dturner@twopensource.com> writes:
> 
>> 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?
> 
> 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.

Show 23 quoted 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.
>>
>> 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.
> 
>> 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
-- 
Michael Haggerty
mhagger@alum.mit.edu
David Turner· Jul 10, 2015, 17:59 UTC · re: Michael Haggerty · lore

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

On Fri, 2015-07-10 at 06:30 +0200, Michael Haggerty wrote:
Show 57 quoted lines
> On 07/10/2015 12:06 AM, Junio C Hamano wrote:
> > David Turner <dturner@twopensource.com> writes:
> > 
> >> 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?
> > 
> > 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.

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?

Show 52 quoted 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.
> >>
> >> 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.
> > 
> >> 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.

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.

David Turner· Jul 14, 2015, 04:33 UTC · re: Michael Haggerty · lore

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

[j6t to bcc as it looks like his concerns have been addressed]
On Fri, 2015-07-10 at 06:30 +0200, Michael Haggerty wrote:
Show 42 quoted lines
> On 07/10/2015 12:06 AM, Junio C Hamano wrote:
> > David Turner <dturner@twopensource.com> writes:
> > 
> >> 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?
> > 
> > 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.

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.

Junio C Hamano· Jul 15, 2015, 16:24 UTC · re: David Turner · lore

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

David Turner <dturner@twopensource.com> writes:
Show 7 quoted lines
> 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.
Show 5 quoted 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.

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

David Turner· Jul 15, 2015, 18:04 UTC · re: Junio C Hamano · lore

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

On Wed, 2015-07-15 at 09:24 -0700, Junio C Hamano wrote:
Show 11 quoted 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.

Show 10 quoted 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.

David Turner· Jul 8, 2015, 23:41 UTC · re: Johannes Sixt · lore

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

On Wed, 2015-07-08 at 23:14 +0200, Johannes Sixt wrote:
Show 50 quoted lines
> Am 08.07.2015 um 21:16 schrieb David Turner:
> > On Wed, 2015-07-08 at 19:46 +0200, Johannes Sixt wrote:
> >> Am 08.07.2015 um 02:55 schrieb David Turner:
> >>> Instead of directly writing to and reading from files in
> >>> $GIT_DIR, use ref API to interact with CHERRY_PICK_HEAD
> >>> and REVERT_HEAD.
> >>>
> >>> Signed-off-by: David Turner <dturner@twopensource.com>
> >>> ---
> >> ...
> >>> diff --git a/contrib/completion/git-prompt.sh b/contrib/completion/git-prompt.sh
> >>> index 366f0bc..e2c5583 100644
> >>> --- a/contrib/completion/git-prompt.sh
> >>> +++ b/contrib/completion/git-prompt.sh
> >>> @@ -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"
> >>
> >> 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.
> 
> 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.
David Turner· Jul 8, 2015, 00:55 UTC · re: David Turner · lore

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

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

Signed-off-by: David Turner <dturner@twopensource.com>
---
 git-bisect.sh | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
Show changes to git-bisect.sh +7 −3
diff --git a/git-bisect.sh b/git-bisect.sh
index ae3fec2..2fd8ea6 100755
--- a/git-bisect.sh
+++ b/git-bisect.sh
@@ -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>'.")"
-- 
2.0.5.499.g01f6352.dirty-twtrsrc
David Turner· Jul 8, 2015, 00:55 UTC · re: David Turner · lore

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

This is just for clarity.
Signed-off-by: David Turner <dturner@twopensource.com>
---
 refs.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
Show changes to refs.c +11 −5
diff --git a/refs.c b/refs.c
index e891bed..e694359 100644
--- a/refs.c
+++ b/refs.c
@@ -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));
-- 
2.0.5.499.g01f6352.dirty-twtrsrc
David Turner· Jul 8, 2015, 00:56 UTC · re: David Turner · lore

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

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.

Signed-off-by: David Turner <dturner@twopensource.com>
---
 builtin/checkout.c | 16 +++++-----------
 refs.c             | 25 +++++++++++++++++++++----
 refs.h             |  2 +-
 3 files changed, 27 insertions(+), 16 deletions(-)
Show changes to 3 files +27 −16

builtin/checkout.c, refs.c, refs.h

diff --git a/builtin/checkout.c b/builtin/checkout.c
index 93f63d3..c840f33 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -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
diff --git a/refs.c b/refs.c
index e694359..01b0af5 100644
--- a/refs.c
+++ b/refs.c
@@ -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;
diff --git a/refs.h b/refs.h
index debdefc..3b90e16 100644
--- a/refs.h
+++ b/refs.h
@@ -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,
-- 
2.0.5.499.g01f6352.dirty-twtrsrc
Michael Haggerty· Jul 8, 2015, 11:49 UTC · re: David Turner · lore

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

On 07/08/2015 02:56 AM, David Turner wrote:
Show 42 quoted lines
> 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.
> 
> Signed-off-by: David Turner <dturner@twopensource.com>
> ---
>  builtin/checkout.c | 16 +++++-----------
>  refs.c             | 25 +++++++++++++++++++++----
>  refs.h             |  2 +-
>  3 files changed, 27 insertions(+), 16 deletions(-)
> 
> [...]
> diff --git a/refs.c b/refs.c
> index e694359..01b0af5 100644
> --- a/refs.c
> +++ b/refs.c
> @@ -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;

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
-- 
Michael Haggerty
mhagger@alum.mit.edu
David Turner· Jul 8, 2015, 00:56 UTC · re: David Turner · lore

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

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.

Signed-off-by: David Turner <dturner@twopensource.com>
---
 Documentation/git-reflog.txt |  4 ++++
 builtin/reflog.c             | 33 ++++++++++++++++++++++++++++++++-
 t/t1411-reflog-show.sh       |  5 +++++
 3 files changed, 41 insertions(+), 1 deletion(-)
Show changes to 3 files +41 −1

Documentation/git-reflog.txt, builtin/reflog.c, t/t1411-reflog-show.sh

diff --git a/Documentation/git-reflog.txt b/Documentation/git-reflog.txt
index 5e7908e..4b08fc7 100644
--- a/Documentation/git-reflog.txt
+++ b/Documentation/git-reflog.txt
@@ -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
 -------
diff --git a/builtin/reflog.c b/builtin/reflog.c
index c2eb8ff..7ed0e85 100644
--- a/builtin/reflog.c
+++ b/builtin/reflog.c
@@ -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);
 }
diff --git a/t/t1411-reflog-show.sh b/t/t1411-reflog-show.sh
index 6f47c0d..3eb4f10 100755
--- a/t/t1411-reflog-show.sh
+++ b/t/t1411-reflog-show.sh
@@ -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
-- 
2.0.5.499.g01f6352.dirty-twtrsrc
David Turner· Jul 8, 2015, 00:56 UTC · re: David Turner · lore

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

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.

Signed-off-by: David Turner <dturner@twopensource.com>
---
 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(-)
Show changes to 6 files +74 −5

Documentation/git-tag.txt, Documentation/git-update-ref.txt, builtin/tag.c, builtin/update-ref.c, t/t1400-update-ref.sh, t/t7004-tag.sh

diff --git a/Documentation/git-tag.txt b/Documentation/git-tag.txt
index 034d10d..2312980 100644
--- a/Documentation/git-tag.txt
+++ b/Documentation/git-tag.txt
@@ -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
diff --git a/Documentation/git-update-ref.txt b/Documentation/git-update-ref.txt
index c8f5ae5..969bfab 100644
--- a/Documentation/git-update-ref.txt
+++ b/Documentation/git-update-ref.txt
@@ -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
diff --git a/builtin/tag.c b/builtin/tag.c
index 5f6cdc5..896f434 100644
--- a/builtin/tag.c
+++ b/builtin/tag.c
@@ -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,
diff --git a/builtin/update-ref.c b/builtin/update-ref.c
index 6763cf1..d570e5e 100644
--- a/builtin/update-ref.c
+++ b/builtin/update-ref.c
@@ -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);
+	}
 }
diff --git a/t/t1400-update-ref.sh b/t/t1400-update-ref.sh
index d787bf5..9d21c19 100755
--- a/t/t1400-update-ref.sh
+++ b/t/t1400-update-ref.sh
@@ -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 &&
diff --git a/t/t7004-tag.sh b/t/t7004-tag.sh
index d1ff5c9..426d285 100755
--- a/t/t7004-tag.sh
+++ b/t/t7004-tag.sh
@@ -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' '
-- 
2.0.5.499.g01f6352.dirty-twtrsrc
Michael Haggerty· Jul 8, 2015, 13:44 UTC · re: David Turner · lore

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
On 07/08/2015 02:56 AM, David Turner wrote:
> update-ref and tag: add --create-reflog arg
`--create-reflog` is an "option", not an "arg".
> 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).

Show 9 quoted 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.

Show 83 quoted lines
> Signed-off-by: David Turner <dturner@twopensource.com>
> ---
>  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(-)
> 
> diff --git a/Documentation/git-tag.txt b/Documentation/git-tag.txt
> index 034d10d..2312980 100644
> --- a/Documentation/git-tag.txt
> +++ b/Documentation/git-tag.txt
> @@ -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
> diff --git a/Documentation/git-update-ref.txt b/Documentation/git-update-ref.txt
> index c8f5ae5..969bfab 100644
> --- a/Documentation/git-update-ref.txt
> +++ b/Documentation/git-update-ref.txt
> @@ -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
> diff --git a/builtin/tag.c b/builtin/tag.c
> index 5f6cdc5..896f434 100644
> --- a/builtin/tag.c
> +++ b/builtin/tag.c
> @@ -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);
> +

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

Show 22 quoted lines
>  	transaction = ref_transaction_begin(&err);
>  	if (!transaction ||
>  	    ref_transaction_update(transaction, ref.buf, object, prev,
> diff --git a/builtin/update-ref.c b/builtin/update-ref.c
> index 6763cf1..d570e5e 100644
> --- a/builtin/update-ref.c
> +++ b/builtin/update-ref.c
> @@ -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);
> +

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.

Show 13 quoted lines
>  	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?

Show 26 quoted lines
> @@ -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);

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

Show 10 quoted lines
> +
>  		return update_ref(msg, refname, sha1, oldval ? oldsha1 : NULL,
>  				  flags, UPDATE_REFS_DIE_ON_ERR);
> +	}
>  }
> diff --git a/t/t1400-update-ref.sh b/t/t1400-update-ref.sh
> index d787bf5..9d21c19 100755
> --- a/t/t1400-update-ref.sh
> +++ b/t/t1400-update-ref.sh
> [...]
Michael
-- 
Michael Haggerty
mhagger@alum.mit.edu
David Turner· Jul 8, 2015, 20:21 UTC · re: Michael Haggerty · lore

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

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

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

David Turner· Jul 8, 2015, 00:56 UTC · re: David Turner · lore

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

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

Signed-off-by: David Turner <dturner@twopensource.com>
---
 git-stash.sh | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
Show changes to git-stash.sh +2 −4
diff --git a/git-stash.sh b/git-stash.sh
index 8e9e2cd..1d5ba7a 100755
--- a/git-stash.sh
+++ b/git-stash.sh
@@ -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
-- 
2.0.5.499.g01f6352.dirty-twtrsrc
Junio C Hamano· Jul 8, 2015, 07:28 UTC · re: David Turner · lore

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

David Turner <dturner@twopensource.com> writes:
Show 5 quoted lines
> This is in support of alternate ref backends which don't necessarily
> store reflogs as files.
>
> Signed-off-by: David Turner <dturner@twopensource.com>
> ---

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?
Junio C Hamano· Jul 8, 2015, 07:33 UTC · re: David Turner · lore

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

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.
Michael Haggerty· Jul 8, 2015, 13:50 UTC · re: David Turner · lore

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
-- 
Michael Haggerty
mhagger@alum.mit.edu
Michael Haggerty· Jul 8, 2015, 11:36 UTC · re: David Turner · lore

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

On 07/08/2015 02:55 AM, David Turner wrote:
Show 13 quoted lines
> 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.

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

Show 25 quoted lines
> Update of a patch by Ronnie Sahlberg.
> 
> Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>
> Signed-off-by: David Turner <dturner@twopensource.com>
> ---
>  builtin/checkout.c |   8 ++--
>  refs.c             | 130 +++++++++++++++++++++++++++++------------------------
>  refs.h             |   4 +-
>  3 files changed, 79 insertions(+), 63 deletions(-)
> 
> [...]
> diff --git a/refs.c b/refs.c
> index fb568d7..e891bed 100644
> --- a/refs.c
> +++ b/refs.c
> [...]
> @@ -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.

Show 15 quoted lines
>  	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)) {
> -		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
>  	if (strcmp(lock->orig_ref_name, "HEAD") != 0) {
>  		/*
> [...]
Michael
-- 
Michael Haggerty
mhagger@alum.mit.edu
David Turner· Jul 8, 2015, 20:01 UTC · re: Michael Haggerty · lore

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

On Wed, 2015-07-08 at 13:36 +0200, Michael Haggerty wrote:
Show 19 quoted lines
> On 07/08/2015 02:55 AM, David Turner wrote:
> > 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.
> 
> 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.

← back to recent threads