Skip to contentSkip to conversation

set remote/HEAD with fetch

RFC PATCH Superseded Bence Ferdinandy 11 participants 258 messages Sep 10, 2024 – Jan 6, 2025 Documentation Page 2 of 3

lore
Share & tools
lore
#104 lore raw

PATCH v11 5/8 refs: add TRANSACTION_CREATE_EXISTS error

In reply to Bence Ferdinandy

Currently there is only one special error for transaction, for when there is a naming conflict, all other errors are dumped under a generic error. Add a new special error case for when the caller requests the reference to be updated only when it does not yet exist and the reference actually does exist.

---

Notes:
v4: new patch
v5: no change
v6: no change

v7: - change commit prefix to be more in line with project standards
- changed error checking to Karthik's suggestion

v8: no change

v9: - no change

v10: no change

v11: no change

 refs.h                  |  4 +++-
 refs/files-backend.c    | 24 ++++++++++++++++--------
 refs/reftable-backend.c |  6 ++++--
 3 files changed, 23 insertions(+), 11 deletions(-)
refs.h +3 -1
index 259191a485..a5bc25442b 100644
@@ -762,8 +762,10 @@ int ref_transaction_verify(struct ref_transaction *transaction,
 
 /* Naming conflict (for example, the ref names A and A/B conflict). */
 #define TRANSACTION_NAME_CONFLICT -1
+/* When only creation was requested, but the ref already exists. */
+#define TRANSACTION_CREATE_EXISTS -2
 /* All other errors. */
-#define TRANSACTION_GENERIC_ERROR -2
+#define TRANSACTION_GENERIC_ERROR -3
 
 /*
  * Perform the preparatory stages of committing `transaction`. Acquire
refs/files-backend.c +16 -8
index 0824c0b8a9..e743ec44b5 100644
@@ -2502,14 +2502,18 @@ static int split_symref_update(struct ref_update *update,
 static int check_old_oid(struct ref_update *update, struct object_id *oid,
 			 struct strbuf *err)
 {
+	int ret = TRANSACTION_GENERIC_ERROR;
+
 	if (!(update->flags & REF_HAVE_OLD) ||
 		   oideq(oid, &update->old_oid))
 		return 0;
 
-	if (is_null_oid(&update->old_oid))
+	if (is_null_oid(&update->old_oid)) {
 		strbuf_addf(err, "cannot lock ref '%s': "
 			    "reference already exists",
 			    ref_update_original_update_refname(update));
+		ret = TRANSACTION_CREATE_EXISTS;
+	}
 	else if (is_null_oid(oid))
 		strbuf_addf(err, "cannot lock ref '%s': "
 			    "reference is missing but expected %s",
@@ -2522,7 +2526,7 @@ static int check_old_oid(struct ref_update *update, struct object_id *oid,
 			    oid_to_hex(oid),
 			    oid_to_hex(&update->old_oid));
 
-	return -1;
+	return ret;
 }
 
 /*
@@ -2602,9 +2606,11 @@ static int lock_ref_for_update(struct files_ref_store *refs,
 					ret = TRANSACTION_GENERIC_ERROR;
 					goto out;
 				}
-			} else if  (check_old_oid(update, &lock->old_oid, err)) {
-				ret = TRANSACTION_GENERIC_ERROR;
-				goto out;
+			} else {
+				ret = check_old_oid(update, &lock->old_oid, err);
+				if  (ret) {
+					goto out;
+				}
 			}
 		} else {
 			/*
@@ -2635,9 +2641,11 @@ static int lock_ref_for_update(struct files_ref_store *refs,
 				    update->old_target);
 			ret = TRANSACTION_GENERIC_ERROR;
 			goto out;
-		} else if  (check_old_oid(update, &lock->old_oid, err)) {
-			ret = TRANSACTION_GENERIC_ERROR;
-			goto out;
+		} else {
+			ret = check_old_oid(update, &lock->old_oid, err);
+			if  (ret) {
+				goto out;
+			}
 		}
 
 		/*
refs/reftable-backend.c +4 -2
index 3c6107c7ce..b3b5ce77dd 100644
@@ -1206,10 +1206,13 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store,
 				goto done;
 			}
 		} else if ((u->flags & REF_HAVE_OLD) && !oideq(&current_oid, &u->old_oid)) {
-			if (is_null_oid(&u->old_oid))
+			ret = TRANSACTION_NAME_CONFLICT;
+			if (is_null_oid(&u->old_oid)) {
 				strbuf_addf(err, _("cannot lock ref '%s': "
 						   "reference already exists"),
 					    ref_update_original_update_refname(u));
+				ret = TRANSACTION_CREATE_EXISTS;
+			}
 			else if (is_null_oid(&current_oid))
 				strbuf_addf(err, _("cannot lock ref '%s': "
 						   "reference is missing but expected %s"),
@@ -1221,7 +1224,6 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store,
 					    ref_update_original_update_refname(u),
 					    oid_to_hex(&current_oid),
 					    oid_to_hex(&u->old_oid));
-			ret = -1;
 			goto done;
 		}
 
Signature · 1 line
2.47.0.94.gb64850d498
#105 lore raw

PATCH v11 6/8 refs: add create_only option to refs_update_symref_extended

In reply to Bence Ferdinandy

Allow the caller to specify that it only wants to update the symref if it does not already exist. Silently ignore the error from the transaction API if the symref already exists.

---

Notes:
v4: new patch
v5: no change

v6: - switched from bool to int for create_only
- refactored logic in refs_update_symref with goto as layed out by Junio

v7: - change commit prefix to be more in line with project standards
- refactored code to accommodate changes in the first patch, but otherwise no change

v8: no change

v9: - no change (except for following through the
refs_update_symref_extended refactoring)

v10: no change

v11: no change

 builtin/remote.c |  2 +-
 refs.c           | 32 +++++++++++++++++++++++---------
 refs.h           |  2 +-
 3 files changed, 25 insertions(+), 11 deletions(-)
builtin/remote.c +1 -1
index 108f1271d3..b1eba75a2b 100644
@@ -1471,7 +1471,7 @@ static int set_head(int argc, const char **argv, const char *prefix)
 		if (!refs_ref_exists(refs, b_remote_head.buf))
 			result |= error(_("Not a valid ref: %s"), b_remote_head.buf);
 		else if (refs_update_symref_extended(refs, b_head.buf, b_remote_head.buf,
-					"remote set-head", &b_local_head))
+					"remote set-head", &b_local_head, 0))
 			result |= error(_("Could not setup %s"), b_head.buf);
 		else if (opt_a)
 			report_set_head_auto(argv[0], head_name, &b_local_head);
refs.c +23 -9
index 24a4172cd2..093ee11ab0 100644
@@ -2116,31 +2116,45 @@ int peel_iterated_oid(struct repository *r, const struct object_id *base, struct
 int refs_update_symref(struct ref_store *refs, const char *ref,
 		       const char *target, const char *logmsg)
 {
-	return refs_update_symref_extended(refs, ref, target, logmsg, NULL);
+	return refs_update_symref_extended(refs, ref, target, logmsg, NULL, 0);
 }
 
 int refs_update_symref_extended(struct ref_store *refs, const char *ref,
 		       const char *target, const char *logmsg,
-		       struct strbuf *referent)
+		       struct strbuf *referent, int create_only)
 {
 	struct ref_transaction *transaction;
 	struct strbuf err = STRBUF_INIT;
-	int ret = 0;
+	int ret = 0, prepret = 0;
 
 	transaction = ref_store_transaction_begin(refs, &err);
-	if (!transaction ||
-		ref_transaction_update(transaction, ref, NULL, NULL,
-				   target, NULL, REF_NO_DEREF,
-				   logmsg, &err) ||
-		ref_transaction_prepare(transaction, &err)) {
+	if (!transaction) {
+	error_return:
 		ret = error("%s", err.buf);
 		goto cleanup;
 	}
+	if (create_only) {
+		if (ref_transaction_create(transaction, ref, NULL, target,
+					   REF_NO_DEREF, logmsg, &err))
+			goto error_return;
+		prepret = ref_transaction_prepare(transaction, &err);
+		if (prepret && prepret != TRANSACTION_CREATE_EXISTS)
+			goto error_return;
+	} else {
+		if (ref_transaction_update(transaction, ref, NULL, NULL,
+					   target, NULL, REF_NO_DEREF,
+					   logmsg, &err) ||
+			ref_transaction_prepare(transaction, &err))
+			goto error_return;
+	}
+
 	if (referent)
 		refs_read_symbolic_ref(refs, ref, referent);
 
+	if (prepret == TRANSACTION_CREATE_EXISTS)
+		goto cleanup;
 	if (ref_transaction_commit(transaction, &err))
-		ret = error("%s", err.buf);
+		goto error_return;
 
 cleanup:
 	strbuf_release(&err);
refs.h +1 -1
index a5bc25442b..458582ebcf 100644
@@ -575,7 +575,7 @@ int refs_update_symref(struct ref_store *refs, const char *refname,
 
 int refs_update_symref_extended(struct ref_store *refs, const char *refname,
 		       const char *target, const char *logmsg,
-		       struct strbuf *referent);
+		       struct strbuf *referent, int create_only);
 
 enum action_on_err {
 	UPDATE_REFS_MSG_ON_ERR,
Signature · 1 line
2.47.0.94.gb64850d498
#106 lore raw

PATCH v11 7/8 fetch: set remote/HEAD if it does not exist

In reply to Bence Ferdinandy

If the user has remote/HEAD set already and it looks like it has changed on the server, then print a message, otherwise set it if we can. Silently pass if the user already has the same remote/HEAD set as reported by the server or if we encounter any errors along the way.

---

Notes:
v3: - does not rely on remote set-head anymore so it only authenticates
once
- uses the new REF_CREATE_ONLY to atomically check if the ref exists and only write it if it doesn't
- in all other cases the maximum it does is print a warning

v4: - instead of the discarded REF_CREATE_ONLY, it uses the existing,
but updated transaction api to request a silent create only
- it now uses the atomic before_target to determine reporting
- refactored for legibility

v5: - instead of printing a not too useful message, it now fails
silently, this in line with the objective to only set up remote/HEAD automatically if the right thing is trivial, for everything else there is remote set-head
- fixed all failing tests
- added two new tests, one for checking if remote/HEAD is set to the correct one, and one to test that we do not override remote/HEAD if it has changed on the server from what we have locally

v6: - fixed style issues and unintended extra empty line
- updated function call with bool to int from previous patch's change
- removed calls to error(...) inherited from builtin/remote.c so we actually fail silently
- set the test for remote set-head --auto to the correct value here, which was previously erronously set in the remote set-head patch

v7: - no change

v8: - changed logmsg in call to refs_update_symref from "remote
set-head" to "fetch"

v9: - follow through with refs_update_symref_extended
- fix test errors uncovered by the new patch

v10: no change

v11: fixed some memory leaks

 builtin/fetch.c                  |  86 ++++++++++++
 t/t4207-log-decoration-colors.sh |   3 +-
 t/t5505-remote.sh                |  21 ++-
 t/t5510-fetch.sh                 | 229 ++++++++++++++++---------------
 t/t5512-ls-remote.sh             |   2 +
 t/t5514-fetch-multiple.sh        |  17 ++-
 t/t5516-fetch-push.sh            |   3 +-
 t/t5527-fetch-odd-refs.sh        |   3 +-
 t/t7900-maintenance.sh           |   3 +-
 t/t9210-scalar.sh                |   5 +-
 t/t9211-scalar-clone.sh          |   6 +-
 t/t9902-completion.sh            |  65 +++++++++
 12 files changed, 319 insertions(+), 124 deletions(-)
builtin/fetch.c +86 -0
index d9027e4dc9..cadfd2407a 100644
@@ -1578,6 +1578,84 @@ static int backfill_tags(struct display_state *display_state,
 	return retcode;
 }
 
+static void report_set_head(const char *remote, const char *head_name,
+			struct strbuf *buf_prev) {
+	struct strbuf buf_prefix = STRBUF_INIT;
+	const char *prev_head = NULL;
+
+	strbuf_addf(&buf_prefix, "refs/remotes/%s/", remote);
+	skip_prefix(buf_prev->buf, buf_prefix.buf, &prev_head);
+
+	if (prev_head && strcmp(prev_head, head_name)) {
+		printf("'HEAD' at '%s' has changed from '%s' to '%s'\n",
+			remote, prev_head, head_name);
+		printf("Run 'git remote set-head %s %s' to follow the change.\n",
+			remote, head_name);
+	}
+	strbuf_release(&buf_prefix);
+}
+
+static const char *strip_refshead(const char *name){
+	skip_prefix(name, "refs/heads/", &name);
+	return name;
+}
+
+static int set_head(const struct ref *remote_refs)
+{
+	int result = 0;
+	struct strbuf b_head = STRBUF_INIT, b_remote_head = STRBUF_INIT,
+		b_local_head = STRBUF_INIT;
+	const char *remote = gtransport->remote->name;
+	char *head_name = NULL;
+	struct ref *ref, *matches;
+	struct ref *fetch_map = NULL, **fetch_map_tail = &fetch_map;
+	struct refspec_item refspec = {
+		.force = 0,
+		.pattern = 1,
+		.src = (char *) "refs/heads/*",
+		.dst = (char *) "refs/heads/*",
+	};
+	struct string_list heads = STRING_LIST_INIT_DUP;
+	struct ref_store *refs = get_main_ref_store(the_repository);
+
+	get_fetch_map(remote_refs, &refspec, &fetch_map_tail, 0);
+	matches = guess_remote_head(find_ref_by_name(remote_refs, "HEAD"),
+				    fetch_map, 1);
+	for (ref = matches; ref; ref = ref->next) {
+		string_list_append(&heads, strip_refshead(ref->name));
+	}
+
+
+	if (!heads.nr)
+		result = 1;
+	else if (heads.nr > 1)
+		result = 1;
+	else
+		head_name = xstrdup(heads.items[0].string);
+	if (head_name) {
+		strbuf_addf(&b_head, "refs/remotes/%s/HEAD", remote);
+		strbuf_addf(&b_remote_head, "refs/remotes/%s/%s", remote, head_name);
+		/* make sure it's valid */
+		if (!refs_ref_exists(refs, b_remote_head.buf))
+			result = 1;
+		else if (refs_update_symref_extended(refs, b_head.buf, b_remote_head.buf,
+					"fetch", &b_local_head, 1))
+			result = 1;
+		else
+			report_set_head(remote, head_name, &b_local_head);
+
+		free(head_name);
+	}
+
+	free_refs(fetch_map);
+	free_refs(matches);
+	string_list_clear(&heads, 0);
+	strbuf_release(&b_head);
+	strbuf_release(&b_local_head);
+	strbuf_release(&b_remote_head);
+	return result;
+}
+
 static int do_fetch(struct transport *transport,
 		    struct refspec *rs,
 		    const struct fetch_config *config)
@@ -1647,6 +1725,8 @@ static int do_fetch(struct transport *transport,
 				    "refs/tags/");
 	}
 
+	strvec_push(&transport_ls_refs_options.ref_prefixes, "HEAD");
+
 	if (must_list_refs) {
 		trace2_region_enter("fetch", "remote_refs", the_repository);
 		remote_refs = transport_get_remote_refs(transport,
@@ -1791,6 +1871,12 @@ static int do_fetch(struct transport *transport,
 				  "you need to specify exactly one branch with the --set-upstream option"));
 		}
 	}
+	if (set_head(remote_refs))
+		;
+		/*
+		 * Way too many cases where this can go wrong
+		 * so let's just fail silently for now.
+		 */
 
 cleanup:
 	if (retcode) {
t/t4207-log-decoration-colors.sh +2 -1
index 73ea9e5155..d55d22cb2f 100755
@@ -59,7 +59,8 @@ ${c_reset}${c_tag}tag: ${c_reset}${c_tag}v1.0${c_reset}${c_commit}, \
 ${c_reset}${c_tag}tag: ${c_reset}${c_tag}B${c_reset}${c_commit})${c_reset} B
 ${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_reset}\
 ${c_tag}tag: ${c_reset}${c_tag}A1${c_reset}${c_commit}, \
-${c_reset}${c_remoteBranch}other/main${c_reset}${c_commit})${c_reset} A1
+${c_reset}${c_remoteBranch}other/main${c_reset}${c_commit}, \
+${c_reset}${c_remoteBranch}other/HEAD${c_reset}${c_commit})${c_reset} A1
 	${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_reset}\
 ${c_stash}refs/stash${c_reset}${c_commit})${c_reset} On main: Changes to A.t
 	${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_reset}\
t/t5505-remote.sh +15 -6
index 0ea86d51a4..4990d00209 100755
@@ -74,7 +74,7 @@ test_expect_success 'add another remote' '
 		cd test &&
 		git remote add -f second ../two &&
 		tokens_match "origin second" "$(git remote)" &&
-		check_tracking_branch second main side another &&
+		check_tracking_branch second main side another HEAD &&
 		git for-each-ref "--format=%(refname)" refs/remotes |
 		sed -e "/^refs\/remotes\/origin\//d" \
 		    -e "/^refs\/remotes\/second\//d" >actual &&
@@ -476,7 +476,7 @@ test_expect_success 'set-head --auto has no problem w/multiple HEADs' '
 		cd test &&
 		git fetch two "refs/heads/*:refs/remotes/two/*" &&
 		git remote set-head --auto two >output 2>&1 &&
-		echo "${SQ}two/HEAD${SQ} is now created and points to ${SQ}main${SQ}" >expect &&
+		echo "${SQ}two/HEAD${SQ} is unchanged and points to ${SQ}main${SQ}" >expect &&
 		test_cmp expect output
 	)
 '
@@ -764,8 +764,10 @@ test_expect_success 'reject --no-no-tags' '
 '
 
 cat >one/expect <<\EOF
+  apis/HEAD -> apis/main
   apis/main
   apis/side
+  drosophila/HEAD -> drosophila/main
   drosophila/another
   drosophila/main
   drosophila/side
@@ -783,11 +785,14 @@ test_expect_success 'update' '
 '
 
 cat >one/expect <<\EOF
+  drosophila/HEAD -> drosophila/main
   drosophila/another
   drosophila/main
   drosophila/side
+  manduca/HEAD -> manduca/main
   manduca/main
   manduca/side
+  megaloprepus/HEAD -> megaloprepus/main
   megaloprepus/main
   megaloprepus/side
 EOF
@@ -795,7 +800,7 @@ EOF
 test_expect_success 'update with arguments' '
 	(
 		cd one &&
-		for b in $(git branch -r)
+		for b in $(git branch -r | grep -v HEAD)
 		do
 		git branch -r -d $b || exit 1
 		done &&
@@ -827,10 +832,13 @@ test_expect_success 'update --prune' '
 '
 
 cat >one/expect <<-\EOF
+  apis/HEAD -> apis/main
   apis/main
   apis/side
+  manduca/HEAD -> manduca/main
   manduca/main
   manduca/side
+  megaloprepus/HEAD -> megaloprepus/main
   megaloprepus/main
   megaloprepus/side
 EOF
@@ -838,7 +846,7 @@ EOF
 test_expect_success 'update default' '
 	(
 		cd one &&
-		for b in $(git branch -r)
+		for b in $(git branch -r | grep -v HEAD)
 		do
 		git branch -r -d $b || exit 1
 		done &&
@@ -850,6 +858,7 @@ test_expect_success 'update default' '
 '
 
 cat >one/expect <<\EOF
+  drosophila/HEAD -> drosophila/main
   drosophila/another
   drosophila/main
   drosophila/side
@@ -858,7 +867,7 @@ EOF
 test_expect_success 'update default (overridden, with funny whitespace)' '
 	(
 		cd one &&
-		for b in $(git branch -r)
+		for b in $(git branch -r | grep -v HEAD)
 		do
 		git branch -r -d $b || exit 1
 		done &&
@@ -872,7 +881,7 @@ test_expect_success 'update default (overridden, with funny whitespace)' '
 test_expect_success 'update (with remotes.default defined)' '
 	(
 		cd one &&
-		for b in $(git branch -r)
+		for b in $(git branch -r | grep -v HEAD)
 		do
 		git branch -r -d $b || exit 1
 		done &&
t/t5510-fetch.sh +122 -107

356 lines not on this page · raw message

t/t5512-ls-remote.sh +2 -0
index 64b3491e4e..1b3865e154 100755
@@ -293,6 +293,8 @@ test_expect_success 'ls-remote with filtered symref (refname)' '
 	cat >expect <<-EOF &&
 	ref: refs/heads/main	HEAD
 	$rev	HEAD
+	ref: refs/remotes/origin/main	refs/remotes/origin/HEAD
+	$rev	refs/remotes/origin/HEAD
 	EOF
 	git ls-remote --symref . HEAD >actual &&
 	test_cmp expect actual
t/t5514-fetch-multiple.sh +15 -2
index 579872c258..e3482b27b2 100755
@@ -45,14 +45,17 @@ test_expect_success setup '
 '
 
 cat > test/expect << EOF
+  one/HEAD -> one/main
   one/main
   one/side
   origin/HEAD -> origin/main
   origin/main
   origin/side
+  three/HEAD -> three/main
   three/another
   three/main
   three/side
+  two/HEAD -> two/main
   two/another
   two/main
   two/side
@@ -97,6 +100,7 @@ cat > expect << EOF
   origin/HEAD -> origin/main
   origin/main
   origin/side
+  three/HEAD -> three/main
   three/another
   three/main
   three/side
@@ -112,8 +116,10 @@ test_expect_success 'git fetch --multiple (but only one remote)' '
 '
 
 cat > expect << EOF
+  one/HEAD -> one/main
   one/main
   one/side
+  two/HEAD -> two/main
   two/another
   two/main
   two/side
@@ -141,7 +147,7 @@ test_expect_success 'git fetch --multiple (bad remote names)' '
 
 test_expect_success 'git fetch --all (skipFetchAll)' '
 	(cd test4 &&
-	 for b in $(git branch -r)
+	 for b in $(git branch -r | grep -v HEAD)
 	 do
 		git branch -r -d $b || exit 1
 	 done &&
@@ -153,11 +159,14 @@ test_expect_success 'git fetch --all (skipFetchAll)' '
 '
 
 cat > expect << EOF
+  one/HEAD -> one/main
   one/main
   one/side
+  three/HEAD -> three/main
   three/another
   three/main
   three/side
+  two/HEAD -> two/main
   two/another
   two/main
   two/side
@@ -165,7 +174,7 @@ EOF
 
 test_expect_success 'git fetch --multiple (ignoring skipFetchAll)' '
 	(cd test4 &&
-	 for b in $(git branch -r)
+	 for b in $(git branch -r | grep -v HEAD)
 	 do
 		git branch -r -d $b || exit 1
 	 done &&
@@ -221,14 +230,17 @@ test_expect_success 'git fetch --multiple --jobs=0 picks a default' '
 
 create_fetch_all_expect () {
 	cat >expect <<-\EOF
+	  one/HEAD -> one/main
 	  one/main
 	  one/side
 	  origin/HEAD -> origin/main
 	  origin/main
 	  origin/side
+	  three/HEAD -> three/main
 	  three/another
 	  three/main
 	  three/side
+	  two/HEAD -> two/main
 	  two/another
 	  two/main
 	  two/side
@@ -265,6 +277,7 @@ test_expect_success 'git fetch (fetch all remotes with fetch.all = true)' '
 
 create_fetch_one_expect () {
 	cat >expect <<-\EOF
+	  one/HEAD -> one/main
 	  one/main
 	  one/side
 	  origin/HEAD -> origin/main
t/t5516-fetch-push.sh +2 -1
index 331778bd42..5a051aa0c7 100755
@@ -1395,7 +1395,8 @@ test_expect_success 'fetch follows tags by default' '
 		git tag -m "annotated" tag &&
 		git for-each-ref >tmp1 &&
 		sed -n "p; s|refs/heads/main$|refs/remotes/origin/main|p" tmp1 |
-		sort -k 3 >../expect
+		sed -n "p; s|refs/heads/main$|refs/remotes/origin/HEAD|p"  |
+		sort -k 4 >../expect
 	) &&
 	test_when_finished "rm -rf dst" &&
 	git init dst &&
t/t5527-fetch-odd-refs.sh +2 -1
index 98ece27c6a..d3996af6ee 100755
@@ -52,7 +52,8 @@ test_expect_success LONG_REF 'fetch handles extremely long refname' '
 	long
 	main
 	EOF
-	git for-each-ref --format="%(subject)" refs/remotes/long >actual &&
+	git for-each-ref --format="%(subject)" refs/remotes/long \
+		--exclude=refs/remotes/long/HEAD >actual &&
 	test_cmp expect actual
 '
 
t/t7900-maintenance.sh +2 -1
index a66d0e089d..88331d1ceb 100755
@@ -329,7 +329,8 @@ test_expect_success 'incremental-repack task' '
 
 	# Delete refs that have not been repacked in these packs.
 	git for-each-ref --format="delete %(refname)" \
-		refs/prefetch refs/tags refs/remotes >refs &&
+		refs/prefetch refs/tags refs/remotes \
+		--exclude=refs/remotes/*/HEAD >refs &&
 	git update-ref --stdin <refs &&
 
 	# Replace the object directory with this pack layout.
t/t9210-scalar.sh +3 -2
index a30b2c9f70..2237844550 100755
@@ -151,7 +151,8 @@ test_expect_success 'scalar clone' '
 			"$(pwd)" &&
 
 		git for-each-ref --format="%(refname)" refs/remotes/origin/ >actual &&
-		echo "refs/remotes/origin/parallel" >expect &&
+		echo "refs/remotes/origin/HEAD" >>expect &&
+		echo "refs/remotes/origin/parallel" >>expect &&
 		test_cmp expect actual &&
 
 		test_path_is_missing 1/2 &&
@@ -220,7 +221,7 @@ test_expect_success 'scalar reconfigure --all with includeIf.onbranch' '
 	done
 '
 
- test_expect_success 'scalar reconfigure --all with detached HEADs' '
+test_expect_success 'scalar reconfigure --all with detached HEADs' '
 	repos="two three four" &&
 	for num in $repos
 	do
t/t9211-scalar-clone.sh +3 -3
index c16ea67c1d..d9cb6b9a3e 100755
@@ -32,7 +32,7 @@ test_expect_success 'set up repository to clone' '
 	)
 '
 
-cleanup_clone () {
+cleanup_clone() {
 	rm -rf "$1"
 }
 
@@ -128,7 +128,7 @@ test_expect_success '--single-branch clones HEAD only' '
 	(
 		cd $enlistment/src &&
 		git for-each-ref refs/remotes/origin >out &&
-		test_line_count = 1 out &&
+		test_line_count = 2 out &&
 		grep "refs/remotes/origin/base" out
 	) &&
 
@@ -142,7 +142,7 @@ test_expect_success '--no-single-branch clones all branches' '
 	(
 		cd $enlistment/src &&
 		git for-each-ref refs/remotes/origin >out &&
-		test_line_count = 2 out &&
+		test_line_count = 3 out &&
 		grep "refs/remotes/origin/base" out &&
 		grep "refs/remotes/origin/parallel" out
 	) &&
t/t9902-completion.sh +65 -0

443 lines not on this page · raw message

Signature · 1 line
2.47.0.94.gb64850d498
#107 lore raw

PATCH v11 8/8 fetch set_head: handle mirrored bare repositories

In reply to Bence Ferdinandy

When adding a remote to bare repository with "git remote add --mirror", running fetch will fail to update HEAD to the remote's HEAD, since it does not know how to handle bare repositories. On the other hand HEAD already has content, since "git init --bare" has already set HEAD to whatever is the default branch set for the user. Unless this - by chance
- is the same as the remote's HEAD, HEAD will be pointing to a bad symref. Teach set_head to handle bare repositories, by overwriting HEAD so it mirrors the remote's HEAD.

Note, that in this case overriding the local HEAD reference is necessary, since HEAD will exist before fetch can be run, but this should not be an issue, since the whole purpose of --mirror is to be an exact mirror of the remote, so following any changes to HEAD makes sense.

Also note, that although "git remote set-head" also fails when trying to update the remote's locally tracked HEAD in a mirrored bare repository, the usage of the command does not make much sense after this patch:
fetch will update the remote HEAD correctly, and setting it manually to something else is antithetical to the concept of mirroring.

---

Notes:
v10: - new patch
- handles the issue discovered in
https://lore.kernel.org/git/[email protected]/T/

v11: no change

 builtin/fetch.c   | 16 ++++++++++++----
 t/t5505-remote.sh | 10 ++++++++++
 2 files changed, 22 insertions(+), 4 deletions(-)
builtin/fetch.c +12 -4
index cadfd2407a..8af3efd6ac 100644
@@ -1633,14 +1633,22 @@ static int set_head(const struct ref *remote_refs)
 	else
 		head_name = xstrdup(heads.items[0].string);
 	if (head_name) {
-		strbuf_addf(&b_head, "refs/remotes/%s/HEAD", remote);
-		strbuf_addf(&b_remote_head, "refs/remotes/%s/%s", remote, head_name);
+		int is_bare = is_bare_repository();
+		if (is_bare) {
+			strbuf_addstr(&b_head, "HEAD");
+			strbuf_addf(&b_remote_head, "refs/heads/%s", head_name);
+		} else {
+			strbuf_addf(&b_head, "refs/remotes/%s/HEAD", remote);
+			strbuf_addf(&b_remote_head, "refs/remotes/%s/%s", remote, head_name);
+		}
 		/* make sure it's valid */
-		if (!refs_ref_exists(refs, b_remote_head.buf))
+		if (!is_bare && !refs_ref_exists(refs, b_remote_head.buf)) {
 			result = 1;
+		}
 		else if (refs_update_symref_extended(refs, b_head.buf, b_remote_head.buf,
-					"fetch", &b_local_head, 1))
+					"fetch", &b_local_head, !is_bare)) {
 			result = 1;
+		}
 		else
 			report_set_head(remote, head_name, &b_local_head);
 
t/t5505-remote.sh +10 -0
index 4990d00209..dfa78f3e8d 100755
@@ -545,6 +545,16 @@ test_expect_success 'add --mirror && prune' '
 	)
 '
 
+test_expect_success 'add --mirror setting HEAD' '
+	mkdir headmirror &&
+	(
+		cd headmirror &&
+		git init --bare -b notmain &&
+		git remote add --mirror -f origin ../one &&
+		test "$(git symbolic-ref HEAD)" = "refs/heads/main"
+	)
+'
+
 test_expect_success 'add --mirror=fetch' '
 	mkdir mirror-fetch &&
 	git init -b main mirror-fetch/parent &&
Signature · 1 line
2.47.0.94.gb64850d498
#108 lore raw

v12 cover set-head/fetch remote/HEAD updates

In reply to Bence Ferdinandy

v12: added missing S-o-b

v11: fixed leaks found by running tests after make SANITIZE=leak

    green pipeline:
    https://github.com/ferdinandyb/git/actions/runs/11466803523

v10: added a new patch (8/8) on top of v9 to handle HEAD in mirrored bare repositories, rest of the patches are unchanged

v9:
the two most notable changes are
- the new patch 1/7 which address
https://lore.kernel.org/git/Zw8IKyPkG0Hr6%[email protected]/, but see
https://lore.kernel.org/git/[email protected]/
- the refs_update_symref -> refs_update_symref_extended change in 2/7, reflecting on Phillip's comments (see
https://lore.kernel.org/git/[email protected]/)

Bence Ferdinandy (8):
t/t5505-remote: set default branch to main
refs: atomically record overwritten ref in update_symref remote set-head: refactor for readability
remote set-head: better output for --auto
refs: add TRANSACTION_CREATE_EXISTS error
refs: add create_only option to refs_update_symref_extended
fetch: set remote/HEAD if it does not exist
fetch set_head: handle mirrored bare repositories

 builtin/fetch.c                  |  94 +++++++++++++
 builtin/remote.c                 |  52 +++++--
 refs.c                           |  41 +++++-
 refs.h                           |   8 +-
 refs/files-backend.c             |  24 ++--
 refs/reftable-backend.c          |   6 +-
 t/t4207-log-decoration-colors.sh |   3 +-
 t/t5505-remote.sh                |  83 ++++++++++-
 t/t5510-fetch.sh                 | 229 ++++++++++++++++---------------
 t/t5512-ls-remote.sh             |   2 +
 t/t5514-fetch-multiple.sh        |  17 ++-
 t/t5516-fetch-push.sh            |   3 +-
 t/t5527-fetch-odd-refs.sh        |   3 +-
 t/t7900-maintenance.sh           |   3 +-
 t/t9210-scalar.sh                |   5 +-
 t/t9211-scalar-clone.sh          |   6 +-
 t/t9902-completion.sh            |  65 +++++++++
 17 files changed, 490 insertions(+), 154 deletions(-)
Signature · 1 line
2.47.0.94.g1247fb88fd.dirty
#109 lore raw

PATCH v12 1/8 t/t5505-remote: set default branch to main

Consider the bare repository called "mirror" in the test. Running git remote add --mirror -f origin ../one will not change HEAD, consequently if init.defaultBranch is not the same as what HEAD in the remote ("one"), HEAD in "mirror" will be pointing to a non-existent reference. Hence if "mirror" is used as a remote by yet another repository, ls-remote will not show HEAD. On the other hand, if init.defaultBranch happens to match HEAD in "one", then ls-remote will show HEAD.

Since the CI globally exports GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main, there's a drift between how the test repositories are set up in the CI and during local testing. This issue does not manifest currently, as the test does not do any remote HEAD manipulation where this would come up, but should such things be added, a locally passing test would break the CI vice-versa.

Set GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main in the test to be consistent with the CI.

---

Notes:
v9: - new patch
- a bandaid for the CI issue noticed by Taylor (cf:
https://lore.kernel.org/git/Zw8IKyPkG0Hr6%[email protected]/), but
see
https://lore.kernel.org/git/[email protected]/ for the root cause in detail

v10: no change

v11: no change

v12: added forgotten sign-off

 t/t5505-remote.sh | 3 +++
 1 file changed, 3 insertions(+)
t/t5505-remote.sh +3 -0
index 532035933f..9b50276646 100755
@@ -2,6 +2,9 @@
 
 test_description='git remote porcelain-ish'
 
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
 TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
Signature · 1 line
2.47.0.94.g1247fb88fd.dirty
#110 lore raw

Re: [PATCH v12 1/8] t/t5505-remote: set default branch to main

Quoted 7 lines · Bence Ferdinandy

Consider the bare repository called "mirror" in the test. Running git remote add --mirror -f origin ../one will not change HEAD, consequently if init.defaultBranch is not the same as what HEAD in the remote ("one"), HEAD in "mirror" will be pointing to a non-existent reference. Hence if "mirror" is used as a remote by yet another repository, ls-remote will not show HEAD. On the other hand, if init.defaultBranch happens to match HEAD in "one", then ls-remote will show HEAD.

Making sure that the hardcoded (in the Git binary) default branch name would not affect the outcome of the test is a good thing to do. I like the patch text, but ...

Quoted 6 lines

Since the CI globally exports GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main, there's a drift between how the test repositories are set up in the CI and during local testing. This issue does not manifest currently, as the test does not do any remote HEAD manipulation where this would come up, but should such things be added, a locally passing test would break the CI vice-versa.

... this description may not quite be accurate. Don't some jobs of CI use 'main' while the rest use 'master'?

#111 lore raw

Re: [PATCH v12 1/8] t/t5505-remote: set default branch to main

Quoted 21 lines · Junio C Hamano
Quoted 7 lines · Bence Ferdinandy

Consider the bare repository called "mirror" in the test. Running git remote add --mirror -f origin ../one will not change HEAD, consequently if init.defaultBranch is not the same as what HEAD in the remote ("one"), HEAD in "mirror" will be pointing to a non-existent reference. Hence if "mirror" is used as a remote by yet another repository, ls-remote will not show HEAD. On the other hand, if init.defaultBranch happens to match HEAD in "one", then ls-remote will show HEAD.

Making sure that the hardcoded (in the Git binary) default branch name would not affect the outcome of the test is a good thing to do. I like the patch text, but ...

Quoted 6 lines

Since the CI globally exports GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main, there's a drift between how the test repositories are set up in the CI and during local testing. This issue does not manifest currently, as the test does not do any remote HEAD manipulation where this would come up, but should such things be added, a locally passing test would break the CI vice-versa.

Quoted 2 lines

... this description may not quite be accurate. Don't some jobs of CI use 'main' while the rest use 'master'?

True, it's that one specific job, and of course "there may be a drift depending on the particular test" is more accurate. I'll update for a v13.

#112 lore raw

PATCH v12 2/8 refs: atomically record overwritten ref in update_symref

In reply to Bence Ferdinandy

When updating a symref with update_symref it's currently not possible to know for sure what was the previous value that was overwritten. Extend refs_update_symref under a new function name, to record the value after the ref has been locked if the caller of refs_update_symref_extended requests it via a new variable in the function call. Keep the original refs_update_symref function with the same signature, but now as a wrapper around refs_update_symref_extended.

---

Notes:
v4: new patch

v5: - added before_target to reftables backend
- added an extra safety check for transaction's existence in refs.c

v6: - no change

v7: - remove the whole before_target concept from the backends and
handle checking it in refs.c instead (thanks Karthik)
- rename the before_target to referent which is how the same concept is called in the backends
- change commit prefix to be more in line with project standards

v8: no change

v9: - instead of adding parameters to refs_update_symref, rename what
was in v8 as refs_update_symref_extended and make refs_update_symref a wrapper for that. This significantly reduces the number of files that need to be touched, and avoids adding a lot of dummy NULL-s in unrelated places.

v10: no change

v11: no change

v12: no change

 refs.c | 19 ++++++++++++++++---
 refs.h |  4 ++++
 2 files changed, 20 insertions(+), 3 deletions(-)
refs.c +16 -3
index 5f729ed412..24a4172cd2 100644
@@ -2115,6 +2115,13 @@ int peel_iterated_oid(struct repository *r, const struct object_id *base, struct
 
 int refs_update_symref(struct ref_store *refs, const char *ref,
 		       const char *target, const char *logmsg)
+{
+	return refs_update_symref_extended(refs, ref, target, logmsg, NULL);
+}
+
+int refs_update_symref_extended(struct ref_store *refs, const char *ref,
+		       const char *target, const char *logmsg,
+		       struct strbuf *referent)
 {
 	struct ref_transaction *transaction;
 	struct strbuf err = STRBUF_INIT;
@@ -2122,13 +2129,20 @@ int refs_update_symref(struct ref_store *refs, const char *ref,
 
 	transaction = ref_store_transaction_begin(refs, &err);
 	if (!transaction ||
-	    ref_transaction_update(transaction, ref, NULL, NULL,
+		ref_transaction_update(transaction, ref, NULL, NULL,
 				   target, NULL, REF_NO_DEREF,
 				   logmsg, &err) ||
-	    ref_transaction_commit(transaction, &err)) {
+		ref_transaction_prepare(transaction, &err)) {
 		ret = error("%s", err.buf);
+		goto cleanup;
 	}
+	if (referent)
+		refs_read_symbolic_ref(refs, ref, referent);
 
+	if (ref_transaction_commit(transaction, &err))
+		ret = error("%s", err.buf);
+
+cleanup:
 	strbuf_release(&err);
 	if (transaction)
 		ref_transaction_free(transaction);
@@ -2948,4 +2962,3 @@ int ref_update_expects_existing_old_ref(struct ref_update *update)
 	return (update->flags & REF_HAVE_OLD) &&
 		(!is_null_oid(&update->old_oid) || update->old_target);
 }
-
refs.h +4 -0
index 108dfc93b3..259191a485 100644
@@ -573,6 +573,10 @@ int refs_copy_existing_ref(struct ref_store *refs, const char *oldref,
 int refs_update_symref(struct ref_store *refs, const char *refname,
 		       const char *target, const char *logmsg);
 
+int refs_update_symref_extended(struct ref_store *refs, const char *refname,
+		       const char *target, const char *logmsg,
+		       struct strbuf *referent);
+
 enum action_on_err {
 	UPDATE_REFS_MSG_ON_ERR,
 	UPDATE_REFS_DIE_ON_ERR,
Signature · 1 line
2.47.0.94.g1247fb88fd.dirty
#113 lore raw

Re: [PATCH v12 2/8] refs: atomically record overwritten ref in update_symref

refs.c int peel_iterated_oid(struct repository *r, const struct object_id *base, struct · line 2120

Quoted 5 lines · Bence Ferdinandy

int refs_update_symref(struct ref_store *refs, const char *ref,

refs.c +3 -0
 		       const char *target, const char *logmsg)
+{
+	return refs_update_symref_extended(refs, ref, target, logmsg, NULL);
+}

OK. As the enhanced and renamed function is also external, we do not have to worry about reordering the old one to come after the new one.

refs.c int refs_update_symref(struct ref_store *refs, const char *ref, · line 2132

Quoted 12 lines
refs.c +4 -1
+int refs_update_symref_extended(struct ref_store *refs, const char *ref,
+		       const char *target, const char *logmsg,
+		       struct strbuf *referent)
 {
 	struct ref_transaction *transaction;
 	struct strbuf err = STRBUF_INIT;
@@ -2122,13 +2129,20 @@ int refs_update_symref(struct ref_store *refs, const char *ref,
 
 	transaction = ref_store_transaction_begin(refs, &err);
 	if (!transaction ||
-	    ref_transaction_update(transaction, ref, NULL, NULL,
+		ref_transaction_update(transaction, ref, NULL, NULL,

An unwanted patch noise?

refs.c int refs_update_symref(struct ref_store *refs, const char *ref, · line 2135

Quoted 4 lines
refs.c +1 -1
 				   target, NULL, REF_NO_DEREF,
 				   logmsg, &err) ||
-	    ref_transaction_commit(transaction, &err)) {
+		ref_transaction_prepare(transaction, &err)) {

Likewise, but the noise distracts from the real change made on this line, which is even worse.

The real change here is to only call _prepare(), which also asks the transaction hook if we are OK to proceed. If we fail, we stop here

refs.c int refs_update_symref(struct ref_store *refs, const char *ref, · line 2137

Quoted 2 lines
refs.c +1 -0
 		ret = error("%s", err.buf);
+		goto cleanup;

This is also a real change. We could instead make the additional code below into the else clause (see below).

refs.c int refs_update_symref(struct ref_store *refs, const char *ref, · line 2140

Quoted 3 lines
refs.c +2 -0
 	}
+	if (referent)
+		refs_read_symbolic_ref(refs, ref, referent);

And if we were asked to give the value of the symbolic ref, we make this call. What should this code do when reading fails (I know it ignores, as written, but I am asking what it _should_ do)?

refs.c int refs_update_symref(struct ref_store *refs, const char *ref, · line 2143

Quoted 2 lines
refs.c +2 -0
+	if (ref_transaction_commit(transaction, &err))
+		ret = error("%s", err.buf);

And then we commit, or we fail to commit.

refs.c int refs_update_symref(struct ref_store *refs, const char *ref, · line 2145

Quoted 1 line
refs.c +1 -0
+cleanup:

We could write the whole thing as a single "do these and leave as soon as we see any failure" ||-cascade,

	if (!(transaction = ref_store_transaction_begin(refs, &err)) ||

	    ref_transaction_update(transaction, ref, NULL, NULL,
				   target, NULL, REF_NO_DEREF,
				   logmsg, &err) ||

	    ref_transaction_prepare(transaction, &err)) ||

	    (referent
	    ? refs_read_symbolic_ref(refs, ref, referent)
	    : 0) ||

	    ref_transaction_commit(transaction, &err)) {
		if (!err.len)
			... stuff default error message to err ...;
		ret = error("%s", err.buf);
	}

which may not necessarily easier to follow (and in fact it is rather ugly), but at least, the resulting code does not have to special case the "optionally peek into the symref" step.

#114 lore raw

Re: [PATCH v12 2/8] refs: atomically record overwritten ref in update_symref

Quoted 11 lines · Junio C Hamano

refs.c int peel_iterated_oid(struct repository *r, const struct object_id *base, struct · line 2120

Quoted 5 lines · Bence Ferdinandy

int refs_update_symref(struct ref_store *refs, const char *ref,

refs.c +3 -0
 		       const char *target, const char *logmsg)
+{
+	return refs_update_symref_extended(refs, ref, target, logmsg, NULL);
+}

OK. As the enhanced and renamed function is also external, we do not have to worry about reordering the old one to come after the new one.

I guess this also decides that the name "_extended" is fine :)

Quoted 16 lines

refs.c int refs_update_symref(struct ref_store *refs, const char *ref, · line 2132

Quoted 13 lines
refs.c +4 -1
+int refs_update_symref_extended(struct ref_store *refs, const char *ref,
+		       const char *target, const char *logmsg,
+		       struct strbuf *referent)
 {
 	struct ref_transaction *transaction;
 	struct strbuf err = STRBUF_INIT;
@@ -2122,13 +2129,20 @@ int refs_update_symref(struct ref_store *refs, const char *ref,
 
 	transaction = ref_store_transaction_begin(refs, &err);
 	if (!transaction ||
-	    ref_transaction_update(transaction, ref, NULL, NULL,
+		ref_transaction_update(transaction, ref, NULL, NULL,

An unwanted patch noise?

Quoted 7 lines

refs.c int refs_update_symref(struct ref_store *refs, const char *ref, · line 2135

Quoted 4 lines
refs.c +1 -1
 				   target, NULL, REF_NO_DEREF,
 				   logmsg, &err) ||
-	    ref_transaction_commit(transaction, &err)) {
+		ref_transaction_prepare(transaction, &err)) {

Likewise, but the noise distracts from the real change made on this line, which is even worse.

Mea culpa, I'll get this cleaned up.

Quoted 10 lines

The real change here is to only call _prepare(), which also asks the transaction hook if we are OK to proceed. If we fail, we stop here

refs.c int refs_update_symref(struct ref_store *refs, const char *ref, · line 2137

Quoted 2 lines
refs.c +1 -0
 		ret = error("%s", err.buf);
+		goto cleanup;

This is also a real change. We could instead make the additional code below into the else clause (see below).

Quoted 7 lines

refs.c int refs_update_symref(struct ref_store *refs, const char *ref, · line 2140

Quoted 3 lines
refs.c +2 -0
 	}
+	if (referent)
+		refs_read_symbolic_ref(refs, ref, referent);

And if we were asked to give the value of the symbolic ref, we make this call. What should this code do when reading fails (I know it ignores, as written, but I am asking what it _should_ do)?

I think this should do _nothing_ if it fails (although should it stay this way, I guess it should be marked with a comment that this is on purpose). My reasoning is that running git fetch will be running this part of the code, which means that should reading the symbolic ref fail for any reason, a fetch that previously ran without error would now fail. We now pass up an empty string as the previous which does mask that there was an error here. What I think we could maybe do is pass up a special string that means there was an error? Something that for sure cannot be a valid value for an existing reference? I'm not sure how much sense that makes.

Quoted 29 lines

refs.c int refs_update_symref(struct ref_store *refs, const char *ref, · line 2143

Quoted 3 lines
refs.c +2 -0
+	if (ref_transaction_commit(transaction, &err))
+		ret = error("%s", err.buf);

And then we commit, or we fail to commit.

refs.c int refs_update_symref(struct ref_store *refs, const char *ref, · line 2145

Quoted 1 line
refs.c +1 -0
+cleanup:

We could write the whole thing as a single "do these and leave as soon as we see any failure" ||-cascade,

	if (!(transaction = ref_store_transaction_begin(refs, &err)) ||

	    ref_transaction_update(transaction, ref, NULL, NULL,
				   target, NULL, REF_NO_DEREF,
				   logmsg, &err) ||

	    ref_transaction_prepare(transaction, &err)) ||

	    (referent
	    ? refs_read_symbolic_ref(refs, ref, referent)
	    : 0) ||

	    ref_transaction_commit(transaction, &err)) {
		if (!err.len)
			... stuff default error message to err ...;
		ret = error("%s", err.buf);
	}
Quoted 3 lines

which may not necessarily easier to follow (and in fact it is rather ugly), but at least, the resulting code does not have to special case the "optionally peek into the symref" step.

As I said above, I don't think we actually want to fail the update even if the symbolic ref reading fails, so I think the special casing should stay. I'll wait here to see more clearly on what to do here.

#115 lore raw

Re: [PATCH v12 2/8] refs: atomically record overwritten ref in update_symref

2024. nov. 15. 23:18:10 Bence Ferdinandy <[email protected]>:

Quoted 63 lines · Junio C Hamano
Quoted 12 lines
Quoted 6 lines · Bence Ferdinandy

int refs_update_symref(struct ref_store *refs, const char *ref, const char *target, const char *logmsg) +{
+   return refs_update_symref_extended(refs, ref, target, logmsg, NULL);
+}

OK.  As the enhanced and renamed function is also external, we do not have to worry about reordering the old one to come after the new one.

I guess this also decides that the name "_extended" is fine :)

Quoted 18 lines
Quoted 12 lines

+int refs_update_symref_extended(struct ref_store *refs, const char *ref,
+              const char *target, const char *logmsg,
+              struct strbuf *referent)
{
    struct ref_transaction *transaction;
    struct strbuf err = STRBUF_INIT;

refs.c +0 -0
@@ -2122,13 +2129,20 @@ int refs_update_symref(struct ref_store 
 *refs, const char *ref,
 
     transaction = ref_store_transaction_begin(refs, &err);
Quoted 3 lines

    if (!transaction ||
-       ref_transaction_update(transaction, ref, NULL, NULL, +       ref_transaction_update(transaction, ref, NULL, NULL,

An unwanted patch noise?

Quoted 7 lines
Quoted 4 lines

                   target, NULL, REF_NO_DEREF, logmsg, &err) ||
-       ref_transaction_commit(transaction, &err)) { +       ref_transaction_prepare(transaction, &err)) {

Likewise, but the noise distracts from the real change made on this line, which is even worse.

Mea culpa, I'll get this cleaned up.

Quoted 10 lines

The real change here is to only call _prepare(), which also asks the transaction hook if we are OK to proceed.  If we fail, we stop here

Quoted 2 lines

        ret = error("%s", err.buf);
+       goto cleanup;

This is also a real change.  We could instead make the additional code below into the else clause (see below).

Quoted 7 lines
Quoted 3 lines

    }
+   if (referent)
+       refs_read_symbolic_ref(refs, ref, referent);

And if we were asked to give the value of the symbolic ref, we make this call.  What should this code do when reading fails (I know it ignores, as written, but I am asking what it _should_ do)?

Quoted 9 lines

I think this should do _nothing_ if it fails (although should it stay this way, I guess it should be marked with a comment that this is on purpose). My reasoning is that running git fetch will be running this part of the code, which means that should reading the symbolic ref fail for any reason, a fetch that previously ran without error would now fail. We now pass up an empty string as the previous which does mask that there was an error here. What I think we could maybe do is pass up a special string that means there was an error? Something that for sure cannot be a valid value for an existing reference? I'm not sure how much sense that makes.

Sorry, it's late. The above is slightly bollocks since fetch ignores any set_head errors later :) But the idea stands that if we can set the head, let's do it.
The previous head is not important enough to die on.

As mentioned on the other patch, we could try to read a non-symref instead also, if symref fails.

Quoted 37 lines
Quoted 30 lines
Quoted 4 lines

+   if (ref_transaction_commit(transaction, &err)) +       ret = error("%s", err.buf);

And then we commit, or we fail to commit.

Quoted 1 line

+cleanup:

We could write the whole thing as a single "do these and leave as soon as we see any failure" ||-cascade,

    if (!(transaction = ref_store_transaction_begin(refs, &err)) ||

        ref_transaction_update(transaction, ref, NULL, NULL, target, NULL, REF_NO_DEREF, logmsg, &err) ||

        ref_transaction_prepare(transaction, &err)) ||

        (referent
        ? refs_read_symbolic_ref(refs, ref, referent) : 0) ||

        ref_transaction_commit(transaction, &err)) { if (!err.len)
            ... stuff default error message to err ...; ret = error("%s", err.buf);
    }

Quoted 3 lines

which may not necessarily easier to follow (and in fact it is rather ugly), but at least, the resulting code does not have to special case the "optionally peek into the symref" step.

As I said above, I don't think we actually want to fail the update even if the symbolic ref reading fails, so I think the special casing should stay. I'll wait here to see more clearly on what to do here.

#116 lore raw

Re: [PATCH v12 2/8] refs: atomically record overwritten ref in update_symref

Quoted 4 lines · Bence Ferdinandy

Sorry, it's late. The above is slightly bollocks since fetch ignores any set_head errors later :)
But the idea stands that if we can set the head, let's do it. The previous head is not important enough to die on.

Yes, I didn't mean to suggest aborting the fetch. I just wanted to make sure we _react_ correctly to a failing call to read a symref, possibly because the refs/remotes/$there/HEAD (1) does not exist, or
(2) is not a symbolic ref but signals that the other side has its HEAD detached. Treating any failure to read a symref as if the symref does not exist would miss the latter case.

Thanks.

#117 lore raw

Re: [PATCH v12 2/8] refs: atomically record overwritten ref in update_symref

In reply to Junio C Hamano
Quoted 3 lines

Review

Quoted 3 lines
(quoted diff) +0 -0
 	} +	if (referent) +		refs_read_symbolic_ref(refs, ref,
 	referent);

So I've been working on detecting a detached remote/HEAD, and it seems that "refs_read_symbolic_ref" behaves differently for the files and the reftables backend. These are the exit codes in the various states:

reftables files
detached -1 1
doesn't exist -1 -1

I would assume this is a bug in reftables? At least the behaviour of files is more useful for this case ...

This now works fine with the files backend:

	if (referent && refs_read_symbolic_ref(refs, ref, referent) == 1) {
		struct object_id oid;
		refs_read_ref(refs, ref, &oid);
		strbuf_addstr(referent, oid_to_hex(&oid));
		ret = -1;
	}

And 4/8 can now also detect being detached, by checking the return value using the test you suggested, but this fails for reftables. Just in case it might be something about the test not being correct:

test_expect_success 'set-head --auto to update a non symbolic ref' '

	(
		cd test &&
		git update-ref --no-deref -d refs/remotes/origin/HEAD &&
		git update-ref refs/remotes/origin/HEAD HEAD &&
		HEAD=$(git log --pretty="%H") &&
		git remote set-head --auto origin >output &&
		echo "${SQ}origin/HEAD${SQ} was detached at ${SQ}${HEAD}${SQ} and now points to ${SQ}main${SQ}" >expect &&
		test_cmp expect output
	)

'

Should I fix the reftables problem or should I mark the above test as a known breakage? Can that be done for just reftables somehow? The only problem reftables suffers this way is to incorrectly report "creation" instead of correctly saying it was a detached head.

Quoted 21 lines

We could write the whole thing as a single "do these and leave as soon as we see any failure" ||-cascade,

	if (!(transaction = ref_store_transaction_begin(refs, &err)) ||

	    ref_transaction_update(transaction, ref, NULL, NULL,
				   target, NULL, REF_NO_DEREF,
				   logmsg, &err) ||

	    ref_transaction_prepare(transaction, &err)) ||

	    (referent
	    ? refs_read_symbolic_ref(refs, ref, referent)
	    : 0) ||

	    ref_transaction_commit(transaction, &err)) {
		if (!err.len)
			... stuff default error message to err ...;
		ret = error("%s", err.buf);
	}
Quoted 3 lines

which may not necessarily easier to follow (and in fact it is rather ugly), but at least, the resulting code does not have to special case the "optionally peek into the symref" step.

I realised I misread the above snippet the last time I looked at it, but at a first glance that would make 6/8 painful.

Thanks,
Bence

Signature · 1 line
bence.ferdinandy.com
#118 lore raw

Re: [PATCH v12 2/8] refs: atomically record overwritten ref in update_symref

Quoted 10 lines · Bence Ferdinandy
Quoted 3 lines

Review

Quoted 3 lines
(quoted diff) +0 -0
 	} +	if (referent) +		refs_read_symbolic_ref(refs, ref,
 	referent);

So I've been working on detecting a detached remote/HEAD, and it seems that "refs_read_symbolic_ref" behaves differently for the files and the reftables backend. These are the exit codes in the various states:

Quoted 6 lines

reftables files
detached -1 1
doesn't exist -1 -1

I would assume this is a bug in reftables? At least the behaviour of files is more useful for this case ...

Interesting. Patrick, comments?

Quoted 24 lines

This now works fine with the files backend:

	if (referent && refs_read_symbolic_ref(refs, ref, referent) == 1) {
		struct object_id oid;
		refs_read_ref(refs, ref, &oid);
		strbuf_addstr(referent, oid_to_hex(&oid));
		ret = -1;
	}

And 4/8 can now also detect being detached, by checking the return value using the test you suggested, but this fails for reftables. Just in case it might be something about the test not being correct:

test_expect_success 'set-head --auto to update a non symbolic ref' '

	(
		cd test &&
		git update-ref --no-deref -d refs/remotes/origin/HEAD &&
		git update-ref refs/remotes/origin/HEAD HEAD &&
		HEAD=$(git log --pretty="%H") &&
		git remote set-head --auto origin >output &&
		echo "${SQ}origin/HEAD${SQ} was detached at ${SQ}${HEAD}${SQ} and now points to ${SQ}main${SQ}" >expect &&
		test_cmp expect output
	)
Quoted 6 lines

'

Should I fix the reftables problem or should I mark the above test as a known breakage? Can that be done for just reftables somehow? The only problem reftables suffers this way is to incorrectly report "creation" instead of correctly saying it was a detached head.

Thanks.

#119 lore raw

Re: [PATCH v12 2/8] refs: atomically record overwritten ref in update_symref

In reply to Bence Ferdinandy
Quoted 26 lines · Bence Ferdinandy
Quoted 3 lines

Review

Quoted 3 lines
(quoted diff) +0 -0
 	} +	if (referent) +		refs_read_symbolic_ref(refs, ref,
 	referent);

So I've been working on detecting a detached remote/HEAD, and it seems that "refs_read_symbolic_ref" behaves differently for the files and the reftables backend. These are the exit codes in the various states:

reftables files
detached -1 1
doesn't exist -1 -1

I would assume this is a bug in reftables? At least the behaviour of files is more useful for this case ...

This now works fine with the files backend:

	if (referent && refs_read_symbolic_ref(refs, ref, referent) == 1) {
		struct object_id oid;
		refs_read_ref(refs, ref, &oid);
		strbuf_addstr(referent, oid_to_hex(&oid));
		ret = -1;
	}
Quoted 3 lines

And 4/8 can now also detect being detached, by checking the return value using the test you suggested, but this fails for reftables. Just in case it might be something about the test not being correct:

So from what I understand you try to execute refs_read_symbolic_ref() on a non-symbolic-ref, and your expectation is:

- It returns -1 when reading the ref has failed.

- It returns 0 when reading the ref was successful and it was a symref.

- It retuns 1 when reading the ref was successful, but it was a regular ref.

This behaviour isn't documented anywhere, so I wouldn't declare it a bug in the reftable backend. But what is a bug is that the two backends behave differently, and that should be fixed indeed.

I couldn't find any callsites of refs_read_symbolic_ref() where we rely on the current behaviour of either of the backends. We do have a check whether refs_read_symbolic_ref() returns negative in "refs.c" in migrate_one_ref(), but that one should be mostly fine given that we check for the type of the ref beforehand. "Mostly" though because it can happen that we race with another writer that happened to convert the ref we are about to migrate from a symbolic ref into a normal ref. Unlikely, but it can happen in theory.

I think it's an easy mistake to make to check for a negative return code. So maybe we should adapt both backends to return -1 for generic failures and -2 in case the ref is a regular ref?

Patrick

#120 lore raw

Re: [PATCH v12 2/8] refs: atomically record overwritten ref in update_symref

Quoted 11 lines · Patrick Steinhardt

So from what I understand you try to execute refs_read_symbolic_ref() on a non-symbolic-ref, and your expectation is:

- It returns -1 when reading the ref has failed.

- It returns 0 when reading the ref was successful and it was a symref.

- It retuns 1 when reading the ref was successful, but it was a regular ref.

Well, the other way around (it's how files backend does it), but I guess that is irrelevant at this point.

Quoted 14 lines

This behaviour isn't documented anywhere, so I wouldn't declare it a bug in the reftable backend. But what is a bug is that the two backends behave differently, and that should be fixed indeed.

I couldn't find any callsites of refs_read_symbolic_ref() where we rely on the current behaviour of either of the backends. We do have a check whether refs_read_symbolic_ref() returns negative in "refs.c" in migrate_one_ref(), but that one should be mostly fine given that we check for the type of the ref beforehand. "Mostly" though because it can happen that we race with another writer that happened to convert the ref we are about to migrate from a symbolic ref into a normal ref. Unlikely, but it can happen in theory.

Quoted 3 lines

I think it's an easy mistake to make to check for a negative return code. So maybe we should adapt both backends to return -1 for generic failures and -2 in case the ref is a regular ref?

I've been wondering about this when writing other parts of the series and now is a good a time as any to ask: I've already seen this pattern of returning various negative integers as error codes, but never quite got the logic behind it. Why not just return the same numbers but positive?

Anyhow, the proposed solution sounds good and as far as I see how things are done in the code. I guess if I want the series to land I should just fix that as well, there are already a couple of not-entirely-related fixes in there :)

Two questions about that:

- what would be the ideal place to document this behaviour? In refs.c with refs_read_symbolic_ref or with the struct ref_storage_be in refs/refs-internal.h?

- should I look into adding specific tests for this? Since the rest of the series will depend on this behaviour it will be implicit tested anyway, so I don't particularly think it would be necessary, but I don't know what the general approach is.

Thanks,
Bence

#121 lore raw

Re: [PATCH v12 2/8] refs: atomically record overwritten ref in update_symref

Quoted 14 lines · Bence Ferdinandy
Quoted 13 lines · Patrick Steinhardt

This behaviour isn't documented anywhere, so I wouldn't declare it a bug in the reftable backend. But what is a bug is that the two backends behave differently, and that should be fixed indeed.

I couldn't find any callsites of refs_read_symbolic_ref() where we rely on the current behaviour of either of the backends. We do have a check whether refs_read_symbolic_ref() returns negative in "refs.c" in migrate_one_ref(), but that one should be mostly fine given that we check for the type of the ref beforehand. "Mostly" though because it can happen that we race with another writer that happened to convert the ref we are about to migrate from a symbolic ref into a normal ref. Unlikely, but it can happen in theory.

Quoted 8 lines
Quoted 3 lines

I think it's an easy mistake to make to check for a negative return code. So maybe we should adapt both backends to return -1 for generic failures and -2 in case the ref is a regular ref?

I've been wondering about this when writing other parts of the series and now is a good a time as any to ask: I've already seen this pattern of returning various negative integers as error codes, but never quite got the logic behind it. Why not just return the same numbers but positive?

It's a matter of style, I guess. Many functions use the return value as both an indicator for error and as the actual returned value. Think e.g. function calls like open(3p), where a negative value indicates an error and everything else is an actual file descriptor. This carries over into our codebase for many functions, but we're not consistent.

Quoted 9 lines

Anyhow, the proposed solution sounds good and as far as I see how things are done in the code. I guess if I want the series to land I should just fix that as well, there are already a couple of not-entirely-related fixes in there :)

Two questions about that:

- what would be the ideal place to document this behaviour? In refs.c with refs_read_symbolic_ref or with the struct ref_storage_be in refs/refs-internal.h?

I'd document this in "refs.h", where the user-facing function is declared, and in "refs-internal.h", where the callback is defined.

Quoted 4 lines

- should I look into adding specific tests for this? Since the rest of the series will depend on this behaviour it will be implicit tested anyway, so I don't particularly think it would be necessary, but I don't know what the general approach is.

I had a look and couldn't find another way to test the behaviour because we use refs_read_symbolic_ref() sparingly, only. So I think it's okay to implicitly test this, only.

Patrick

#122 lore raw

Re: [PATCH v12 2/8] refs: atomically record overwritten ref in update_symref

Quoted 43 lines · Patrick Steinhardt
Quoted 14 lines · Bence Ferdinandy
Quoted 13 lines · Patrick Steinhardt

This behaviour isn't documented anywhere, so I wouldn't declare it a bug in the reftable backend. But what is a bug is that the two backends behave differently, and that should be fixed indeed.

I couldn't find any callsites of refs_read_symbolic_ref() where we rely on the current behaviour of either of the backends. We do have a check whether refs_read_symbolic_ref() returns negative in "refs.c" in migrate_one_ref(), but that one should be mostly fine given that we check for the type of the ref beforehand. "Mostly" though because it can happen that we race with another writer that happened to convert the ref we are about to migrate from a symbolic ref into a normal ref. Unlikely, but it can happen in theory.

Quoted 8 lines
Quoted 3 lines

I think it's an easy mistake to make to check for a negative return code. So maybe we should adapt both backends to return -1 for generic failures and -2 in case the ref is a regular ref?

I've been wondering about this when writing other parts of the series and now is a good a time as any to ask: I've already seen this pattern of returning various negative integers as error codes, but never quite got the logic behind it. Why not just return the same numbers but positive?

It's a matter of style, I guess. Many functions use the return value as both an indicator for error and as the actual returned value. Think e.g. function calls like open(3p), where a negative value indicates an error and everything else is an actual file descriptor. This carries over into our codebase for many functions, but we're not consistent.

Quoted 9 lines

Anyhow, the proposed solution sounds good and as far as I see how things are done in the code. I guess if I want the series to land I should just fix that as well, there are already a couple of not-entirely-related fixes in there :)

Two questions about that:

- what would be the ideal place to document this behaviour? In refs.c with refs_read_symbolic_ref or with the struct ref_storage_be in refs/refs-internal.h?

I'd document this in "refs.h", where the user-facing function is declared, and in "refs-internal.h", where the callback is defined.

Quoted 8 lines
Quoted 4 lines

- should I look into adding specific tests for this? Since the rest of the series will depend on this behaviour it will be implicit tested anyway, so I don't particularly think it would be necessary, but I don't know what the general approach is.

I had a look and couldn't find another way to test the behaviour because we use refs_read_symbolic_ref() sparingly, only. So I think it's okay to implicitly test this, only.

Thanks, it was surprisingly easy to do, I'll do a touchup of the other patches and will send a v13 today with the fix.

Best,
Bence

#123 lore raw

PATCH v12 3/8 remote set-head: refactor for readability

In reply to Bence Ferdinandy

Rename buf and buf2 to something more explanatory.

Instead of calling get_main_ref_store(the_repository) multiple times, call it once and store in a new refs variable. Although this change probably offers some performance benefits, the main purpose is to shorten the line lengths of function calls using this variable.

---

Notes:
v5: new patch (split from the next patch as a preparatory step)

v6: no change

v7: - change commit prefix to be more in line with project standards

v8: no change

v9: - further improve readability by renaming buf, and buf2 consistently
with how patch 6 was already done

v10: no change

v11: no change

v12: no change

 builtin/remote.c | 23 ++++++++++++-----------
 1 file changed, 12 insertions(+), 11 deletions(-)
builtin/remote.c +12 -11
index 76670ddd8b..1d68c5b2ba 100644
@@ -1402,8 +1402,9 @@ static int show(int argc, const char **argv, const char *prefix)
 static int set_head(int argc, const char **argv, const char *prefix)
 {
 	int i, opt_a = 0, opt_d = 0, result = 0;
-	struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT;
+	struct strbuf b_head = STRBUF_INIT, b_remote_head = STRBUF_INIT;
 	char *head_name = NULL;
+	struct ref_store *refs = get_main_ref_store(the_repository);
 
 	struct option options[] = {
 		OPT_BOOL('a', "auto", &opt_a,
@@ -1415,7 +1416,7 @@ static int set_head(int argc, const char **argv, const char *prefix)
 	argc = parse_options(argc, argv, prefix, options,
 			     builtin_remote_sethead_usage, 0);
 	if (argc)
-		strbuf_addf(&buf, "refs/remotes/%s/HEAD", argv[0]);
+		strbuf_addf(&b_head, "refs/remotes/%s/HEAD", argv[0]);
 
 	if (!opt_a && !opt_d && argc == 2) {
 		head_name = xstrdup(argv[1]);
@@ -1434,25 +1435,25 @@ static int set_head(int argc, const char **argv, const char *prefix)
 			head_name = xstrdup(states.heads.items[0].string);
 		free_remote_ref_states(&states);
 	} else if (opt_d && !opt_a && argc == 1) {
-		if (refs_delete_ref(get_main_ref_store(the_repository), NULL, buf.buf, NULL, REF_NO_DEREF))
-			result |= error(_("Could not delete %s"), buf.buf);
+		if (refs_delete_ref(refs, NULL, b_head.buf, NULL, REF_NO_DEREF))
+			result |= error(_("Could not delete %s"), b_head.buf);
 	} else
 		usage_with_options(builtin_remote_sethead_usage, options);
 
 	if (head_name) {
-		strbuf_addf(&buf2, "refs/remotes/%s/%s", argv[0], head_name);
+		strbuf_addf(&b_remote_head, "refs/remotes/%s/%s", argv[0], head_name);
 		/* make sure it's valid */
-		if (!refs_ref_exists(get_main_ref_store(the_repository), buf2.buf))
-			result |= error(_("Not a valid ref: %s"), buf2.buf);
-		else if (refs_update_symref(get_main_ref_store(the_repository), buf.buf, buf2.buf, "remote set-head"))
-			result |= error(_("Could not setup %s"), buf.buf);
+		if (!refs_ref_exists(refs, b_remote_head.buf))
+			result |= error(_("Not a valid ref: %s"), b_remote_head.buf);
+		else if (refs_update_symref(refs, b_head.buf, b_remote_head.buf, "remote set-head"))
+			result |= error(_("Could not setup %s"), b_head.buf);
 		else if (opt_a)
 			printf("%s/HEAD set to %s\n", argv[0], head_name);
 		free(head_name);
 	}
 
-	strbuf_release(&buf);
-	strbuf_release(&buf2);
+	strbuf_release(&b_head);
+	strbuf_release(&b_remote_head);
 	return result;
 }
 
Signature · 1 line
2.47.0.94.g1247fb88fd.dirty
#124 lore raw

Re: [PATCH v12 3/8] remote set-head: refactor for readability

Quoted 6 lines · Bence Ferdinandy

Rename buf and buf2 to something more explanatory.

Instead of calling get_main_ref_store(the_repository) multiple times, call it once and store in a new refs variable. Although this change probably offers some performance benefits, the main purpose is to shorten the line lengths of function calls using this variable.

Also two strbufs are renamed, which is in line with the objective of the other change---to make the resulting code easier to follow.

Looking good.

Quoted 2 lines

v9: - further improve readability by renaming buf, and buf2 consistently
with how patch 6 was already done

#125 lore raw

PATCH v12 4/8 remote set-head: better output for --auto

In reply to Bence Ferdinandy

Currently, set-head --auto will print a message saying "remote/HEAD set to branch", which implies something was changed.

Change the output of --auto, so the output actually reflects what was
done: a) set a previously unset HEAD, b) change HEAD because remote changed or c) no updates. As a fourth output, if HEAD is changed from a previous value that was not a remote branch, explicitly call attention to this fact.

---

Notes:
v1-v2:

was RFC in https://lore.kernel.org/git/[email protected]/

v3:

This patch was originally sent along when I thought set-head was going to be invoked by fetch, but the discussion on the RFC concluded that it should be not. This opened the possibility to make it more explicit.

Note: although I feel both things the patch does are really just cosmetic, an argument could be made for breaking it into two, one for the no-op part and one for the --auto print update.

Was sent in:
https://lore.kernel.org/git/[email protected]/

v4:
- changes are now handled atomically via the ref update transaction
- outputs have changed along the lines of Junio's suggestion
- minor refactor to set_head for improved legibility

v5: - the minor refactor has been split out into its own patch

v6: - fixed uninitialized prev_head
- fixed case of unusual previous target
- fixed a test that would have been actually broken at this patch (the output was only correct with the later update to fetch)
- added 4 tests for the 4 output cases

v7: - change commit prefix to be more in line with project standards
- fixed tests to also work with the reftable backend
- renamed report function, fixed style issue with checking buf len
- fixed not releasing an strbuf

v8: no change

v9: - mark output strings in report_set_head_auto as translatable
- rename buf_prev to b_local_head for consistency
- use ${SQ} in tests instead of '\''

v10: no change

v11: no change

v12: no change

 builtin/remote.c  | 33 +++++++++++++++++++++++++++---
 t/t5505-remote.sh | 51 ++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 80 insertions(+), 4 deletions(-)
builtin/remote.c +30 -3
index 1d68c5b2ba..108f1271d3 100644
@@ -1399,10 +1399,35 @@ static int show(int argc, const char **argv, const char *prefix)
 	return result;
 }
 
+static void report_set_head_auto(const char *remote, const char *head_name,
+			struct strbuf *b_local_head) {
+	struct strbuf buf_prefix = STRBUF_INIT;
+	const char *prev_head = NULL;
+
+	strbuf_addf(&buf_prefix, "refs/remotes/%s/", remote);
+	skip_prefix(b_local_head->buf, buf_prefix.buf, &prev_head);
+
+	if (prev_head && !strcmp(prev_head, head_name))
+		printf(_("'%s/HEAD' is unchanged and points to '%s'\n"),
+			remote, head_name);
+	else if (prev_head)
+		printf(_("'%s/HEAD' has changed from '%s' and now points to '%s'\n"),
+			remote, prev_head, head_name);
+	else if (!b_local_head->len)
+		printf(_("'%s/HEAD' is now created and points to '%s'\n"),
+			remote, head_name);
+	else
+		printf(_("'%s/HEAD' used to point to '%s' "
+			"(which is not a remote branch), but now points to '%s'\n"),
+			remote, b_local_head->buf, head_name);
+	strbuf_release(&buf_prefix);
+}
+
 static int set_head(int argc, const char **argv, const char *prefix)
 {
 	int i, opt_a = 0, opt_d = 0, result = 0;
-	struct strbuf b_head = STRBUF_INIT, b_remote_head = STRBUF_INIT;
+	struct strbuf b_head = STRBUF_INIT, b_remote_head = STRBUF_INIT,
+		b_local_head = STRBUF_INIT;
 	char *head_name = NULL;
 	struct ref_store *refs = get_main_ref_store(the_repository);
 
@@ -1445,15 +1470,17 @@ static int set_head(int argc, const char **argv, const char *prefix)
 		/* make sure it's valid */
 		if (!refs_ref_exists(refs, b_remote_head.buf))
 			result |= error(_("Not a valid ref: %s"), b_remote_head.buf);
-		else if (refs_update_symref(refs, b_head.buf, b_remote_head.buf, "remote set-head"))
+		else if (refs_update_symref_extended(refs, b_head.buf, b_remote_head.buf,
+					"remote set-head", &b_local_head))
 			result |= error(_("Could not setup %s"), b_head.buf);
 		else if (opt_a)
-			printf("%s/HEAD set to %s\n", argv[0], head_name);
+			report_set_head_auto(argv[0], head_name, &b_local_head);
 		free(head_name);
 	}
 
 	strbuf_release(&b_head);
 	strbuf_release(&b_remote_head);
+	strbuf_release(&b_local_head);
 	return result;
 }
 
t/t5505-remote.sh +50 -1

68 lines not on this page · raw message

Signature · 1 line
2.47.0.94.g1247fb88fd.dirty
#126 lore raw

Re: [PATCH v12 4/8] remote set-head: better output for --auto

Quoted 8 lines · Bence Ferdinandy

Currently, set-head --auto will print a message saying "remote/HEAD set to branch", which implies something was changed.

Change the output of --auto, so the output actually reflects what was
done: a) set a previously unset HEAD, b) change HEAD because remote changed or c) no updates. As a fourth output, if HEAD is changed from a previous value that was not a remote branch, explicitly call attention to this fact.

OK. That's sensible.

There is a slight variant of the fourth case. HEAD may have been a symbolic ref that pointed at an unexpected place (which you addressed), or HEAD may have been a non-symbolic ref (which the new code would mistakenly say "HEAD is now created", if I am reading the patch correctly).

t/t5505-remote.sh test_expect_success 'set-head --auto' ' · line 438

Quoted 12 lines
t/t5505-remote.sh +4 -0
index 9b50276646..0ea86d51a4 100755
@@ -432,12 +432,51 @@ test_expect_success 'set-head --auto' '
 	)
 '
 
+test_expect_success 'set-head --auto detects creation' '
+	(
+		cd test &&
+		git symbolic-ref -d refs/remotes/origin/HEAD &&

Are we sure refs/remotes/origin/HEAD exists at this point in the test, regardless of which earlier tests were skipped or failed? If not, perhaps

		git update-ref --no-deref -d refs/remotes/origin/HEAD &&

is a better alternative.

t/t5505-remote.sh test_expect_success 'set-head --auto' ' · line 443

Quoted 5 lines
t/t5505-remote.sh +5 -0
+		git remote set-head --auto origin >output &&
+		echo "${SQ}origin/HEAD${SQ} is now created and points to ${SQ}main${SQ}" >expect &&
+		test_cmp expect output
+	)
+'

Here, we could insert another one:

test_expect_success 'set-head --auto to update a non symbolic ref' '

	(
		cd test &&
		git update-ref --no-deref -d refs/remotes/origin/HEAD &&
		git update-ref refs/remotes/origin/HEAD HEAD &&
		git remote set-head --auto origin >output &&

I'd imagine "output" should at least say that we are setting up a symref origin/HEAD to point at some ref the --auto option figured out, and if we wanted to report its previous state, it was a non symbolic ref that pointed at some commit. In any case,

		echo "${SQ}origin/HEAD${SQ} is now created and points to ${SQ}main${SQ}" >expect &&

is not what we want to see here, I suspect.

Can we detect the case where we overwrite a non symref with a symref without going back to step 2/8 and doing a major surgery?

		test_cmp expect output
	)

'

#127 lore raw

Re: [PATCH v12 4/8] remote set-head: better output for --auto

Quoted 11 lines · Junio C Hamano
Quoted 8 lines · Bence Ferdinandy

Currently, set-head --auto will print a message saying "remote/HEAD set to branch", which implies something was changed.

Change the output of --auto, so the output actually reflects what was
done: a) set a previously unset HEAD, b) change HEAD because remote changed or c) no updates. As a fourth output, if HEAD is changed from a previous value that was not a remote branch, explicitly call attention to this fact.

Quoted 7 lines

OK. That's sensible.

There is a slight variant of the fourth case. HEAD may have been a symbolic ref that pointed at an unexpected place (which you addressed), or HEAD may have been a non-symbolic ref (which the new code would mistakenly say "HEAD is now created", if I am reading the patch correctly).

Good point, and yes, that is what happens. (Although I'm not quite sure how valid that state is where a remote's HEAD is not a branch).

Quoted 18 lines

t/t5505-remote.sh test_expect_success 'set-head --auto' ' · line 438

Quoted 13 lines
t/t5505-remote.sh +4 -0
index 9b50276646..0ea86d51a4 100755
@@ -432,12 +432,51 @@ test_expect_success 'set-head --auto' '
 	)
 '
 
+test_expect_success 'set-head --auto detects creation' '
+	(
+		cd test &&
+		git symbolic-ref -d refs/remotes/origin/HEAD &&

Are we sure refs/remotes/origin/HEAD exists at this point in the test, regardless of which earlier tests were skipped or failed? If not, perhaps

Quoted 3 lines
		git update-ref --no-deref -d refs/remotes/origin/HEAD &&

is a better alternative.

Ack.

Quoted 23 lines

t/t5505-remote.sh test_expect_success 'set-head --auto' ' · line 443

Quoted 6 lines
t/t5505-remote.sh +5 -0
+		git remote set-head --auto origin >output &&
+		echo "${SQ}origin/HEAD${SQ} is now created and points to ${SQ}main${SQ}" >expect &&
+		test_cmp expect output
+	)
+'

Here, we could insert another one:

test_expect_success 'set-head --auto to update a non symbolic ref' '

	(
		cd test &&
		git update-ref --no-deref -d refs/remotes/origin/HEAD &&
		git update-ref refs/remotes/origin/HEAD HEAD &&
		git remote set-head --auto origin >output &&

I'd imagine "output" should at least say that we are setting up a symref origin/HEAD to point at some ref the --auto option figured out, and if we wanted to report its previous state, it was a non symbolic ref that pointed at some commit. In any case,

		echo "${SQ}origin/HEAD${SQ} is now created and points to ${SQ}main${SQ}" >expect &&
Quoted 8 lines

is not what we want to see here, I suspect.

Can we detect the case where we overwrite a non symref with a symref without going back to step 2/8 and doing a major surgery?

		test_cmp expect output
	)

'

I agree, adding this makes sense. And this also takes us back to the question of what we should do in 2/8 when refs_read_symbolic_ref exits with 1. I now tested the behaviour and if origin/HEAD is gibberish, git already dies before with

error: cannot lock ref 'refs/remotes/origin/HEAD': unable to resolve reference 'refs/remotes/origin/HEAD': reference broken

so refs_read_symbolic_ref -> 1 only happens if there's a valid non-symbolic ref in origin/HEAD. So maybe if we put "Not a symbolic reference." in the referent (which should be an invalid symref), the caller could check for that and then should be able to distinguish this special case?

Thanks,
Bence

Signature · 1 line
bence.ferdinandy.com
#128 lore raw

Re: [PATCH v12 4/8] remote set-head: better output for --auto

Quoted 85 lines · Bence Ferdinandy
Quoted 11 lines · Junio C Hamano
Quoted 8 lines · Bence Ferdinandy

Currently, set-head --auto will print a message saying "remote/HEAD set to branch", which implies something was changed.

Change the output of --auto, so the output actually reflects what was
done: a) set a previously unset HEAD, b) change HEAD because remote changed or c) no updates. As a fourth output, if HEAD is changed from a previous value that was not a remote branch, explicitly call attention to this fact.

Quoted 7 lines

OK. That's sensible.

There is a slight variant of the fourth case. HEAD may have been a symbolic ref that pointed at an unexpected place (which you addressed), or HEAD may have been a non-symbolic ref (which the new code would mistakenly say "HEAD is now created", if I am reading the patch correctly).

Good point, and yes, that is what happens. (Although I'm not quite sure how valid that state is where a remote's HEAD is not a branch).

Quoted 18 lines

t/t5505-remote.sh test_expect_success 'set-head --auto' ' · line 438

Quoted 13 lines
t/t5505-remote.sh +4 -0
index 9b50276646..0ea86d51a4 100755
@@ -432,12 +432,51 @@ test_expect_success 'set-head --auto' '
 	)
 '
 
+test_expect_success 'set-head --auto detects creation' '
+	(
+		cd test &&
+		git symbolic-ref -d refs/remotes/origin/HEAD &&

Are we sure refs/remotes/origin/HEAD exists at this point in the test, regardless of which earlier tests were skipped or failed? If not, perhaps

Quoted 3 lines
		git update-ref --no-deref -d refs/remotes/origin/HEAD &&

is a better alternative.

Ack.

Quoted 23 lines

t/t5505-remote.sh test_expect_success 'set-head --auto' ' · line 443

Quoted 6 lines
t/t5505-remote.sh +5 -0
+		git remote set-head --auto origin >output &&
+		echo "${SQ}origin/HEAD${SQ} is now created and points to ${SQ}main${SQ}" >expect &&
+		test_cmp expect output
+	)
+'

Here, we could insert another one:

test_expect_success 'set-head --auto to update a non symbolic ref' '

	(
		cd test &&
		git update-ref --no-deref -d refs/remotes/origin/HEAD &&
		git update-ref refs/remotes/origin/HEAD HEAD &&
		git remote set-head --auto origin >output &&

I'd imagine "output" should at least say that we are setting up a symref origin/HEAD to point at some ref the --auto option figured out, and if we wanted to report its previous state, it was a non symbolic ref that pointed at some commit. In any case,

		echo "${SQ}origin/HEAD${SQ} is now created and points to ${SQ}main${SQ}" >expect &&
Quoted 8 lines

is not what we want to see here, I suspect.

Can we detect the case where we overwrite a non symref with a symref without going back to step 2/8 and doing a major surgery?

		test_cmp expect output
	)

'

I agree, adding this makes sense. And this also takes us back to the question of what we should do in 2/8 when refs_read_symbolic_ref exits with 1. I now tested the behaviour and if origin/HEAD is gibberish, git already dies before with

Quoted 6 lines

error: cannot lock ref 'refs/remotes/origin/HEAD': unable to resolve reference 'refs/remotes/origin/HEAD': reference broken

so refs_read_symbolic_ref -> 1 only happens if there's a valid non-symbolic ref in origin/HEAD. So maybe if we put "Not a symbolic reference." in the referent (which should be an invalid symref), the caller could check for that and then should be able to distinguish this special case?

On second thought, it would maybe make even more sense to get the reference hash and put that into referent. In that case the output could still be

"'%s/HEAD' has changed from '%s' and now points to '%s'\n"

but with a non-symref after from.

#129 lore raw

Re: [PATCH v12 4/8] remote set-head: better output for --auto

Quoted 11 lines · Bence Ferdinandy
Quoted 4 lines

so refs_read_symbolic_ref -> 1 only happens if there's a valid non-symbolic ref in origin/HEAD. So maybe if we put "Not a symbolic reference." in the referent (which should be an invalid symref), the caller could check for that and then should be able to distinguish this special case?

On second thought, it would maybe make even more sense to get the reference hash and put that into referent. In that case the output could still be

"'%s/HEAD' has changed from '%s' and now points to '%s'\n"

but with a non-symref after from.

The output may look like the above, but people would certainly wish to clarify and/or translate "from '%s'" part. And to allow that, we may need to signal a bit more explicitly that we saw a non-symref HEAD there so that we can tell between a detached HEAD and a confusingly named branch whose name is 40-hex.

Otherwise, one who tries to update that "has changed from '%s'" message to clarify between "pointed at branch '%s'" and "pointed at commit '%s'" (or translate into a language where the distinction matters, perhaps because branch and commit have different gender there that affects conjugation for "has changed from" part, or something hand-wavy) have insufficient information to work with.

Thanks.

#130 lore raw

Re: [PATCH v12 4/8] remote set-head: better output for --auto

In reply to Bence Ferdinandy
Quoted 2 lines · Bence Ferdinandy

Good point, and yes, that is what happens. (Although I'm not quite sure how valid that state is where a remote's HEAD is not a branch).

Not often when you cloned from a publishing repository, but if you cloned from a repository for your own work with an worktree, the HEAD at such an origin repository may have been detached, so it would be sensible (I think "git clone" historically guesses over eagerly to hide the detached HEAD state of the other side, though) if the clone's remote-tracking HEAD reflects it, I would think.

Quoted 3 lines

... So maybe if we put "Not a symbolic reference." in the referent (which should be an invalid symref), the caller could check for that and then should be able to distinguish this special case?

Yuck.

Are we limited by the narrow interface that only passes "referent", or are you in the position that allows you to extend the interface to "do it right"?

#131 lore raw

Re: [PATCH v12 4/8] remote set-head: better output for --auto

Quoted 16 lines · Junio C Hamano
Quoted 2 lines · Bence Ferdinandy

Good point, and yes, that is what happens. (Although I'm not quite sure how valid that state is where a remote's HEAD is not a branch).

Not often when you cloned from a publishing repository, but if you cloned from a repository for your own work with an worktree, the HEAD at such an origin repository may have been detached, so it would be sensible (I think "git clone" historically guesses over eagerly to hide the detached HEAD state of the other side, though) if the clone's remote-tracking HEAD reflects it, I would think.

Quoted 3 lines

... So maybe if we put "Not a symbolic reference." in the referent (which should be an invalid symref), the caller could check for that and then should be able to distinguish this special case?

Quoted 5 lines

Yuck.

Are we limited by the narrow interface that only passes "referent", or are you in the position that allows you to extend the interface to "do it right"?

How about we do the following if we can't read a symref:

1) remember to return a new exit code for refs_update_symref_extended (I guess 2), but continue on with updating HEAD

2) read the non-symref ref into "referent" (I think this should not be able to fail since, otherwise locking HEAD will already fail earlier)

this way the caller can check the exit code, see that it's 2, so not entirely a success, but HEAD has been updated and can report things accordingly.

Does that sounds more reasonable?

Thanks,
Bence

#132 lore raw

PATCH v12 5/8 refs: add TRANSACTION_CREATE_EXISTS error

In reply to Bence Ferdinandy

Currently there is only one special error for transaction, for when there is a naming conflict, all other errors are dumped under a generic error. Add a new special error case for when the caller requests the reference to be updated only when it does not yet exist and the reference actually does exist.

---

Notes:
v4: new patch
v5: no change
v6: no change

v7: - change commit prefix to be more in line with project standards
- changed error checking to Karthik's suggestion

v8: no change

v9: - no change

v10: no change

v11: no change

v12: no change

 refs.h                  |  4 +++-
 refs/files-backend.c    | 24 ++++++++++++++++--------
 refs/reftable-backend.c |  6 ++++--
 3 files changed, 23 insertions(+), 11 deletions(-)
refs.h +3 -1
index 259191a485..a5bc25442b 100644
@@ -762,8 +762,10 @@ int ref_transaction_verify(struct ref_transaction *transaction,
 
 /* Naming conflict (for example, the ref names A and A/B conflict). */
 #define TRANSACTION_NAME_CONFLICT -1
+/* When only creation was requested, but the ref already exists. */
+#define TRANSACTION_CREATE_EXISTS -2
 /* All other errors. */
-#define TRANSACTION_GENERIC_ERROR -2
+#define TRANSACTION_GENERIC_ERROR -3
 
 /*
  * Perform the preparatory stages of committing `transaction`. Acquire
refs/files-backend.c +16 -8
index 0824c0b8a9..e743ec44b5 100644
@@ -2502,14 +2502,18 @@ static int split_symref_update(struct ref_update *update,
 static int check_old_oid(struct ref_update *update, struct object_id *oid,
 			 struct strbuf *err)
 {
+	int ret = TRANSACTION_GENERIC_ERROR;
+
 	if (!(update->flags & REF_HAVE_OLD) ||
 		   oideq(oid, &update->old_oid))
 		return 0;
 
-	if (is_null_oid(&update->old_oid))
+	if (is_null_oid(&update->old_oid)) {
 		strbuf_addf(err, "cannot lock ref '%s': "
 			    "reference already exists",
 			    ref_update_original_update_refname(update));
+		ret = TRANSACTION_CREATE_EXISTS;
+	}
 	else if (is_null_oid(oid))
 		strbuf_addf(err, "cannot lock ref '%s': "
 			    "reference is missing but expected %s",
@@ -2522,7 +2526,7 @@ static int check_old_oid(struct ref_update *update, struct object_id *oid,
 			    oid_to_hex(oid),
 			    oid_to_hex(&update->old_oid));
 
-	return -1;
+	return ret;
 }
 
 /*
@@ -2602,9 +2606,11 @@ static int lock_ref_for_update(struct files_ref_store *refs,
 					ret = TRANSACTION_GENERIC_ERROR;
 					goto out;
 				}
-			} else if  (check_old_oid(update, &lock->old_oid, err)) {
-				ret = TRANSACTION_GENERIC_ERROR;
-				goto out;
+			} else {
+				ret = check_old_oid(update, &lock->old_oid, err);
+				if  (ret) {
+					goto out;
+				}
 			}
 		} else {
 			/*
@@ -2635,9 +2641,11 @@ static int lock_ref_for_update(struct files_ref_store *refs,
 				    update->old_target);
 			ret = TRANSACTION_GENERIC_ERROR;
 			goto out;
-		} else if  (check_old_oid(update, &lock->old_oid, err)) {
-			ret = TRANSACTION_GENERIC_ERROR;
-			goto out;
+		} else {
+			ret = check_old_oid(update, &lock->old_oid, err);
+			if  (ret) {
+				goto out;
+			}
 		}
 
 		/*
refs/reftable-backend.c +4 -2
index 3c6107c7ce..b3b5ce77dd 100644
@@ -1206,10 +1206,13 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store,
 				goto done;
 			}
 		} else if ((u->flags & REF_HAVE_OLD) && !oideq(&current_oid, &u->old_oid)) {
-			if (is_null_oid(&u->old_oid))
+			ret = TRANSACTION_NAME_CONFLICT;
+			if (is_null_oid(&u->old_oid)) {
 				strbuf_addf(err, _("cannot lock ref '%s': "
 						   "reference already exists"),
 					    ref_update_original_update_refname(u));
+				ret = TRANSACTION_CREATE_EXISTS;
+			}
 			else if (is_null_oid(&current_oid))
 				strbuf_addf(err, _("cannot lock ref '%s': "
 						   "reference is missing but expected %s"),
@@ -1221,7 +1224,6 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store,
 					    ref_update_original_update_refname(u),
 					    oid_to_hex(&current_oid),
 					    oid_to_hex(&u->old_oid));
-			ret = -1;
 			goto done;
 		}
 
Signature · 1 line
2.47.0.94.g1247fb88fd.dirty
#133 lore raw

PATCH v12 6/8 refs: add create_only option to refs_update_symref_extended

In reply to Bence Ferdinandy

Allow the caller to specify that it only wants to update the symref if it does not already exist. Silently ignore the error from the transaction API if the symref already exists.

---

Notes:
v4: new patch
v5: no change

v6: - switched from bool to int for create_only
- refactored logic in refs_update_symref with goto as layed out by Junio

v7: - change commit prefix to be more in line with project standards
- refactored code to accommodate changes in the first patch, but otherwise no change

v8: no change

v9: - no change (except for following through the
refs_update_symref_extended refactoring)

v10: no change

v11: no change

v12: no change

 builtin/remote.c |  2 +-
 refs.c           | 32 +++++++++++++++++++++++---------
 refs.h           |  2 +-
 3 files changed, 25 insertions(+), 11 deletions(-)
builtin/remote.c +1 -1
index 108f1271d3..b1eba75a2b 100644
@@ -1471,7 +1471,7 @@ static int set_head(int argc, const char **argv, const char *prefix)
 		if (!refs_ref_exists(refs, b_remote_head.buf))
 			result |= error(_("Not a valid ref: %s"), b_remote_head.buf);
 		else if (refs_update_symref_extended(refs, b_head.buf, b_remote_head.buf,
-					"remote set-head", &b_local_head))
+					"remote set-head", &b_local_head, 0))
 			result |= error(_("Could not setup %s"), b_head.buf);
 		else if (opt_a)
 			report_set_head_auto(argv[0], head_name, &b_local_head);
refs.c +23 -9
index 24a4172cd2..093ee11ab0 100644
@@ -2116,31 +2116,45 @@ int peel_iterated_oid(struct repository *r, const struct object_id *base, struct
 int refs_update_symref(struct ref_store *refs, const char *ref,
 		       const char *target, const char *logmsg)
 {
-	return refs_update_symref_extended(refs, ref, target, logmsg, NULL);
+	return refs_update_symref_extended(refs, ref, target, logmsg, NULL, 0);
 }
 
 int refs_update_symref_extended(struct ref_store *refs, const char *ref,
 		       const char *target, const char *logmsg,
-		       struct strbuf *referent)
+		       struct strbuf *referent, int create_only)
 {
 	struct ref_transaction *transaction;
 	struct strbuf err = STRBUF_INIT;
-	int ret = 0;
+	int ret = 0, prepret = 0;
 
 	transaction = ref_store_transaction_begin(refs, &err);
-	if (!transaction ||
-		ref_transaction_update(transaction, ref, NULL, NULL,
-				   target, NULL, REF_NO_DEREF,
-				   logmsg, &err) ||
-		ref_transaction_prepare(transaction, &err)) {
+	if (!transaction) {
+	error_return:
 		ret = error("%s", err.buf);
 		goto cleanup;
 	}
+	if (create_only) {
+		if (ref_transaction_create(transaction, ref, NULL, target,
+					   REF_NO_DEREF, logmsg, &err))
+			goto error_return;
+		prepret = ref_transaction_prepare(transaction, &err);
+		if (prepret && prepret != TRANSACTION_CREATE_EXISTS)
+			goto error_return;
+	} else {
+		if (ref_transaction_update(transaction, ref, NULL, NULL,
+					   target, NULL, REF_NO_DEREF,
+					   logmsg, &err) ||
+			ref_transaction_prepare(transaction, &err))
+			goto error_return;
+	}
+
 	if (referent)
 		refs_read_symbolic_ref(refs, ref, referent);
 
+	if (prepret == TRANSACTION_CREATE_EXISTS)
+		goto cleanup;
 	if (ref_transaction_commit(transaction, &err))
-		ret = error("%s", err.buf);
+		goto error_return;
 
 cleanup:
 	strbuf_release(&err);
refs.h +1 -1
index a5bc25442b..458582ebcf 100644
@@ -575,7 +575,7 @@ int refs_update_symref(struct ref_store *refs, const char *refname,
 
 int refs_update_symref_extended(struct ref_store *refs, const char *refname,
 		       const char *target, const char *logmsg,
-		       struct strbuf *referent);
+		       struct strbuf *referent, int create_only);
 
 enum action_on_err {
 	UPDATE_REFS_MSG_ON_ERR,
Signature · 1 line
2.47.0.94.g1247fb88fd.dirty
#134 lore raw

PATCH v12 7/8 fetch: set remote/HEAD if it does not exist

In reply to Bence Ferdinandy

If the user has remote/HEAD set already and it looks like it has changed on the server, then print a message, otherwise set it if we can. Silently pass if the user already has the same remote/HEAD set as reported by the server or if we encounter any errors along the way.

---

Notes:
v3: - does not rely on remote set-head anymore so it only authenticates
once
- uses the new REF_CREATE_ONLY to atomically check if the ref exists and only write it if it doesn't
- in all other cases the maximum it does is print a warning

v4: - instead of the discarded REF_CREATE_ONLY, it uses the existing,
but updated transaction api to request a silent create only
- it now uses the atomic before_target to determine reporting
- refactored for legibility

v5: - instead of printing a not too useful message, it now fails
silently, this in line with the objective to only set up remote/HEAD automatically if the right thing is trivial, for everything else there is remote set-head
- fixed all failing tests
- added two new tests, one for checking if remote/HEAD is set to the correct one, and one to test that we do not override remote/HEAD if it has changed on the server from what we have locally

v6: - fixed style issues and unintended extra empty line
- updated function call with bool to int from previous patch's change
- removed calls to error(...) inherited from builtin/remote.c so we actually fail silently
- set the test for remote set-head --auto to the correct value here, which was previously erronously set in the remote set-head patch

v7: - no change

v8: - changed logmsg in call to refs_update_symref from "remote
set-head" to "fetch"

v9: - follow through with refs_update_symref_extended
- fix test errors uncovered by the new patch

v10: no change

v11: fixed some memory leaks

v12: no change

 builtin/fetch.c                  |  86 ++++++++++++
 t/t4207-log-decoration-colors.sh |   3 +-
 t/t5505-remote.sh                |  21 ++-
 t/t5510-fetch.sh                 | 229 ++++++++++++++++---------------
 t/t5512-ls-remote.sh             |   2 +
 t/t5514-fetch-multiple.sh        |  17 ++-
 t/t5516-fetch-push.sh            |   3 +-
 t/t5527-fetch-odd-refs.sh        |   3 +-
 t/t7900-maintenance.sh           |   3 +-
 t/t9210-scalar.sh                |   5 +-
 t/t9211-scalar-clone.sh          |   6 +-
 t/t9902-completion.sh            |  65 +++++++++
 12 files changed, 319 insertions(+), 124 deletions(-)
builtin/fetch.c +86 -0
index d9027e4dc9..cadfd2407a 100644
@@ -1578,6 +1578,84 @@ static int backfill_tags(struct display_state *display_state,
 	return retcode;
 }
 
+static void report_set_head(const char *remote, const char *head_name,
+			struct strbuf *buf_prev) {
+	struct strbuf buf_prefix = STRBUF_INIT;
+	const char *prev_head = NULL;
+
+	strbuf_addf(&buf_prefix, "refs/remotes/%s/", remote);
+	skip_prefix(buf_prev->buf, buf_prefix.buf, &prev_head);
+
+	if (prev_head && strcmp(prev_head, head_name)) {
+		printf("'HEAD' at '%s' has changed from '%s' to '%s'\n",
+			remote, prev_head, head_name);
+		printf("Run 'git remote set-head %s %s' to follow the change.\n",
+			remote, head_name);
+	}
+	strbuf_release(&buf_prefix);
+}
+
+static const char *strip_refshead(const char *name){
+	skip_prefix(name, "refs/heads/", &name);
+	return name;
+}
+
+static int set_head(const struct ref *remote_refs)
+{
+	int result = 0;
+	struct strbuf b_head = STRBUF_INIT, b_remote_head = STRBUF_INIT,
+		b_local_head = STRBUF_INIT;
+	const char *remote = gtransport->remote->name;
+	char *head_name = NULL;
+	struct ref *ref, *matches;
+	struct ref *fetch_map = NULL, **fetch_map_tail = &fetch_map;
+	struct refspec_item refspec = {
+		.force = 0,
+		.pattern = 1,
+		.src = (char *) "refs/heads/*",
+		.dst = (char *) "refs/heads/*",
+	};
+	struct string_list heads = STRING_LIST_INIT_DUP;
+	struct ref_store *refs = get_main_ref_store(the_repository);
+
+	get_fetch_map(remote_refs, &refspec, &fetch_map_tail, 0);
+	matches = guess_remote_head(find_ref_by_name(remote_refs, "HEAD"),
+				    fetch_map, 1);
+	for (ref = matches; ref; ref = ref->next) {
+		string_list_append(&heads, strip_refshead(ref->name));
+	}
+
+
+	if (!heads.nr)
+		result = 1;
+	else if (heads.nr > 1)
+		result = 1;
+	else
+		head_name = xstrdup(heads.items[0].string);
+	if (head_name) {
+		strbuf_addf(&b_head, "refs/remotes/%s/HEAD", remote);
+		strbuf_addf(&b_remote_head, "refs/remotes/%s/%s", remote, head_name);
+		/* make sure it's valid */
+		if (!refs_ref_exists(refs, b_remote_head.buf))
+			result = 1;
+		else if (refs_update_symref_extended(refs, b_head.buf, b_remote_head.buf,
+					"fetch", &b_local_head, 1))
+			result = 1;
+		else
+			report_set_head(remote, head_name, &b_local_head);
+
+		free(head_name);
+	}
+
+	free_refs(fetch_map);
+	free_refs(matches);
+	string_list_clear(&heads, 0);
+	strbuf_release(&b_head);
+	strbuf_release(&b_local_head);
+	strbuf_release(&b_remote_head);
+	return result;
+}
+
 static int do_fetch(struct transport *transport,
 		    struct refspec *rs,
 		    const struct fetch_config *config)
@@ -1647,6 +1725,8 @@ static int do_fetch(struct transport *transport,
 				    "refs/tags/");
 	}
 
+	strvec_push(&transport_ls_refs_options.ref_prefixes, "HEAD");
+
 	if (must_list_refs) {
 		trace2_region_enter("fetch", "remote_refs", the_repository);
 		remote_refs = transport_get_remote_refs(transport,
@@ -1791,6 +1871,12 @@ static int do_fetch(struct transport *transport,
 				  "you need to specify exactly one branch with the --set-upstream option"));
 		}
 	}
+	if (set_head(remote_refs))
+		;
+		/*
+		 * Way too many cases where this can go wrong
+		 * so let's just fail silently for now.
+		 */
 
 cleanup:
 	if (retcode) {
t/t4207-log-decoration-colors.sh +2 -1
index 73ea9e5155..d55d22cb2f 100755
@@ -59,7 +59,8 @@ ${c_reset}${c_tag}tag: ${c_reset}${c_tag}v1.0${c_reset}${c_commit}, \
 ${c_reset}${c_tag}tag: ${c_reset}${c_tag}B${c_reset}${c_commit})${c_reset} B
 ${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_reset}\
 ${c_tag}tag: ${c_reset}${c_tag}A1${c_reset}${c_commit}, \
-${c_reset}${c_remoteBranch}other/main${c_reset}${c_commit})${c_reset} A1
+${c_reset}${c_remoteBranch}other/main${c_reset}${c_commit}, \
+${c_reset}${c_remoteBranch}other/HEAD${c_reset}${c_commit})${c_reset} A1
 	${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_reset}\
 ${c_stash}refs/stash${c_reset}${c_commit})${c_reset} On main: Changes to A.t
 	${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_reset}\
t/t5505-remote.sh +15 -6
index 0ea86d51a4..4990d00209 100755
@@ -74,7 +74,7 @@ test_expect_success 'add another remote' '
 		cd test &&
 		git remote add -f second ../two &&
 		tokens_match "origin second" "$(git remote)" &&
-		check_tracking_branch second main side another &&
+		check_tracking_branch second main side another HEAD &&
 		git for-each-ref "--format=%(refname)" refs/remotes |
 		sed -e "/^refs\/remotes\/origin\//d" \
 		    -e "/^refs\/remotes\/second\//d" >actual &&
@@ -476,7 +476,7 @@ test_expect_success 'set-head --auto has no problem w/multiple HEADs' '
 		cd test &&
 		git fetch two "refs/heads/*:refs/remotes/two/*" &&
 		git remote set-head --auto two >output 2>&1 &&
-		echo "${SQ}two/HEAD${SQ} is now created and points to ${SQ}main${SQ}" >expect &&
+		echo "${SQ}two/HEAD${SQ} is unchanged and points to ${SQ}main${SQ}" >expect &&
 		test_cmp expect output
 	)
 '
@@ -764,8 +764,10 @@ test_expect_success 'reject --no-no-tags' '
 '
 
 cat >one/expect <<\EOF
+  apis/HEAD -> apis/main
   apis/main
   apis/side
+  drosophila/HEAD -> drosophila/main
   drosophila/another
   drosophila/main
   drosophila/side
@@ -783,11 +785,14 @@ test_expect_success 'update' '
 '
 
 cat >one/expect <<\EOF
+  drosophila/HEAD -> drosophila/main
   drosophila/another
   drosophila/main
   drosophila/side
+  manduca/HEAD -> manduca/main
   manduca/main
   manduca/side
+  megaloprepus/HEAD -> megaloprepus/main
   megaloprepus/main
   megaloprepus/side
 EOF
@@ -795,7 +800,7 @@ EOF
 test_expect_success 'update with arguments' '
 	(
 		cd one &&
-		for b in $(git branch -r)
+		for b in $(git branch -r | grep -v HEAD)
 		do
 		git branch -r -d $b || exit 1
 		done &&
@@ -827,10 +832,13 @@ test_expect_success 'update --prune' '
 '
 
 cat >one/expect <<-\EOF
+  apis/HEAD -> apis/main
   apis/main
   apis/side
+  manduca/HEAD -> manduca/main
   manduca/main
   manduca/side
+  megaloprepus/HEAD -> megaloprepus/main
   megaloprepus/main
   megaloprepus/side
 EOF
@@ -838,7 +846,7 @@ EOF
 test_expect_success 'update default' '
 	(
 		cd one &&
-		for b in $(git branch -r)
+		for b in $(git branch -r | grep -v HEAD)
 		do
 		git branch -r -d $b || exit 1
 		done &&
@@ -850,6 +858,7 @@ test_expect_success 'update default' '
 '
 
 cat >one/expect <<\EOF
+  drosophila/HEAD -> drosophila/main
   drosophila/another
   drosophila/main
   drosophila/side
@@ -858,7 +867,7 @@ EOF
 test_expect_success 'update default (overridden, with funny whitespace)' '
 	(
 		cd one &&
-		for b in $(git branch -r)
+		for b in $(git branch -r | grep -v HEAD)
 		do
 		git branch -r -d $b || exit 1
 		done &&
@@ -872,7 +881,7 @@ test_expect_success 'update default (overridden, with funny whitespace)' '
 test_expect_success 'update (with remotes.default defined)' '
 	(
 		cd one &&
-		for b in $(git branch -r)
+		for b in $(git branch -r | grep -v HEAD)
 		do
 		git branch -r -d $b || exit 1
 		done &&
t/t5510-fetch.sh +122 -107

356 lines not on this page · raw message

t/t5512-ls-remote.sh +2 -0
index 64b3491e4e..1b3865e154 100755
@@ -293,6 +293,8 @@ test_expect_success 'ls-remote with filtered symref (refname)' '
 	cat >expect <<-EOF &&
 	ref: refs/heads/main	HEAD
 	$rev	HEAD
+	ref: refs/remotes/origin/main	refs/remotes/origin/HEAD
+	$rev	refs/remotes/origin/HEAD
 	EOF
 	git ls-remote --symref . HEAD >actual &&
 	test_cmp expect actual
t/t5514-fetch-multiple.sh +15 -2
index 579872c258..e3482b27b2 100755
@@ -45,14 +45,17 @@ test_expect_success setup '
 '
 
 cat > test/expect << EOF
+  one/HEAD -> one/main
   one/main
   one/side
   origin/HEAD -> origin/main
   origin/main
   origin/side
+  three/HEAD -> three/main
   three/another
   three/main
   three/side
+  two/HEAD -> two/main
   two/another
   two/main
   two/side
@@ -97,6 +100,7 @@ cat > expect << EOF
   origin/HEAD -> origin/main
   origin/main
   origin/side
+  three/HEAD -> three/main
   three/another
   three/main
   three/side
@@ -112,8 +116,10 @@ test_expect_success 'git fetch --multiple (but only one remote)' '
 '
 
 cat > expect << EOF
+  one/HEAD -> one/main
   one/main
   one/side
+  two/HEAD -> two/main
   two/another
   two/main
   two/side
@@ -141,7 +147,7 @@ test_expect_success 'git fetch --multiple (bad remote names)' '
 
 test_expect_success 'git fetch --all (skipFetchAll)' '
 	(cd test4 &&
-	 for b in $(git branch -r)
+	 for b in $(git branch -r | grep -v HEAD)
 	 do
 		git branch -r -d $b || exit 1
 	 done &&
@@ -153,11 +159,14 @@ test_expect_success 'git fetch --all (skipFetchAll)' '
 '
 
 cat > expect << EOF
+  one/HEAD -> one/main
   one/main
   one/side
+  three/HEAD -> three/main
   three/another
   three/main
   three/side
+  two/HEAD -> two/main
   two/another
   two/main
   two/side
@@ -165,7 +174,7 @@ EOF
 
 test_expect_success 'git fetch --multiple (ignoring skipFetchAll)' '
 	(cd test4 &&
-	 for b in $(git branch -r)
+	 for b in $(git branch -r | grep -v HEAD)
 	 do
 		git branch -r -d $b || exit 1
 	 done &&
@@ -221,14 +230,17 @@ test_expect_success 'git fetch --multiple --jobs=0 picks a default' '
 
 create_fetch_all_expect () {
 	cat >expect <<-\EOF
+	  one/HEAD -> one/main
 	  one/main
 	  one/side
 	  origin/HEAD -> origin/main
 	  origin/main
 	  origin/side
+	  three/HEAD -> three/main
 	  three/another
 	  three/main
 	  three/side
+	  two/HEAD -> two/main
 	  two/another
 	  two/main
 	  two/side
@@ -265,6 +277,7 @@ test_expect_success 'git fetch (fetch all remotes with fetch.all = true)' '
 
 create_fetch_one_expect () {
 	cat >expect <<-\EOF
+	  one/HEAD -> one/main
 	  one/main
 	  one/side
 	  origin/HEAD -> origin/main
t/t5516-fetch-push.sh +2 -1
index 331778bd42..5a051aa0c7 100755
@@ -1395,7 +1395,8 @@ test_expect_success 'fetch follows tags by default' '
 		git tag -m "annotated" tag &&
 		git for-each-ref >tmp1 &&
 		sed -n "p; s|refs/heads/main$|refs/remotes/origin/main|p" tmp1 |
-		sort -k 3 >../expect
+		sed -n "p; s|refs/heads/main$|refs/remotes/origin/HEAD|p"  |
+		sort -k 4 >../expect
 	) &&
 	test_when_finished "rm -rf dst" &&
 	git init dst &&
t/t5527-fetch-odd-refs.sh +2 -1
index 98ece27c6a..d3996af6ee 100755
@@ -52,7 +52,8 @@ test_expect_success LONG_REF 'fetch handles extremely long refname' '
 	long
 	main
 	EOF
-	git for-each-ref --format="%(subject)" refs/remotes/long >actual &&
+	git for-each-ref --format="%(subject)" refs/remotes/long \
+		--exclude=refs/remotes/long/HEAD >actual &&
 	test_cmp expect actual
 '
 
t/t7900-maintenance.sh +2 -1
index a66d0e089d..88331d1ceb 100755
@@ -329,7 +329,8 @@ test_expect_success 'incremental-repack task' '
 
 	# Delete refs that have not been repacked in these packs.
 	git for-each-ref --format="delete %(refname)" \
-		refs/prefetch refs/tags refs/remotes >refs &&
+		refs/prefetch refs/tags refs/remotes \
+		--exclude=refs/remotes/*/HEAD >refs &&
 	git update-ref --stdin <refs &&
 
 	# Replace the object directory with this pack layout.
t/t9210-scalar.sh +3 -2
index a30b2c9f70..2237844550 100755
@@ -151,7 +151,8 @@ test_expect_success 'scalar clone' '
 			"$(pwd)" &&
 
 		git for-each-ref --format="%(refname)" refs/remotes/origin/ >actual &&
-		echo "refs/remotes/origin/parallel" >expect &&
+		echo "refs/remotes/origin/HEAD" >>expect &&
+		echo "refs/remotes/origin/parallel" >>expect &&
 		test_cmp expect actual &&
 
 		test_path_is_missing 1/2 &&
@@ -220,7 +221,7 @@ test_expect_success 'scalar reconfigure --all with includeIf.onbranch' '
 	done
 '
 
- test_expect_success 'scalar reconfigure --all with detached HEADs' '
+test_expect_success 'scalar reconfigure --all with detached HEADs' '
 	repos="two three four" &&
 	for num in $repos
 	do
t/t9211-scalar-clone.sh +3 -3
index c16ea67c1d..d9cb6b9a3e 100755
@@ -32,7 +32,7 @@ test_expect_success 'set up repository to clone' '
 	)
 '
 
-cleanup_clone () {
+cleanup_clone() {
 	rm -rf "$1"
 }
 
@@ -128,7 +128,7 @@ test_expect_success '--single-branch clones HEAD only' '
 	(
 		cd $enlistment/src &&
 		git for-each-ref refs/remotes/origin >out &&
-		test_line_count = 1 out &&
+		test_line_count = 2 out &&
 		grep "refs/remotes/origin/base" out
 	) &&
 
@@ -142,7 +142,7 @@ test_expect_success '--no-single-branch clones all branches' '
 	(
 		cd $enlistment/src &&
 		git for-each-ref refs/remotes/origin >out &&
-		test_line_count = 2 out &&
+		test_line_count = 3 out &&
 		grep "refs/remotes/origin/base" out &&
 		grep "refs/remotes/origin/parallel" out
 	) &&
t/t9902-completion.sh +65 -0

443 lines not on this page · raw message

Signature · 1 line
2.47.0.94.g1247fb88fd.dirty
#135 lore raw

Re: [PATCH v12 7/8] fetch: set remote/HEAD if it does not exist

Quoted 42 lines · Bence Ferdinandy

If the user has remote/HEAD set already and it looks like it has changed on the server, then print a message, otherwise set it if we can. Silently pass if the user already has the same remote/HEAD set as reported by the server or if we encounter any errors along the way.

---

Notes:
v3: - does not rely on remote set-head anymore so it only authenticates
once
- uses the new REF_CREATE_ONLY to atomically check if the ref exists and only write it if it doesn't
- in all other cases the maximum it does is print a warning

v4: - instead of the discarded REF_CREATE_ONLY, it uses the existing,
but updated transaction api to request a silent create only
- it now uses the atomic before_target to determine reporting
- refactored for legibility

v5: - instead of printing a not too useful message, it now fails
silently, this in line with the objective to only set up remote/HEAD automatically if the right thing is trivial, for everything else there is remote set-head
- fixed all failing tests
- added two new tests, one for checking if remote/HEAD is set to the correct one, and one to test that we do not override remote/HEAD if it has changed on the server from what we have locally

v6: - fixed style issues and unintended extra empty line
- updated function call with bool to int from previous patch's change
- removed calls to error(...) inherited from builtin/remote.c so we actually fail silently
- set the test for remote set-head --auto to the correct value here, which was previously erronously set in the remote set-head patch

v7: - no change

v8: - changed logmsg in call to refs_update_symref from "remote
set-head" to "fetch"

Quoted 8 lines

v9: - follow through with refs_update_symref_extended
- fix test errors uncovered by the new patch

v10: no change

v11: fixed some memory leaks

v12: no change

I think it would be better to reverse-order these patch changelog comments so that the newest is on top/first. (for next time)

Thanks for the careful versioning here.

Signature · 1 line
Kristoffer Haugsbakk
#136 lore raw

Re: [PATCH v12 7/8] fetch: set remote/HEAD if it does not exist

Quoted 48 lines · Kristoffer Haugsbakk
Quoted 47 lines · Bence Ferdinandy

If the user has remote/HEAD set already and it looks like it has changed on the server, then print a message, otherwise set it if we can. Silently pass if the user already has the same remote/HEAD set as reported by the server or if we encounter any errors along the way.

---

Notes:
v3: - does not rely on remote set-head anymore so it only authenticates
once
- uses the new REF_CREATE_ONLY to atomically check if the ref exists and only write it if it doesn't
- in all other cases the maximum it does is print a warning

v4: - instead of the discarded REF_CREATE_ONLY, it uses the existing,
but updated transaction api to request a silent create only
- it now uses the atomic before_target to determine reporting
- refactored for legibility

v5: - instead of printing a not too useful message, it now fails
silently, this in line with the objective to only set up remote/HEAD automatically if the right thing is trivial, for everything else there is remote set-head
- fixed all failing tests
- added two new tests, one for checking if remote/HEAD is set to the correct one, and one to test that we do not override remote/HEAD if it has changed on the server from what we have locally

v6: - fixed style issues and unintended extra empty line
- updated function call with bool to int from previous patch's change
- removed calls to error(...) inherited from builtin/remote.c so we actually fail silently
- set the test for remote set-head --auto to the correct value here, which was previously erronously set in the remote set-head patch

v7: - no change

v8: - changed logmsg in call to refs_update_symref from "remote
set-head" to "fetch"

v9: - follow through with refs_update_symref_extended
- fix test errors uncovered by the new patch

v10: no change

Quoted 8 lines
Quoted 3 lines

v11: fixed some memory leaks

v12: no change

I think it would be better to reverse-order these patch changelog comments so that the newest is on top/first. (for next time)

Thanks for the careful versioning here.

Yeah, this works fine when you only go up to v3 and it all fits on a screen :D This is definitely the longest patch series I've made thus far, both in number of commits and versions. I also noticed this today when I realized that I did the cover letter in reverse and how much better readable that was than some of the patches ...

One thing that would not be easy is that with so many patches, how I start out the notes is

git rev-list HEAD~8..HEAD | xargs -i git notes append {} -m "v12: no change"

and there's no "git notes prepend". That could be another patch for another time :) It also bothers me, that the branch description that can be used to save cover letters is not a note, just a local configuration, but that's a third issue.

Anyhow, thanks for calling it out, if we do come to a v13 I might just spend some time on reversing (or be lazy and just continue at the top ...).

Best,
Bence

Signature · 1 line
bence.ferdinandy.com
#137 lore raw

PATCH v12 8/8 fetch set_head: handle mirrored bare repositories

In reply to Bence Ferdinandy

When adding a remote to bare repository with "git remote add --mirror", running fetch will fail to update HEAD to the remote's HEAD, since it does not know how to handle bare repositories. On the other hand HEAD already has content, since "git init --bare" has already set HEAD to whatever is the default branch set for the user. Unless this - by chance
- is the same as the remote's HEAD, HEAD will be pointing to a bad symref. Teach set_head to handle bare repositories, by overwriting HEAD so it mirrors the remote's HEAD.

Note, that in this case overriding the local HEAD reference is necessary, since HEAD will exist before fetch can be run, but this should not be an issue, since the whole purpose of --mirror is to be an exact mirror of the remote, so following any changes to HEAD makes sense.

Also note, that although "git remote set-head" also fails when trying to update the remote's locally tracked HEAD in a mirrored bare repository, the usage of the command does not make much sense after this patch:
fetch will update the remote HEAD correctly, and setting it manually to something else is antithetical to the concept of mirroring.

---

Notes:
v10: - new patch
- handles the issue discovered in
https://lore.kernel.org/git/[email protected]/T/

v11: no change

v12: no change

 builtin/fetch.c   | 16 ++++++++++++----
 t/t5505-remote.sh | 10 ++++++++++
 2 files changed, 22 insertions(+), 4 deletions(-)
builtin/fetch.c +12 -4
index cadfd2407a..8af3efd6ac 100644
@@ -1633,14 +1633,22 @@ static int set_head(const struct ref *remote_refs)
 	else
 		head_name = xstrdup(heads.items[0].string);
 	if (head_name) {
-		strbuf_addf(&b_head, "refs/remotes/%s/HEAD", remote);
-		strbuf_addf(&b_remote_head, "refs/remotes/%s/%s", remote, head_name);
+		int is_bare = is_bare_repository();
+		if (is_bare) {
+			strbuf_addstr(&b_head, "HEAD");
+			strbuf_addf(&b_remote_head, "refs/heads/%s", head_name);
+		} else {
+			strbuf_addf(&b_head, "refs/remotes/%s/HEAD", remote);
+			strbuf_addf(&b_remote_head, "refs/remotes/%s/%s", remote, head_name);
+		}
 		/* make sure it's valid */
-		if (!refs_ref_exists(refs, b_remote_head.buf))
+		if (!is_bare && !refs_ref_exists(refs, b_remote_head.buf)) {
 			result = 1;
+		}
 		else if (refs_update_symref_extended(refs, b_head.buf, b_remote_head.buf,
-					"fetch", &b_local_head, 1))
+					"fetch", &b_local_head, !is_bare)) {
 			result = 1;
+		}
 		else
 			report_set_head(remote, head_name, &b_local_head);
 
t/t5505-remote.sh +10 -0
index 4990d00209..dfa78f3e8d 100755
@@ -545,6 +545,16 @@ test_expect_success 'add --mirror && prune' '
 	)
 '
 
+test_expect_success 'add --mirror setting HEAD' '
+	mkdir headmirror &&
+	(
+		cd headmirror &&
+		git init --bare -b notmain &&
+		git remote add --mirror -f origin ../one &&
+		test "$(git symbolic-ref HEAD)" = "refs/heads/main"
+	)
+'
+
 test_expect_success 'add --mirror=fetch' '
 	mkdir mirror-fetch &&
 	git init -b main mirror-fetch/parent &&
Signature · 1 line
2.47.0.94.g1247fb88fd.dirty
#138 lore raw

Re: [PATCH v12 0/8] set-head/fetch remote/HEAD updates

In reply to Bence Ferdinandy

Hi all,

I just wanted to ping this thread in hopes of another round of reviews.

Aside from some smaller fixes, the main things that have not yet been reviewed are:

- the new patches 1/8 and 8/8
(https://lore.kernel.org/git/Zw8IKyPkG0Hr6%[email protected]/, but see
https://lore.kernel.org/git/[email protected]/)
- changes to 2/8 (refs_update_symref -> refs_update_symref_extended change reflecting on Phillip's comments see
https://lore.kernel.org/git/[email protected]/)

Regarding the latter name: reading another thread about code style here I was wondering if refs_update_symref_1 would be a better choice.

I'm hoping that otherwise the series is mature now.

Thanks,
Bence

#139 lore raw

v13 cover set-head/fetch remote/HEAD updates

In reply to Bence Ferdinandy

This iteration handles the special case when the previous HEAD was detached. This case is now specifically printed by both remote set-head -a and fetch. There were also some small cleanups in commit messages and unintended formattings.

Although the patches themselves are versioned:

- 2/9 is completely new (the above uncovered a discrepancy between backends)
- 3/9 had smaller changes to accomodate this
- 5/9 and 8/9 needed more substantial changes

Bence Ferdinandy (9):
t/t5505-remote: set default branch to main
refs: standardize output of refs_read_symbolic_ref
refs: atomically record overwritten ref in update_symref remote set-head: refactor for readability
remote set-head: better output for --auto
refs: add TRANSACTION_CREATE_EXISTS error
refs: add create_only option to refs_update_symref_extended
fetch: set remote/HEAD if it does not exist
fetch set_head: handle mirrored bare repositories

 builtin/fetch.c                  | 105 +++++++++++++++++++++++++++++++
 builtin/remote.c                 |  72 +++++++++++++++------
 refs.c                           |  49 ++++++++++++---
 refs.h                           |  14 ++++-
 refs/files-backend.c             |  31 +++++----
 refs/refs-internal.h             |   6 ++
 refs/reftable-backend.c          |  10 ++-
 t/t4207-log-decoration-colors.sh |   3 +-
 t/t5505-remote.sh                |  95 ++++++++++++++++++++++++++--
 t/t5510-fetch.sh                 |  24 +++++++
 t/t5512-ls-remote.sh             |   2 +
 t/t5514-fetch-multiple.sh        |  17 ++++-
 t/t5516-fetch-push.sh            |   3 +-
 t/t5527-fetch-odd-refs.sh        |   3 +-
 t/t7900-maintenance.sh           |   3 +-
 t/t9210-scalar.sh                |   5 +-
 t/t9211-scalar-clone.sh          |   6 +-
 t/t9902-completion.sh            |  65 +++++++++++++++++++
 18 files changed, 455 insertions(+), 58 deletions(-)
Signature · 1 line
2.47.0.296.gda1ecfef29.dirty
#140 lore raw

PATCH v13 1/9 t/t5505-remote: set default branch to main

Consider the bare repository called "mirror" in the test. Running git remote add --mirror -f origin ../one will not change HEAD, consequently if init.defaultBranch is not the same as what HEAD in the remote ("one"), HEAD in "mirror" will be pointing to a non-existent reference. Hence if "mirror" is used as a remote by yet another repository, ls-remote will not show HEAD. On the other hand, if init.defaultBranch happens to match HEAD in "one", then ls-remote will show HEAD.

Since the "ci/run-build-and-tests.sh" script globally exports GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main, there may be a drift in some tests between how the test repositories are set up in the CI and during local testing, if the test itself uses "master" as default instead of "main". In particular, this happens in t5505-remote.sh. This issue does not manifest currently, as the test does not do any remote HEAD manipulation where this would come up, but should such things be added, a locally passing test would break the CI and vice-versa.

Set GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main in t5505-remote to be consistent with the CI.

---

Notes:
v9: - new patch
- a bandaid for the CI issue noticed by Taylor (cf:
https://lore.kernel.org/git/Zw8IKyPkG0Hr6%[email protected]/), but
see
https://lore.kernel.org/git/[email protected]/ for the root cause in detail

v10: no change

v11: no change

v12: added forgotten sign-off

v13: commit message udpated to be more precise

 t/t5505-remote.sh | 3 +++
 1 file changed, 3 insertions(+)
t/t5505-remote.sh +3 -0
index 532035933f..9b50276646 100755
@@ -2,6 +2,9 @@
 
 test_description='git remote porcelain-ish'
 
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
 TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
Signature · 1 line
2.47.0.296.gda1ecfef29.dirty
#141 lore raw

Re: [PATCH v13 1/9] t/t5505-remote: set default branch to main

Quoted 10 lines · Bence Ferdinandy

Consider the bare repository called "mirror" in the test. Running git remote add --mirror -f origin ../one will not change HEAD, consequently if init.defaultBranch is not the same as what HEAD in the remote ("one"), HEAD in "mirror" will be pointing to a non-existent reference. Hence if "mirror" is used as a remote by yet another repository, ls-remote will not show HEAD. On the other hand, if init.defaultBranch happens to match HEAD in "one", then ls-remote will show HEAD.

Since the "ci/run-build-and-tests.sh" script globally exports GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main, there may be a drift in some

"main," -> "main for some (but not all) jobs," or something. IOW, the point of that forced export is to test both.

Quoted 3 lines

tests between how the test repositories are set up in the CI and during local testing, if the test itself uses "master" as default instead of "main".

#142 lore raw

PATCH v13 2/9 refs: standardize output of refs_read_symbolic_ref

In reply to Bence Ferdinandy

When the symbolic reference we want to read with refs_read_symbolic_ref is actually not a symbolic reference, the files and the reftable backends return different values (1 and -1 respectively). Standardize the returned values so that 0 is success, -1 is a generic error and -2 is that the reference was actually non-symbolic.

---

Notes:
v13: new patch

 refs.h                  | 6 ++++++
 refs/files-backend.c    | 7 +++----
 refs/refs-internal.h    | 6 ++++++
 refs/reftable-backend.c | 4 +++-
 4 files changed, 18 insertions(+), 5 deletions(-)
refs.h +6 -0
index 108dfc93b3..f8b714ca1d 100644
@@ -83,6 +83,12 @@ int refs_read_ref_full(struct ref_store *refs, const char *refname,
 
 int refs_read_ref(struct ref_store *refs, const char *refname, struct object_id *oid);
 
+/*
+ * Return 0 if the symbolic reference could be read without error.
+ * Return -1 for generic errors.
+ * Return -2 if the reference was actually non-symbolic.
+ */
+
 int refs_read_symbolic_ref(struct ref_store *ref_store, const char *refname,
 			   struct strbuf *referent);
 
refs/files-backend.c +3 -4
index 0824c0b8a9..81e650ec48 100644
@@ -596,10 +596,9 @@ static int files_read_symbolic_ref(struct ref_store *ref_store, const char *refn
 	unsigned int type;
 
 	ret = read_ref_internal(ref_store, refname, &oid, referent, &type, &failure_errno, 1);
-	if (ret)
-		return ret;
-
-	return !(type & REF_ISSYMREF);
+	if (!ret && !(type & REF_ISSYMREF))
+		return -2;
+	return ret;
 }
 
 int parse_loose_ref_contents(const struct git_hash_algo *algop,
refs/refs-internal.h +6 -0
index 2313c830d8..f0ef354bce 100644
@@ -673,6 +673,12 @@ struct ref_storage_be {
 
 	ref_iterator_begin_fn *iterator_begin;
 	read_raw_ref_fn *read_raw_ref;
+
+	/*
+	 * Return 0 if the symbolic reference could be read without error.
+	 * Return -1 for generic errors.
+	 * Return -2 if the reference was actually non-symbolic.
+	 */
 	read_symbolic_ref_fn *read_symbolic_ref;
 
 	reflog_iterator_begin_fn *reflog_iterator_begin;
refs/reftable-backend.c +3 -1
index 38eb14d591..60cb83f23a 100644
@@ -830,7 +830,9 @@ static int reftable_be_read_symbolic_ref(struct ref_store *ref_store,
 		return ret;
 
 	ret = reftable_stack_read_ref(stack, refname, &ref);
-	if (ret == 0 && ref.value_type == REFTABLE_REF_SYMREF)
+	if (!ret && (ref.value_type != REFTABLE_REF_SYMREF))
+		ret = -2;
+	else if (!ret && (ref.value_type == REFTABLE_REF_SYMREF))
 		strbuf_addstr(referent, ref.value.symref);
 	else
 		ret = -1;
Signature · 1 line
2.47.0.296.gda1ecfef29.dirty
#143 lore raw

Re: [PATCH v13 2/9] refs: standardize output of refs_read_symbolic_ref

Quoted 1 line · Bence Ferdinandy

Subject: Re: [PATCH v13 2/9] refs: standardize output of refs_read_symbolic_ref

"output" -> "return values", or something.

Quoted 5 lines

When the symbolic reference we want to read with refs_read_symbolic_ref is actually not a symbolic reference, the files and the reftable backends return different values (1 and -1 respectively). Standardize the returned values so that 0 is success, -1 is a generic error and -2 is that the reference was actually non-symbolic.

Are all the existing callers OK with this switch from 1 to -2?

IOW, if a caller using the ref-files backend start behaving differently with this change upon seeing a return value of -2 (where it previously was expecting to see 1), that would not be nice.

Because "reftable was already broken" is not a good excuse to introduce a separate regression to ref-files users, we'd want to be careful if we want to do this kind of "while at it" change.

Quoted 5 lines

+/*
+ * Return 0 if the symbolic reference could be read without error.
+ * Return -1 for generic errors.
+ * Return -2 if the reference was actually non-symbolic.
+ */

As this is an implementation of ref_stroage_be.read_symbolic_ref, the above comment must stay in sync with the comment over there (and a comment before the corresponding function in the other backend).

I personally would not add the above comment for that reason, but as long as we are consistent, I am OK either way. So if we add this, we should do the same to the reftable side as well.

By the way, it is arguable that it is an error when a caller asks "here is a ref, please read it as a symbolic ref" and the ref turns out to be not a symbolic ref. I'd say that it is a valid view that the caller asked the question to find out if the ref was a symbolic before making the call, and "that ref is not symbolic" is one of the valid and expected results. So if you wanted to change the value from 1 to -2 only because "you called read_symbolic_ref() without checking if it is a symbolic to begin with, which is your fault and you deserve to get an error", I am not sure if I agree with that view and reasoning.

In any case, this "not a symbolic ref" may want to have a symbolic constant. With NOT_A_SYMREF being a non-error, you could structure the caller like so:

	status = read_symref()
	if (status < 0)
		... we got an error so we stop here ...
		... we do not behave differently on error type ...

	switch (status) {
	case 0:
		... everything is peachy ...
		break ;;
	case NOT_A_SYMREF:
		... we want to handle non-symref here ...
		break ;;
	default:
		BUG("unhandled case");
	}

Also, even if we decide that we want to treat this as an error, lumping everything else as "generic error" might make it awkward to evolve the API, I suspect.

Quoted 15 lines
refs/reftable-backend.c +3 -1
index 38eb14d591..60cb83f23a 100644
@@ -830,7 +830,9 @@ static int reftable_be_read_symbolic_ref(struct ref_store *ref_store,
 		return ret;
 
 	ret = reftable_stack_read_ref(stack, refname, &ref);
-	if (ret == 0 && ref.value_type == REFTABLE_REF_SYMREF)
+	if (!ret && (ref.value_type != REFTABLE_REF_SYMREF))
+		ret = -2;
+	else if (!ret && (ref.value_type == REFTABLE_REF_SYMREF))
 		strbuf_addstr(referent, ref.value.symref);
 	else
 		ret = -1;
#144 lore raw

Re: [PATCH v13 2/9] refs: standardize output of refs_read_symbolic_ref

Quoted 14 lines · Junio C Hamano
Quoted 1 line · Bence Ferdinandy

Subject: Re: [PATCH v13 2/9] refs: standardize output of refs_read_symbolic_ref

"output" -> "return values", or something.

Quoted 5 lines

When the symbolic reference we want to read with refs_read_symbolic_ref is actually not a symbolic reference, the files and the reftable backends return different values (1 and -1 respectively). Standardize the returned values so that 0 is success, -1 is a generic error and -2 is that the reference was actually non-symbolic.

Are all the existing callers OK with this switch from 1 to -2?

Quoted 7 lines

IOW, if a caller using the ref-files backend start behaving differently with this change upon seeing a return value of -2 (where it previously was expecting to see 1), that would not be nice.

Because "reftable was already broken" is not a good excuse to introduce a separate regression to ref-files users, we'd want to be careful if we want to do this kind of "while at it" change.

There are only three callers:

- "remote.c:ignore_symref_update()" only cares whether the return value is 0 or not.

- "builtin/remote.c:mv()" is the same.

- "refs.c:migrate_one_ref()" assumes the behaviour of the reftable backend and only checks for negative error codes.

So you could argue that it's the "files" backend that is broken, not the "reftable" backend. Doesn't matter ultimately though, the real problem is that this wasn't ever documented anywhere.

I agree that this should be part of the commit message.

Quoted 6 lines
Quoted 5 lines

+/*
+ * Return 0 if the symbolic reference could be read without error.
+ * Return -1 for generic errors.
+ * Return -2 if the reference was actually non-symbolic.
+ */

Quoted 7 lines

As this is an implementation of ref_stroage_be.read_symbolic_ref, the above comment must stay in sync with the comment over there (and a comment before the corresponding function in the other backend).

I personally would not add the above comment for that reason, but as long as we are consistent, I am OK either way. So if we add this, we should do the same to the reftable side as well.

Another solution could be to have the comment in "refs.h" for the caller-facing function, while the backend pointer simply says "Please refer to the documentation of refs_read_symbolic_ref()."

Quoted 10 lines

By the way, it is arguable that it is an error when a caller asks "here is a ref, please read it as a symbolic ref" and the ref turns out to be not a symbolic ref. I'd say that it is a valid view that the caller asked the question to find out if the ref was a symbolic before making the call, and "that ref is not symbolic" is one of the valid and expected results. So if you wanted to change the value from 1 to -2 only because "you called read_symbolic_ref() without checking if it is a symbolic to begin with, which is your fault and you deserve to get an error", I am not sure if I agree with that view and reasoning.

The reason why I've been proposing to return negative is because we have the idiom of checking err < 0 in many places, so a function that returns a positive value in the case where it didn't return the expected result can easily lead to bugs.

Quoted 3 lines

In any case, this "not a symbolic ref" may want to have a symbolic constant. With NOT_A_SYMREF being a non-error, you could structure the caller like so:

That'd be a good idea.

Quoted 16 lines
	status = read_symref()
	if (status < 0)
		... we got an error so we stop here ...
		... we do not behave differently on error type ...

	switch (status) {
	case 0:
		... everything is peachy ...
		break ;;
	case NOT_A_SYMREF:
		... we want to handle non-symref here ...
		break ;;
	default:
		BUG("unhandled case");
	}
Quoted 3 lines

Also, even if we decide that we want to treat this as an error, lumping everything else as "generic error" might make it awkward to evolve the API, I suspect.

I guess most callers don't care about the exact error code, they only care about whether or not they have been able to read the symref. I think this idiom would easily be extensible in the future if all callers know to check for err < 0 by default, because introducing a new error code would continue to work for them. And if they want to handle that new error code they can adapt as you propose above with the switch.

Patrick

#145 lore raw

Re: [PATCH v13 2/9] refs: standardize output of refs_read_symbolic_ref

Quoted 5 lines · Patrick Steinhardt

There are only three callers:

- "remote.c:ignore_symref_update()" only cares whether the return value is 0 or not.

Quoted 8 lines

- "builtin/remote.c:mv()" is the same.

- "refs.c:migrate_one_ref()" assumes the behaviour of the reftable backend and only checks for negative error codes.

So you could argue that it's the "files" backend that is broken, not the "reftable" backend. Doesn't matter ultimately though, the real problem is that this wasn't ever documented anywhere.

You're correct that it does not matter ultimately. But as a general rule, which also applies here, anything newly introduced one does differently without having a good reason is a bug in the new thing, I would say.

Quoted 3 lines

Another solution could be to have the comment in "refs.h" for the caller-facing function, while the backend pointer simply says "Please refer to the documentation of refs_read_symbolic_ref()."

Yup, avoiding unnecessary duplication is a good thing.

Quoted 4 lines

The reason why I've been proposing to return negative is because we have the idiom of checking err < 0 in many places, so a function that returns a positive value in the case where it didn't return the expected result can easily lead to bugs.

I agree with the general reasoning. I am saying this may or may not be an error, and if it turns out that it is not an error but is just one of the generally expected outcome, treating it as an error and having "if (status < 0)" to lump the case together with other error cases may not be nice to the callers.

#146 lore raw

Re: [PATCH v13 2/9] refs: standardize output of refs_read_symbolic_ref

Quoted 11 lines · Junio C Hamano
Quoted 4 lines · Patrick Steinhardt

The reason why I've been proposing to return negative is because we have the idiom of checking err < 0 in many places, so a function that returns a positive value in the case where it didn't return the expected result can easily lead to bugs.

I agree with the general reasoning. I am saying this may or may not be an error, and if it turns out that it is not an error but is just one of the generally expected outcome, treating it as an error and having "if (status < 0)" to lump the case together with other error cases may not be nice to the callers.

The question to me is whether the function returns something sensible in all non-error cases that a caller can use properly without having to explicitly check for the value. And I'd say that this is not the case with refs_read_symbolic_ref(), which wouldn't end up setting the value of referent.

So regardless of whether we define this as error or non-error, the caller would have to exlicitly handle the case where it's not a symref in order to make sense of it because the result is not well-defined.

Patrick

#147 lore raw

Re: [PATCH v13 2/9] refs: standardize output of refs_read_symbolic_ref

Quoted 19 lines · Patrick Steinhardt
Quoted 11 lines · Junio C Hamano
Quoted 4 lines · Patrick Steinhardt

The reason why I've been proposing to return negative is because we have the idiom of checking err < 0 in many places, so a function that returns a positive value in the case where it didn't return the expected result can easily lead to bugs.

I agree with the general reasoning. I am saying this may or may not be an error, and if it turns out that it is not an error but is just one of the generally expected outcome, treating it as an error and having "if (status < 0)" to lump the case together with other error cases may not be nice to the callers.

The question to me is whether the function returns something sensible in all non-error cases that a caller can use properly without having to explicitly check for the value. And I'd say that this is not the case with refs_read_symbolic_ref(), which wouldn't end up setting the value of referent.

Quoted 3 lines

So regardless of whether we define this as error or non-error, the caller would have to exlicitly handle the case where it's not a symref in order to make sense of it because the result is not well-defined.

I agree with Patrick re the -1, -2 return values. The non-error behavior should be when referent is set, anything else is something the caller would need to consider if they want to do something with it or not.

#148 lore raw

Re: [PATCH v13 2/9] refs: standardize output of refs_read_symbolic_ref

In reply to Bence Ferdinandy
Quoted 5 lines · Bence Ferdinandy
refs/reftable-backend.c +0 -0
index 38eb14d591..60cb83f23a 100644

refs/reftable-backend.c static int reftable_be_read_symbolic_ref(struct ref_store *ref_store, · line 838

Quoted 10 lines
refs/reftable-backend.c +3 -1
@@ -830,7 +830,9 @@ static int reftable_be_read_symbolic_ref(struct ref_store *ref_store,
 		return ret;
 
 	ret = reftable_stack_read_ref(stack, refname, &ref);
-	if (ret == 0 && ref.value_type == REFTABLE_REF_SYMREF)
+	if (!ret && (ref.value_type != REFTABLE_REF_SYMREF))
+		ret = -2;
+	else if (!ret && (ref.value_type == REFTABLE_REF_SYMREF))
 		strbuf_addstr(referent, ref.value.symref);
 	else
 		ret = -1;

The ref.value_type can be either equal to REFTABLE_REF_SYMREF or not equal to it, and there is no other choice.

Wouldn't it be easier to reason about if the above code were written more like this:

        if (ret)
		ret = -1;
	else if (ref.value_type == REFTABLE_REF_SYMREF)
		strbuf_addstr(...);
	else
		ret = -2;

I found it curious when I read it again while attempting to resolve conflicts with 5413d69f (refs/reftable: refactor reading symbolic refs to use reftable backend, 2024-11-05). The resolution has to update this part of code to use the new implementation that asks reftable_backend_read_ref() and becomes a lot simpler, so the way it is written in your topic does not make much difference in the longer term when both topics graduate.

IOW, if we were rebuilding your topic on top of Patrick's topoic that includes 5413d69f, this part would read like so, I think.

refs/reftable-backend.c +3 -1
index 6298991da7..b6bc3039a5 100644
@@ -920,8 +920,10 @@ static int reftable_be_read_symbolic_ref(struct ref_store *ref_store,
 		return ret;
 
 	ret = reftable_backend_read_ref(be, refname, &oid, referent, &type);
-	if (type != REF_ISSYMREF)
+	if (ret)
 		ret = -1;
+	else if (type != REF_ISSYMREF)
+		ret = -2;
 	return ret;
 }
 
#149 lore raw

Re: [PATCH v13 2/9] refs: standardize output of refs_read_symbolic_ref

Quoted 9 lines · Junio C Hamano
Quoted 7 lines · Bence Ferdinandy
refs/reftable-backend.c +0 -0
index 38eb14d591..60cb83f23a 100644
@@ -830,7 +830,9 @@ static int reftable_be_read_symbolic_ref(struct ref_store *ref_store,
 		return ret;
 
Quoted 11 lines

refs/reftable-backend.c static int reftable_be_read_symbolic_ref(struct ref_store *ref_store, · line 838

Quoted 8 lines
refs/reftable-backend.c +3 -1
 	ret = reftable_stack_read_ref(stack, refname, &ref);
-	if (ret == 0 && ref.value_type == REFTABLE_REF_SYMREF)
+	if (!ret && (ref.value_type != REFTABLE_REF_SYMREF))
+		ret = -2;
+	else if (!ret && (ref.value_type == REFTABLE_REF_SYMREF))
 		strbuf_addstr(referent, ref.value.symref);
 	else
 		ret = -1;

The ref.value_type can be either equal to REFTABLE_REF_SYMREF or not equal to it, and there is no other choice.

Ah, ok, I didn't realize this.

Quoted 11 lines

Wouldn't it be easier to reason about if the above code were written more like this:

        if (ret)
		ret = -1;
	else if (ref.value_type == REFTABLE_REF_SYMREF)
		strbuf_addstr(...);
	else
		ret = -2;
Quoted 7 lines

I found it curious when I read it again while attempting to resolve conflicts with 5413d69f (refs/reftable: refactor reading symbolic refs to use reftable backend, 2024-11-05). The resolution has to update this part of code to use the new implementation that asks reftable_backend_read_ref() and becomes a lot simpler, so the way it is written in your topic does not make much difference in the longer term when both topics graduate.

I'll update the patch with the above,

Quoted 10 lines

IOW, if we were rebuilding your topic on top of Patrick's topoic that includes 5413d69f, this part would read like so, I think.

refs/reftable-backend.c +0 -0
index 6298991da7..b6bc3039a5 100644
@@ -920,8 +920,10 @@ static int reftable_be_read_symbolic_ref(struct ref_store *ref_store,
 		return ret;

refs/reftable-backend.c static int reftable_be_read_symbolic_ref(struct ref_store *ref_store, · line 929

Quoted 10 lines
refs/reftable-backend.c +3 -1
 
 	ret = reftable_backend_read_ref(be, refname, &oid, referent, &type);
-	if (type != REF_ISSYMREF)
+	if (ret)
 		ret = -1;
+	else if (type != REF_ISSYMREF)
+		ret = -2;
 	return ret;
 }
 

but I'll save this as well, I would not be completely surprised if Patrick's topic makes it in sooner :)

Thanks,
Bence

#150 lore raw

Re: [PATCH v13 2/9] refs: standardize output of refs_read_symbolic_ref

In reply to Bence Ferdinandy
Quoted 5 lines · Bence Ferdinandy
refs.h +0 -0
index 108dfc93b3..f8b714ca1d 100644

refs.h int refs_read_ref_full(struct ref_store *refs, const char *refname, · line 91

Quoted 9 lines
refs.h +6 -0
@@ -83,6 +83,12 @@ int refs_read_ref_full(struct ref_store *refs, const char *refname,
 
 int refs_read_ref(struct ref_store *refs, const char *refname, struct object_id *oid);
 
+/*
+ * Return 0 if the symbolic reference could be read without error.
+ * Return -1 for generic errors.
+ * Return -2 if the reference was actually non-symbolic.
+ */
+

Extraneous empty newline.

Also, how about the following:

/*
* Read the symbolic ref named "refname" and write its immediate
* referent into the provided buffer. This does not resolve the
* symbolic ref recursively in case the target is a symbolic ref, as
* well.
*
* Returns 0 on success, -2 if the "refname" is not a symbolic ref,
* -1 otherwise.
*/

Quoted 5 lines
refs/refs-internal.h +0 -0
index 2313c830d8..f0ef354bce 100644

refs/refs-internal.h struct ref_storage_be { · line 682

Quoted 10 lines
refs/refs-internal.h +6 -0
@@ -673,6 +673,12 @@ struct ref_storage_be {
 
 	ref_iterator_begin_fn *iterator_begin;
 	read_raw_ref_fn *read_raw_ref;
+
+	/*
+	 * Return 0 if the symbolic reference could be read without error.
+	 * Return -1 for generic errors.
+	 * Return -2 if the reference was actually non-symbolic.
+	 */
 	read_symbolic_ref_fn *read_symbolic_ref;

As proposed in the other thread, this could instead be:

/*
* Please refer to refs_read_symbolic_ref() for the expected
* behaviour.
/

Patrick

#151 lore raw

Re: [PATCH v13 2/9] refs: standardize output of refs_read_symbolic_ref

Quoted 48 lines · Patrick Steinhardt
Quoted 5 lines · Bence Ferdinandy
refs.h +0 -0
index 108dfc93b3..f8b714ca1d 100644

refs.h int refs_read_ref_full(struct ref_store *refs, const char *refname, · line 91

Quoted 9 lines
refs.h +6 -0
@@ -83,6 +83,12 @@ int refs_read_ref_full(struct ref_store *refs, const char *refname,
 
 int refs_read_ref(struct ref_store *refs, const char *refname, struct object_id *oid);
 
+/*
+ * Return 0 if the symbolic reference could be read without error.
+ * Return -1 for generic errors.
+ * Return -2 if the reference was actually non-symbolic.
+ */
+

Extraneous empty newline.

Also, how about the following:

/*
* Read the symbolic ref named "refname" and write its immediate
* referent into the provided buffer. This does not resolve the
* symbolic ref recursively in case the target is a symbolic ref, as
* well.
*
* Returns 0 on success, -2 if the "refname" is not a symbolic ref,
* -1 otherwise.
*/

Quoted 5 lines
refs/refs-internal.h +0 -0
index 2313c830d8..f0ef354bce 100644

refs/refs-internal.h struct ref_storage_be { · line 682

Quoted 10 lines
refs/refs-internal.h +6 -0
@@ -673,6 +673,12 @@ struct ref_storage_be {
 
 	ref_iterator_begin_fn *iterator_begin;
 	read_raw_ref_fn *read_raw_ref;
+
+	/*
+	 * Return 0 if the symbolic reference could be read without error.
+	 * Return -1 for generic errors.
+	 * Return -2 if the reference was actually non-symbolic.
+	 */
 	read_symbolic_ref_fn *read_symbolic_ref;

As proposed in the other thread, this could instead be:

Quoted 4 lines

/*
* Please refer to refs_read_symbolic_ref() for the expected
* behaviour.
/

Thanks, that makes sense. So as a summary, I'll update the comments and also the implementation detail Junio pointed out.

#152 lore raw

PATCH v13 3/9 refs: atomically record overwritten ref in update_symref

In reply to Bence Ferdinandy

When updating a symref with update_symref it's currently not possible to know for sure what was the previous value that was overwritten. Extend refs_update_symref under a new function name, to record the value after the ref has been locked if the caller of refs_update_symref_extended requests it via a new variable in the function call. Keep the original refs_update_symref function with the same signature, but now as a wrapper around refs_update_symref_extended.

---

Notes:
v4: new patch

v5: - added before_target to reftables backend
- added an extra safety check for transaction's existence in refs.c

v6: - no change

v7: - remove the whole before_target concept from the backends and
handle checking it in refs.c instead (thanks Karthik)
- rename the before_target to referent which is how the same concept is called in the backends
- change commit prefix to be more in line with project standards

v8: no change

v9: - instead of adding parameters to refs_update_symref, rename what
was in v8 as refs_update_symref_extended and make refs_update_symref a wrapper for that. This significantly reduces the number of files that need to be touched, and avoids adding a lot of dummy NULL-s in unrelated places.

v10: no change

v11: no change

v12: no change

v13: if referent is a non-symbolic ref, record the hash in referent and
signal this with a return value of -1

 refs.c | 24 ++++++++++++++++++++++--
 refs.h |  4 ++++
 2 files changed, 26 insertions(+), 2 deletions(-)
refs.c +22 -2
index 5f729ed412..c4500a7582 100644
@@ -2115,6 +2115,13 @@ int peel_iterated_oid(struct repository *r, const struct object_id *base, struct
 
 int refs_update_symref(struct ref_store *refs, const char *ref,
 		       const char *target, const char *logmsg)
+{
+	return refs_update_symref_extended(refs, ref, target, logmsg, NULL);
+}
+
+int refs_update_symref_extended(struct ref_store *refs, const char *ref,
+		       const char *target, const char *logmsg,
+		       struct strbuf *referent)
 {
 	struct ref_transaction *transaction;
 	struct strbuf err = STRBUF_INIT;
@@ -2125,10 +2132,24 @@ int refs_update_symref(struct ref_store *refs, const char *ref,
 	    ref_transaction_update(transaction, ref, NULL, NULL,
 				   target, NULL, REF_NO_DEREF,
 				   logmsg, &err) ||
-	    ref_transaction_commit(transaction, &err)) {
+	    ref_transaction_prepare(transaction, &err)) {
 		ret = error("%s", err.buf);
+		goto cleanup;
+	}
+	if (referent && refs_read_symbolic_ref(refs, ref, referent) == -2) {
+		struct object_id oid;
+		if (!refs_read_ref(refs, ref, &oid)) {
+			strbuf_addstr(referent, oid_to_hex(&oid));
+			ret = -1;
+		} else {
+			ret = 1;
+		}
 	}
 
+	if (ref_transaction_commit(transaction, &err))
+		ret = error("%s", err.buf);
+
+cleanup:
 	strbuf_release(&err);
 	if (transaction)
 		ref_transaction_free(transaction);
@@ -2948,4 +2969,3 @@ int ref_update_expects_existing_old_ref(struct ref_update *update)
 	return (update->flags & REF_HAVE_OLD) &&
 		(!is_null_oid(&update->old_oid) || update->old_target);
 }
-
refs.h +4 -0
index f8b714ca1d..92622e807d 100644
@@ -579,6 +579,10 @@ int refs_copy_existing_ref(struct ref_store *refs, const char *oldref,
 int refs_update_symref(struct ref_store *refs, const char *refname,
 		       const char *target, const char *logmsg);
 
+int refs_update_symref_extended(struct ref_store *refs, const char *refname,
+		       const char *target, const char *logmsg,
+		       struct strbuf *referent);
+
 enum action_on_err {
 	UPDATE_REFS_MSG_ON_ERR,
 	UPDATE_REFS_DIE_ON_ERR,
Signature · 1 line
2.47.0.296.gda1ecfef29.dirty
#153 lore raw

PATCH v13 4/9 remote set-head: refactor for readability

In reply to Bence Ferdinandy

Make two different readability refactors:

Rename strbufs "buf" and "buf2" to something more explanatory.

Instead of calling get_main_ref_store(the_repository) multiple times, call it once and store the result in a new refs variable. Although this change probably offers some performance benefits, the main purpose is to shorten the line lengths of function calls using this variable.

---

Notes:
v5: new patch (split from the next patch as a preparatory step)

v6: no change

v7: - change commit prefix to be more in line with project standards

v8: no change

v9: - further improve readability by renaming buf, and buf2 consistently
with how patch 6 was already done

v10: no change

v11: no change

v12: no change

v13: more precise wording for commit message

 builtin/remote.c | 23 ++++++++++++-----------
 1 file changed, 12 insertions(+), 11 deletions(-)
builtin/remote.c +12 -11
index 76670ddd8b..1d68c5b2ba 100644
@@ -1402,8 +1402,9 @@ static int show(int argc, const char **argv, const char *prefix)
 static int set_head(int argc, const char **argv, const char *prefix)
 {
 	int i, opt_a = 0, opt_d = 0, result = 0;
-	struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT;
+	struct strbuf b_head = STRBUF_INIT, b_remote_head = STRBUF_INIT;
 	char *head_name = NULL;
+	struct ref_store *refs = get_main_ref_store(the_repository);
 
 	struct option options[] = {
 		OPT_BOOL('a', "auto", &opt_a,
@@ -1415,7 +1416,7 @@ static int set_head(int argc, const char **argv, const char *prefix)
 	argc = parse_options(argc, argv, prefix, options,
 			     builtin_remote_sethead_usage, 0);
 	if (argc)
-		strbuf_addf(&buf, "refs/remotes/%s/HEAD", argv[0]);
+		strbuf_addf(&b_head, "refs/remotes/%s/HEAD", argv[0]);
 
 	if (!opt_a && !opt_d && argc == 2) {
 		head_name = xstrdup(argv[1]);
@@ -1434,25 +1435,25 @@ static int set_head(int argc, const char **argv, const char *prefix)
 			head_name = xstrdup(states.heads.items[0].string);
 		free_remote_ref_states(&states);
 	} else if (opt_d && !opt_a && argc == 1) {
-		if (refs_delete_ref(get_main_ref_store(the_repository), NULL, buf.buf, NULL, REF_NO_DEREF))
-			result |= error(_("Could not delete %s"), buf.buf);
+		if (refs_delete_ref(refs, NULL, b_head.buf, NULL, REF_NO_DEREF))
+			result |= error(_("Could not delete %s"), b_head.buf);
 	} else
 		usage_with_options(builtin_remote_sethead_usage, options);
 
 	if (head_name) {
-		strbuf_addf(&buf2, "refs/remotes/%s/%s", argv[0], head_name);
+		strbuf_addf(&b_remote_head, "refs/remotes/%s/%s", argv[0], head_name);
 		/* make sure it's valid */
-		if (!refs_ref_exists(get_main_ref_store(the_repository), buf2.buf))
-			result |= error(_("Not a valid ref: %s"), buf2.buf);
-		else if (refs_update_symref(get_main_ref_store(the_repository), buf.buf, buf2.buf, "remote set-head"))
-			result |= error(_("Could not setup %s"), buf.buf);
+		if (!refs_ref_exists(refs, b_remote_head.buf))
+			result |= error(_("Not a valid ref: %s"), b_remote_head.buf);
+		else if (refs_update_symref(refs, b_head.buf, b_remote_head.buf, "remote set-head"))
+			result |= error(_("Could not setup %s"), b_head.buf);
 		else if (opt_a)
 			printf("%s/HEAD set to %s\n", argv[0], head_name);
 		free(head_name);
 	}
 
-	strbuf_release(&buf);
-	strbuf_release(&buf2);
+	strbuf_release(&b_head);
+	strbuf_release(&b_remote_head);
 	return result;
 }
 
Signature · 1 line
2.47.0.296.gda1ecfef29.dirty
#154 lore raw

PATCH v13 5/9 remote set-head: better output for --auto

In reply to Bence Ferdinandy

Currently, set-head --auto will print a message saying "remote/HEAD set to branch", which implies something was changed.

Change the output of --auto, so the output actually reflects what was
done: a) set a previously unset HEAD, b) change HEAD because remote changed or c) no updates. As edge cases, if HEAD is changed from a previous symbolic reference that was not a remote branch, explicitly call attention to this fact, and also notify the user if the previous reference was not a symbolic reference.

---

Notes:
v1-v2:

was RFC in https://lore.kernel.org/git/[email protected]/

v3:

This patch was originally sent along when I thought set-head was going to be invoked by fetch, but the discussion on the RFC concluded that it should be not. This opened the possibility to make it more explicit.

Note: although I feel both things the patch does are really just cosmetic, an argument could be made for breaking it into two, one for the no-op part and one for the --auto print update.

Was sent in:
https://lore.kernel.org/git/[email protected]/

v4:
- changes are now handled atomically via the ref update transaction
- outputs have changed along the lines of Junio's suggestion
- minor refactor to set_head for improved legibility

v5: - the minor refactor has been split out into its own patch

v6: - fixed uninitialized prev_head
- fixed case of unusual previous target
- fixed a test that would have been actually broken at this patch (the output was only correct with the later update to fetch)
- added 4 tests for the 4 output cases

v7: - change commit prefix to be more in line with project standards
- fixed tests to also work with the reftable backend
- renamed report function, fixed style issue with checking buf len
- fixed not releasing an strbuf

v8: no change

v9: - mark output strings in report_set_head_auto as translatable
- rename buf_prev to b_local_head for consistency
- use ${SQ} in tests instead of '\''

v10: no change

v11: no change

v12: no change

v13: added handling the edge case of previous remote/HEAD being a detached HEAD

 builtin/remote.c  | 59 +++++++++++++++++++++++++++++++++++---------
 t/t5505-remote.sh | 63 ++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 109 insertions(+), 13 deletions(-)
builtin/remote.c +47 -12
index 1d68c5b2ba..a682ef5df2 100644
@@ -1399,10 +1399,38 @@ static int show(int argc, const char **argv, const char *prefix)
 	return result;
 }
 
+static void report_set_head_auto(const char *remote, const char *head_name,
+			struct strbuf *b_local_head, int updateres) {
+	struct strbuf buf_prefix = STRBUF_INIT;
+	const char *prev_head = NULL;
+
+	strbuf_addf(&buf_prefix, "refs/remotes/%s/", remote);
+	skip_prefix(b_local_head->buf, buf_prefix.buf, &prev_head);
+
+	if (prev_head && !strcmp(prev_head, head_name))
+		printf(_("'%s/HEAD' is unchanged and points to '%s'\n"),
+			remote, head_name);
+	else if (prev_head)
+		printf(_("'%s/HEAD' has changed from '%s' and now points to '%s'\n"),
+			remote, prev_head, head_name);
+	else if (!b_local_head->len)
+		printf(_("'%s/HEAD' is now created and points to '%s'\n"),
+			remote, head_name);
+	else if (!!updateres && b_local_head->len)
+		printf(_("'%s/HEAD' was detached at '%s' and now points to '%s'\n"),
+			remote, b_local_head->buf, head_name);
+	else
+		printf(_("'%s/HEAD' used to point to '%s' "
+			"(which is not a remote branch), but now points to '%s'\n"),
+			remote, b_local_head->buf, head_name);
+	strbuf_release(&buf_prefix);
+}
+
 static int set_head(int argc, const char **argv, const char *prefix)
 {
-	int i, opt_a = 0, opt_d = 0, result = 0;
-	struct strbuf b_head = STRBUF_INIT, b_remote_head = STRBUF_INIT;
+	int i, opt_a = 0, opt_d = 0, result = 0, updateres;
+	struct strbuf b_head = STRBUF_INIT, b_remote_head = STRBUF_INIT,
+		b_local_head = STRBUF_INIT;
 	char *head_name = NULL;
 	struct ref_store *refs = get_main_ref_store(the_repository);
 
@@ -1440,20 +1468,27 @@ static int set_head(int argc, const char **argv, const char *prefix)
 	} else
 		usage_with_options(builtin_remote_sethead_usage, options);
 
-	if (head_name) {
-		strbuf_addf(&b_remote_head, "refs/remotes/%s/%s", argv[0], head_name);
-		/* make sure it's valid */
-		if (!refs_ref_exists(refs, b_remote_head.buf))
-			result |= error(_("Not a valid ref: %s"), b_remote_head.buf);
-		else if (refs_update_symref(refs, b_head.buf, b_remote_head.buf, "remote set-head"))
-			result |= error(_("Could not setup %s"), b_head.buf);
-		else if (opt_a)
-			printf("%s/HEAD set to %s\n", argv[0], head_name);
-		free(head_name);
+	if (!head_name)
+		goto cleanup;
+	strbuf_addf(&b_remote_head, "refs/remotes/%s/%s", argv[0], head_name);
+	if (!refs_ref_exists(refs, b_remote_head.buf)) {
+		result |= error(_("Not a valid ref: %s"), b_remote_head.buf);
+		goto cleanup;
+	}
+	updateres = refs_update_symref_extended(refs, b_head.buf, b_remote_head.buf,
+			"remote set-head", &b_local_head);
+	if (updateres == -2) {
+		result |= error(_("Could not setup %s"), b_head.buf);
+		goto cleanup;
 	}
+	if (opt_a)
+		report_set_head_auto(argv[0], head_name, &b_local_head, updateres);
 
+cleanup:
+	free(head_name);
 	strbuf_release(&b_head);
 	strbuf_release(&b_remote_head);
+	strbuf_release(&b_local_head);
 	return result;
 }
 
t/t5505-remote.sh +62 -1

80 lines not on this page · raw message

Signature · 1 line
2.47.0.296.gda1ecfef29.dirty
#155 lore raw

Re: [PATCH v13 5/9] remote set-head: better output for --auto

builtin/remote.c static int show(int argc, const char **argv, const char *prefix) · line 1403

Quoted 2 lines · Bence Ferdinandy
builtin/remote.c +2 -0
+static void report_set_head_auto(const char *remote, const char *head_name,
+			struct strbuf *b_local_head, int updateres) {

"updateres" was too mysterious a name. "res" stands for what, "resource"?

Looking at the way the parameter is used by the code, it seems to indicate that the remote HEAD originally was in a detached state, so "was_detached" may be a better name, perhaps?

builtin/remote.c static int show(int argc, const char **argv, const char *prefix) · line 1421

Quoted 3 lines
builtin/remote.c +3 -0
+	else if (!!updateres && b_local_head->len)
+		printf(_("'%s/HEAD' was detached at '%s' and now points to '%s'\n"),
+			remote, b_local_head->buf, head_name);

There is no need for !!; any non-zero integer is true. !! is useful only in a context that takes only 0 and 1 (like when you are making an assignment to a variable or a structure member that takes only 0 or 1).

Quoted 19 lines

static int set_head(int argc, const char **argv, const char *prefix) {

builtin/remote.c +3 -9
-	int i, opt_a = 0, opt_d = 0, result = 0;
-	struct strbuf b_head = STRBUF_INIT, b_remote_head = STRBUF_INIT;
+	int i, opt_a = 0, opt_d = 0, result = 0, updateres;
+	struct strbuf b_head = STRBUF_INIT, b_remote_head = STRBUF_INIT,
+		b_local_head = STRBUF_INIT;
@@ -1440,20 +1468,27 @@ static int set_head(int argc, const char **argv, const char *prefix)
 	} else
 		usage_with_options(builtin_remote_sethead_usage, options);
 
-	if (head_name) {
-		strbuf_addf(&b_remote_head, "refs/remotes/%s/%s", argv[0], head_name);
-		/* make sure it's valid */
-		if (!refs_ref_exists(refs, b_remote_head.buf))
-			result |= error(_("Not a valid ref: %s"), b_remote_head.buf);
-		else if (refs_update_symref(refs, b_head.buf, b_remote_head.buf, "remote set-head"))
-			result |= error(_("Could not setup %s"), b_head.buf);

builtin/remote.c static int set_head(int argc, const char **argv, const char *prefix) · line 1477

Quoted 10 lines
builtin/remote.c +7 -3
-		else if (opt_a)
-			printf("%s/HEAD set to %s\n", argv[0], head_name);
-		free(head_name);
+	if (!head_name)
+		goto cleanup;
+	strbuf_addf(&b_remote_head, "refs/remotes/%s/%s", argv[0], head_name);
+	if (!refs_ref_exists(refs, b_remote_head.buf)) {
+		result |= error(_("Not a valid ref: %s"), b_remote_head.buf);
+		goto cleanup;
+	}

OK, we refuse to allow a manual "remote set-head" to create a dangling symref, which is a faithful rewrite from the original.

builtin/remote.c static int set_head(int argc, const char **argv, const char *prefix)

Quoted 4 lines
builtin/remote.c +3 -0
+	updateres = refs_update_symref_extended(refs, b_head.buf, b_remote_head.buf,
+			"remote set-head", &b_local_head);
 
+	if (updateres == -2) {

Where does this -2 come from? It is not the "you asked to read it as a symref but it wasn't a symref" thing, which was mapped to -1 with [PATCH 3/9].

It is an unusual way to construct an extensible API function to say "all different kinds of errors we happen to know when this particular caller was written return -2, but some special cases are not -2".

Rather, "all negatives, other than these selected few values we special-case and handle, are errors" is more natural, isn't it?

Maybe I am misreading the code and missing where the -2 comes from or the significance of the value? I dunno.

#156 lore raw

Re: [PATCH v13 5/9] remote set-head: better output for --auto

Quoted 11 lines · Junio C Hamano

builtin/remote.c static int show(int argc, const char **argv, const char *prefix) · line 1403

Quoted 2 lines · Bence Ferdinandy
builtin/remote.c +2 -0
+static void report_set_head_auto(const char *remote, const char *head_name,
+			struct strbuf *b_local_head, int updateres) {

"updateres" was too mysterious a name. "res" stands for what, "resource"?

Looking at the way the parameter is used by the code, it seems to indicate that the remote HEAD originally was in a detached state, so "was_detached" may be a better name, perhaps?

"res" wanted to be short for result, but "was_detached" is definitely more readable.

Quoted 43 lines

builtin/remote.c static int show(int argc, const char **argv, const char *prefix) · line 1421

Quoted 4 lines
builtin/remote.c +3 -0
+	else if (!!updateres && b_local_head->len)
+		printf(_("'%s/HEAD' was detached at '%s' and now points to '%s'\n"),
+			remote, b_local_head->buf, head_name);

There is no need for !!; any non-zero integer is true. !! is useful only in a context that takes only 0 and 1 (like when you are making an assignment to a variable or a structure member that takes only 0 or 1).

Quoted 19 lines

static int set_head(int argc, const char **argv, const char *prefix) {

builtin/remote.c +3 -9
-	int i, opt_a = 0, opt_d = 0, result = 0;
-	struct strbuf b_head = STRBUF_INIT, b_remote_head = STRBUF_INIT;
+	int i, opt_a = 0, opt_d = 0, result = 0, updateres;
+	struct strbuf b_head = STRBUF_INIT, b_remote_head = STRBUF_INIT,
+		b_local_head = STRBUF_INIT;
@@ -1440,20 +1468,27 @@ static int set_head(int argc, const char **argv, const char *prefix)
 	} else
 		usage_with_options(builtin_remote_sethead_usage, options);
 
-	if (head_name) {
-		strbuf_addf(&b_remote_head, "refs/remotes/%s/%s", argv[0], head_name);
-		/* make sure it's valid */
-		if (!refs_ref_exists(refs, b_remote_head.buf))
-			result |= error(_("Not a valid ref: %s"), b_remote_head.buf);
-		else if (refs_update_symref(refs, b_head.buf, b_remote_head.buf, "remote set-head"))
-			result |= error(_("Could not setup %s"), b_head.buf);

builtin/remote.c static int set_head(int argc, const char **argv, const char *prefix) · line 1477

Quoted 10 lines
builtin/remote.c +7 -3
-		else if (opt_a)
-			printf("%s/HEAD set to %s\n", argv[0], head_name);
-		free(head_name);
+	if (!head_name)
+		goto cleanup;
+	strbuf_addf(&b_remote_head, "refs/remotes/%s/%s", argv[0], head_name);
+	if (!refs_ref_exists(refs, b_remote_head.buf)) {
+		result |= error(_("Not a valid ref: %s"), b_remote_head.buf);
+		goto cleanup;
+	}

OK, we refuse to allow a manual "remote set-head" to create a dangling symref, which is a faithful rewrite from the original.

Quoted 8 lines

builtin/remote.c static int set_head(int argc, const char **argv, const char *prefix)

Quoted 4 lines
builtin/remote.c +3 -0
+	updateres = refs_update_symref_extended(refs, b_head.buf, b_remote_head.buf,
+			"remote set-head", &b_local_head);
 
+	if (updateres == -2) {

Where does this -2 come from? It is not the "you asked to read it as a symref but it wasn't a symref" thing, which was mapped to -1 with [PATCH 3/9].

No, it is not, but it's also a mistake. It should be updateres == 1. refs_update_symref_extended outputs -1 for "not a symref" and 1 for any other error currently. Before I touched the code it was 1 for any error, so I left that as is. So we want to error out on set_head if we get a 1 and continue if we get 0 or -1 (and handle the difference in the report_set_head_auto).

Thanks for noticing, I'll get that fixed in v14.

#157 lore raw

Re: [PATCH v13 5/9] remote set-head: better output for --auto

Quoted 7 lines · Bence Ferdinandy

No, it is not, but it's also a mistake. It should be updateres == 1. refs_update_symref_extended outputs -1 for "not a symref" and 1 for any other error currently. Before I touched the code it was 1 for any error, so I left that as is. So we want to error out on set_head if we get a 1 and continue if we get 0 or -1 (and handle the difference in the report_set_head_auto).

Thanks for noticing, I'll get that fixed in v14.

It is good that somebody noticed it (and it may have happened to be me), but if it is a "mistake" as you said, I wonder why none of your tests caught it. Do we have a gap in test coverage?

Thanks.

#158 lore raw

Re: [PATCH v13 5/9] remote set-head: better output for --auto

Quoted 8 lines · Junio C Hamano
Quoted 6 lines · Bence Ferdinandy

No, it is not, but it's also a mistake. It should be updateres == 1. refs_update_symref_extended outputs -1 for "not a symref" and 1 for any other error currently. Before I touched the code it was 1 for any error, so I left that as is. So we want to error out on set_head if we get a 1 and continue if we get 0 or -1 (and handle the difference in the report_set_head_auto).

Quoted 5 lines
Quoted 1 line

Thanks for noticing, I'll get that fixed in v14.

It is good that somebody noticed it (and it may have happened to be me), but if it is a "mistake" as you said, I wonder why none of your tests caught it. Do we have a gap in test coverage?

I think there is no test that is testing this branch:

	updateres = refs_update_symref_extended(refs, b_head.buf, b_remote_head.buf,
			"remote set-head", &b_local_head, 0);
	if (updateres == 1) {
		result |= error(_("Could not setup %s"), b_head.buf);
		goto cleanup;

Running this in t/

grep -r "Could not setup"

also yield nothing, so that's probably true. I'm wondering what would be the best way to trigger this error, refs_update_symref needs to fail for this.

#159 lore raw

Re: [PATCH v13 5/9] remote set-head: better output for --auto

In reply to Junio C Hamano
Quoted 8 lines · Junio C Hamano
Quoted 6 lines · Bence Ferdinandy

No, it is not, but it's also a mistake. It should be updateres == 1. refs_update_symref_extended outputs -1 for "not a symref" and 1 for any other error currently. Before I touched the code it was 1 for any error, so I left that as is. So we want to error out on set_head if we get a 1 and continue if we get 0 or -1 (and handle the difference in the report_set_head_auto).

Quoted 7 lines
Quoted 1 line

Thanks for noticing, I'll get that fixed in v14.

It is good that somebody noticed it (and it may have happened to be me), but if it is a "mistake" as you said, I wonder why none of your tests caught it. Do we have a gap in test coverage?

Thanks.

I've been looking into how I can force an error on remote set-head to test the error branch. For the files backend it seems to be pretty easy

touch .git/refs/remotes/origin/HEAD.lock
git remote set-head [something]
rm .git/refs/remotes/origin/HEAD.lock

My problem is that in this case, since I want to force an error on a user command, I don't see a way that is possible with git commands and I'm not sure how I could manipulate the reftable file in a way that can be reversed and only errors out the particular thing I need.

I've been looking through the functions called here and it seems other errors all depend passing wrong parameters but that is not possible (and should not be possible) to trigger with remote set-head, so I think something "manual" needs to be done, like the above.

Since this particular test just wants to test what happens if refs_update_symref_extended returns with 1 and not testing correct behaviour of backends and such, would it be acceptable if this particular test case would check for the backend and if it is reftables it will just pass without actually checking and do the manually locking thing above for the files backend?

Thanks,
Bence

#160 lore raw

Re: [PATCH v13 5/9] remote set-head: better output for --auto

Quoted 5 lines · Bence Ferdinandy

Since this particular test just wants to test what happens if refs_update_symref_extended returns with 1 and not testing correct behaviour of backends and such, would it be acceptable if this particular test case would check for the backend and if it is reftables it will just pass without actually checking and do the manually locking thing above for the files backend?

I think we have some pre-made test prerequisite to skip tests unless run with a particular ref backend, exactly for that. Perhaps

test_expect_success REFFILES 'block update to check error' '
... manually block update and see how the operation ... errors out
'

or something?

#161 lore raw

PATCH v13 6/9 refs: add TRANSACTION_CREATE_EXISTS error

In reply to Bence Ferdinandy

Currently there is only one special error for transaction, for when there is a naming conflict, all other errors are dumped under a generic error. Add a new special error case for when the caller requests the reference to be updated only when it does not yet exist and the reference actually does exist.

---

Notes:
v4: new patch
v5: no change
v6: no change

v7: - change commit prefix to be more in line with project standards
- changed error checking to Karthik's suggestion

v8: no change

v9: - no change

v10: no change

v11: no change

v12: no change

v13: no change

 refs.h                  |  4 +++-
 refs/files-backend.c    | 24 ++++++++++++++++--------
 refs/reftable-backend.c |  6 ++++--
 3 files changed, 23 insertions(+), 11 deletions(-)
refs.h +3 -1
index 92622e807d..6a32f82f8c 100644
@@ -768,8 +768,10 @@ int ref_transaction_verify(struct ref_transaction *transaction,
 
 /* Naming conflict (for example, the ref names A and A/B conflict). */
 #define TRANSACTION_NAME_CONFLICT -1
+/* When only creation was requested, but the ref already exists. */
+#define TRANSACTION_CREATE_EXISTS -2
 /* All other errors. */
-#define TRANSACTION_GENERIC_ERROR -2
+#define TRANSACTION_GENERIC_ERROR -3
 
 /*
  * Perform the preparatory stages of committing `transaction`. Acquire
refs/files-backend.c +16 -8
index 81e650ec48..86674705a7 100644
@@ -2501,14 +2501,18 @@ static int split_symref_update(struct ref_update *update,
 static int check_old_oid(struct ref_update *update, struct object_id *oid,
 			 struct strbuf *err)
 {
+	int ret = TRANSACTION_GENERIC_ERROR;
+
 	if (!(update->flags & REF_HAVE_OLD) ||
 		   oideq(oid, &update->old_oid))
 		return 0;
 
-	if (is_null_oid(&update->old_oid))
+	if (is_null_oid(&update->old_oid)) {
 		strbuf_addf(err, "cannot lock ref '%s': "
 			    "reference already exists",
 			    ref_update_original_update_refname(update));
+		ret = TRANSACTION_CREATE_EXISTS;
+	}
 	else if (is_null_oid(oid))
 		strbuf_addf(err, "cannot lock ref '%s': "
 			    "reference is missing but expected %s",
@@ -2521,7 +2525,7 @@ static int check_old_oid(struct ref_update *update, struct object_id *oid,
 			    oid_to_hex(oid),
 			    oid_to_hex(&update->old_oid));
 
-	return -1;
+	return ret;
 }
 
 /*
@@ -2601,9 +2605,11 @@ static int lock_ref_for_update(struct files_ref_store *refs,
 					ret = TRANSACTION_GENERIC_ERROR;
 					goto out;
 				}
-			} else if  (check_old_oid(update, &lock->old_oid, err)) {
-				ret = TRANSACTION_GENERIC_ERROR;
-				goto out;
+			} else {
+				ret = check_old_oid(update, &lock->old_oid, err);
+				if  (ret) {
+					goto out;
+				}
 			}
 		} else {
 			/*
@@ -2634,9 +2640,11 @@ static int lock_ref_for_update(struct files_ref_store *refs,
 				    update->old_target);
 			ret = TRANSACTION_GENERIC_ERROR;
 			goto out;
-		} else if  (check_old_oid(update, &lock->old_oid, err)) {
-			ret = TRANSACTION_GENERIC_ERROR;
-			goto out;
+		} else {
+			ret = check_old_oid(update, &lock->old_oid, err);
+			if  (ret) {
+				goto out;
+			}
 		}
 
 		/*
refs/reftable-backend.c +4 -2
index 60cb83f23a..4f2e615eae 100644
@@ -1208,10 +1208,13 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store,
 				goto done;
 			}
 		} else if ((u->flags & REF_HAVE_OLD) && !oideq(&current_oid, &u->old_oid)) {
-			if (is_null_oid(&u->old_oid))
+			ret = TRANSACTION_NAME_CONFLICT;
+			if (is_null_oid(&u->old_oid)) {
 				strbuf_addf(err, _("cannot lock ref '%s': "
 						   "reference already exists"),
 					    ref_update_original_update_refname(u));
+				ret = TRANSACTION_CREATE_EXISTS;
+			}
 			else if (is_null_oid(&current_oid))
 				strbuf_addf(err, _("cannot lock ref '%s': "
 						   "reference is missing but expected %s"),
@@ -1223,7 +1226,6 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store,
 					    ref_update_original_update_refname(u),
 					    oid_to_hex(&current_oid),
 					    oid_to_hex(&u->old_oid));
-			ret = -1;
 			goto done;
 		}
 
Signature · 1 line
2.47.0.296.gda1ecfef29.dirty
#162 lore raw

PATCH v13 7/9 refs: add create_only option to refs_update_symref_extended

In reply to Bence Ferdinandy

Allow the caller to specify that it only wants to update the symref if it does not already exist. Silently ignore the error from the transaction API if the symref already exists.

---

Notes:
v4: new patch
v5: no change

v6: - switched from bool to int for create_only
- refactored logic in refs_update_symref with goto as layed out by Junio

v7: - change commit prefix to be more in line with project standards
- refactored code to accommodate changes in the first patch, but otherwise no change

v8: no change

v9: - no change (except for following through the
refs_update_symref_extended refactoring)

v10: no change

v11: no change

v12: no change

v13: changes only due to changes in previous patch

 builtin/remote.c |  2 +-
 refs.c           | 33 ++++++++++++++++++++++++---------
 refs.h           |  2 +-
 3 files changed, 26 insertions(+), 11 deletions(-)
builtin/remote.c +1 -1
index a682ef5df2..7b4eb0e30b 100644
@@ -1476,7 +1476,7 @@ static int set_head(int argc, const char **argv, const char *prefix)
 		goto cleanup;
 	}
 	updateres = refs_update_symref_extended(refs, b_head.buf, b_remote_head.buf,
-			"remote set-head", &b_local_head);
+			"remote set-head", &b_local_head, 0);
 	if (updateres == -2) {
 		result |= error(_("Could not setup %s"), b_head.buf);
 		goto cleanup;
refs.c +24 -9
index c4500a7582..f4d8a8dcb1 100644
@@ -2116,26 +2116,38 @@ int peel_iterated_oid(struct repository *r, const struct object_id *base, struct
 int refs_update_symref(struct ref_store *refs, const char *ref,
 		       const char *target, const char *logmsg)
 {
-	return refs_update_symref_extended(refs, ref, target, logmsg, NULL);
+	return refs_update_symref_extended(refs, ref, target, logmsg, NULL, 0);
 }
 
 int refs_update_symref_extended(struct ref_store *refs, const char *ref,
 		       const char *target, const char *logmsg,
-		       struct strbuf *referent)
+		       struct strbuf *referent, int create_only)
 {
 	struct ref_transaction *transaction;
 	struct strbuf err = STRBUF_INIT;
-	int ret = 0;
+	int ret = 0, prepret = 0;
 
 	transaction = ref_store_transaction_begin(refs, &err);
-	if (!transaction ||
-	    ref_transaction_update(transaction, ref, NULL, NULL,
-				   target, NULL, REF_NO_DEREF,
-				   logmsg, &err) ||
-	    ref_transaction_prepare(transaction, &err)) {
+	if (!transaction) {
+	error_return:
 		ret = error("%s", err.buf);
 		goto cleanup;
 	}
+	if (create_only) {
+		if (ref_transaction_create(transaction, ref, NULL, target,
+					   REF_NO_DEREF, logmsg, &err))
+			goto error_return;
+		prepret = ref_transaction_prepare(transaction, &err);
+		if (prepret && prepret != TRANSACTION_CREATE_EXISTS)
+			goto error_return;
+	} else {
+		if (ref_transaction_update(transaction, ref, NULL, NULL,
+					   target, NULL, REF_NO_DEREF,
+					   logmsg, &err) ||
+			ref_transaction_prepare(transaction, &err))
+			goto error_return;
+	}
+
 	if (referent && refs_read_symbolic_ref(refs, ref, referent) == -2) {
 		struct object_id oid;
 		if (!refs_read_ref(refs, ref, &oid)) {
@@ -2146,8 +2158,11 @@ int refs_update_symref_extended(struct ref_store *refs, const char *ref,
 		}
 	}
 
+	if (prepret == TRANSACTION_CREATE_EXISTS)
+		goto cleanup;
+
 	if (ref_transaction_commit(transaction, &err))
-		ret = error("%s", err.buf);
+		goto error_return;
 
 cleanup:
 	strbuf_release(&err);
refs.h +1 -1
index 6a32f82f8c..f131e89fe6 100644
@@ -581,7 +581,7 @@ int refs_update_symref(struct ref_store *refs, const char *refname,
 
 int refs_update_symref_extended(struct ref_store *refs, const char *refname,
 		       const char *target, const char *logmsg,
-		       struct strbuf *referent);
+		       struct strbuf *referent, int create_only);
 
 enum action_on_err {
 	UPDATE_REFS_MSG_ON_ERR,
Signature · 1 line
2.47.0.296.gda1ecfef29.dirty
#163 lore raw

Re: [PATCH v13 7/9] refs: add create_only option to refs_update_symref_extended

Quoted 3 lines · Bence Ferdinandy

Allow the caller to specify that it only wants to update the symref if it does not already exist. Silently ignore the error from the transaction API if the symref already exists.

Because it is not even an error, from the point of view of "create only if it does not exist", checking the current condition and finding that there already is one, this makes perfect sense.

#164 lore raw

PATCH v13 8/9 fetch: set remote/HEAD if it does not exist

In reply to Bence Ferdinandy

If the user has remote/HEAD set already and it looks like it has changed on the server, then print a message, otherwise set it if we can. Silently pass if the user already has the same remote/HEAD set as reported by the server or if we encounter any errors along the way.

---

Notes:
v3: - does not rely on remote set-head anymore so it only authenticates
once
- uses the new REF_CREATE_ONLY to atomically check if the ref exists and only write it if it doesn't
- in all other cases the maximum it does is print a warning

v4: - instead of the discarded REF_CREATE_ONLY, it uses the existing,
but updated transaction api to request a silent create only
- it now uses the atomic before_target to determine reporting
- refactored for legibility

v5: - instead of printing a not too useful message, it now fails
silently, this in line with the objective to only set up remote/HEAD automatically if the right thing is trivial, for everything else there is remote set-head
- fixed all failing tests
- added two new tests, one for checking if remote/HEAD is set to the correct one, and one to test that we do not override remote/HEAD if it has changed on the server from what we have locally

v6: - fixed style issues and unintended extra empty line
- updated function call with bool to int from previous patch's change
- removed calls to error(...) inherited from builtin/remote.c so we actually fail silently
- set the test for remote set-head --auto to the correct value here, which was previously erronously set in the remote set-head patch

v7: - no change

v8: - changed logmsg in call to refs_update_symref from "remote
set-head" to "fetch"

v9: - follow through with refs_update_symref_extended
- fix test errors uncovered by the new patch

v10: no change

v11: fixed some memory leaks

v12: no change

v13: - fix printed information if the local HEAD is detached
- remove accidental formatting noise in a test

 builtin/fetch.c                  | 99 ++++++++++++++++++++++++++++++++
 t/t4207-log-decoration-colors.sh |  3 +-
 t/t5505-remote.sh                | 21 +++++--
 t/t5510-fetch.sh                 | 24 ++++++++
 t/t5512-ls-remote.sh             |  2 +
 t/t5514-fetch-multiple.sh        | 17 +++++-
 t/t5516-fetch-push.sh            |  3 +-
 t/t5527-fetch-odd-refs.sh        |  3 +-
 t/t7900-maintenance.sh           |  3 +-
 t/t9210-scalar.sh                |  5 +-
 t/t9211-scalar-clone.sh          |  6 +-
 t/t9902-completion.sh            | 65 +++++++++++++++++++++
 12 files changed, 234 insertions(+), 17 deletions(-)
builtin/fetch.c +99 -0
index 18eff4e5fa..3d70cd1add 100644
@@ -1578,6 +1578,97 @@ static int backfill_tags(struct display_state *display_state,
 	return retcode;
 }
 
+static void report_set_head(const char *remote, const char *head_name,
+			struct strbuf *buf_prev, int updateres) {
+	struct strbuf buf_prefix = STRBUF_INIT;
+	const char *prev_head = NULL;
+
+	strbuf_addf(&buf_prefix, "refs/remotes/%s/", remote);
+	skip_prefix(buf_prev->buf, buf_prefix.buf, &prev_head);
+
+	if (prev_head && strcmp(prev_head, head_name)) {
+		printf("'HEAD' at '%s' has changed from '%s' to '%s'\n",
+			remote, prev_head, head_name);
+		printf("Run 'git remote set-head %s %s' to follow the change.\n",
+			remote, head_name);
+	}
+	else if (!!updateres && buf_prev->len) {
+		printf("detached 'HEAD' at '%s' has changed from '%s' to '%s'\n",
+			remote, buf_prev->buf, head_name);
+		printf("Run 'git remote set-head %s %s' to follow the change.\n",
+			remote, head_name);
+	}
+	strbuf_release(&buf_prefix);
+}
+
+static const char *strip_refshead(const char *name){
+	skip_prefix(name, "refs/heads/", &name);
+	return name;
+}
+
+static int set_head(const struct ref *remote_refs)
+{
+	int result = 0, updateres;
+	struct strbuf b_head = STRBUF_INIT, b_remote_head = STRBUF_INIT,
+		b_local_head = STRBUF_INIT;
+	const char *remote = gtransport->remote->name;
+	char *head_name = NULL;
+	struct ref *ref, *matches;
+	struct ref *fetch_map = NULL, **fetch_map_tail = &fetch_map;
+	struct refspec_item refspec = {
+		.force = 0,
+		.pattern = 1,
+		.src = (char *) "refs/heads/*",
+		.dst = (char *) "refs/heads/*",
+	};
+	struct string_list heads = STRING_LIST_INIT_DUP;
+	struct ref_store *refs = get_main_ref_store(the_repository);
+
+	get_fetch_map(remote_refs, &refspec, &fetch_map_tail, 0);
+	matches = guess_remote_head(find_ref_by_name(remote_refs, "HEAD"),
+				    fetch_map, 1);
+	for (ref = matches; ref; ref = ref->next) {
+		string_list_append(&heads, strip_refshead(ref->name));
+	}
+
+
+	if (!heads.nr)
+		result = 1;
+	else if (heads.nr > 1)
+		result = 1;
+	else
+		head_name = xstrdup(heads.items[0].string);
+
+	if (!head_name)
+		goto cleanup;
+	strbuf_addf(&b_head, "refs/remotes/%s/HEAD", remote);
+	strbuf_addf(&b_remote_head, "refs/remotes/%s/%s", remote, head_name);
+		/* make sure it's valid */
+	if (!refs_ref_exists(refs, b_remote_head.buf)) {
+		result = 1;
+		goto cleanup;
+	}
+	updateres = refs_update_symref_extended(refs, b_head.buf, b_remote_head.buf,
+					"fetch", &b_local_head, 1);
+	if (updateres == -1) {
+		result = 1;
+		goto cleanup;
+	}
+	report_set_head(remote, head_name, &b_local_head, updateres);
+
+
+
+cleanup:
+	free(head_name);
+	free_refs(fetch_map);
+	free_refs(matches);
+	string_list_clear(&heads, 0);
+	strbuf_release(&b_head);
+	strbuf_release(&b_local_head);
+	strbuf_release(&b_remote_head);
+	return result;
+}
+
 static int do_fetch(struct transport *transport,
 		    struct refspec *rs,
 		    const struct fetch_config *config)
@@ -1647,6 +1738,8 @@ static int do_fetch(struct transport *transport,
 				    "refs/tags/");
 	}
 
+	strvec_push(&transport_ls_refs_options.ref_prefixes, "HEAD");
+
 	if (must_list_refs) {
 		trace2_region_enter("fetch", "remote_refs", the_repository);
 		remote_refs = transport_get_remote_refs(transport,
@@ -1791,6 +1884,12 @@ static int do_fetch(struct transport *transport,
 				  "you need to specify exactly one branch with the --set-upstream option"));
 		}
 	}
+	if (set_head(remote_refs))
+		;
+		/*
+		 * Way too many cases where this can go wrong
+		 * so let's just fail silently for now.
+		 */
 
 cleanup:
 	if (retcode) {
t/t4207-log-decoration-colors.sh +2 -1
index 73ea9e5155..d55d22cb2f 100755
@@ -59,7 +59,8 @@ ${c_reset}${c_tag}tag: ${c_reset}${c_tag}v1.0${c_reset}${c_commit}, \
 ${c_reset}${c_tag}tag: ${c_reset}${c_tag}B${c_reset}${c_commit})${c_reset} B
 ${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_reset}\
 ${c_tag}tag: ${c_reset}${c_tag}A1${c_reset}${c_commit}, \
-${c_reset}${c_remoteBranch}other/main${c_reset}${c_commit})${c_reset} A1
+${c_reset}${c_remoteBranch}other/main${c_reset}${c_commit}, \
+${c_reset}${c_remoteBranch}other/HEAD${c_reset}${c_commit})${c_reset} A1
 	${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_reset}\
 ${c_stash}refs/stash${c_reset}${c_commit})${c_reset} On main: Changes to A.t
 	${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_reset}\
t/t5505-remote.sh +15 -6
index 807df00ba7..660310239c 100755
@@ -74,7 +74,7 @@ test_expect_success 'add another remote' '
 		cd test &&
 		git remote add -f second ../two &&
 		tokens_match "origin second" "$(git remote)" &&
-		check_tracking_branch second main side another &&
+		check_tracking_branch second main side another HEAD &&
 		git for-each-ref "--format=%(refname)" refs/remotes |
 		sed -e "/^refs\/remotes\/origin\//d" \
 		    -e "/^refs\/remotes\/second\//d" >actual &&
@@ -488,7 +488,7 @@ test_expect_success 'set-head --auto has no problem w/multiple HEADs' '
 		cd test &&
 		git fetch two "refs/heads/*:refs/remotes/two/*" &&
 		git remote set-head --auto two >output 2>&1 &&
-		echo "${SQ}two/HEAD${SQ} is now created and points to ${SQ}main${SQ}" >expect &&
+		echo "${SQ}two/HEAD${SQ} is unchanged and points to ${SQ}main${SQ}" >expect &&
 		test_cmp expect output
 	)
 '
@@ -776,8 +776,10 @@ test_expect_success 'reject --no-no-tags' '
 '
 
 cat >one/expect <<\EOF
+  apis/HEAD -> apis/main
   apis/main
   apis/side
+  drosophila/HEAD -> drosophila/main
   drosophila/another
   drosophila/main
   drosophila/side
@@ -795,11 +797,14 @@ test_expect_success 'update' '
 '
 
 cat >one/expect <<\EOF
+  drosophila/HEAD -> drosophila/main
   drosophila/another
   drosophila/main
   drosophila/side
+  manduca/HEAD -> manduca/main
   manduca/main
   manduca/side
+  megaloprepus/HEAD -> megaloprepus/main
   megaloprepus/main
   megaloprepus/side
 EOF
@@ -807,7 +812,7 @@ EOF
 test_expect_success 'update with arguments' '
 	(
 		cd one &&
-		for b in $(git branch -r)
+		for b in $(git branch -r | grep -v HEAD)
 		do
 		git branch -r -d $b || exit 1
 		done &&
@@ -839,10 +844,13 @@ test_expect_success 'update --prune' '
 '
 
 cat >one/expect <<-\EOF
+  apis/HEAD -> apis/main
   apis/main
   apis/side
+  manduca/HEAD -> manduca/main
   manduca/main
   manduca/side
+  megaloprepus/HEAD -> megaloprepus/main
   megaloprepus/main
   megaloprepus/side
 EOF
@@ -850,7 +858,7 @@ EOF
 test_expect_success 'update default' '
 	(
 		cd one &&
-		for b in $(git branch -r)
+		for b in $(git branch -r | grep -v HEAD)
 		do
 		git branch -r -d $b || exit 1
 		done &&
@@ -862,6 +870,7 @@ test_expect_success 'update default' '
 '
 
 cat >one/expect <<\EOF
+  drosophila/HEAD -> drosophila/main
   drosophila/another
   drosophila/main
   drosophila/side
@@ -870,7 +879,7 @@ EOF
 test_expect_success 'update default (overridden, with funny whitespace)' '
 	(
 		cd one &&
-		for b in $(git branch -r)
+		for b in $(git branch -r | grep -v HEAD)
 		do
 		git branch -r -d $b || exit 1
 		done &&
@@ -884,7 +893,7 @@ test_expect_success 'update default (overridden, with funny whitespace)' '
 test_expect_success 'update (with remotes.default defined)' '
 	(
 		cd one &&
-		for b in $(git branch -r)
+		for b in $(git branch -r | grep -v HEAD)
 		do
 		git branch -r -d $b || exit 1
 		done &&
t/t5510-fetch.sh +24 -0
index 0890b9f61c..87698341f5 100755
@@ -75,6 +75,30 @@ test_expect_success "fetch test for-merge" '
 	cut -f -2 .git/FETCH_HEAD >actual &&
 	test_cmp expected actual'
 
+test_expect_success "fetch test remote HEAD" '
+	cd "$D" &&
+	cd two &&
+	git fetch &&
+	git rev-parse --verify refs/remotes/origin/HEAD &&
+	git rev-parse --verify refs/remotes/origin/main &&
+	head=$(git rev-parse refs/remotes/origin/HEAD) &&
+	branch=$(git rev-parse refs/remotes/origin/main) &&
+	test "z$head" = "z$branch"'
+
+test_expect_success "fetch test remote HEAD change" '
+	cd "$D" &&
+	cd two &&
+	git switch -c other &&
+	git push -u origin other &&
+	git rev-parse --verify refs/remotes/origin/HEAD &&
+	git rev-parse --verify refs/remotes/origin/main &&
+	git rev-parse --verify refs/remotes/origin/other &&
+	git remote set-head origin other &&
+	git fetch &&
+	head=$(git rev-parse refs/remotes/origin/HEAD) &&
+	branch=$(git rev-parse refs/remotes/origin/other) &&
+	test "z$head" = "z$branch"'
+
 test_expect_success 'fetch --prune on its own works as expected' '
 	cd "$D" &&
 	git clone . prune &&
t/t5512-ls-remote.sh +2 -0
index 64b3491e4e..1b3865e154 100755
@@ -293,6 +293,8 @@ test_expect_success 'ls-remote with filtered symref (refname)' '
 	cat >expect <<-EOF &&
 	ref: refs/heads/main	HEAD
 	$rev	HEAD
+	ref: refs/remotes/origin/main	refs/remotes/origin/HEAD
+	$rev	refs/remotes/origin/HEAD
 	EOF
 	git ls-remote --symref . HEAD >actual &&
 	test_cmp expect actual
t/t5514-fetch-multiple.sh +15 -2
index 579872c258..e3482b27b2 100755
@@ -45,14 +45,17 @@ test_expect_success setup '
 '
 
 cat > test/expect << EOF
+  one/HEAD -> one/main
   one/main
   one/side
   origin/HEAD -> origin/main
   origin/main
   origin/side
+  three/HEAD -> three/main
   three/another
   three/main
   three/side
+  two/HEAD -> two/main
   two/another
   two/main
   two/side
@@ -97,6 +100,7 @@ cat > expect << EOF
   origin/HEAD -> origin/main
   origin/main
   origin/side
+  three/HEAD -> three/main
   three/another
   three/main
   three/side
@@ -112,8 +116,10 @@ test_expect_success 'git fetch --multiple (but only one remote)' '
 '
 
 cat > expect << EOF
+  one/HEAD -> one/main
   one/main
   one/side
+  two/HEAD -> two/main
   two/another
   two/main
   two/side
@@ -141,7 +147,7 @@ test_expect_success 'git fetch --multiple (bad remote names)' '
 
 test_expect_success 'git fetch --all (skipFetchAll)' '
 	(cd test4 &&
-	 for b in $(git branch -r)
+	 for b in $(git branch -r | grep -v HEAD)
 	 do
 		git branch -r -d $b || exit 1
 	 done &&
@@ -153,11 +159,14 @@ test_expect_success 'git fetch --all (skipFetchAll)' '
 '
 
 cat > expect << EOF
+  one/HEAD -> one/main
   one/main
   one/side
+  three/HEAD -> three/main
   three/another
   three/main
   three/side
+  two/HEAD -> two/main
   two/another
   two/main
   two/side
@@ -165,7 +174,7 @@ EOF
 
 test_expect_success 'git fetch --multiple (ignoring skipFetchAll)' '
 	(cd test4 &&
-	 for b in $(git branch -r)
+	 for b in $(git branch -r | grep -v HEAD)
 	 do
 		git branch -r -d $b || exit 1
 	 done &&
@@ -221,14 +230,17 @@ test_expect_success 'git fetch --multiple --jobs=0 picks a default' '
 
 create_fetch_all_expect () {
 	cat >expect <<-\EOF
+	  one/HEAD -> one/main
 	  one/main
 	  one/side
 	  origin/HEAD -> origin/main
 	  origin/main
 	  origin/side
+	  three/HEAD -> three/main
 	  three/another
 	  three/main
 	  three/side
+	  two/HEAD -> two/main
 	  two/another
 	  two/main
 	  two/side
@@ -265,6 +277,7 @@ test_expect_success 'git fetch (fetch all remotes with fetch.all = true)' '
 
 create_fetch_one_expect () {
 	cat >expect <<-\EOF
+	  one/HEAD -> one/main
 	  one/main
 	  one/side
 	  origin/HEAD -> origin/main
t/t5516-fetch-push.sh +2 -1
index 331778bd42..5a051aa0c7 100755
@@ -1395,7 +1395,8 @@ test_expect_success 'fetch follows tags by default' '
 		git tag -m "annotated" tag &&
 		git for-each-ref >tmp1 &&
 		sed -n "p; s|refs/heads/main$|refs/remotes/origin/main|p" tmp1 |
-		sort -k 3 >../expect
+		sed -n "p; s|refs/heads/main$|refs/remotes/origin/HEAD|p"  |
+		sort -k 4 >../expect
 	) &&
 	test_when_finished "rm -rf dst" &&
 	git init dst &&
t/t5527-fetch-odd-refs.sh +2 -1
index 98ece27c6a..d3996af6ee 100755
@@ -52,7 +52,8 @@ test_expect_success LONG_REF 'fetch handles extremely long refname' '
 	long
 	main
 	EOF
-	git for-each-ref --format="%(subject)" refs/remotes/long >actual &&
+	git for-each-ref --format="%(subject)" refs/remotes/long \
+		--exclude=refs/remotes/long/HEAD >actual &&
 	test_cmp expect actual
 '
 
t/t7900-maintenance.sh +2 -1
index c224c8450c..edb85b7145 100755
@@ -329,7 +329,8 @@ test_expect_success 'incremental-repack task' '
 
 	# Delete refs that have not been repacked in these packs.
 	git for-each-ref --format="delete %(refname)" \
-		refs/prefetch refs/tags refs/remotes >refs &&
+		refs/prefetch refs/tags refs/remotes \
+		--exclude=refs/remotes/*/HEAD >refs &&
 	git update-ref --stdin <refs &&
 
 	# Replace the object directory with this pack layout.
t/t9210-scalar.sh +3 -2
index a30b2c9f70..2237844550 100755
@@ -151,7 +151,8 @@ test_expect_success 'scalar clone' '
 			"$(pwd)" &&
 
 		git for-each-ref --format="%(refname)" refs/remotes/origin/ >actual &&
-		echo "refs/remotes/origin/parallel" >expect &&
+		echo "refs/remotes/origin/HEAD" >>expect &&
+		echo "refs/remotes/origin/parallel" >>expect &&
 		test_cmp expect actual &&
 
 		test_path_is_missing 1/2 &&
@@ -220,7 +221,7 @@ test_expect_success 'scalar reconfigure --all with includeIf.onbranch' '
 	done
 '
 
- test_expect_success 'scalar reconfigure --all with detached HEADs' '
+test_expect_success 'scalar reconfigure --all with detached HEADs' '
 	repos="two three four" &&
 	for num in $repos
 	do
t/t9211-scalar-clone.sh +3 -3
index c16ea67c1d..d9cb6b9a3e 100755
@@ -32,7 +32,7 @@ test_expect_success 'set up repository to clone' '
 	)
 '
 
-cleanup_clone () {
+cleanup_clone() {
 	rm -rf "$1"
 }
 
@@ -128,7 +128,7 @@ test_expect_success '--single-branch clones HEAD only' '
 	(
 		cd $enlistment/src &&
 		git for-each-ref refs/remotes/origin >out &&
-		test_line_count = 1 out &&
+		test_line_count = 2 out &&
 		grep "refs/remotes/origin/base" out
 	) &&
 
@@ -142,7 +142,7 @@ test_expect_success '--no-single-branch clones all branches' '
 	(
 		cd $enlistment/src &&
 		git for-each-ref refs/remotes/origin >out &&
-		test_line_count = 2 out &&
+		test_line_count = 3 out &&
 		grep "refs/remotes/origin/base" out &&
 		grep "refs/remotes/origin/parallel" out
 	) &&
t/t9902-completion.sh +65 -0

443 lines not on this page · raw message

Signature · 1 line
2.47.0.296.gda1ecfef29.dirty
#165 lore raw

Re: [PATCH v13 8/9] fetch: set remote/HEAD if it does not exist

Quoted 2 lines · Bence Ferdinandy

If the user has remote/HEAD set already and it looks like it has changed on the server, then print a message, otherwise set it if we can.

The user uses remote/origin/HEAD to point at the "primary thing the user is interested in at the remote 'origin'". "git clone" propagates remote's HEAD to local because their HEAD is the opinion of the remote which branch they want the users to consider the primary thing. But the user can have valid reasons to consider a branch different from what the remote suggests as the primary thing, and an explicit "set-head" is a way to do so.

After such a set-up is made and the user is perfectly happy, would it make sense to repeatedly remind that their HEAD points at something the user is not interested in?

Perhaps it may make sense when given the "--verbose" option, or after the first time the difference was noticed, but otherwise, I suspect it becomes annoying to those who keep them deliberately different from each other.

I wonder if it is more sensible to automate for these two different classes of users with a new configuration per remote.

- When "git clone" initially sets up, or "git remote set-head --auto" matches the local to the remote, we set the new "remote.$name.autoUpdateHEAD" configuration variable to true.

- When "git remote set-head" explicitly sets remote/$name/HEAD to some value (if we can detect the case where it is different from what the remote has, that would be a plus, but it incurrs network traffic, so care must be taken to design this part), we drop the "remote.$name.autoUpdateHEAD" configuration variable, if and only if it is set to true.

- When remote.$name.autoUpdateHEAD configuration is true, "git fetch" automatically matches remote/$name/HEAD to what the HEAD at the remote points at. If remote.$name.autoUpdateHEAD is set to some other value (e.g., "warn"), show the warning so that the user can choose to do "git remote set-head" to a specific value (which would not drop the remote.$name.autoUpdateHEAD set to "warn") or "git remote set-head --auto" (which would start to follow whatever remote uses).

or something like that?

With something like the above, those who just want to follow along would get remote.$name.autoUpdateHEAD=true and silently follow along, while those who want to explicitly control would be able to do so. I dunno, but I am fairly negative on a persistent warning message that tells the user that the only normal case is to have remote/origin/HEAD point at the same branch the remote's HEAD points at.

Thanks.

#166 lore raw

Re: [PATCH v13 8/9] fetch: set remote/HEAD if it does not exist

Quoted 17 lines · Junio C Hamano
Quoted 2 lines · Bence Ferdinandy

If the user has remote/HEAD set already and it looks like it has changed on the server, then print a message, otherwise set it if we can.

The user uses remote/origin/HEAD to point at the "primary thing the user is interested in at the remote 'origin'". "git clone" propagates remote's HEAD to local because their HEAD is the opinion of the remote which branch they want the users to consider the primary thing. But the user can have valid reasons to consider a branch different from what the remote suggests as the primary thing, and an explicit "set-head" is a way to do so.

After such a set-up is made and the user is perfectly happy, would it make sense to repeatedly remind that their HEAD points at something the user is not interested in?

Quoted 7 lines

Perhaps it may make sense when given the "--verbose" option, or after the first time the difference was noticed, but otherwise, I suspect it becomes annoying to those who keep them deliberately different from each other.

I wonder if it is more sensible to automate for these two different classes of users with a new configuration per remote.

Since this discussion reaches a bit back I looked up the first message where the current behaviour was discussed, and where in the end it was said we don't need configuration (the very first implementation always updated HEAD). Starting here:

https://lore.kernel.org/git/[email protected]/

That being said:

Your suggestion seems to be the original tri-state configuration that came up in one of the original discussions. It was recently requested again for fetch to just do this automatically
(https://lore.kernel.org/git/CAAHKNRGv19rhnqCkJMpE2FomNQBHvSS36aC=fh0UwO+9-6RRfA@mail.gmail.com),
so at least some people would like to have this fully automated.

Quoted 4 lines

- When "git clone" initially sets up, or "git remote set-head --auto" matches the local to the remote, we set the new "remote.$name.autoUpdateHEAD" configuration variable to true.

I think this is a good default. -> Coming back here I think "warn" is a better default see below.

Quoted 7 lines

- When "git remote set-head" explicitly sets remote/$name/HEAD to some value (if we can detect the case where it is different from what the remote has, that would be a plus, but it incurrs network traffic, so care must be taken to design this part), we drop the "remote.$name.autoUpdateHEAD" configuration variable, if and only if it is set to true.

I also agree with this. I don't think it would be necessary to do a network check at this point. If somebody is deliberately changing the remote head manually, they are probably aware of the current situation and can always set their configuration to "warn" if they care, or just check manually once in a while. At least I feel needing network connection to do this would be more a hindrance than useful.

Quoted 9 lines

- When remote.$name.autoUpdateHEAD configuration is true, "git fetch" automatically matches remote/$name/HEAD to what the HEAD at the remote points at. If remote.$name.autoUpdateHEAD is set to some other value (e.g., "warn"), show the warning so that the user can choose to do "git remote set-head" to a specific value (which would not drop the remote.$name.autoUpdateHEAD set to "warn") or "git remote set-head --auto" (which would start to follow whatever remote uses).

Make sense.

One thing you haven't mentioned is when you start locally with git init && git remote add && git fetch. But since this whole series started by wanting to make this the same as cloning I assume it should be "true" as well. In fact, if the default behaviour is true this would just work.

On the other hand: considering your points in the linked very early discussion, maybe "warn" would be a better default? I'm not sure. Most people would not see warnings anyway since I doubt a) many users change their remote/HEAD manually or b) many projects change their HEAD at all or if they do, more than once. Gaving "true" as default would change the current behaviour with silently updating existing remote/HEADs. The warning could also tell them that remote set-head --auto will set this to automatic in the future as well and thus remove any warnings or that if they know what they want the can set it to false.

Quoted 8 lines

With something like the above, those who just want to follow along would get remote.$name.autoUpdateHEAD=true and silently follow along, while those who want to explicitly control would be able to do so. I dunno, but I am fairly negative on a persistent warning message that tells the user that the only normal case is to have remote/origin/HEAD point at the same branch the remote's HEAD points at.

Fair point.

This series has a tendency for growing in complication in each iteration :)

Best,
Bence

#167 lore raw

Re: [PATCH v13 8/9] fetch: set remote/HEAD if it does not exist

Quoted 5 lines · Bence Ferdinandy

Your suggestion seems to be the original tri-state configuration that came up in one of the original discussions. It was recently requested again for fetch to just do this automatically
(https://lore.kernel.org/git/CAAHKNRGv19rhnqCkJMpE2FomNQBHvSS36aC=fh0UwO+9-6RRfA@mail.gmail.com),
so at least some people would like to have this fully automated.

And that is *not* the only option, and I am trying to help others by preventing unconditional warning messages from annoying them.

Quoted 2 lines

On the other hand: considering your points in the linked very early discussion, maybe "warn" would be a better default?

Those who want to stick to their manual choice would have to do an explicit set-head once, whether the default is "true" or "warn", so their inconvenience would not affect the choice of the default.

I think "warn" is a good default _only_ for folks who want to verify and then expect they will follow what the upstream does 99% of the time. For them, when they do not want to switch to what the upstream says (which is the rest 1%), there is no good way to squelch the warning other than dropping the configuration, but then they will lose the ability to notice further flips at the remote after that, which may turn out to be a problem.

Instead of "true", "warn", and "false", we might need "if it is set to _this_ value, do not warn and keep the current setting" to help those who cautiously only allow the remote affect what they use after inspecting, but expect that the choice made by the remote will not be outrageous most of the time for them.

If we want to solve this completely for them, that is. I dunno.

Those who want to blindly follow what the upstream says 100% of the time will have to switch to "true" just once manually, if the default were "warn", but once may be one time too many for them.

And that is why I said "clone" (and "fetch" setting a non-existing HEAD automatically) may want to default to "true".

Quoted 3 lines

Fair point.

This series has a tendency for growing in complication in each iteration :)

Anytime somebody gets swayed by an opinion to add unconditional warning or unconditional futzing without considering that such a move may annoy some users that are different from the opinion giver, we need to make sure there are escape hatches. Under the assumption that most projects only flip HEAD very rarely, and considering the fact that we have lived with manual setting with set-head and nothing else, we could drop the automatic setting by "fetch" altogether to reduce the scope of the series without losing much *and* more importantly without harming existing users.

I think we could make it a multi-step transition in order to keep changes to "git fetch" we make in each step smaller, which may go like so:

1) Let "git fetch" create missing remotes/$name/HEAD. No other changes.

2) Introduce a remote.$name.followRemoteHEAD that can say "the last branch we observed that they point at is 'main'". Set it when we clone, When "git fetch" notices that the remote flipped its HEAD to a different value, warn and update (which would give us the "warn only once, do not annoy users by constantly warning"). The user can delete the configuration to squelch "git fetch" (in other words, the repository does not remember the last setting, so we refrain from warning "it has changed since the last time"). No other changes.

3) Allow remote.$name.followRemoteHEAD to be set to

3-a) "always", which would squelch the warning and causes "git fetch" to always repoint the local remotes/$name/HEAD to whatever the remote says,

3-b) "warn", which would cause "git fetch" to warn when the remote's HEAD is different from local remotes/$name/HEAD,

3-c) "never", which would cause "git fetch" to even omit the "copy if it does not exist" we add in 1) to help those who deliberately want to keep remotes/$name/HEAD missing (to force themselves to be more explicit, saying things like "git checkout -b topic origin/maint" and "git rebase origin/master", but never "git checkout --detach origin").

And we can stop this round at the first step (plus any other changes outside "git fetch", like "set-head --auto").

Thanks.

#168 lore raw

Re: [PATCH v13 8/9] fetch: set remote/HEAD if it does not exist

Quoted 10 lines · Junio C Hamano
Quoted 5 lines · Bence Ferdinandy

Your suggestion seems to be the original tri-state configuration that came up in one of the original discussions. It was recently requested again for fetch to just do this automatically
(https://lore.kernel.org/git/CAAHKNRGv19rhnqCkJMpE2FomNQBHvSS36aC=fh0UwO+9-6RRfA@mail.gmail.com),
so at least some people would like to have this fully automated.

And that is *not* the only option, and I am trying to help others by preventing unconditional warning messages from annoying them.

By the way, because I've left it out from my messages, some folks who are reading from sideways might not realize this, so let's make it a bit more explicit. One thing to note is that the assumption that HEAD at the remote rarely changes _only_ holds for forges and the like, a typically bare repository used for publishing without a working tree.

If you have a clone from a repository with a working tree (this happens when you have two places to work, either on two machines or two separate repositories on a same machine) to allow you to work here during the day and work there after work, the HEAD at the clone source may change to the branch you happen to be working on when left there. In such a setting, it is reasonable to make the repositories aware of each other (i.e. the after-work repository may have refs/remotes/at-work/* remote-tracking branches while the at-work repository keeps refs/remotes/after-work/* remote-tracking branches, to keep track of each other), and "git fetch" that warns every time it notices the remote end has HEAD pointing at a different branch would be annoying, as they would likely be working on different "project" when they leave after work.

Users who fall outside of a typical "we have a central repository and the only interaction with the repository is we clone and fetch/pull from it and nothing else" hosting site setting, are why I'll encourage folks to tread carefully when they are tempted to make anything unconditional.

Thanks.

#169 lore raw

Re: [PATCH v13 8/9] fetch: set remote/HEAD if it does not exist

In reply to Junio C Hamano
Quoted 26 lines · Junio C Hamano
Quoted 5 lines · Bence Ferdinandy

Your suggestion seems to be the original tri-state configuration that came up in one of the original discussions. It was recently requested again for fetch to just do this automatically
(https://lore.kernel.org/git/CAAHKNRGv19rhnqCkJMpE2FomNQBHvSS36aC=fh0UwO+9-6RRfA@mail.gmail.com),
so at least some people would like to have this fully automated.

And that is *not* the only option, and I am trying to help others by preventing unconditional warning messages from annoying them.

Quoted 2 lines

On the other hand: considering your points in the linked very early discussion, maybe "warn" would be a better default?

Those who want to stick to their manual choice would have to do an explicit set-head once, whether the default is "true" or "warn", so their inconvenience would not affect the choice of the default.

I think "warn" is a good default _only_ for folks who want to verify and then expect they will follow what the upstream does 99% of the time. For them, when they do not want to switch to what the upstream says (which is the rest 1%), there is no good way to squelch the warning other than dropping the configuration, but then they will lose the ability to notice further flips at the remote after that, which may turn out to be a problem.

Quoted 7 lines

Instead of "true", "warn", and "false", we might need "if it is set to _this_ value, do not warn and keep the current setting" to help those who cautiously only allow the remote affect what they use after inspecting, but expect that the choice made by the remote will not be outrageous most of the time for them.

If we want to solve this completely for them, that is. I dunno.

I initially thought you meant a fourth setting here, but what you are suggesting here is that in case the user has "remote.origin.followremotehead = never" set even in case of "init && remote add && fetch" HEAD should not be created, which is a bit different, because originally we discussed only not changing after the initial creation. This makes a lot of sense.

Quoted 7 lines

Those who want to blindly follow what the upstream says 100% of the time will have to switch to "true" just once manually, if the default were "warn", but once may be one time too many for them.

And that is why I said "clone" (and "fetch" setting a non-existing HEAD automatically) may want to default to "true".

Fair point again.

Quoted 5 lines
Quoted 4 lines

Fair point.

This series has a tendency for growing in complication in each iteration :)

Quoted 9 lines

Anytime somebody gets swayed by an opinion to add unconditional warning or unconditional futzing without considering that such a move may annoy some users that are different from the opinion giver, we need to make sure there are escape hatches. Under the assumption that most projects only flip HEAD very rarely, and considering the fact that we have lived with manual setting with set-head and nothing else, we could drop the automatic setting by "fetch" altogether to reduce the scope of the series without losing much *and* more importantly without harming existing users.

Don't get me wrong, I'd like to do the right thing here, and appreciate that my perspective is rather limited. I was just reflecting on the fact that the first "working" version of the series was only two patches. Writing this is fun and educational for me.

Quoted 4 lines

I think we could make it a multi-step transition in order to keep changes to "git fetch" we make in each step smaller, which may go like so:

That being said, if it's possible to graduate a part of the series when it's ready and where it makes sense to split, that would be welcome. As you write below the first such place would be remote set-head --auto. Not that that is ready yet, I need to fix the mistake you spotted, update a commit message, and add a test-coverage. But in that case I might send a v14 with just these updates and work on getting the fetch settings in parallel for a v15. Technically I would just need to build the series off next for a while, right?

Quoted 3 lines

1) Let "git fetch" create missing remotes/$name/HEAD. No other changes.

So this is a bit simpler version of what we have now (as long as it doesn't bother for example you, that there is a state which will create HEAD without being able to disable it :)) Should this even print anything? Probably not, clone also doesn't tell you that the HEAD was set.

Quoted 11 lines

2) Introduce a remote.$name.followRemoteHEAD that can say "the last branch we observed that they point at is 'main'". Set it when we clone, When "git fetch" notices that the remote flipped its HEAD to a different value, warn and update (which would give us the "warn only once, do not annoy users by constantly warning"). The user can delete the configuration to squelch "git fetch" (in other words, the repository does not remember the last setting, so we refrain from warning "it has changed since the last time"). No other changes.

Quoted 5 lines

3) Allow remote.$name.followRemoteHEAD to be set to

3-a) "always", which would squelch the warning and causes "git fetch" to always repoint the local remotes/$name/HEAD to whatever the remote says,

But this should print similar messages as remote set-head --auto when there is an actual change done, right? And should probably respect --quiet?

Quoted 3 lines

3-b) "warn", which would cause "git fetch" to warn when the remote's HEAD is different from local remotes/$name/HEAD,

This should also respect --quite I assume.

Quoted 7 lines

3-c) "never", which would cause "git fetch" to even omit the "copy if it does not exist" we add in 1) to help those who deliberately want to keep remotes/$name/HEAD missing (to force themselves to be more explicit, saying things like "git checkout -b topic origin/maint" and "git rebase origin/master", but never "git checkout --detach origin").

I think skipping 2 and jumping straight to the final configuration would be easier, no need to wrap one's head around a configuration state that will essentially never really exist and most of the extra complexity (at least from my perspective) comes from having to have a configuration at all.

Thanks,
Bence

#170 lore raw

PATCH v13 9/9 fetch set_head: handle mirrored bare repositories

In reply to Bence Ferdinandy

When adding a remote to bare repository with "git remote add --mirror", running fetch will fail to update HEAD to the remote's HEAD, since it does not know how to handle bare repositories. On the other hand HEAD already has content, since "git init --bare" has already set HEAD to whatever is the default branch set for the user. Unless this - by chance
- is the same as the remote's HEAD, HEAD will be pointing to a bad symref. Teach set_head to handle bare repositories, by overwriting HEAD so it mirrors the remote's HEAD.

Note, that in this case overriding the local HEAD reference is necessary, since HEAD will exist before fetch can be run, but this should not be an issue, since the whole purpose of --mirror is to be an exact mirror of the remote, so following any changes to HEAD makes sense.

Also note, that although "git remote set-head" also fails when trying to update the remote's locally tracked HEAD in a mirrored bare repository, the usage of the command does not make much sense after this patch:
fetch will update the remote HEAD correctly, and setting it manually to something else is antithetical to the concept of mirroring.

---

Notes:
v10: - new patch
- handles the issue discovered in
https://lore.kernel.org/git/[email protected]/T/

v11: no change

v12: no change

v13: properly print the previous head if it was detached

 builtin/fetch.c   | 16 +++++++++++-----
 t/t5505-remote.sh | 10 ++++++++++
 2 files changed, 21 insertions(+), 5 deletions(-)
builtin/fetch.c +11 -5
index 3d70cd1add..5e37b9c00f 100644
@@ -1608,7 +1608,7 @@ static const char *strip_refshead(const char *name){
 
 static int set_head(const struct ref *remote_refs)
 {
-	int result = 0, updateres;
+	int result = 0, updateres, is_bare;
 	struct strbuf b_head = STRBUF_INIT, b_remote_head = STRBUF_INIT,
 		b_local_head = STRBUF_INIT;
 	const char *remote = gtransport->remote->name;
@@ -1641,15 +1641,21 @@ static int set_head(const struct ref *remote_refs)
 
 	if (!head_name)
 		goto cleanup;
-	strbuf_addf(&b_head, "refs/remotes/%s/HEAD", remote);
-	strbuf_addf(&b_remote_head, "refs/remotes/%s/%s", remote, head_name);
+	is_bare = is_bare_repository();
+	if (is_bare) {
+		strbuf_addstr(&b_head, "HEAD");
+		strbuf_addf(&b_remote_head, "refs/heads/%s", head_name);
+	} else {
+		strbuf_addf(&b_head, "refs/remotes/%s/HEAD", remote);
+		strbuf_addf(&b_remote_head, "refs/remotes/%s/%s", remote, head_name);
+	}
 		/* make sure it's valid */
-	if (!refs_ref_exists(refs, b_remote_head.buf)) {
+	if (!is_bare && !refs_ref_exists(refs, b_remote_head.buf)) {
 		result = 1;
 		goto cleanup;
 	}
 	updateres = refs_update_symref_extended(refs, b_head.buf, b_remote_head.buf,
-					"fetch", &b_local_head, 1);
+					"fetch", &b_local_head, !is_bare);
 	if (updateres == -1) {
 		result = 1;
 		goto cleanup;
t/t5505-remote.sh +10 -0
index 660310239c..4cc5df2e61 100755
@@ -557,6 +557,16 @@ test_expect_success 'add --mirror && prune' '
 	)
 '
 
+test_expect_success 'add --mirror setting HEAD' '
+	mkdir headmirror &&
+	(
+		cd headmirror &&
+		git init --bare -b notmain &&
+		git remote add --mirror -f origin ../one &&
+		test "$(git symbolic-ref HEAD)" = "refs/heads/main"
+	)
+'
+
 test_expect_success 'add --mirror=fetch' '
 	mkdir mirror-fetch &&
 	git init -b main mirror-fetch/parent &&
Signature · 1 line
2.47.0.296.gda1ecfef29.dirty
#171 lore raw

v14 cover set-head/fetch remote/HEAD updates

In reply to Bence Ferdinandy

Notable changes since v13:

- As discussed in
https://lore.kernel.org/git/[email protected]/ this iteration has dropped printing any warning during fetch and now only sets HEAD if it doesn't already exist, otherwise it doesn't do anything.

- A new patch adds a new test case for testing the failure branch of remote set-head. This also allowed to clear up a bug in a later patch.

I will start working on a follow up patch that will add configuration options to control how fetch sets HEAD or not. The current state should be fine for most people, except those who explicitly wish NOT to have remote/HEAD set. Other than that the current state is now hopefully mature enough to graduate (unless I made yet another mistake :)).

Bence Ferdinandy (10):
t/t5505-remote: set default branch to main t/t5505-remote: test failure of set-head
refs: standardize output of refs_read_symbolic_ref
refs: atomically record overwritten ref in update_symref remote set-head: refactor for readability
remote set-head: better output for --auto
refs: add TRANSACTION_CREATE_EXISTS error
refs: add create_only option to refs_update_symref_extended
fetch: set remote/HEAD if it does not exist
fetch set_head: handle mirrored bare repositories

 builtin/fetch.c                  |  74 +++++++++++++++++++++
 builtin/remote.c                 |  72 +++++++++++++++------
 refs.c                           |  47 ++++++++++++--
 refs.h                           |  19 +++++-
 refs/files-backend.c             |  31 +++++----
 refs/refs-internal.h             |   5 ++
 refs/reftable-backend.c          |  14 ++--
 t/t4207-log-decoration-colors.sh |   3 +-
 t/t5505-remote.sh                | 107 +++++++++++++++++++++++++++++--
 t/t5510-fetch.sh                 |  24 +++++++
 t/t5512-ls-remote.sh             |   2 +
 t/t5514-fetch-multiple.sh        |  17 ++++-
 t/t5516-fetch-push.sh            |   3 +-
 t/t5527-fetch-odd-refs.sh        |   3 +-
 t/t7900-maintenance.sh           |   3 +-
 t/t9210-scalar.sh                |   5 +-
 t/t9211-scalar-clone.sh          |   6 +-
 t/t9902-completion.sh            |  65 +++++++++++++++++++
 18 files changed, 440 insertions(+), 60 deletions(-)
Signature · 1 line
2.47.0.298.g52a96ec17b
#172 lore raw

PATCH v14 1/10 t/t5505-remote: set default branch to main

Consider the bare repository called "mirror" in the test. Running git remote add --mirror -f origin ../one will not change HEAD, consequently if init.defaultBranch is not the same as what HEAD in the remote ("one"), HEAD in "mirror" will be pointing to a non-existent reference. Hence if "mirror" is used as a remote by yet another repository, ls-remote will not show HEAD. On the other hand, if init.defaultBranch happens to match HEAD in "one", then ls-remote will show HEAD.

Since the "ci/run-build-and-tests.sh" script globally exports GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main for some (but not all) jobs, there may be a drift in some tests between how the test repositories are set up in the CI and during local testing, if the test itself uses "master" as default instead of "main". In particular, this happens in t5505-remote.sh. This issue does not manifest currently, as the test does not do any remote HEAD manipulation where this would come up, but should such things be added, a locally passing test would break the CI and vice-versa.

Set GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main in t5505-remote to be consistent with the CI.

---

Notes:
v9: - new patch
- a bandaid for the CI issue noticed by Taylor (cf:
https://lore.kernel.org/git/Zw8IKyPkG0Hr6%[email protected]/), but
see
https://lore.kernel.org/git/[email protected]/ for the root cause in detail

v10: no change

v11: no change

v12: added forgotten sign-off

v13: commit message udpated to be more precise

v14: made explicit in the commit message that the default branch is only
set for some CI jobs

 t/t5505-remote.sh | 3 +++
 1 file changed, 3 insertions(+)
t/t5505-remote.sh +3 -0
index 532035933f..9b50276646 100755
@@ -2,6 +2,9 @@
 
 test_description='git remote porcelain-ish'
 
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
 TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
Signature · 1 line
2.47.0.298.g52a96ec17b
#173 lore raw

PATCH v14 2/10 t/t5505-remote: test failure of set-head

In reply to Bence Ferdinandy

The test coverage was missing a test for the failure branch of remote set-head auto's output. Add the missing text and while we are at it, correct a small grammatical mistake in the error's output ("setup" is the noun, "set up" is the verb).

---

Notes:
v14: new patch

 builtin/remote.c  |  2 +-
 t/t5505-remote.sh | 12 ++++++++++++
 2 files changed, 13 insertions(+), 1 deletion(-)
builtin/remote.c +1 -1
index 76670ddd8b..8a182439f2 100644
@@ -1445,7 +1445,7 @@ static int set_head(int argc, const char **argv, const char *prefix)
 		if (!refs_ref_exists(get_main_ref_store(the_repository), buf2.buf))
 			result |= error(_("Not a valid ref: %s"), buf2.buf);
 		else if (refs_update_symref(get_main_ref_store(the_repository), buf.buf, buf2.buf, "remote set-head"))
-			result |= error(_("Could not setup %s"), buf.buf);
+			result |= error(_("Could not set up %s"), buf.buf);
 		else if (opt_a)
 			printf("%s/HEAD set to %s\n", argv[0], head_name);
 		free(head_name);
t/t5505-remote.sh +12 -0
index 9b50276646..4e127bf5b8 100755
@@ -432,6 +432,18 @@ test_expect_success 'set-head --auto' '
 	)
 '
 
+test_expect_success REFFILES 'set-head --auto failure' '
+	(
+		cd test &&
+		touch .git/refs/remotes/origin/HEAD.lock &&
+		git remote set-head --auto origin 2>errormsg ||
+		tail -n1 errormsg >output &&
+		rm .git/refs/remotes/origin/HEAD.lock &&
+		echo "error: Could not set up refs/remotes/origin/HEAD" >expect &&
+		test_cmp expect output
+	)
+'
+
 test_expect_success 'set-head --auto has no problem w/multiple HEADs' '
 	(
 		cd test &&
Signature · 1 line
2.47.0.298.g52a96ec17b
#174 lore raw

Re: [PATCH v14 02/10] t/t5505-remote: test failure of set-head

builtin/remote.c static int set_head(int argc, const char **argv, const char *prefix) · line 1448

Quoted 2 lines · Bence Ferdinandy
builtin/remote.c +1 -1
-			result |= error(_("Could not setup %s"), buf.buf);
+			result |= error(_("Could not set up %s"), buf.buf);

Good eyes.

t/t5505-remote.sh test_expect_success 'set-head --auto' ' · line 435

Quoted 9 lines
t/t5505-remote.sh +1 -0
index 9b50276646..4e127bf5b8 100755
@@ -432,6 +432,18 @@ test_expect_success 'set-head --auto' '
 	)
 '
 
+test_expect_success REFFILES 'set-head --auto failure' '

Writing

	test_when_finished "rm -f test/.git/refs/remotes/origin/HEAD.lock" &&

here allows us not to worry about commands in the sequence before the "rm" below failing unexpectedly.

t/t5505-remote.sh test_expect_success 'set-head --auto' ' · line 440

Quoted 6 lines
t/t5505-remote.sh +5 -0
+	(
+		cd test &&
+		touch .git/refs/remotes/origin/HEAD.lock &&
 
+		git remote set-head --auto origin 2>errormsg ||
+		tail -n1 errormsg >output &&

Are we saying that it is OK for set-head not to fail here? If not, then this should be

		test_must_fail git remote set-head ... 2>err &&
		tail -n 1 err >actual &&

instead.

Quoted 9 lines
t/t5505-remote.sh +6 -0
+		rm .git/refs/remotes/origin/HEAD.lock &&
+		echo "error: Could not set up refs/remotes/origin/HEAD" >expect &&
+		test_cmp expect output
+	)
+'
+
 test_expect_success 'set-head --auto has no problem w/multiple HEADs' '
 	(

cd test &&

#175 lore raw

PATCH v14 3/10 refs: standardize output of refs_read_symbolic_ref

In reply to Bence Ferdinandy

When the symbolic reference we want to read with refs_read_symbolic_ref is actually not a symbolic reference, the files and the reftable backends return different values (1 and -1 respectively). Standardize the returned values so that 0 is success, -1 is a generic error and -2 is that the reference was actually non-symbolic.

---

Notes:
v13: new patch

v14: - simplified the logic in reftables backend (thanks Junio)
- update the comment in refs.h (thanks Patrick)
- rewrote comment in refs-internal.h to point to the one in refs.h
- created NOT_A_SYMREF=-2 constant

 refs.h                  | 11 +++++++++++
 refs/files-backend.c    |  7 +++----
 refs/refs-internal.h    |  5 +++++
 refs/reftable-backend.c |  8 +++++---
 4 files changed, 24 insertions(+), 7 deletions(-)
refs.h +11 -0
index 108dfc93b3..22c2b45b8b 100644
@@ -83,6 +83,17 @@ int refs_read_ref_full(struct ref_store *refs, const char *refname,
 
 int refs_read_ref(struct ref_store *refs, const char *refname, struct object_id *oid);
 
+#define NOT_A_SYMREF -2
+
+/*
+ * Read the symbolic ref named "refname" and write its immediate referent into
+ * the provided buffer. Referent is left empty if "refname" is not a symbolic
+ * ref. It does not resolve the symbolic reference recursively in case the
+ * target is also a symbolic ref.
+ *
+ * Returns 0 on success, -2 if the "refname" is not a symbolic ref,
+ * -1 otherwise.
+ */
 int refs_read_symbolic_ref(struct ref_store *ref_store, const char *refname,
 			   struct strbuf *referent);
 
refs/files-backend.c +3 -4
index 0824c0b8a9..4cc43c32f2 100644
@@ -596,10 +596,9 @@ static int files_read_symbolic_ref(struct ref_store *ref_store, const char *refn
 	unsigned int type;
 
 	ret = read_ref_internal(ref_store, refname, &oid, referent, &type, &failure_errno, 1);
-	if (ret)
-		return ret;
-
-	return !(type & REF_ISSYMREF);
+	if (!ret && !(type & REF_ISSYMREF))
+		return NOT_A_SYMREF;
+	return ret;
 }
 
 int parse_loose_ref_contents(const struct git_hash_algo *algop,
refs/refs-internal.h +5 -0
index 2313c830d8..1399fee61c 100644
@@ -673,6 +673,11 @@ struct ref_storage_be {
 
 	ref_iterator_begin_fn *iterator_begin;
 	read_raw_ref_fn *read_raw_ref;
+
+	/*
+	 * Please refer to `refs_read_symbolic_ref()` for the expected
+	 * behaviour.
+	 */
 	read_symbolic_ref_fn *read_symbolic_ref;
 
 	reflog_iterator_begin_fn *reflog_iterator_begin;
refs/reftable-backend.c +5 -3
index 38eb14d591..1809e3426a 100644
@@ -830,10 +830,12 @@ static int reftable_be_read_symbolic_ref(struct ref_store *ref_store,
 		return ret;
 
 	ret = reftable_stack_read_ref(stack, refname, &ref);
-	if (ret == 0 && ref.value_type == REFTABLE_REF_SYMREF)
+        if (ret)
+                ret = -1;
+        else if (ref.value_type == REFTABLE_REF_SYMREF)
 		strbuf_addstr(referent, ref.value.symref);
-	else
-		ret = -1;
+        else
+                ret = NOT_A_SYMREF;
 
 	reftable_ref_record_release(&ref);
 	return ret;
Signature · 1 line
2.47.0.298.g52a96ec17b
#176 lore raw

Re: [PATCH v14 03/10] refs: standardize output of refs_read_symbolic_ref

Bence Ferdinandy <[email protected]> writes:

[snip]

Quoted 8 lines
refs/reftable-backend.c +0 -0
index 38eb14d591..1809e3426a 100644
@@ -830,10 +830,12 @@ static int reftable_be_read_symbolic_ref(struct ref_store *ref_store,
 		return ret;
 
 	ret = reftable_stack_read_ref(stack, refname, &ref);

refs/reftable-backend.c static int reftable_be_read_symbolic_ref(struct ref_store *ref_store, · line 839

Quoted 10 lines
refs/reftable-backend.c +5 -3
-	if (ret == 0 && ref.value_type == REFTABLE_REF_SYMREF)
+        if (ret)
+                ret = -1;
+        else if (ref.value_type == REFTABLE_REF_SYMREF)
 		strbuf_addstr(referent, ref.value.symref);
-	else
-		ret = -1;
+        else
+                ret = NOT_A_SYMREF;
 

I was building my series on top of this, and noticed whitespace issues here. A simple way to check your series is to run:

  $ git log --check --pretty=format:"---% h% s"
Quoted 4 lines
refs/reftable-backend.c +0 -1
 	reftable_ref_record_release(&ref);
 	return ret;
--

2.47.0.298.g52a96ec17b

#177 lore raw

Re: [PATCH v14 03/10] refs: standardize output of refs_read_symbolic_ref

Quoted 26 lines · karthik nayak

Bence Ferdinandy <[email protected]> writes:

[snip]

Quoted 8 lines
refs/reftable-backend.c +0 -0
index 38eb14d591..1809e3426a 100644
@@ -830,10 +830,12 @@ static int reftable_be_read_symbolic_ref(struct ref_store *ref_store,
 		return ret;
 
 	ret = reftable_stack_read_ref(stack, refname, &ref);

refs/reftable-backend.c static int reftable_be_read_symbolic_ref(struct ref_store *ref_store, · line 839

Quoted 10 lines
refs/reftable-backend.c +5 -3
-	if (ret == 0 && ref.value_type == REFTABLE_REF_SYMREF)
+        if (ret)
+                ret = -1;
+        else if (ref.value_type == REFTABLE_REF_SYMREF)
 		strbuf_addstr(referent, ref.value.symref);
-	else
-		ret = -1;
+        else
+                ret = NOT_A_SYMREF;
 

I was building my series on top of this, and noticed whitespace issues here. A simple way to check your series is to run:

Quoted 1 line
  $ git log --check --pretty=format:"---% h% s"

I ran this on v15 and it didn't produce any output. I read what --check is in the manpages, although the format is a bit cryptic for me. What does that do exactly?

Anyhow if there was no output for v15 I should be fine, right?

Quoted 5 lines
Quoted 5 lines
refs/reftable-backend.c +0 -1
 	reftable_ref_record_release(&ref);
 	return ret;
--

2.47.0.298.g52a96ec17b

Signature · 1 line
bence.ferdinandy.com
#178 lore raw

Re: [PATCH v14 03/10] refs: standardize output of refs_read_symbolic_ref

Quoted 28 lines · Bence Ferdinandy
Quoted 26 lines · karthik nayak

Bence Ferdinandy <[email protected]> writes:

[snip]

Quoted 8 lines
refs/reftable-backend.c +0 -0
index 38eb14d591..1809e3426a 100644
@@ -830,10 +830,12 @@ static int reftable_be_read_symbolic_ref(struct ref_store *ref_store,
 		return ret;
 
 	ret = reftable_stack_read_ref(stack, refname, &ref);

refs/reftable-backend.c static int reftable_be_read_symbolic_ref(struct ref_store *ref_store, · line 839

Quoted 10 lines
refs/reftable-backend.c +5 -3
-	if (ret == 0 && ref.value_type == REFTABLE_REF_SYMREF)
+        if (ret)
+                ret = -1;
+        else if (ref.value_type == REFTABLE_REF_SYMREF)
 		strbuf_addstr(referent, ref.value.symref);
-	else
-		ret = -1;
+        else
+                ret = NOT_A_SYMREF;
 

I was building my series on top of this, and noticed whitespace issues here. A simple way to check your series is to run:

Quoted 7 lines
Quoted 1 line
  $ git log --check --pretty=format:"---% h% s"

I ran this on v15 and it didn't produce any output. I read what --check is in the manpages, although the format is a bit cryptic for me. What does that do exactly?

Anyhow if there was no output for v15 I should be fine, right?

But then again: this patch is exactly like v14 so I'm not so sure about that ...

Signature · 1 line
bence.ferdinandy.com
#179 lore raw

Re: [PATCH v14 03/10] refs: standardize output of refs_read_symbolic_ref

In reply to Bence Ferdinandy
Quoted 27 lines · Bence Ferdinandy
Quoted 26 lines · karthik nayak

Bence Ferdinandy <[email protected]> writes:

[snip]

Quoted 8 lines
refs/reftable-backend.c +0 -0
index 38eb14d591..1809e3426a 100644
@@ -830,10 +830,12 @@ static int reftable_be_read_symbolic_ref(struct ref_store *ref_store,
 		return ret;
 
 	ret = reftable_stack_read_ref(stack, refname, &ref);

refs/reftable-backend.c static int reftable_be_read_symbolic_ref(struct ref_store *ref_store, · line 839

Quoted 10 lines
refs/reftable-backend.c +5 -3
-	if (ret == 0 && ref.value_type == REFTABLE_REF_SYMREF)
+        if (ret)
+                ret = -1;
+        else if (ref.value_type == REFTABLE_REF_SYMREF)
 		strbuf_addstr(referent, ref.value.symref);
-	else
-		ret = -1;
+        else
+                ret = NOT_A_SYMREF;
 

I was building my series on top of this, and noticed whitespace issues here. A simple way to check your series is to run:

Quoted 3 lines
Quoted 1 line
  $ git log --check --pretty=format:"---% h% s"

I ran this on v15 and it didn't produce any output.

Did you already post v15? I only see v14

Quoted 4 lines

I read what --check is in
the manpages, although the format is a bit cryptic for me. What does that do exactly?

It ensures that commits don't have conflict markers and that there are no trailing whitespaces and spaces followed by tabs by default.

Also this is included in the CI checks (see ci/check-whitespace.sh), so if you use either GitLab or GitHub you should see these shown as errors on the CI. You'll have to raise a MR/PR to trigger the CI I believe.

On a sidenote, do you think we should modify the manpage? I found it comprehensible, but would be nice to clarify anything cryptic.

Quoted 2 lines

Anyhow if there was no output for v15 I should be fine, right?

At the least you should see git log's output, but if there are issues they should be shown inline. So when you say 'no output' do you mean you see absolutely no output?

Quoted 11 lines
Quoted 5 lines
Quoted 5 lines
refs/reftable-backend.c +0 -1
 	reftable_ref_record_release(&ref);
 	return ret;
--

2.47.0.298.g52a96ec17b

--
bence.ferdinandy.com

#180 lore raw

Re: [PATCH v14 03/10] refs: standardize output of refs_read_symbolic_ref

Quoted 29 lines · karthik nayak
Quoted 27 lines · Bence Ferdinandy
Quoted 26 lines · karthik nayak

Bence Ferdinandy <[email protected]> writes:

[snip]

Quoted 8 lines
refs/reftable-backend.c +0 -0
index 38eb14d591..1809e3426a 100644
@@ -830,10 +830,12 @@ static int reftable_be_read_symbolic_ref(struct ref_store *ref_store,
 		return ret;
 
 	ret = reftable_stack_read_ref(stack, refname, &ref);

refs/reftable-backend.c static int reftable_be_read_symbolic_ref(struct ref_store *ref_store, · line 839

Quoted 10 lines
refs/reftable-backend.c +5 -3
-	if (ret == 0 && ref.value_type == REFTABLE_REF_SYMREF)
+        if (ret)
+                ret = -1;
+        else if (ref.value_type == REFTABLE_REF_SYMREF)
 		strbuf_addstr(referent, ref.value.symref);
-	else
-		ret = -1;
+        else
+                ret = NOT_A_SYMREF;
 

I was building my series on top of this, and noticed whitespace issues here. A simple way to check your series is to run:

Quoted 5 lines
Quoted 3 lines
Quoted 1 line
  $ git log --check --pretty=format:"---% h% s"

I ran this on v15 and it didn't produce any output.

Did you already post v15? I only see v14

Not yet, but I'll be sending it in a pinch.

Quoted 6 lines
Quoted 5 lines

I read what --check is in
the manpages, although the format is a bit cryptic for me. What does that do exactly?

Quoted 6 lines

It ensures that commits don't have conflict markers and that there are no trailing whitespaces and spaces followed by tabs by default.

Also this is included in the CI checks (see ci/check-whitespace.sh), so if you use either GitLab or GitHub you should see these shown as errors on the CI. You'll have to raise a MR/PR to trigger the CI I believe.

I've been running the CI by pushing to a fork since Taylor first caught an error I didn't see locally and it never flagged. Now that you mention it, I'll also add log --check to my CI-s.

Quoted 3 lines

On a sidenote, do you think we should modify the manpage? I found it comprehensible, but would be nice to clarify anything cryptic.

No, --check was quite clear, the --pretty=format:"---% h% s" part was what was cryptic.

Quoted 7 lines
Quoted 3 lines

Anyhow if there was no output for v15 I should be fine, right?

At the least you should see git log's output, but if there are issues they should be shown inline. So when you say 'no output' do you mean you see absolutely no output?

Absolutely no output:
https://asciinema.org/a/lsqp4e1bNst6cFWw9M2jX1IqC

But I figured out why: the whitespace and the tabs were not mixed on the line, just across lines. As I read it, that is not an error to have tabs on one line and spaces on the next.

Anyhow that should be now cleared up, thanks. Gotta say, I was expecting to learn about internals doing this, but I also ended up picking up a couple of usage things as well, like --range-diff for format patch and such.

Thanks,
Bence

#181 lore raw

Re: [PATCH v14 03/10] refs: standardize output of refs_read_symbolic_ref

Quoted 10 lines · Bence Ferdinandy
Quoted 3 lines

At the least you should see git log's output, but if there are issues they should be shown inline. So when you say 'no output' do you mean you see absolutely no output?

Absolutely no output:
https://asciinema.org/a/lsqp4e1bNst6cFWw9M2jX1IqC

But I figured out why: the whitespace and the tabs were not mixed on the line, just across lines. As I read it, that is not an error to have tabs on one line and spaces on the next.

Our .gitattribute starts like so:

* whitespace=!indent,trail,space
*.[ch] whitespace=indent,trail,space diff=cpp

so, unless otherwise specified, we frown upon trailing whitespace and space before tab and indenting with non tab is permitted, but C source and header files have further care about "indent" (short for "indent-with-non-tab".

So mixed or not, if you indented with spaces and not tabs, that would be noticed.

Quoted 3 lines

Anyhow that should be now cleared up, thanks. Gotta say, I was expecting to learn about internals doing this, but I also ended up picking up a couple of usage things as well, like --range-diff for format patch and such.

I usually have "--whitespace=fix" so if you did "git log" on the commits I made out of your patches, it is not surprising if your "log --check" was silent.

I re-applied your v14 with "git am -s --whitespace=nowarn" and here is what I saw.

commit 75a6a3e6597d5f3959eb269122e8c5f4e4baac0e

Author: Bence Ferdinandy <[email protected]>
Date:   Thu Nov 21 23:55:03 2024 +0100

refs: standardize output of refs_read_symbolic_ref

When the symbolic reference we want to read with refs_read_symbolic_ref is actually not a symbolic reference, the files and the reftable backends return different values (1 and -1 respectively). Standardize the returned values so that 0 is success, -1 is a generic error and -2 is that the reference was actually non-symbolic.

refs/reftable-backend.c:833: indent with spaces.
+ if (ret)
refs/reftable-backend.c:834: indent with spaces.
+ ret = -1;
refs/reftable-backend.c:835: indent with spaces.
+ else if (ref.value_type == REFTABLE_REF_SYMREF)
refs/reftable-backend.c:837: indent with spaces.
+ else
refs/reftable-backend.c:838: indent with spaces.
+ ret = NOT_A_SYMREF;

#182 lore raw

Re: [PATCH v14 03/10] refs: standardize output of refs_read_symbolic_ref

sorry about the other thread, I saw "extend whitespace checks" and I thought based on what Kartik wrote that log --check should have caught it, which is why I thought it might be appropriate there.

Quoted 18 lines · Junio C Hamano
Quoted 10 lines · Bence Ferdinandy
Quoted 3 lines

At the least you should see git log's output, but if there are issues they should be shown inline. So when you say 'no output' do you mean you see absolutely no output?

Absolutely no output:
https://asciinema.org/a/lsqp4e1bNst6cFWw9M2jX1IqC

But I figured out why: the whitespace and the tabs were not mixed on the line, just across lines. As I read it, that is not an error to have tabs on one line and spaces on the next.

Our .gitattribute starts like so:

* whitespace=!indent,trail,space
*.[ch] whitespace=indent,trail,space diff=cpp

Quoted 7 lines

so, unless otherwise specified, we frown upon trailing whitespace and space before tab and indenting with non tab is permitted, but C source and header files have further care about "indent" (short for "indent-with-non-tab".

So mixed or not, if you indented with spaces and not tabs, that would be noticed.

So git log --check --pretty=format:"---% h% s" was _not_ supposed to catch this?

I ran the CI with this patch again:
https://github.com/ferdinandyb/git/actions/runs/12031250376

and it's all green, so wherever this _should_ have been caught: it didn't work.

Quoted 27 lines
Quoted 4 lines

Anyhow that should be now cleared up, thanks. Gotta say, I was expecting to learn about internals doing this, but I also ended up picking up a couple of usage things as well, like --range-diff for format patch and such.

I usually have "--whitespace=fix" so if you did "git log" on the commits I made out of your patches, it is not surprising if your "log --check" was silent.

I re-applied your v14 with "git am -s --whitespace=nowarn" and here is what I saw.

commit 75a6a3e6597d5f3959eb269122e8c5f4e4baac0e

Author: Bence Ferdinandy <[email protected]>
Date:   Thu Nov 21 23:55:03 2024 +0100

refs: standardize output of refs_read_symbolic_ref

When the symbolic reference we want to read with refs_read_symbolic_ref is actually not a symbolic reference, the files and the reftable backends return different values (1 and -1 respectively). Standardize the returned values so that 0 is success, -1 is a generic error and -2 is that the reference was actually non-symbolic.

Quoted 10 lines

refs/reftable-backend.c:833: indent with spaces.
+ if (ret)
refs/reftable-backend.c:834: indent with spaces.
+ ret = -1;
refs/reftable-backend.c:835: indent with spaces.
+ else if (ref.value_type == REFTABLE_REF_SYMREF)
refs/reftable-backend.c:837: indent with spaces.
+ else
refs/reftable-backend.c:838: indent with spaces.
+ ret = NOT_A_SYMREF;

So this did find it. Maybe something is misconfigured in the CI?

Best,
Bence

#183 lore raw

Re: [PATCH v14 03/10] refs: standardize output of refs_read_symbolic_ref

Quoted 4 lines · Bence Ferdinandy

sorry about the other thread, I saw "extend whitespace checks" and I thought based on what Kartik wrote that log --check should have caught it, which is why I thought it might be appropriate there.

With v14, running git log --check --pretty=format:"---% h% s" master.. gives me:

--- f73b2c577b fetch set_head: handle mirrored bare repositories

--- c0c84fb7f9 fetch: set remote/HEAD if it does not exist

--- c366911074 refs: add create_only option to refs_update_symref_extended

--- c47704d4df refs: add TRANSACTION_CREATE_EXISTS error

  --- 22e7a9bcae remote set-head: better output for --auto

--- 25d9944eb2 remote set-head: refactor for readability

--- 01fe46c842 refs: atomically record overwritten ref in update_symref

--- fed56bc6cc refs: standardize output of refs_read_symbolic_ref refs/reftable-backend.c:833: indent with spaces.
+ if (ret)
refs/reftable-backend.c:834: indent with spaces.
+ ret = -1;
refs/reftable-backend.c:835: indent with spaces.
+ else if (ref.value_type == REFTABLE_REF_SYMREF)
refs/reftable-backend.c:837: indent with spaces.
+ else
refs/reftable-backend.c:838: indent with spaces.
+ ret = NOT_A_SYMREF;

--- d5534d6c67 t/t5505-remote: test failure of set-head

--- a77b3b7774 t/t5505-remote: set default branch to main

Quoted 27 lines · Junio C Hamano
Quoted 18 lines
Quoted 10 lines · Bence Ferdinandy
Quoted 3 lines

At the least you should see git log's output, but if there are issues they should be shown inline. So when you say 'no output' do you mean you see absolutely no output?

Absolutely no output:
https://asciinema.org/a/lsqp4e1bNst6cFWw9M2jX1IqC

But I figured out why: the whitespace and the tabs were not mixed on the line, just across lines. As I read it, that is not an error to have tabs on one line and spaces on the next.

Our .gitattribute starts like so:

* whitespace=!indent,trail,space
*.[ch] whitespace=indent,trail,space diff=cpp

Quoted 7 lines

so, unless otherwise specified, we frown upon trailing whitespace and space before tab and indenting with non tab is permitted, but C source and header files have further care about "indent" (short for "indent-with-non-tab".

So mixed or not, if you indented with spaces and not tabs, that would be noticed.

Quoted 7 lines

So git log --check --pretty=format:"---% h% s" was _not_ supposed to catch this?

I ran the CI with this patch again:
https://github.com/ferdinandyb/git/actions/runs/12031250376

and it's all green, so wherever this _should_ have been caught: it didn't work.

I'm not an expert in GitHub actions, but if you look at .github/workflows/check-whitespace.yml, it says it is only triggered for pull_request. Did you raise a pull request? From your link, I don't see the whitespace job being triggered, so it didn't work would be inaccurate, since it didn't even trigger the job ;)

[snip]

#184 lore raw

Re: [PATCH v14 03/10] refs: standardize output of refs_read_symbolic_ref

Quoted 9 lines · karthik nayak

With v14, running git log --check --pretty=format:"---% h% s" master.. gives me:
...
--- fed56bc6cc refs: standardize output of refs_read_symbolic_ref refs/reftable-backend.c:833: indent with spaces.
+ if (ret)
refs/reftable-backend.c:834: indent with spaces.
+ ret = -1;
...

Thanks, the above matches what I saw in my message Bence responded to (to which you are responding). I was unsure if/how the status of each "diff --check" is propagated up to the driving "git log", but the job is reading from the output of the command and not using the exit status of "git log --check", so even if "git log --check" did not signal a check failure with its status, it did not matter ;-) A quick local check says "git log --check" does exit with 2 if there is an offending commit in the range, so we should be OK.

By the way, I appreciate the cuteness of "% s" but I do not see the point of "% h", as I do not think of a situation where '%h' can ever be empty. It seems that this was carried from the first day when GitHub CI learned the whitespace checks 32c83afc (ci: github action
- add check for whitespace errors, 2020-09-22).

#185 lore raw

Re: [PATCH v14 03/10] refs: standardize output of refs_read_symbolic_ref

Quoted 12 lines · Junio C Hamano
Quoted 9 lines · karthik nayak

With v14, running git log --check --pretty=format:"---% h% s" master.. gives me:
...
--- fed56bc6cc refs: standardize output of refs_read_symbolic_ref refs/reftable-backend.c:833: indent with spaces.
+ if (ret)
refs/reftable-backend.c:834: indent with spaces.
+ ret = -1;
...

Quoted 8 lines

Thanks, the above matches what I saw in my message Bence responded to (to which you are responding). I was unsure if/how the status of each "diff --check" is propagated up to the driving "git log", but the job is reading from the output of the command and not using the exit status of "git log --check", so even if "git log --check" did not signal a check failure with its status, it did not matter ;-) A quick local check says "git log --check" does exit with 2 if there is an offending commit in the range, so we should be OK.

Ok, so git diff --check master at least does produce the output for me, and now that you mentioned the exit code, I checked that even though I get zero output from git log --check, the exit code is indeed 2. So now I just don't get why I don't see any output ...

#186 lore raw

Re: [PATCH v14 03/10] refs: standardize output of refs_read_symbolic_ref

Quoted 4 lines · Bence Ferdinandy

Ok, so git diff --check master at least does produce the output for me, and now that you mentioned the exit code, I checked that even though I get zero output from git log --check, the exit code is indeed 2. So now I just don't get why I don't see any output ...

I was about to say "perhaps you are accidentally giving a wrong revision range, like 'git checkout master && git log --check master..'?" but then the command should exit with 0 for an empty range, so that is not it.

Do let us know when you find out why ;-)

Thanks.

#187 lore raw

Re: [PATCH v14 03/10] refs: standardize output of refs_read_symbolic_ref

In reply to karthik nayak
Quoted 36 lines · karthik nayak
Quoted 4 lines · Bence Ferdinandy

sorry about the other thread, I saw "extend whitespace checks" and I thought based on what Kartik wrote that log --check should have caught it, which is why I thought it might be appropriate there.

With v14, running git log --check --pretty=format:"---% h% s" master.. gives me:

--- f73b2c577b fetch set_head: handle mirrored bare repositories

--- c0c84fb7f9 fetch: set remote/HEAD if it does not exist

--- c366911074 refs: add create_only option to refs_update_symref_extended

--- c47704d4df refs: add TRANSACTION_CREATE_EXISTS error

  --- 22e7a9bcae remote set-head: better output for --auto

--- 25d9944eb2 remote set-head: refactor for readability

--- 01fe46c842 refs: atomically record overwritten ref in update_symref

--- fed56bc6cc refs: standardize output of refs_read_symbolic_ref refs/reftable-backend.c:833: indent with spaces.
+ if (ret)
refs/reftable-backend.c:834: indent with spaces.
+ ret = -1;
refs/reftable-backend.c:835: indent with spaces.
+ else if (ref.value_type == REFTABLE_REF_SYMREF)
refs/reftable-backend.c:837: indent with spaces.
+ else
refs/reftable-backend.c:838: indent with spaces.
+ ret = NOT_A_SYMREF;

Quoted 3 lines

--- d5534d6c67 t/t5505-remote: test failure of set-head

--- a77b3b7774 t/t5505-remote: set default branch to main

So at least now I know why I'm confused :) I copied your exact command (well, with upstream/master..), and I still have zero output. Since .gitattributes is committed to the repository, what else could be the difference?

[snip]

Quoted 7 lines
Quoted 7 lines

So git log --check --pretty=format:"---% h% s" was _not_ supposed to catch this?

I ran the CI with this patch again:
https://github.com/ferdinandyb/git/actions/runs/12031250376

Quoted 7 lines
Quoted 1 line

and it's all green, so wherever this _should_ have been caught: it didn't work.

I'm not an expert in GitHub actions, but if you look at .github/workflows/check-whitespace.yml, it says it is only triggered for pull_request. Did you raise a pull request? From your link, I don't see the whitespace job being triggered, so it didn't work would be inaccurate, since it didn't even trigger the job ;)

Well that make sense, the commit introducing it also talks about pull requests. On the other hand: why does this run only on a pull request? The main.yml runs on both pull requests and pushes and since the project doesn't really use pull requests, it would make more sense to also run this on push? The check-style.yml is similar.

#188 lore raw

Re: [PATCH v14 03/10] refs: standardize output of refs_read_symbolic_ref

In reply to karthik nayak
Quoted 11 lines · karthik nayak

Bence Ferdinandy <[email protected]> writes:

[snip]

Quoted 7 lines
refs/reftable-backend.c +0 -0
index 38eb14d591..1809e3426a 100644
@@ -830,10 +830,12 @@ static int reftable_be_read_symbolic_ref(struct ref_store *ref_store,
 		return ret;
 
Quoted 14 lines

refs/reftable-backend.c static int reftable_be_read_symbolic_ref(struct ref_store *ref_store, · line 839

Quoted 11 lines
refs/reftable-backend.c +5 -3
 	ret = reftable_stack_read_ref(stack, refname, &ref);
-	if (ret == 0 && ref.value_type == REFTABLE_REF_SYMREF)
+        if (ret)
+                ret = -1;
+        else if (ref.value_type == REFTABLE_REF_SYMREF)
 		strbuf_addstr(referent, ref.value.symref);
-	else
-		ret = -1;
+        else
+                ret = NOT_A_SYMREF;
 

I was building my series on top of this, and noticed whitespace issues here. A simple way to check your series is to run:

Found it, thanks for catching, I had tabs and whitespaces mixed.

git log --check didn't find a problem with that though.

Quoted 5 lines
Quoted 5 lines
refs/reftable-backend.c +0 -1
 	reftable_ref_record_release(&ref);
 	return ret;
--

2.47.0.298.g52a96ec17b

Signature · 1 line
bence.ferdinandy.com
#189 lore raw

PATCH v14 4/10 refs: atomically record overwritten ref in update_symref

In reply to Bence Ferdinandy

When updating a symref with update_symref it's currently not possible to know for sure what was the previous value that was overwritten. Extend refs_update_symref under a new function name, to record the value after the ref has been locked if the caller of refs_update_symref_extended requests it via a new variable in the function call. Make the return value of the function notify the caller, if the previous value was actually not a symbolic reference. Keep the original refs_update_symref function with the same signature, but now as a wrapper around refs_update_symref_extended.

---

Notes:
v4: new patch

v5: - added before_target to reftables backend
- added an extra safety check for transaction's existence in refs.c

v6: - no change

v7: - remove the whole before_target concept from the backends and
handle checking it in refs.c instead (thanks Karthik)
- rename the before_target to referent which is how the same concept is called in the backends
- change commit prefix to be more in line with project standards

v8: no change

v9: - instead of adding parameters to refs_update_symref, rename what
was in v8 as refs_update_symref_extended and make refs_update_symref a wrapper for that. This significantly reduces the number of files that need to be touched, and avoids adding a lot of dummy NULL-s in unrelated places.

v10: no change

v11: no change

v12: no change

v13: if referent is a non-symbolic ref, record the hash in referent and
signal this with a return value of -1

v14: - mistakes were made: trying to read the referent should NOT lead to
an actual error, even if it doesn't work out in the end, that part of the code should only set the return to -2 or not touch it
- the returned error code by the above was also incorrect and now is -2 (and uses NOT_A_SYMREF now)

 refs.c | 22 ++++++++++++++++++++--
 refs.h |  4 ++++
 2 files changed, 24 insertions(+), 2 deletions(-)
refs.c +20 -2
index 5f729ed412..d80efd58f0 100644
@@ -2115,6 +2115,13 @@ int peel_iterated_oid(struct repository *r, const struct object_id *base, struct
 
 int refs_update_symref(struct ref_store *refs, const char *ref,
 		       const char *target, const char *logmsg)
+{
+	return refs_update_symref_extended(refs, ref, target, logmsg, NULL);
+}
+
+int refs_update_symref_extended(struct ref_store *refs, const char *ref,
+		       const char *target, const char *logmsg,
+		       struct strbuf *referent)
 {
 	struct ref_transaction *transaction;
 	struct strbuf err = STRBUF_INIT;
@@ -2125,10 +2132,22 @@ int refs_update_symref(struct ref_store *refs, const char *ref,
 	    ref_transaction_update(transaction, ref, NULL, NULL,
 				   target, NULL, REF_NO_DEREF,
 				   logmsg, &err) ||
-	    ref_transaction_commit(transaction, &err)) {
+	    ref_transaction_prepare(transaction, &err)) {
 		ret = error("%s", err.buf);
+		goto cleanup;
 	}
+	if (referent && refs_read_symbolic_ref(refs, ref, referent) == NOT_A_SYMREF) {
+		struct object_id oid;
+		if (!refs_read_ref(refs, ref, &oid)) {
+			strbuf_addstr(referent, oid_to_hex(&oid));
+			ret = NOT_A_SYMREF;
+		}
+	}
+
+	if (ref_transaction_commit(transaction, &err))
+		ret = error("%s", err.buf);
 
+cleanup:
 	strbuf_release(&err);
 	if (transaction)
 		ref_transaction_free(transaction);
@@ -2948,4 +2967,3 @@ int ref_update_expects_existing_old_ref(struct ref_update *update)
 	return (update->flags & REF_HAVE_OLD) &&
 		(!is_null_oid(&update->old_oid) || update->old_target);
 }
-
refs.h +4 -0
index 22c2b45b8b..5c46ac9f34 100644
@@ -584,6 +584,10 @@ int refs_copy_existing_ref(struct ref_store *refs, const char *oldref,
 int refs_update_symref(struct ref_store *refs, const char *refname,
 		       const char *target, const char *logmsg);
 
+int refs_update_symref_extended(struct ref_store *refs, const char *refname,
+		       const char *target, const char *logmsg,
+		       struct strbuf *referent);
+
 enum action_on_err {
 	UPDATE_REFS_MSG_ON_ERR,
 	UPDATE_REFS_DIE_ON_ERR,
Signature · 1 line
2.47.0.298.g52a96ec17b
#190 lore raw

PATCH v14 5/10 remote set-head: refactor for readability

In reply to Bence Ferdinandy

Make two different readability refactors:

Rename strbufs "buf" and "buf2" to something more explanatory.

Instead of calling get_main_ref_store(the_repository) multiple times, call it once and store the result in a new refs variable. Although this change probably offers some performance benefits, the main purpose is to shorten the line lengths of function calls using this variable.

---

Notes:
v5: new patch (split from the next patch as a preparatory step)

v6: no change

v7: - change commit prefix to be more in line with project standards

v8: no change

v9: - further improve readability by renaming buf, and buf2 consistently
with how patch 6 was already done

v10: no change

v11: no change

v12: no change

v13: more precise wording for commit message

v14: no change

 builtin/remote.c | 23 ++++++++++++-----------
 1 file changed, 12 insertions(+), 11 deletions(-)
builtin/remote.c +12 -11
index 8a182439f2..bcc3ee91ef 100644
@@ -1402,8 +1402,9 @@ static int show(int argc, const char **argv, const char *prefix)
 static int set_head(int argc, const char **argv, const char *prefix)
 {
 	int i, opt_a = 0, opt_d = 0, result = 0;
-	struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT;
+	struct strbuf b_head = STRBUF_INIT, b_remote_head = STRBUF_INIT;
 	char *head_name = NULL;
+	struct ref_store *refs = get_main_ref_store(the_repository);
 
 	struct option options[] = {
 		OPT_BOOL('a', "auto", &opt_a,
@@ -1415,7 +1416,7 @@ static int set_head(int argc, const char **argv, const char *prefix)
 	argc = parse_options(argc, argv, prefix, options,
 			     builtin_remote_sethead_usage, 0);
 	if (argc)
-		strbuf_addf(&buf, "refs/remotes/%s/HEAD", argv[0]);
+		strbuf_addf(&b_head, "refs/remotes/%s/HEAD", argv[0]);
 
 	if (!opt_a && !opt_d && argc == 2) {
 		head_name = xstrdup(argv[1]);
@@ -1434,25 +1435,25 @@ static int set_head(int argc, const char **argv, const char *prefix)
 			head_name = xstrdup(states.heads.items[0].string);
 		free_remote_ref_states(&states);
 	} else if (opt_d && !opt_a && argc == 1) {
-		if (refs_delete_ref(get_main_ref_store(the_repository), NULL, buf.buf, NULL, REF_NO_DEREF))
-			result |= error(_("Could not delete %s"), buf.buf);
+		if (refs_delete_ref(refs, NULL, b_head.buf, NULL, REF_NO_DEREF))
+			result |= error(_("Could not delete %s"), b_head.buf);
 	} else
 		usage_with_options(builtin_remote_sethead_usage, options);
 
 	if (head_name) {
-		strbuf_addf(&buf2, "refs/remotes/%s/%s", argv[0], head_name);
+		strbuf_addf(&b_remote_head, "refs/remotes/%s/%s", argv[0], head_name);
 		/* make sure it's valid */
-		if (!refs_ref_exists(get_main_ref_store(the_repository), buf2.buf))
-			result |= error(_("Not a valid ref: %s"), buf2.buf);
-		else if (refs_update_symref(get_main_ref_store(the_repository), buf.buf, buf2.buf, "remote set-head"))
-			result |= error(_("Could not set up %s"), buf.buf);
+		if (!refs_ref_exists(refs, b_remote_head.buf))
+			result |= error(_("Not a valid ref: %s"), b_remote_head.buf);
+		else if (refs_update_symref(refs, b_head.buf, b_remote_head.buf, "remote set-head"))
+			result |= error(_("Could not set up %s"), b_head.buf);
 		else if (opt_a)
 			printf("%s/HEAD set to %s\n", argv[0], head_name);
 		free(head_name);
 	}
 
-	strbuf_release(&buf);
-	strbuf_release(&buf2);
+	strbuf_release(&b_head);
+	strbuf_release(&b_remote_head);
 	return result;
 }
 
Signature · 1 line
2.47.0.298.g52a96ec17b
#191 lore raw

PATCH v14 6/10 remote set-head: better output for --auto

In reply to Bence Ferdinandy

Currently, set-head --auto will print a message saying "remote/HEAD set to branch", which implies something was changed.

Change the output of --auto, so the output actually reflects what was
done: a) set a previously unset HEAD, b) change HEAD because remote changed or c) no updates. As edge cases, if HEAD is changed from a previous symbolic reference that was not a remote branch, explicitly call attention to this fact, and also notify the user if the previous reference was not a symbolic reference.

---

Notes:
v1-v2:

was RFC in https://lore.kernel.org/git/[email protected]/

v3:

This patch was originally sent along when I thought set-head was going to be invoked by fetch, but the discussion on the RFC concluded that it should be not. This opened the possibility to make it more explicit.

Note: although I feel both things the patch does are really just cosmetic, an argument could be made for breaking it into two, one for the no-op part and one for the --auto print update.

Was sent in:
https://lore.kernel.org/git/[email protected]/

v4:
- changes are now handled atomically via the ref update transaction
- outputs have changed along the lines of Junio's suggestion
- minor refactor to set_head for improved legibility

v5: - the minor refactor has been split out into its own patch

v6: - fixed uninitialized prev_head
- fixed case of unusual previous target
- fixed a test that would have been actually broken at this patch (the output was only correct with the later update to fetch)
- added 4 tests for the 4 output cases

v7: - change commit prefix to be more in line with project standards
- fixed tests to also work with the reftable backend
- renamed report function, fixed style issue with checking buf len
- fixed not releasing an strbuf

v8: no change

v9: - mark output strings in report_set_head_auto as translatable
- rename buf_prev to b_local_head for consistency
- use ${SQ} in tests instead of '\''

v10: no change

v11: no change

v12: no change

v13: added handling the edge case of previous remote/HEAD being a detached HEAD

v14: - fixed badly named variable
- fixed not reporting errors correctly

 builtin/remote.c  | 59 +++++++++++++++++++++++++++++++++++---------
 t/t5505-remote.sh | 63 ++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 109 insertions(+), 13 deletions(-)
builtin/remote.c +47 -12
index bcc3ee91ef..6d659d63ca 100644
@@ -1399,10 +1399,38 @@ static int show(int argc, const char **argv, const char *prefix)
 	return result;
 }
 
+static void report_set_head_auto(const char *remote, const char *head_name,
+			struct strbuf *b_local_head, int was_detached) {
+	struct strbuf buf_prefix = STRBUF_INIT;
+	const char *prev_head = NULL;
+
+	strbuf_addf(&buf_prefix, "refs/remotes/%s/", remote);
+	skip_prefix(b_local_head->buf, buf_prefix.buf, &prev_head);
+
+	if (prev_head && !strcmp(prev_head, head_name))
+		printf(_("'%s/HEAD' is unchanged and points to '%s'\n"),
+			remote, head_name);
+	else if (prev_head)
+		printf(_("'%s/HEAD' has changed from '%s' and now points to '%s'\n"),
+			remote, prev_head, head_name);
+	else if (!b_local_head->len)
+		printf(_("'%s/HEAD' is now created and points to '%s'\n"),
+			remote, head_name);
+	else if (was_detached && b_local_head->len)
+		printf(_("'%s/HEAD' was detached at '%s' and now points to '%s'\n"),
+			remote, b_local_head->buf, head_name);
+	else
+		printf(_("'%s/HEAD' used to point to '%s' "
+			"(which is not a remote branch), but now points to '%s'\n"),
+			remote, b_local_head->buf, head_name);
+	strbuf_release(&buf_prefix);
+}
+
 static int set_head(int argc, const char **argv, const char *prefix)
 {
-	int i, opt_a = 0, opt_d = 0, result = 0;
-	struct strbuf b_head = STRBUF_INIT, b_remote_head = STRBUF_INIT;
+	int i, opt_a = 0, opt_d = 0, result = 0, was_detached;
+	struct strbuf b_head = STRBUF_INIT, b_remote_head = STRBUF_INIT,
+		b_local_head = STRBUF_INIT;
 	char *head_name = NULL;
 	struct ref_store *refs = get_main_ref_store(the_repository);
 
@@ -1440,20 +1468,27 @@ static int set_head(int argc, const char **argv, const char *prefix)
 	} else
 		usage_with_options(builtin_remote_sethead_usage, options);
 
-	if (head_name) {
-		strbuf_addf(&b_remote_head, "refs/remotes/%s/%s", argv[0], head_name);
-		/* make sure it's valid */
-		if (!refs_ref_exists(refs, b_remote_head.buf))
-			result |= error(_("Not a valid ref: %s"), b_remote_head.buf);
-		else if (refs_update_symref(refs, b_head.buf, b_remote_head.buf, "remote set-head"))
-			result |= error(_("Could not set up %s"), b_head.buf);
-		else if (opt_a)
-			printf("%s/HEAD set to %s\n", argv[0], head_name);
-		free(head_name);
+	if (!head_name)
+		goto cleanup;
+	strbuf_addf(&b_remote_head, "refs/remotes/%s/%s", argv[0], head_name);
+	if (!refs_ref_exists(refs, b_remote_head.buf)) {
+		result |= error(_("Not a valid ref: %s"), b_remote_head.buf);
+		goto cleanup;
+	}
+	was_detached = refs_update_symref_extended(refs, b_head.buf, b_remote_head.buf,
+			"remote set-head", &b_local_head);
+	if (was_detached == -1) {
+		result |= error(_("Could not set up %s"), b_head.buf);
+		goto cleanup;
 	}
+	if (opt_a)
+		report_set_head_auto(argv[0], head_name, &b_local_head, was_detached);
 
+cleanup:
+	free(head_name);
 	strbuf_release(&b_head);
 	strbuf_release(&b_remote_head);
+	strbuf_release(&b_local_head);
 	return result;
 }
 
t/t5505-remote.sh +62 -1

80 lines not on this page · raw message

Signature · 1 line
2.47.0.298.g52a96ec17b
#192 lore raw

PATCH v14 7/10 refs: add TRANSACTION_CREATE_EXISTS error

In reply to Bence Ferdinandy

Currently there is only one special error for transaction, for when there is a naming conflict, all other errors are dumped under a generic error. Add a new special error case for when the caller requests the reference to be updated only when it does not yet exist and the reference actually does exist.

---

Notes:
v4: new patch
v5: no change
v6: no change

v7: - change commit prefix to be more in line with project standards
- changed error checking to Karthik's suggestion

v8: no change

v9: - no change

v10: no change

v11: no change

v12: no change

v13: no change

v14: no change

 refs.h                  |  4 +++-
 refs/files-backend.c    | 24 ++++++++++++++++--------
 refs/reftable-backend.c |  6 ++++--
 3 files changed, 23 insertions(+), 11 deletions(-)
refs.h +3 -1
index 5c46ac9f34..b243739e4b 100644
@@ -773,8 +773,10 @@ int ref_transaction_verify(struct ref_transaction *transaction,
 
 /* Naming conflict (for example, the ref names A and A/B conflict). */
 #define TRANSACTION_NAME_CONFLICT -1
+/* When only creation was requested, but the ref already exists. */
+#define TRANSACTION_CREATE_EXISTS -2
 /* All other errors. */
-#define TRANSACTION_GENERIC_ERROR -2
+#define TRANSACTION_GENERIC_ERROR -3
 
 /*
  * Perform the preparatory stages of committing `transaction`. Acquire
refs/files-backend.c +16 -8
index 4cc43c32f2..23ae74089d 100644
@@ -2501,14 +2501,18 @@ static int split_symref_update(struct ref_update *update,
 static int check_old_oid(struct ref_update *update, struct object_id *oid,
 			 struct strbuf *err)
 {
+	int ret = TRANSACTION_GENERIC_ERROR;
+
 	if (!(update->flags & REF_HAVE_OLD) ||
 		   oideq(oid, &update->old_oid))
 		return 0;
 
-	if (is_null_oid(&update->old_oid))
+	if (is_null_oid(&update->old_oid)) {
 		strbuf_addf(err, "cannot lock ref '%s': "
 			    "reference already exists",
 			    ref_update_original_update_refname(update));
+		ret = TRANSACTION_CREATE_EXISTS;
+	}
 	else if (is_null_oid(oid))
 		strbuf_addf(err, "cannot lock ref '%s': "
 			    "reference is missing but expected %s",
@@ -2521,7 +2525,7 @@ static int check_old_oid(struct ref_update *update, struct object_id *oid,
 			    oid_to_hex(oid),
 			    oid_to_hex(&update->old_oid));
 
-	return -1;
+	return ret;
 }
 
 /*
@@ -2601,9 +2605,11 @@ static int lock_ref_for_update(struct files_ref_store *refs,
 					ret = TRANSACTION_GENERIC_ERROR;
 					goto out;
 				}
-			} else if  (check_old_oid(update, &lock->old_oid, err)) {
-				ret = TRANSACTION_GENERIC_ERROR;
-				goto out;
+			} else {
+				ret = check_old_oid(update, &lock->old_oid, err);
+				if  (ret) {
+					goto out;
+				}
 			}
 		} else {
 			/*
@@ -2634,9 +2640,11 @@ static int lock_ref_for_update(struct files_ref_store *refs,
 				    update->old_target);
 			ret = TRANSACTION_GENERIC_ERROR;
 			goto out;
-		} else if  (check_old_oid(update, &lock->old_oid, err)) {
-			ret = TRANSACTION_GENERIC_ERROR;
-			goto out;
+		} else {
+			ret = check_old_oid(update, &lock->old_oid, err);
+			if  (ret) {
+				goto out;
+			}
 		}
 
 		/*
refs/reftable-backend.c +4 -2
index 1809e3426a..75b04e84a2 100644
@@ -1208,10 +1208,13 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store,
 				goto done;
 			}
 		} else if ((u->flags & REF_HAVE_OLD) && !oideq(&current_oid, &u->old_oid)) {
-			if (is_null_oid(&u->old_oid))
+			ret = TRANSACTION_NAME_CONFLICT;
+			if (is_null_oid(&u->old_oid)) {
 				strbuf_addf(err, _("cannot lock ref '%s': "
 						   "reference already exists"),
 					    ref_update_original_update_refname(u));
+				ret = TRANSACTION_CREATE_EXISTS;
+			}
 			else if (is_null_oid(&current_oid))
 				strbuf_addf(err, _("cannot lock ref '%s': "
 						   "reference is missing but expected %s"),
@@ -1223,7 +1226,6 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store,
 					    ref_update_original_update_refname(u),
 					    oid_to_hex(&current_oid),
 					    oid_to_hex(&u->old_oid));
-			ret = -1;
 			goto done;
 		}
 
Signature · 1 line
2.47.0.298.g52a96ec17b
#193 lore raw

PATCH v14 8/10 refs: add create_only option to refs_update_symref_extended

In reply to Bence Ferdinandy

Allow the caller to specify that it only wants to update the symref if it does not already exist. Silently ignore the error from the transaction API if the symref already exists.

---

Notes:
v4: new patch
v5: no change

v6: - switched from bool to int for create_only
- refactored logic in refs_update_symref with goto as layed out by Junio

v7: - change commit prefix to be more in line with project standards
- refactored code to accommodate changes in the first patch, but otherwise no change

v8: no change

v9: - no change (except for following through the
refs_update_symref_extended refactoring)

v10: no change

v11: no change

v12: no change

v13: changes only due to changes in previous patch

v14: no change

 builtin/remote.c |  2 +-
 refs.c           | 33 ++++++++++++++++++++++++---------
 refs.h           |  2 +-
 3 files changed, 26 insertions(+), 11 deletions(-)
builtin/remote.c +1 -1
index 6d659d63ca..3dc1135b5c 100644
@@ -1476,7 +1476,7 @@ static int set_head(int argc, const char **argv, const char *prefix)
 		goto cleanup;
 	}
 	was_detached = refs_update_symref_extended(refs, b_head.buf, b_remote_head.buf,
-			"remote set-head", &b_local_head);
+			"remote set-head", &b_local_head, 0);
 	if (was_detached == -1) {
 		result |= error(_("Could not set up %s"), b_head.buf);
 		goto cleanup;
refs.c +24 -9
index d80efd58f0..2efa6bcc5c 100644
@@ -2116,26 +2116,38 @@ int peel_iterated_oid(struct repository *r, const struct object_id *base, struct
 int refs_update_symref(struct ref_store *refs, const char *ref,
 		       const char *target, const char *logmsg)
 {
-	return refs_update_symref_extended(refs, ref, target, logmsg, NULL);
+	return refs_update_symref_extended(refs, ref, target, logmsg, NULL, 0);
 }
 
 int refs_update_symref_extended(struct ref_store *refs, const char *ref,
 		       const char *target, const char *logmsg,
-		       struct strbuf *referent)
+		       struct strbuf *referent, int create_only)
 {
 	struct ref_transaction *transaction;
 	struct strbuf err = STRBUF_INIT;
-	int ret = 0;
+	int ret = 0, prepret = 0;
 
 	transaction = ref_store_transaction_begin(refs, &err);
-	if (!transaction ||
-	    ref_transaction_update(transaction, ref, NULL, NULL,
-				   target, NULL, REF_NO_DEREF,
-				   logmsg, &err) ||
-	    ref_transaction_prepare(transaction, &err)) {
+	if (!transaction) {
+	error_return:
 		ret = error("%s", err.buf);
 		goto cleanup;
 	}
+	if (create_only) {
+		if (ref_transaction_create(transaction, ref, NULL, target,
+					   REF_NO_DEREF, logmsg, &err))
+			goto error_return;
+		prepret = ref_transaction_prepare(transaction, &err);
+		if (prepret && prepret != TRANSACTION_CREATE_EXISTS)
+			goto error_return;
+	} else {
+		if (ref_transaction_update(transaction, ref, NULL, NULL,
+					   target, NULL, REF_NO_DEREF,
+					   logmsg, &err) ||
+			ref_transaction_prepare(transaction, &err))
+			goto error_return;
+	}
+
 	if (referent && refs_read_symbolic_ref(refs, ref, referent) == NOT_A_SYMREF) {
 		struct object_id oid;
 		if (!refs_read_ref(refs, ref, &oid)) {
@@ -2144,8 +2156,11 @@ int refs_update_symref_extended(struct ref_store *refs, const char *ref,
 		}
 	}
 
+	if (prepret == TRANSACTION_CREATE_EXISTS)
+		goto cleanup;
+
 	if (ref_transaction_commit(transaction, &err))
-		ret = error("%s", err.buf);
+		goto error_return;
 
 cleanup:
 	strbuf_release(&err);
refs.h +1 -1
index b243739e4b..be38377b1f 100644
@@ -586,7 +586,7 @@ int refs_update_symref(struct ref_store *refs, const char *refname,
 
 int refs_update_symref_extended(struct ref_store *refs, const char *refname,
 		       const char *target, const char *logmsg,
-		       struct strbuf *referent);
+		       struct strbuf *referent, int create_only);
 
 enum action_on_err {
 	UPDATE_REFS_MSG_ON_ERR,
Signature · 1 line
2.47.0.298.g52a96ec17b
#194 lore raw

PATCH v14 9/10 fetch: set remote/HEAD if it does not exist

In reply to Bence Ferdinandy

When cloning a repository remote/HEAD is created, but when the user creates a repository with git init, and later adds a remote, remote/HEAD is only created if the user explicitly runs a variant of "remote set-head". Attempt to set remote/HEAD during fetch, if the user does not have it already set. Silently ignore any errors.

---

Notes:
v3: - does not rely on remote set-head anymore so it only authenticates
once
- uses the new REF_CREATE_ONLY to atomically check if the ref exists and only write it if it doesn't
- in all other cases the maximum it does is print a warning

v4: - instead of the discarded REF_CREATE_ONLY, it uses the existing,
but updated transaction api to request a silent create only
- it now uses the atomic before_target to determine reporting
- refactored for legibility

v5: - instead of printing a not too useful message, it now fails
silently, this in line with the objective to only set up remote/HEAD automatically if the right thing is trivial, for everything else there is remote set-head
- fixed all failing tests
- added two new tests, one for checking if remote/HEAD is set to the correct one, and one to test that we do not override remote/HEAD if it has changed on the server from what we have locally

v6: - fixed style issues and unintended extra empty line
- updated function call with bool to int from previous patch's change
- removed calls to error(...) inherited from builtin/remote.c so we actually fail silently
- set the test for remote set-head --auto to the correct value here, which was previously erronously set in the remote set-head patch

v7: - no change

v8: - changed logmsg in call to refs_update_symref from "remote
set-head" to "fetch"

v9: - follow through with refs_update_symref_extended
- fix test errors uncovered by the new patch

v10: no change

v11: fixed some memory leaks

v12: no change

v13: - fix printed information if the local HEAD is detached
- remove accidental formatting noise in a test

v14: remove report_set_head_auto: as discussed, any noise should only be created with an opt out, and the configuration for this will be added in a later patch

 builtin/fetch.c                  | 68 ++++++++++++++++++++++++++++++++
 t/t4207-log-decoration-colors.sh |  3 +-
 t/t5505-remote.sh                | 21 +++++++---
 t/t5510-fetch.sh                 | 24 +++++++++++
 t/t5512-ls-remote.sh             |  2 +
 t/t5514-fetch-multiple.sh        | 17 +++++++-
 t/t5516-fetch-push.sh            |  3 +-
 t/t5527-fetch-odd-refs.sh        |  3 +-
 t/t7900-maintenance.sh           |  3 +-
 t/t9210-scalar.sh                |  5 ++-
 t/t9211-scalar-clone.sh          |  6 +--
 t/t9902-completion.sh            | 65 ++++++++++++++++++++++++++++++
 12 files changed, 203 insertions(+), 17 deletions(-)
builtin/fetch.c +68 -0
index 18eff4e5fa..b569c87f97 100644
@@ -1578,6 +1578,66 @@ static int backfill_tags(struct display_state *display_state,
 	return retcode;
 }
 
+static const char *strip_refshead(const char *name){
+	skip_prefix(name, "refs/heads/", &name);
+	return name;
+}
+
+static int set_head(const struct ref *remote_refs)
+{
+	int result = 0;
+	struct strbuf b_head = STRBUF_INIT, b_remote_head = STRBUF_INIT;
+	const char *remote = gtransport->remote->name;
+	char *head_name = NULL;
+	struct ref *ref, *matches;
+	struct ref *fetch_map = NULL, **fetch_map_tail = &fetch_map;
+	struct refspec_item refspec = {
+		.force = 0,
+		.pattern = 1,
+		.src = (char *) "refs/heads/*",
+		.dst = (char *) "refs/heads/*",
+	};
+	struct string_list heads = STRING_LIST_INIT_DUP;
+	struct ref_store *refs = get_main_ref_store(the_repository);
+
+	get_fetch_map(remote_refs, &refspec, &fetch_map_tail, 0);
+	matches = guess_remote_head(find_ref_by_name(remote_refs, "HEAD"),
+				    fetch_map, 1);
+	for (ref = matches; ref; ref = ref->next) {
+		string_list_append(&heads, strip_refshead(ref->name));
+	}
+
+
+	if (!heads.nr)
+		result = 1;
+	else if (heads.nr > 1)
+		result = 1;
+	else
+		head_name = xstrdup(heads.items[0].string);
+
+	if (!head_name)
+		goto cleanup;
+	strbuf_addf(&b_head, "refs/remotes/%s/HEAD", remote);
+	strbuf_addf(&b_remote_head, "refs/remotes/%s/%s", remote, head_name);
+		/* make sure it's valid */
+	if (!refs_ref_exists(refs, b_remote_head.buf)) {
+		result = 1;
+		goto cleanup;
+	}
+	if (refs_update_symref_extended(refs, b_head.buf, b_remote_head.buf,
+					"fetch", NULL, 1))
+		result = 1;
+
+cleanup:
+	free(head_name);
+	free_refs(fetch_map);
+	free_refs(matches);
+	string_list_clear(&heads, 0);
+	strbuf_release(&b_head);
+	strbuf_release(&b_remote_head);
+	return result;
+}
+
 static int do_fetch(struct transport *transport,
 		    struct refspec *rs,
 		    const struct fetch_config *config)
@@ -1647,6 +1707,8 @@ static int do_fetch(struct transport *transport,
 				    "refs/tags/");
 	}
 
+	strvec_push(&transport_ls_refs_options.ref_prefixes, "HEAD");
+
 	if (must_list_refs) {
 		trace2_region_enter("fetch", "remote_refs", the_repository);
 		remote_refs = transport_get_remote_refs(transport,
@@ -1791,6 +1853,12 @@ static int do_fetch(struct transport *transport,
 				  "you need to specify exactly one branch with the --set-upstream option"));
 		}
 	}
+	if (set_head(remote_refs))
+		;
+		/*
+		 * Way too many cases where this can go wrong
+		 * so let's just fail silently for now.
+		 */
 
 cleanup:
 	if (retcode) {
t/t4207-log-decoration-colors.sh +2 -1
index 73ea9e5155..d55d22cb2f 100755
@@ -59,7 +59,8 @@ ${c_reset}${c_tag}tag: ${c_reset}${c_tag}v1.0${c_reset}${c_commit}, \
 ${c_reset}${c_tag}tag: ${c_reset}${c_tag}B${c_reset}${c_commit})${c_reset} B
 ${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_reset}\
 ${c_tag}tag: ${c_reset}${c_tag}A1${c_reset}${c_commit}, \
-${c_reset}${c_remoteBranch}other/main${c_reset}${c_commit})${c_reset} A1
+${c_reset}${c_remoteBranch}other/main${c_reset}${c_commit}, \
+${c_reset}${c_remoteBranch}other/HEAD${c_reset}${c_commit})${c_reset} A1
 	${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_reset}\
 ${c_stash}refs/stash${c_reset}${c_commit})${c_reset} On main: Changes to A.t
 	${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_reset}\
t/t5505-remote.sh +15 -6
index e6acff4afe..01d2601174 100755
@@ -74,7 +74,7 @@ test_expect_success 'add another remote' '
 		cd test &&
 		git remote add -f second ../two &&
 		tokens_match "origin second" "$(git remote)" &&
-		check_tracking_branch second main side another &&
+		check_tracking_branch second main side another HEAD &&
 		git for-each-ref "--format=%(refname)" refs/remotes |
 		sed -e "/^refs\/remotes\/origin\//d" \
 		    -e "/^refs\/remotes\/second\//d" >actual &&
@@ -500,7 +500,7 @@ test_expect_success 'set-head --auto has no problem w/multiple HEADs' '
 		cd test &&
 		git fetch two "refs/heads/*:refs/remotes/two/*" &&
 		git remote set-head --auto two >output 2>&1 &&
-		echo "${SQ}two/HEAD${SQ} is now created and points to ${SQ}main${SQ}" >expect &&
+		echo "${SQ}two/HEAD${SQ} is unchanged and points to ${SQ}main${SQ}" >expect &&
 		test_cmp expect output
 	)
 '
@@ -788,8 +788,10 @@ test_expect_success 'reject --no-no-tags' '
 '
 
 cat >one/expect <<\EOF
+  apis/HEAD -> apis/main
   apis/main
   apis/side
+  drosophila/HEAD -> drosophila/main
   drosophila/another
   drosophila/main
   drosophila/side
@@ -807,11 +809,14 @@ test_expect_success 'update' '
 '
 
 cat >one/expect <<\EOF
+  drosophila/HEAD -> drosophila/main
   drosophila/another
   drosophila/main
   drosophila/side
+  manduca/HEAD -> manduca/main
   manduca/main
   manduca/side
+  megaloprepus/HEAD -> megaloprepus/main
   megaloprepus/main
   megaloprepus/side
 EOF
@@ -819,7 +824,7 @@ EOF
 test_expect_success 'update with arguments' '
 	(
 		cd one &&
-		for b in $(git branch -r)
+		for b in $(git branch -r | grep -v HEAD)
 		do
 		git branch -r -d $b || exit 1
 		done &&
@@ -851,10 +856,13 @@ test_expect_success 'update --prune' '
 '
 
 cat >one/expect <<-\EOF
+  apis/HEAD -> apis/main
   apis/main
   apis/side
+  manduca/HEAD -> manduca/main
   manduca/main
   manduca/side
+  megaloprepus/HEAD -> megaloprepus/main
   megaloprepus/main
   megaloprepus/side
 EOF
@@ -862,7 +870,7 @@ EOF
 test_expect_success 'update default' '
 	(
 		cd one &&
-		for b in $(git branch -r)
+		for b in $(git branch -r | grep -v HEAD)
 		do
 		git branch -r -d $b || exit 1
 		done &&
@@ -874,6 +882,7 @@ test_expect_success 'update default' '
 '
 
 cat >one/expect <<\EOF
+  drosophila/HEAD -> drosophila/main
   drosophila/another
   drosophila/main
   drosophila/side
@@ -882,7 +891,7 @@ EOF
 test_expect_success 'update default (overridden, with funny whitespace)' '
 	(
 		cd one &&
-		for b in $(git branch -r)
+		for b in $(git branch -r | grep -v HEAD)
 		do
 		git branch -r -d $b || exit 1
 		done &&
@@ -896,7 +905,7 @@ test_expect_success 'update default (overridden, with funny whitespace)' '
 test_expect_success 'update (with remotes.default defined)' '
 	(
 		cd one &&
-		for b in $(git branch -r)
+		for b in $(git branch -r | grep -v HEAD)
 		do
 		git branch -r -d $b || exit 1
 		done &&
t/t5510-fetch.sh +24 -0
index 0890b9f61c..87698341f5 100755
@@ -75,6 +75,30 @@ test_expect_success "fetch test for-merge" '
 	cut -f -2 .git/FETCH_HEAD >actual &&
 	test_cmp expected actual'
 
+test_expect_success "fetch test remote HEAD" '
+	cd "$D" &&
+	cd two &&
+	git fetch &&
+	git rev-parse --verify refs/remotes/origin/HEAD &&
+	git rev-parse --verify refs/remotes/origin/main &&
+	head=$(git rev-parse refs/remotes/origin/HEAD) &&
+	branch=$(git rev-parse refs/remotes/origin/main) &&
+	test "z$head" = "z$branch"'
+
+test_expect_success "fetch test remote HEAD change" '
+	cd "$D" &&
+	cd two &&
+	git switch -c other &&
+	git push -u origin other &&
+	git rev-parse --verify refs/remotes/origin/HEAD &&
+	git rev-parse --verify refs/remotes/origin/main &&
+	git rev-parse --verify refs/remotes/origin/other &&
+	git remote set-head origin other &&
+	git fetch &&
+	head=$(git rev-parse refs/remotes/origin/HEAD) &&
+	branch=$(git rev-parse refs/remotes/origin/other) &&
+	test "z$head" = "z$branch"'
+
 test_expect_success 'fetch --prune on its own works as expected' '
 	cd "$D" &&
 	git clone . prune &&
t/t5512-ls-remote.sh +2 -0
index 64b3491e4e..1b3865e154 100755
@@ -293,6 +293,8 @@ test_expect_success 'ls-remote with filtered symref (refname)' '
 	cat >expect <<-EOF &&
 	ref: refs/heads/main	HEAD
 	$rev	HEAD
+	ref: refs/remotes/origin/main	refs/remotes/origin/HEAD
+	$rev	refs/remotes/origin/HEAD
 	EOF
 	git ls-remote --symref . HEAD >actual &&
 	test_cmp expect actual
t/t5514-fetch-multiple.sh +15 -2
index 579872c258..e3482b27b2 100755
@@ -45,14 +45,17 @@ test_expect_success setup '
 '
 
 cat > test/expect << EOF
+  one/HEAD -> one/main
   one/main
   one/side
   origin/HEAD -> origin/main
   origin/main
   origin/side
+  three/HEAD -> three/main
   three/another
   three/main
   three/side
+  two/HEAD -> two/main
   two/another
   two/main
   two/side
@@ -97,6 +100,7 @@ cat > expect << EOF
   origin/HEAD -> origin/main
   origin/main
   origin/side
+  three/HEAD -> three/main
   three/another
   three/main
   three/side
@@ -112,8 +116,10 @@ test_expect_success 'git fetch --multiple (but only one remote)' '
 '
 
 cat > expect << EOF
+  one/HEAD -> one/main
   one/main
   one/side
+  two/HEAD -> two/main
   two/another
   two/main
   two/side
@@ -141,7 +147,7 @@ test_expect_success 'git fetch --multiple (bad remote names)' '
 
 test_expect_success 'git fetch --all (skipFetchAll)' '
 	(cd test4 &&
-	 for b in $(git branch -r)
+	 for b in $(git branch -r | grep -v HEAD)
 	 do
 		git branch -r -d $b || exit 1
 	 done &&
@@ -153,11 +159,14 @@ test_expect_success 'git fetch --all (skipFetchAll)' '
 '
 
 cat > expect << EOF
+  one/HEAD -> one/main
   one/main
   one/side
+  three/HEAD -> three/main
   three/another
   three/main
   three/side
+  two/HEAD -> two/main
   two/another
   two/main
   two/side
@@ -165,7 +174,7 @@ EOF
 
 test_expect_success 'git fetch --multiple (ignoring skipFetchAll)' '
 	(cd test4 &&
-	 for b in $(git branch -r)
+	 for b in $(git branch -r | grep -v HEAD)
 	 do
 		git branch -r -d $b || exit 1
 	 done &&
@@ -221,14 +230,17 @@ test_expect_success 'git fetch --multiple --jobs=0 picks a default' '
 
 create_fetch_all_expect () {
 	cat >expect <<-\EOF
+	  one/HEAD -> one/main
 	  one/main
 	  one/side
 	  origin/HEAD -> origin/main
 	  origin/main
 	  origin/side
+	  three/HEAD -> three/main
 	  three/another
 	  three/main
 	  three/side
+	  two/HEAD -> two/main
 	  two/another
 	  two/main
 	  two/side
@@ -265,6 +277,7 @@ test_expect_success 'git fetch (fetch all remotes with fetch.all = true)' '
 
 create_fetch_one_expect () {
 	cat >expect <<-\EOF
+	  one/HEAD -> one/main
 	  one/main
 	  one/side
 	  origin/HEAD -> origin/main
t/t5516-fetch-push.sh +2 -1
index 331778bd42..5a051aa0c7 100755
@@ -1395,7 +1395,8 @@ test_expect_success 'fetch follows tags by default' '
 		git tag -m "annotated" tag &&
 		git for-each-ref >tmp1 &&
 		sed -n "p; s|refs/heads/main$|refs/remotes/origin/main|p" tmp1 |
-		sort -k 3 >../expect
+		sed -n "p; s|refs/heads/main$|refs/remotes/origin/HEAD|p"  |
+		sort -k 4 >../expect
 	) &&
 	test_when_finished "rm -rf dst" &&
 	git init dst &&
t/t5527-fetch-odd-refs.sh +2 -1
index 98ece27c6a..d3996af6ee 100755
@@ -52,7 +52,8 @@ test_expect_success LONG_REF 'fetch handles extremely long refname' '
 	long
 	main
 	EOF
-	git for-each-ref --format="%(subject)" refs/remotes/long >actual &&
+	git for-each-ref --format="%(subject)" refs/remotes/long \
+		--exclude=refs/remotes/long/HEAD >actual &&
 	test_cmp expect actual
 '
 
t/t7900-maintenance.sh +2 -1
index c224c8450c..edb85b7145 100755
@@ -329,7 +329,8 @@ test_expect_success 'incremental-repack task' '
 
 	# Delete refs that have not been repacked in these packs.
 	git for-each-ref --format="delete %(refname)" \
-		refs/prefetch refs/tags refs/remotes >refs &&
+		refs/prefetch refs/tags refs/remotes \
+		--exclude=refs/remotes/*/HEAD >refs &&
 	git update-ref --stdin <refs &&
 
 	# Replace the object directory with this pack layout.
t/t9210-scalar.sh +3 -2
index a30b2c9f70..2237844550 100755
@@ -151,7 +151,8 @@ test_expect_success 'scalar clone' '
 			"$(pwd)" &&
 
 		git for-each-ref --format="%(refname)" refs/remotes/origin/ >actual &&
-		echo "refs/remotes/origin/parallel" >expect &&
+		echo "refs/remotes/origin/HEAD" >>expect &&
+		echo "refs/remotes/origin/parallel" >>expect &&
 		test_cmp expect actual &&
 
 		test_path_is_missing 1/2 &&
@@ -220,7 +221,7 @@ test_expect_success 'scalar reconfigure --all with includeIf.onbranch' '
 	done
 '
 
- test_expect_success 'scalar reconfigure --all with detached HEADs' '
+test_expect_success 'scalar reconfigure --all with detached HEADs' '
 	repos="two three four" &&
 	for num in $repos
 	do
t/t9211-scalar-clone.sh +3 -3
index c16ea67c1d..d9cb6b9a3e 100755
@@ -32,7 +32,7 @@ test_expect_success 'set up repository to clone' '
 	)
 '
 
-cleanup_clone () {
+cleanup_clone() {
 	rm -rf "$1"
 }
 
@@ -128,7 +128,7 @@ test_expect_success '--single-branch clones HEAD only' '
 	(
 		cd $enlistment/src &&
 		git for-each-ref refs/remotes/origin >out &&
-		test_line_count = 1 out &&
+		test_line_count = 2 out &&
 		grep "refs/remotes/origin/base" out
 	) &&
 
@@ -142,7 +142,7 @@ test_expect_success '--no-single-branch clones all branches' '
 	(
 		cd $enlistment/src &&
 		git for-each-ref refs/remotes/origin >out &&
-		test_line_count = 2 out &&
+		test_line_count = 3 out &&
 		grep "refs/remotes/origin/base" out &&
 		grep "refs/remotes/origin/parallel" out
 	) &&
t/t9902-completion.sh +65 -0

443 lines not on this page · raw message

Signature · 1 line
2.47.0.298.g52a96ec17b
#195 lore raw

PATCH v14 10/10 fetch set_head: handle mirrored bare repositories

In reply to Bence Ferdinandy

When adding a remote to bare repository with "git remote add --mirror", running fetch will fail to update HEAD to the remote's HEAD, since it does not know how to handle bare repositories. On the other hand HEAD already has content, since "git init --bare" has already set HEAD to whatever is the default branch set for the user. Unless this - by chance
- is the same as the remote's HEAD, HEAD will be pointing to a bad symref. Teach set_head to handle bare repositories, by overwriting HEAD so it mirrors the remote's HEAD.

Note, that in this case overriding the local HEAD reference is necessary, since HEAD will exist before fetch can be run, but this should not be an issue, since the whole purpose of --mirror is to be an exact mirror of the remote, so following any changes to HEAD makes sense.

Also note, that although "git remote set-head" also fails when trying to update the remote's locally tracked HEAD in a mirrored bare repository, the usage of the command does not make much sense after this patch:
fetch will update the remote HEAD correctly, and setting it manually to something else is antithetical to the concept of mirroring.

---

Notes:
state:
* last review: none

v10: - new patch
- handles the issue discovered in
https://lore.kernel.org/git/[email protected]/T/

v11: no change

v12: no change

v13: properly print the previous head if it was detached

v14: no change

 builtin/fetch.c   | 16 +++++++++++-----
 t/t5505-remote.sh | 10 ++++++++++
 2 files changed, 21 insertions(+), 5 deletions(-)
builtin/fetch.c +11 -5
index b569c87f97..5103793191 100644
@@ -1585,7 +1585,7 @@ static const char *strip_refshead(const char *name){
 
 static int set_head(const struct ref *remote_refs)
 {
-	int result = 0;
+	int result = 0, is_bare;
 	struct strbuf b_head = STRBUF_INIT, b_remote_head = STRBUF_INIT;
 	const char *remote = gtransport->remote->name;
 	char *head_name = NULL;
@@ -1617,15 +1617,21 @@ static int set_head(const struct ref *remote_refs)
 
 	if (!head_name)
 		goto cleanup;
-	strbuf_addf(&b_head, "refs/remotes/%s/HEAD", remote);
-	strbuf_addf(&b_remote_head, "refs/remotes/%s/%s", remote, head_name);
+	is_bare = is_bare_repository();
+	if (is_bare) {
+		strbuf_addstr(&b_head, "HEAD");
+		strbuf_addf(&b_remote_head, "refs/heads/%s", head_name);
+	} else {
+		strbuf_addf(&b_head, "refs/remotes/%s/HEAD", remote);
+		strbuf_addf(&b_remote_head, "refs/remotes/%s/%s", remote, head_name);
+	}
 		/* make sure it's valid */
-	if (!refs_ref_exists(refs, b_remote_head.buf)) {
+	if (!is_bare && !refs_ref_exists(refs, b_remote_head.buf)) {
 		result = 1;
 		goto cleanup;
 	}
 	if (refs_update_symref_extended(refs, b_head.buf, b_remote_head.buf,
-					"fetch", NULL, 1))
+					"fetch", NULL, !is_bare))
 		result = 1;
 
 cleanup:
t/t5505-remote.sh +10 -0
index 01d2601174..b4f1ae5fb1 100755
@@ -569,6 +569,16 @@ test_expect_success 'add --mirror && prune' '
 	)
 '
 
+test_expect_success 'add --mirror setting HEAD' '
+	mkdir headmirror &&
+	(
+		cd headmirror &&
+		git init --bare -b notmain &&
+		git remote add --mirror -f origin ../one &&
+		test "$(git symbolic-ref HEAD)" = "refs/heads/main"
+	)
+'
+
 test_expect_success 'add --mirror=fetch' '
 	mkdir mirror-fetch &&
 	git init -b main mirror-fetch/parent &&
Signature · 1 line
2.47.0.298.g52a96ec17b
#196 lore raw

v15 cover set-head/fetch remote/HEAD updates

In reply to Bence Ferdinandy

2/10: use test_must_fail and test_when_finished
3/10: whitespace fix

Bence Ferdinandy (10):
t/t5505-remote: set default branch to main t/t5505-remote: test failure of set-head
refs: standardize output of refs_read_symbolic_ref
refs: atomically record overwritten ref in update_symref remote set-head: refactor for readability
remote set-head: better output for --auto
refs: add TRANSACTION_CREATE_EXISTS error
refs: add create_only option to refs_update_symref_extended
fetch: set remote/HEAD if it does not exist
fetch set_head: handle mirrored bare repositories

 builtin/fetch.c                  |  74 +++++++++++++++++++++
 builtin/remote.c                 |  72 +++++++++++++++------
 refs.c                           |  47 ++++++++++++--
 refs.h                           |  19 +++++-
 refs/files-backend.c             |  31 +++++----
 refs/refs-internal.h             |   5 ++
 refs/reftable-backend.c          |  12 ++--
 t/t4207-log-decoration-colors.sh |   3 +-
 t/t5505-remote.sh                | 107 +++++++++++++++++++++++++++++--
 t/t5510-fetch.sh                 |  24 +++++++
 t/t5512-ls-remote.sh             |   2 +
 t/t5514-fetch-multiple.sh        |  17 ++++-
 t/t5516-fetch-push.sh            |   3 +-
 t/t5527-fetch-odd-refs.sh        |   3 +-
 t/t7900-maintenance.sh           |   3 +-
 t/t9210-scalar.sh                |   5 +-
 t/t9211-scalar-clone.sh          |   6 +-
 t/t9902-completion.sh            |  65 +++++++++++++++++++
 18 files changed, 439 insertions(+), 59 deletions(-)

Range-diff against v14:

 1:  49cfd222d5 =  1:  2e1d001d1a t/t5505-remote: set default branch to main
 2:  54bf9c7fff !  2:  d24e62035a t/t5505-remote: test failure of set-head
    @@ Commit message
      ## Notes ##
         v14: new patch
     
    +    v15: - use test_must_fail and test_when_finished
    +
      ## builtin/remote.c ##
     @@ builtin/remote.c: static int set_head(int argc, const char **argv, const char *prefix)
      		if (!refs_ref_exists(get_main_ref_store(the_repository), buf2.buf))
    @@ t/t5505-remote.sh: test_expect_success 'set-head --auto' '
      '
      
     +test_expect_success REFFILES 'set-head --auto failure' '
    ++	test_when_finished "rm -f test/.git/refs/remotes/origin/HEAD.lock" &&
     +	(
     +		cd test &&
     +		touch .git/refs/remotes/origin/HEAD.lock &&
    -+		git remote set-head --auto origin 2>errormsg ||
    -+		tail -n1 errormsg >output &&
    -+		rm .git/refs/remotes/origin/HEAD.lock &&
    ++		test_must_fail git remote set-head --auto origin 2>err &&
    ++		tail -n1 err >output &&
     +		echo "error: Could not set up refs/remotes/origin/HEAD" >expect &&
     +		test_cmp expect output
     +	)
 3:  c95362236e !  3:  1218d521e5 refs: standardize output of refs_read_symbolic_ref
    @@ Notes
              - rewrote comment in refs-internal.h to point to the one in refs.h
              - created NOT_A_SYMREF=-2 constant
     
    +    v15: fix whitespace issues
    +
      ## refs.h ##
     @@ refs.h: int refs_read_ref_full(struct ref_store *refs, const char *refname,
      
    @@ refs/reftable-backend.c: static int reftable_be_read_symbolic_ref(struct ref_sto
      
      	ret = reftable_stack_read_ref(stack, refname, &ref);
     -	if (ret == 0 && ref.value_type == REFTABLE_REF_SYMREF)
    -+        if (ret)
    -+                ret = -1;
    -+        else if (ref.value_type == REFTABLE_REF_SYMREF)
    ++	if (ret)
    ++		ret = -1;
    ++	else if (ref.value_type == REFTABLE_REF_SYMREF)
      		strbuf_addstr(referent, ref.value.symref);
    --	else
    + 	else
     -		ret = -1;
    -+        else
    -+                ret = NOT_A_SYMREF;
    ++		ret = NOT_A_SYMREF;
      
      	reftable_ref_record_release(&ref);
      	return ret;

4: 21533e0e6b ! 4: 4d5f3f4f01 refs: atomically record overwritten ref in update_symref
@@ Notes
- the returned error code by the above was also incorrect and now is -2 (and uses NOT_A_SYMREF now)

+ v15: no change
+
## refs.c ##
@@ refs.c: int peel_iterated_oid(struct repository *r, const struct object_id *base, struct

5: 2455768c24 ! 5: 6f2cb51b0b remote set-head: refactor for readability
@@ Notes

v14: no change

+ v15: no change
+
## builtin/remote.c ##
@@ builtin/remote.c: static int show(int argc, const char **argv, const char *prefix)
static int set_head(int argc, const char **argv, const char *prefix)
6: 396537a598 ! 6: ca8687bae1 remote set-head: better output for --auto
@@ Notes
v14: - fixed badly named variable
- fixed not reporting errors correctly

+ v15: no change
+
## builtin/remote.c ##
@@ builtin/remote.c: static int show(int argc, const char **argv, const char *prefix)
return result;
7: 05e97155d1 ! 7: 45bcb33b52 refs: add TRANSACTION_CREATE_EXISTS error
@@ Notes

v14: no change

+ v15: no change
+
## refs.h ##
@@ refs.h: int ref_transaction_verify(struct ref_transaction *transaction,

8: 3ea8b1b8b6 ! 8: a8a522a089 refs: add create_only option to refs_update_symref_extended
@@ Notes

v14: no change

+ v15: no change
+
## builtin/remote.c ##
@@ builtin/remote.c: static int set_head(int argc, const char **argv, const char *prefix)
goto cleanup;
9: 287c25b801 ! 9: ff294461d8 fetch: set remote/HEAD if it does not exist
@@ Notes
created with an opt out, and the configuration for this will be added in a later patch

+ v15: no change
+
## builtin/fetch.c ##
@@ builtin/fetch.c: static int backfill_tags(struct display_state *display_state,
return retcode;
10: 05d8526399 ! 10: 0e96260ab6 fetch set_head: handle mirrored bare repositories

    @@ Notes
     
         v14: no change
     
    +    v15: no change
    +
      ## builtin/fetch.c ##
     @@ builtin/fetch.c: static const char *strip_refshead(const char *name){
Signature · 1 line
2.47.0.298.g52a96ec17b
#197 lore raw

PATCH v15 1/10 t/t5505-remote: set default branch to main

Consider the bare repository called "mirror" in the test. Running git remote add --mirror -f origin ../one will not change HEAD, consequently if init.defaultBranch is not the same as what HEAD in the remote ("one"), HEAD in "mirror" will be pointing to a non-existent reference. Hence if "mirror" is used as a remote by yet another repository, ls-remote will not show HEAD. On the other hand, if init.defaultBranch happens to match HEAD in "one", then ls-remote will show HEAD.

Since the "ci/run-build-and-tests.sh" script globally exports GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main for some (but not all) jobs, there may be a drift in some tests between how the test repositories are set up in the CI and during local testing, if the test itself uses "master" as default instead of "main". In particular, this happens in t5505-remote.sh. This issue does not manifest currently, as the test does not do any remote HEAD manipulation where this would come up, but should such things be added, a locally passing test would break the CI and vice-versa.

Set GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main in t5505-remote to be consistent with the CI.

---

Notes:
v9: - new patch
- a bandaid for the CI issue noticed by Taylor (cf:
https://lore.kernel.org/git/Zw8IKyPkG0Hr6%[email protected]/), but
see
https://lore.kernel.org/git/[email protected]/ for the root cause in detail

v10: no change

v11: no change

v12: added forgotten sign-off

v13: commit message udpated to be more precise

v14: made explicit in the commit message that the default branch is only
set for some CI jobs

v15: no change

 t/t5505-remote.sh | 3 +++
 1 file changed, 3 insertions(+)
t/t5505-remote.sh +3 -0
index 532035933f..9b50276646 100755
@@ -2,6 +2,9 @@
 
 test_description='git remote porcelain-ish'
 
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
 TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
Signature · 1 line
2.47.0.298.g52a96ec17b
#198 lore raw

PATCH v15 2/10 t/t5505-remote: test failure of set-head

In reply to Bence Ferdinandy

The test coverage was missing a test for the failure branch of remote set-head auto's output. Add the missing text and while we are at it, correct a small grammatical mistake in the error's output ("setup" is the noun, "set up" is the verb).

---

Notes:
v14: new patch

v15: - use test_must_fail and test_when_finished

 builtin/remote.c  |  2 +-
 t/t5505-remote.sh | 12 ++++++++++++
 2 files changed, 13 insertions(+), 1 deletion(-)
builtin/remote.c +1 -1
index 9093600965..8bca3fb04e 100644
@@ -1445,7 +1445,7 @@ static int set_head(int argc, const char **argv, const char *prefix)
 		if (!refs_ref_exists(get_main_ref_store(the_repository), buf2.buf))
 			result |= error(_("Not a valid ref: %s"), buf2.buf);
 		else if (refs_update_symref(get_main_ref_store(the_repository), buf.buf, buf2.buf, "remote set-head"))
-			result |= error(_("Could not setup %s"), buf.buf);
+			result |= error(_("Could not set up %s"), buf.buf);
 		else if (opt_a)
 			printf("%s/HEAD set to %s\n", argv[0], head_name);
 		free(head_name);
t/t5505-remote.sh +12 -0
index 9b50276646..61e3ecc1af 100755
@@ -432,6 +432,18 @@ test_expect_success 'set-head --auto' '
 	)
 '
 
+test_expect_success REFFILES 'set-head --auto failure' '
+	test_when_finished "rm -f test/.git/refs/remotes/origin/HEAD.lock" &&
+	(
+		cd test &&
+		touch .git/refs/remotes/origin/HEAD.lock &&
+		test_must_fail git remote set-head --auto origin 2>err &&
+		tail -n1 err >output &&
+		echo "error: Could not set up refs/remotes/origin/HEAD" >expect &&
+		test_cmp expect output
+	)
+'
+
 test_expect_success 'set-head --auto has no problem w/multiple HEADs' '
 	(
 		cd test &&
Signature · 1 line
2.47.0.298.g52a96ec17b
#199 lore raw

PATCH v15 3/10 refs: standardize output of refs_read_symbolic_ref

In reply to Bence Ferdinandy

When the symbolic reference we want to read with refs_read_symbolic_ref is actually not a symbolic reference, the files and the reftable backends return different values (1 and -1 respectively). Standardize the returned values so that 0 is success, -1 is a generic error and -2 is that the reference was actually non-symbolic.

---

Notes:
v13: new patch

v14: - simplified the logic in reftables backend (thanks Junio)
- update the comment in refs.h (thanks Patrick)
- rewrote comment in refs-internal.h to point to the one in refs.h
- created NOT_A_SYMREF=-2 constant

v15: fix whitespace issues

 refs.h                  | 11 +++++++++++
 refs/files-backend.c    |  7 +++----
 refs/refs-internal.h    |  5 +++++
 refs/reftable-backend.c |  6 ++++--
 4 files changed, 23 insertions(+), 6 deletions(-)
refs.h +11 -0
index 108dfc93b3..22c2b45b8b 100644
@@ -83,6 +83,17 @@ int refs_read_ref_full(struct ref_store *refs, const char *refname,
 
 int refs_read_ref(struct ref_store *refs, const char *refname, struct object_id *oid);
 
+#define NOT_A_SYMREF -2
+
+/*
+ * Read the symbolic ref named "refname" and write its immediate referent into
+ * the provided buffer. Referent is left empty if "refname" is not a symbolic
+ * ref. It does not resolve the symbolic reference recursively in case the
+ * target is also a symbolic ref.
+ *
+ * Returns 0 on success, -2 if the "refname" is not a symbolic ref,
+ * -1 otherwise.
+ */
 int refs_read_symbolic_ref(struct ref_store *ref_store, const char *refname,
 			   struct strbuf *referent);
 
refs/files-backend.c +3 -4
index 0824c0b8a9..4cc43c32f2 100644
@@ -596,10 +596,9 @@ static int files_read_symbolic_ref(struct ref_store *ref_store, const char *refn
 	unsigned int type;
 
 	ret = read_ref_internal(ref_store, refname, &oid, referent, &type, &failure_errno, 1);
-	if (ret)
-		return ret;
-
-	return !(type & REF_ISSYMREF);
+	if (!ret && !(type & REF_ISSYMREF))
+		return NOT_A_SYMREF;
+	return ret;
 }
 
 int parse_loose_ref_contents(const struct git_hash_algo *algop,
refs/refs-internal.h +5 -0
index 2313c830d8..1399fee61c 100644
@@ -673,6 +673,11 @@ struct ref_storage_be {
 
 	ref_iterator_begin_fn *iterator_begin;
 	read_raw_ref_fn *read_raw_ref;
+
+	/*
+	 * Please refer to `refs_read_symbolic_ref()` for the expected
+	 * behaviour.
+	 */
 	read_symbolic_ref_fn *read_symbolic_ref;
 
 	reflog_iterator_begin_fn *reflog_iterator_begin;
refs/reftable-backend.c +4 -2
index 38eb14d591..e8afcda1f9 100644
@@ -830,10 +830,12 @@ static int reftable_be_read_symbolic_ref(struct ref_store *ref_store,
 		return ret;
 
 	ret = reftable_stack_read_ref(stack, refname, &ref);
-	if (ret == 0 && ref.value_type == REFTABLE_REF_SYMREF)
+	if (ret)
+		ret = -1;
+	else if (ref.value_type == REFTABLE_REF_SYMREF)
 		strbuf_addstr(referent, ref.value.symref);
 	else
-		ret = -1;
+		ret = NOT_A_SYMREF;
 
 	reftable_ref_record_release(&ref);
 	return ret;
Signature · 1 line
2.47.0.298.g52a96ec17b
#200 lore raw

PATCH v15 4/10 refs: atomically record overwritten ref in update_symref

In reply to Bence Ferdinandy

When updating a symref with update_symref it's currently not possible to know for sure what was the previous value that was overwritten. Extend refs_update_symref under a new function name, to record the value after the ref has been locked if the caller of refs_update_symref_extended requests it via a new variable in the function call. Make the return value of the function notify the caller, if the previous value was actually not a symbolic reference. Keep the original refs_update_symref function with the same signature, but now as a wrapper around refs_update_symref_extended.

---

Notes:
v4: new patch

v5: - added before_target to reftables backend
- added an extra safety check for transaction's existence in refs.c

v6: - no change

v7: - remove the whole before_target concept from the backends and
handle checking it in refs.c instead (thanks Karthik)
- rename the before_target to referent which is how the same concept is called in the backends
- change commit prefix to be more in line with project standards

v8: no change

v9: - instead of adding parameters to refs_update_symref, rename what
was in v8 as refs_update_symref_extended and make refs_update_symref a wrapper for that. This significantly reduces the number of files that need to be touched, and avoids adding a lot of dummy NULL-s in unrelated places.

v10: no change

v11: no change

v12: no change

v13: if referent is a non-symbolic ref, record the hash in referent and
signal this with a return value of -1

v14: - mistakes were made: trying to read the referent should NOT lead to
an actual error, even if it doesn't work out in the end, that part of the code should only set the return to -2 or not touch it
- the returned error code by the above was also incorrect and now is -2 (and uses NOT_A_SYMREF now)

v15: no change

 refs.c | 22 ++++++++++++++++++++--
 refs.h |  4 ++++
 2 files changed, 24 insertions(+), 2 deletions(-)
refs.c +20 -2
index 5f729ed412..d80efd58f0 100644
@@ -2115,6 +2115,13 @@ int peel_iterated_oid(struct repository *r, const struct object_id *base, struct
 
 int refs_update_symref(struct ref_store *refs, const char *ref,
 		       const char *target, const char *logmsg)
+{
+	return refs_update_symref_extended(refs, ref, target, logmsg, NULL);
+}
+
+int refs_update_symref_extended(struct ref_store *refs, const char *ref,
+		       const char *target, const char *logmsg,
+		       struct strbuf *referent)
 {
 	struct ref_transaction *transaction;
 	struct strbuf err = STRBUF_INIT;
@@ -2125,10 +2132,22 @@ int refs_update_symref(struct ref_store *refs, const char *ref,
 	    ref_transaction_update(transaction, ref, NULL, NULL,
 				   target, NULL, REF_NO_DEREF,
 				   logmsg, &err) ||
-	    ref_transaction_commit(transaction, &err)) {
+	    ref_transaction_prepare(transaction, &err)) {
 		ret = error("%s", err.buf);
+		goto cleanup;
 	}
+	if (referent && refs_read_symbolic_ref(refs, ref, referent) == NOT_A_SYMREF) {
+		struct object_id oid;
+		if (!refs_read_ref(refs, ref, &oid)) {
+			strbuf_addstr(referent, oid_to_hex(&oid));
+			ret = NOT_A_SYMREF;
+		}
+	}
+
+	if (ref_transaction_commit(transaction, &err))
+		ret = error("%s", err.buf);
 
+cleanup:
 	strbuf_release(&err);
 	if (transaction)
 		ref_transaction_free(transaction);
@@ -2948,4 +2967,3 @@ int ref_update_expects_existing_old_ref(struct ref_update *update)
 	return (update->flags & REF_HAVE_OLD) &&
 		(!is_null_oid(&update->old_oid) || update->old_target);
 }
-
refs.h +4 -0
index 22c2b45b8b..5c46ac9f34 100644
@@ -584,6 +584,10 @@ int refs_copy_existing_ref(struct ref_store *refs, const char *oldref,
 int refs_update_symref(struct ref_store *refs, const char *refname,
 		       const char *target, const char *logmsg);
 
+int refs_update_symref_extended(struct ref_store *refs, const char *refname,
+		       const char *target, const char *logmsg,
+		       struct strbuf *referent);
+
 enum action_on_err {
 	UPDATE_REFS_MSG_ON_ERR,
 	UPDATE_REFS_DIE_ON_ERR,
Signature · 1 line
2.47.0.298.g52a96ec17b
#201 lore raw

PATCH v15 5/10 remote set-head: refactor for readability

In reply to Bence Ferdinandy

Make two different readability refactors:

Rename strbufs "buf" and "buf2" to something more explanatory.

Instead of calling get_main_ref_store(the_repository) multiple times, call it once and store the result in a new refs variable. Although this change probably offers some performance benefits, the main purpose is to shorten the line lengths of function calls using this variable.

---

Notes:
v5: new patch (split from the next patch as a preparatory step)

v6: no change

v7: - change commit prefix to be more in line with project standards

v8: no change

v9: - further improve readability by renaming buf, and buf2 consistently
with how patch 6 was already done

v10: no change

v11: no change

v12: no change

v13: more precise wording for commit message

v14: no change

v15: no change

 builtin/remote.c | 23 ++++++++++++-----------
 1 file changed, 12 insertions(+), 11 deletions(-)
builtin/remote.c +12 -11
index 8bca3fb04e..47ca650de8 100644
@@ -1402,8 +1402,9 @@ static int show(int argc, const char **argv, const char *prefix)
 static int set_head(int argc, const char **argv, const char *prefix)
 {
 	int i, opt_a = 0, opt_d = 0, result = 0;
-	struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT;
+	struct strbuf b_head = STRBUF_INIT, b_remote_head = STRBUF_INIT;
 	char *head_name = NULL;
+	struct ref_store *refs = get_main_ref_store(the_repository);
 
 	struct option options[] = {
 		OPT_BOOL('a', "auto", &opt_a,
@@ -1415,7 +1416,7 @@ static int set_head(int argc, const char **argv, const char *prefix)
 	argc = parse_options(argc, argv, prefix, options,
 			     builtin_remote_sethead_usage, 0);
 	if (argc)
-		strbuf_addf(&buf, "refs/remotes/%s/HEAD", argv[0]);
+		strbuf_addf(&b_head, "refs/remotes/%s/HEAD", argv[0]);
 
 	if (!opt_a && !opt_d && argc == 2) {
 		head_name = xstrdup(argv[1]);
@@ -1434,25 +1435,25 @@ static int set_head(int argc, const char **argv, const char *prefix)
 			head_name = xstrdup(states.heads.items[0].string);
 		free_remote_ref_states(&states);
 	} else if (opt_d && !opt_a && argc == 1) {
-		if (refs_delete_ref(get_main_ref_store(the_repository), NULL, buf.buf, NULL, REF_NO_DEREF))
-			result |= error(_("Could not delete %s"), buf.buf);
+		if (refs_delete_ref(refs, NULL, b_head.buf, NULL, REF_NO_DEREF))
+			result |= error(_("Could not delete %s"), b_head.buf);
 	} else
 		usage_with_options(builtin_remote_sethead_usage, options);
 
 	if (head_name) {
-		strbuf_addf(&buf2, "refs/remotes/%s/%s", argv[0], head_name);
+		strbuf_addf(&b_remote_head, "refs/remotes/%s/%s", argv[0], head_name);
 		/* make sure it's valid */
-		if (!refs_ref_exists(get_main_ref_store(the_repository), buf2.buf))
-			result |= error(_("Not a valid ref: %s"), buf2.buf);
-		else if (refs_update_symref(get_main_ref_store(the_repository), buf.buf, buf2.buf, "remote set-head"))
-			result |= error(_("Could not set up %s"), buf.buf);
+		if (!refs_ref_exists(refs, b_remote_head.buf))
+			result |= error(_("Not a valid ref: %s"), b_remote_head.buf);
+		else if (refs_update_symref(refs, b_head.buf, b_remote_head.buf, "remote set-head"))
+			result |= error(_("Could not set up %s"), b_head.buf);
 		else if (opt_a)
 			printf("%s/HEAD set to %s\n", argv[0], head_name);
 		free(head_name);
 	}
 
-	strbuf_release(&buf);
-	strbuf_release(&buf2);
+	strbuf_release(&b_head);
+	strbuf_release(&b_remote_head);
 	return result;
 }
 
Signature · 1 line
2.47.0.298.g52a96ec17b
#202 lore raw

PATCH v15 6/10 remote set-head: better output for --auto

In reply to Bence Ferdinandy

Currently, set-head --auto will print a message saying "remote/HEAD set to branch", which implies something was changed.

Change the output of --auto, so the output actually reflects what was
done: a) set a previously unset HEAD, b) change HEAD because remote changed or c) no updates. As edge cases, if HEAD is changed from a previous symbolic reference that was not a remote branch, explicitly call attention to this fact, and also notify the user if the previous reference was not a symbolic reference.

---

Notes:
v1-v2:

was RFC in https://lore.kernel.org/git/[email protected]/

v3:

This patch was originally sent along when I thought set-head was going to be invoked by fetch, but the discussion on the RFC concluded that it should be not. This opened the possibility to make it more explicit.

Note: although I feel both things the patch does are really just cosmetic, an argument could be made for breaking it into two, one for the no-op part and one for the --auto print update.

Was sent in:
https://lore.kernel.org/git/[email protected]/

v4:
- changes are now handled atomically via the ref update transaction
- outputs have changed along the lines of Junio's suggestion
- minor refactor to set_head for improved legibility

v5: - the minor refactor has been split out into its own patch

v6: - fixed uninitialized prev_head
- fixed case of unusual previous target
- fixed a test that would have been actually broken at this patch (the output was only correct with the later update to fetch)
- added 4 tests for the 4 output cases

v7: - change commit prefix to be more in line with project standards
- fixed tests to also work with the reftable backend
- renamed report function, fixed style issue with checking buf len
- fixed not releasing an strbuf

v8: no change

v9: - mark output strings in report_set_head_auto as translatable
- rename buf_prev to b_local_head for consistency
- use ${SQ} in tests instead of '\''

v10: no change

v11: no change

v12: no change

v13: added handling the edge case of previous remote/HEAD being a detached HEAD

v14: - fixed badly named variable
- fixed not reporting errors correctly

v15: no change

 builtin/remote.c  | 59 +++++++++++++++++++++++++++++++++++---------
 t/t5505-remote.sh | 63 ++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 109 insertions(+), 13 deletions(-)
builtin/remote.c +47 -12
index 47ca650de8..e1f15e68f4 100644
@@ -1399,10 +1399,38 @@ static int show(int argc, const char **argv, const char *prefix)
 	return result;
 }
 
+static void report_set_head_auto(const char *remote, const char *head_name,
+			struct strbuf *b_local_head, int was_detached) {
+	struct strbuf buf_prefix = STRBUF_INIT;
+	const char *prev_head = NULL;
+
+	strbuf_addf(&buf_prefix, "refs/remotes/%s/", remote);
+	skip_prefix(b_local_head->buf, buf_prefix.buf, &prev_head);
+
+	if (prev_head && !strcmp(prev_head, head_name))
+		printf(_("'%s/HEAD' is unchanged and points to '%s'\n"),
+			remote, head_name);
+	else if (prev_head)
+		printf(_("'%s/HEAD' has changed from '%s' and now points to '%s'\n"),
+			remote, prev_head, head_name);
+	else if (!b_local_head->len)
+		printf(_("'%s/HEAD' is now created and points to '%s'\n"),
+			remote, head_name);
+	else if (was_detached && b_local_head->len)
+		printf(_("'%s/HEAD' was detached at '%s' and now points to '%s'\n"),
+			remote, b_local_head->buf, head_name);
+	else
+		printf(_("'%s/HEAD' used to point to '%s' "
+			"(which is not a remote branch), but now points to '%s'\n"),
+			remote, b_local_head->buf, head_name);
+	strbuf_release(&buf_prefix);
+}
+
 static int set_head(int argc, const char **argv, const char *prefix)
 {
-	int i, opt_a = 0, opt_d = 0, result = 0;
-	struct strbuf b_head = STRBUF_INIT, b_remote_head = STRBUF_INIT;
+	int i, opt_a = 0, opt_d = 0, result = 0, was_detached;
+	struct strbuf b_head = STRBUF_INIT, b_remote_head = STRBUF_INIT,
+		b_local_head = STRBUF_INIT;
 	char *head_name = NULL;
 	struct ref_store *refs = get_main_ref_store(the_repository);
 
@@ -1440,20 +1468,27 @@ static int set_head(int argc, const char **argv, const char *prefix)
 	} else
 		usage_with_options(builtin_remote_sethead_usage, options);
 
-	if (head_name) {
-		strbuf_addf(&b_remote_head, "refs/remotes/%s/%s", argv[0], head_name);
-		/* make sure it's valid */
-		if (!refs_ref_exists(refs, b_remote_head.buf))
-			result |= error(_("Not a valid ref: %s"), b_remote_head.buf);
-		else if (refs_update_symref(refs, b_head.buf, b_remote_head.buf, "remote set-head"))
-			result |= error(_("Could not set up %s"), b_head.buf);
-		else if (opt_a)
-			printf("%s/HEAD set to %s\n", argv[0], head_name);
-		free(head_name);
+	if (!head_name)
+		goto cleanup;
+	strbuf_addf(&b_remote_head, "refs/remotes/%s/%s", argv[0], head_name);
+	if (!refs_ref_exists(refs, b_remote_head.buf)) {
+		result |= error(_("Not a valid ref: %s"), b_remote_head.buf);
+		goto cleanup;
+	}
+	was_detached = refs_update_symref_extended(refs, b_head.buf, b_remote_head.buf,
+			"remote set-head", &b_local_head);
+	if (was_detached == -1) {
+		result |= error(_("Could not set up %s"), b_head.buf);
+		goto cleanup;
 	}
+	if (opt_a)
+		report_set_head_auto(argv[0], head_name, &b_local_head, was_detached);
 
+cleanup:
+	free(head_name);
 	strbuf_release(&b_head);
 	strbuf_release(&b_remote_head);
+	strbuf_release(&b_local_head);
 	return result;
 }
 
t/t5505-remote.sh +62 -1

80 lines not on this page · raw message

Signature · 1 line
2.47.0.298.g52a96ec17b
#203 lore raw

PATCH v15 7/10 refs: add TRANSACTION_CREATE_EXISTS error

In reply to Bence Ferdinandy

Currently there is only one special error for transaction, for when there is a naming conflict, all other errors are dumped under a generic error. Add a new special error case for when the caller requests the reference to be updated only when it does not yet exist and the reference actually does exist.

---

Notes:
v4: new patch
v5: no change
v6: no change

v7: - change commit prefix to be more in line with project standards
- changed error checking to Karthik's suggestion

v8: no change

v9: - no change

v10: no change

v11: no change

v12: no change

v13: no change

v14: no change

v15: no change

 refs.h                  |  4 +++-
 refs/files-backend.c    | 24 ++++++++++++++++--------
 refs/reftable-backend.c |  6 ++++--
 3 files changed, 23 insertions(+), 11 deletions(-)
refs.h +3 -1
index 5c46ac9f34..b243739e4b 100644
@@ -773,8 +773,10 @@ int ref_transaction_verify(struct ref_transaction *transaction,
 
 /* Naming conflict (for example, the ref names A and A/B conflict). */
 #define TRANSACTION_NAME_CONFLICT -1
+/* When only creation was requested, but the ref already exists. */
+#define TRANSACTION_CREATE_EXISTS -2
 /* All other errors. */
-#define TRANSACTION_GENERIC_ERROR -2
+#define TRANSACTION_GENERIC_ERROR -3
 
 /*
  * Perform the preparatory stages of committing `transaction`. Acquire
refs/files-backend.c +16 -8
index 4cc43c32f2..23ae74089d 100644
@@ -2501,14 +2501,18 @@ static int split_symref_update(struct ref_update *update,
 static int check_old_oid(struct ref_update *update, struct object_id *oid,
 			 struct strbuf *err)
 {
+	int ret = TRANSACTION_GENERIC_ERROR;
+
 	if (!(update->flags & REF_HAVE_OLD) ||
 		   oideq(oid, &update->old_oid))
 		return 0;
 
-	if (is_null_oid(&update->old_oid))
+	if (is_null_oid(&update->old_oid)) {
 		strbuf_addf(err, "cannot lock ref '%s': "
 			    "reference already exists",
 			    ref_update_original_update_refname(update));
+		ret = TRANSACTION_CREATE_EXISTS;
+	}
 	else if (is_null_oid(oid))
 		strbuf_addf(err, "cannot lock ref '%s': "
 			    "reference is missing but expected %s",
@@ -2521,7 +2525,7 @@ static int check_old_oid(struct ref_update *update, struct object_id *oid,
 			    oid_to_hex(oid),
 			    oid_to_hex(&update->old_oid));
 
-	return -1;
+	return ret;
 }
 
 /*
@@ -2601,9 +2605,11 @@ static int lock_ref_for_update(struct files_ref_store *refs,
 					ret = TRANSACTION_GENERIC_ERROR;
 					goto out;
 				}
-			} else if  (check_old_oid(update, &lock->old_oid, err)) {
-				ret = TRANSACTION_GENERIC_ERROR;
-				goto out;
+			} else {
+				ret = check_old_oid(update, &lock->old_oid, err);
+				if  (ret) {
+					goto out;
+				}
 			}
 		} else {
 			/*
@@ -2634,9 +2640,11 @@ static int lock_ref_for_update(struct files_ref_store *refs,
 				    update->old_target);
 			ret = TRANSACTION_GENERIC_ERROR;
 			goto out;
-		} else if  (check_old_oid(update, &lock->old_oid, err)) {
-			ret = TRANSACTION_GENERIC_ERROR;
-			goto out;
+		} else {
+			ret = check_old_oid(update, &lock->old_oid, err);
+			if  (ret) {
+				goto out;
+			}
 		}
 
 		/*
refs/reftable-backend.c +4 -2
index e8afcda1f9..965c17d795 100644
@@ -1208,10 +1208,13 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store,
 				goto done;
 			}
 		} else if ((u->flags & REF_HAVE_OLD) && !oideq(&current_oid, &u->old_oid)) {
-			if (is_null_oid(&u->old_oid))
+			ret = TRANSACTION_NAME_CONFLICT;
+			if (is_null_oid(&u->old_oid)) {
 				strbuf_addf(err, _("cannot lock ref '%s': "
 						   "reference already exists"),
 					    ref_update_original_update_refname(u));
+				ret = TRANSACTION_CREATE_EXISTS;
+			}
 			else if (is_null_oid(&current_oid))
 				strbuf_addf(err, _("cannot lock ref '%s': "
 						   "reference is missing but expected %s"),
@@ -1223,7 +1226,6 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store,
 					    ref_update_original_update_refname(u),
 					    oid_to_hex(&current_oid),
 					    oid_to_hex(&u->old_oid));
-			ret = -1;
 			goto done;
 		}
 
Signature · 1 line
2.47.0.298.g52a96ec17b
#204 lore raw

PATCH v15 8/10 refs: add create_only option to refs_update_symref_extended

In reply to Bence Ferdinandy

Allow the caller to specify that it only wants to update the symref if it does not already exist. Silently ignore the error from the transaction API if the symref already exists.

---

Notes:
v4: new patch
v5: no change

v6: - switched from bool to int for create_only
- refactored logic in refs_update_symref with goto as layed out by Junio

v7: - change commit prefix to be more in line with project standards
- refactored code to accommodate changes in the first patch, but otherwise no change

v8: no change

v9: - no change (except for following through the
refs_update_symref_extended refactoring)

v10: no change

v11: no change

v12: no change

v13: changes only due to changes in previous patch

v14: no change

v15: no change

 builtin/remote.c |  2 +-
 refs.c           | 33 ++++++++++++++++++++++++---------
 refs.h           |  2 +-
 3 files changed, 26 insertions(+), 11 deletions(-)
builtin/remote.c +1 -1
index e1f15e68f4..4a8b2ef678 100644
@@ -1476,7 +1476,7 @@ static int set_head(int argc, const char **argv, const char *prefix)
 		goto cleanup;
 	}
 	was_detached = refs_update_symref_extended(refs, b_head.buf, b_remote_head.buf,
-			"remote set-head", &b_local_head);
+			"remote set-head", &b_local_head, 0);
 	if (was_detached == -1) {
 		result |= error(_("Could not set up %s"), b_head.buf);
 		goto cleanup;
refs.c +24 -9
index d80efd58f0..2efa6bcc5c 100644
@@ -2116,26 +2116,38 @@ int peel_iterated_oid(struct repository *r, const struct object_id *base, struct
 int refs_update_symref(struct ref_store *refs, const char *ref,
 		       const char *target, const char *logmsg)
 {
-	return refs_update_symref_extended(refs, ref, target, logmsg, NULL);
+	return refs_update_symref_extended(refs, ref, target, logmsg, NULL, 0);
 }
 
 int refs_update_symref_extended(struct ref_store *refs, const char *ref,
 		       const char *target, const char *logmsg,
-		       struct strbuf *referent)
+		       struct strbuf *referent, int create_only)
 {
 	struct ref_transaction *transaction;
 	struct strbuf err = STRBUF_INIT;
-	int ret = 0;
+	int ret = 0, prepret = 0;
 
 	transaction = ref_store_transaction_begin(refs, &err);
-	if (!transaction ||
-	    ref_transaction_update(transaction, ref, NULL, NULL,
-				   target, NULL, REF_NO_DEREF,
-				   logmsg, &err) ||
-	    ref_transaction_prepare(transaction, &err)) {
+	if (!transaction) {
+	error_return:
 		ret = error("%s", err.buf);
 		goto cleanup;
 	}
+	if (create_only) {
+		if (ref_transaction_create(transaction, ref, NULL, target,
+					   REF_NO_DEREF, logmsg, &err))
+			goto error_return;
+		prepret = ref_transaction_prepare(transaction, &err);
+		if (prepret && prepret != TRANSACTION_CREATE_EXISTS)
+			goto error_return;
+	} else {
+		if (ref_transaction_update(transaction, ref, NULL, NULL,
+					   target, NULL, REF_NO_DEREF,
+					   logmsg, &err) ||
+			ref_transaction_prepare(transaction, &err))
+			goto error_return;
+	}
+
 	if (referent && refs_read_symbolic_ref(refs, ref, referent) == NOT_A_SYMREF) {
 		struct object_id oid;
 		if (!refs_read_ref(refs, ref, &oid)) {
@@ -2144,8 +2156,11 @@ int refs_update_symref_extended(struct ref_store *refs, const char *ref,
 		}
 	}
 
+	if (prepret == TRANSACTION_CREATE_EXISTS)
+		goto cleanup;
+
 	if (ref_transaction_commit(transaction, &err))
-		ret = error("%s", err.buf);
+		goto error_return;
 
 cleanup:
 	strbuf_release(&err);
refs.h +1 -1
index b243739e4b..be38377b1f 100644
@@ -586,7 +586,7 @@ int refs_update_symref(struct ref_store *refs, const char *refname,
 
 int refs_update_symref_extended(struct ref_store *refs, const char *refname,
 		       const char *target, const char *logmsg,
-		       struct strbuf *referent);
+		       struct strbuf *referent, int create_only);
 
 enum action_on_err {
 	UPDATE_REFS_MSG_ON_ERR,
Signature · 1 line
2.47.0.298.g52a96ec17b
#205 lore raw

PATCH v15 9/10 fetch: set remote/HEAD if it does not exist

In reply to Bence Ferdinandy

When cloning a repository remote/HEAD is created, but when the user creates a repository with git init, and later adds a remote, remote/HEAD is only created if the user explicitly runs a variant of "remote set-head". Attempt to set remote/HEAD during fetch, if the user does not have it already set. Silently ignore any errors.

---

Notes:
v3: - does not rely on remote set-head anymore so it only authenticates
once
- uses the new REF_CREATE_ONLY to atomically check if the ref exists and only write it if it doesn't
- in all other cases the maximum it does is print a warning

v4: - instead of the discarded REF_CREATE_ONLY, it uses the existing,
but updated transaction api to request a silent create only
- it now uses the atomic before_target to determine reporting
- refactored for legibility

v5: - instead of printing a not too useful message, it now fails
silently, this in line with the objective to only set up remote/HEAD automatically if the right thing is trivial, for everything else there is remote set-head
- fixed all failing tests
- added two new tests, one for checking if remote/HEAD is set to the correct one, and one to test that we do not override remote/HEAD if it has changed on the server from what we have locally

v6: - fixed style issues and unintended extra empty line
- updated function call with bool to int from previous patch's change
- removed calls to error(...) inherited from builtin/remote.c so we actually fail silently
- set the test for remote set-head --auto to the correct value here, which was previously erronously set in the remote set-head patch

v7: - no change

v8: - changed logmsg in call to refs_update_symref from "remote
set-head" to "fetch"

v9: - follow through with refs_update_symref_extended
- fix test errors uncovered by the new patch

v10: no change

v11: fixed some memory leaks

v12: no change

v13: - fix printed information if the local HEAD is detached
- remove accidental formatting noise in a test

v14: remove report_set_head_auto: as discussed, any noise should only be created with an opt out, and the configuration for this will be added in a later patch

v15: no change

 builtin/fetch.c                  | 68 ++++++++++++++++++++++++++++++++
 t/t4207-log-decoration-colors.sh |  3 +-
 t/t5505-remote.sh                | 21 +++++++---
 t/t5510-fetch.sh                 | 24 +++++++++++
 t/t5512-ls-remote.sh             |  2 +
 t/t5514-fetch-multiple.sh        | 17 +++++++-
 t/t5516-fetch-push.sh            |  3 +-
 t/t5527-fetch-odd-refs.sh        |  3 +-
 t/t7900-maintenance.sh           |  3 +-
 t/t9210-scalar.sh                |  5 ++-
 t/t9211-scalar-clone.sh          |  6 +--
 t/t9902-completion.sh            | 65 ++++++++++++++++++++++++++++++
 12 files changed, 203 insertions(+), 17 deletions(-)
builtin/fetch.c +68 -0
index 06b4611958..bbfaf50b63 100644
@@ -1574,6 +1574,66 @@ static int backfill_tags(struct display_state *display_state,
 	return retcode;
 }
 
+static const char *strip_refshead(const char *name){
+	skip_prefix(name, "refs/heads/", &name);
+	return name;
+}
+
+static int set_head(const struct ref *remote_refs)
+{
+	int result = 0;
+	struct strbuf b_head = STRBUF_INIT, b_remote_head = STRBUF_INIT;
+	const char *remote = gtransport->remote->name;
+	char *head_name = NULL;
+	struct ref *ref, *matches;
+	struct ref *fetch_map = NULL, **fetch_map_tail = &fetch_map;
+	struct refspec_item refspec = {
+		.force = 0,
+		.pattern = 1,
+		.src = (char *) "refs/heads/*",
+		.dst = (char *) "refs/heads/*",
+	};
+	struct string_list heads = STRING_LIST_INIT_DUP;
+	struct ref_store *refs = get_main_ref_store(the_repository);
+
+	get_fetch_map(remote_refs, &refspec, &fetch_map_tail, 0);
+	matches = guess_remote_head(find_ref_by_name(remote_refs, "HEAD"),
+				    fetch_map, 1);
+	for (ref = matches; ref; ref = ref->next) {
+		string_list_append(&heads, strip_refshead(ref->name));
+	}
+
+
+	if (!heads.nr)
+		result = 1;
+	else if (heads.nr > 1)
+		result = 1;
+	else
+		head_name = xstrdup(heads.items[0].string);
+
+	if (!head_name)
+		goto cleanup;
+	strbuf_addf(&b_head, "refs/remotes/%s/HEAD", remote);
+	strbuf_addf(&b_remote_head, "refs/remotes/%s/%s", remote, head_name);
+		/* make sure it's valid */
+	if (!refs_ref_exists(refs, b_remote_head.buf)) {
+		result = 1;
+		goto cleanup;
+	}
+	if (refs_update_symref_extended(refs, b_head.buf, b_remote_head.buf,
+					"fetch", NULL, 1))
+		result = 1;
+
+cleanup:
+	free(head_name);
+	free_refs(fetch_map);
+	free_refs(matches);
+	string_list_clear(&heads, 0);
+	strbuf_release(&b_head);
+	strbuf_release(&b_remote_head);
+	return result;
+}
+
 static int do_fetch(struct transport *transport,
 		    struct refspec *rs,
 		    const struct fetch_config *config)
@@ -1643,6 +1703,8 @@ static int do_fetch(struct transport *transport,
 				    "refs/tags/");
 	}
 
+	strvec_push(&transport_ls_refs_options.ref_prefixes, "HEAD");
+
 	if (must_list_refs) {
 		trace2_region_enter("fetch", "remote_refs", the_repository);
 		remote_refs = transport_get_remote_refs(transport,
@@ -1787,6 +1849,12 @@ static int do_fetch(struct transport *transport,
 				  "you need to specify exactly one branch with the --set-upstream option"));
 		}
 	}
+	if (set_head(remote_refs))
+		;
+		/*
+		 * Way too many cases where this can go wrong
+		 * so let's just fail silently for now.
+		 */
 
 cleanup:
 	if (retcode) {
t/t4207-log-decoration-colors.sh +2 -1
index 73ea9e5155..d55d22cb2f 100755
@@ -59,7 +59,8 @@ ${c_reset}${c_tag}tag: ${c_reset}${c_tag}v1.0${c_reset}${c_commit}, \
 ${c_reset}${c_tag}tag: ${c_reset}${c_tag}B${c_reset}${c_commit})${c_reset} B
 ${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_reset}\
 ${c_tag}tag: ${c_reset}${c_tag}A1${c_reset}${c_commit}, \
-${c_reset}${c_remoteBranch}other/main${c_reset}${c_commit})${c_reset} A1
+${c_reset}${c_remoteBranch}other/main${c_reset}${c_commit}, \
+${c_reset}${c_remoteBranch}other/HEAD${c_reset}${c_commit})${c_reset} A1
 	${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_reset}\
 ${c_stash}refs/stash${c_reset}${c_commit})${c_reset} On main: Changes to A.t
 	${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_reset}\
t/t5505-remote.sh +15 -6
index d15b579c95..afa261409f 100755
@@ -74,7 +74,7 @@ test_expect_success 'add another remote' '
 		cd test &&
 		git remote add -f second ../two &&
 		tokens_match "origin second" "$(git remote)" &&
-		check_tracking_branch second main side another &&
+		check_tracking_branch second main side another HEAD &&
 		git for-each-ref "--format=%(refname)" refs/remotes |
 		sed -e "/^refs\/remotes\/origin\//d" \
 		    -e "/^refs\/remotes\/second\//d" >actual &&
@@ -500,7 +500,7 @@ test_expect_success 'set-head --auto has no problem w/multiple HEADs' '
 		cd test &&
 		git fetch two "refs/heads/*:refs/remotes/two/*" &&
 		git remote set-head --auto two >output 2>&1 &&
-		echo "${SQ}two/HEAD${SQ} is now created and points to ${SQ}main${SQ}" >expect &&
+		echo "${SQ}two/HEAD${SQ} is unchanged and points to ${SQ}main${SQ}" >expect &&
 		test_cmp expect output
 	)
 '
@@ -788,8 +788,10 @@ test_expect_success 'reject --no-no-tags' '
 '
 
 cat >one/expect <<\EOF
+  apis/HEAD -> apis/main
   apis/main
   apis/side
+  drosophila/HEAD -> drosophila/main
   drosophila/another
   drosophila/main
   drosophila/side
@@ -807,11 +809,14 @@ test_expect_success 'update' '
 '
 
 cat >one/expect <<\EOF
+  drosophila/HEAD -> drosophila/main
   drosophila/another
   drosophila/main
   drosophila/side
+  manduca/HEAD -> manduca/main
   manduca/main
   manduca/side
+  megaloprepus/HEAD -> megaloprepus/main
   megaloprepus/main
   megaloprepus/side
 EOF
@@ -819,7 +824,7 @@ EOF
 test_expect_success 'update with arguments' '
 	(
 		cd one &&
-		for b in $(git branch -r)
+		for b in $(git branch -r | grep -v HEAD)
 		do
 		git branch -r -d $b || exit 1
 		done &&
@@ -851,10 +856,13 @@ test_expect_success 'update --prune' '
 '
 
 cat >one/expect <<-\EOF
+  apis/HEAD -> apis/main
   apis/main
   apis/side
+  manduca/HEAD -> manduca/main
   manduca/main
   manduca/side
+  megaloprepus/HEAD -> megaloprepus/main
   megaloprepus/main
   megaloprepus/side
 EOF
@@ -862,7 +870,7 @@ EOF
 test_expect_success 'update default' '
 	(
 		cd one &&
-		for b in $(git branch -r)
+		for b in $(git branch -r | grep -v HEAD)
 		do
 		git branch -r -d $b || exit 1
 		done &&
@@ -874,6 +882,7 @@ test_expect_success 'update default' '
 '
 
 cat >one/expect <<\EOF
+  drosophila/HEAD -> drosophila/main
   drosophila/another
   drosophila/main
   drosophila/side
@@ -882,7 +891,7 @@ EOF
 test_expect_success 'update default (overridden, with funny whitespace)' '
 	(
 		cd one &&
-		for b in $(git branch -r)
+		for b in $(git branch -r | grep -v HEAD)
 		do
 		git branch -r -d $b || exit 1
 		done &&
@@ -896,7 +905,7 @@ test_expect_success 'update default (overridden, with funny whitespace)' '
 test_expect_success 'update (with remotes.default defined)' '
 	(
 		cd one &&
-		for b in $(git branch -r)
+		for b in $(git branch -r | grep -v HEAD)
 		do
 		git branch -r -d $b || exit 1
 		done &&
t/t5510-fetch.sh +24 -0
index 0890b9f61c..87698341f5 100755
@@ -75,6 +75,30 @@ test_expect_success "fetch test for-merge" '
 	cut -f -2 .git/FETCH_HEAD >actual &&
 	test_cmp expected actual'
 
+test_expect_success "fetch test remote HEAD" '
+	cd "$D" &&
+	cd two &&
+	git fetch &&
+	git rev-parse --verify refs/remotes/origin/HEAD &&
+	git rev-parse --verify refs/remotes/origin/main &&
+	head=$(git rev-parse refs/remotes/origin/HEAD) &&
+	branch=$(git rev-parse refs/remotes/origin/main) &&
+	test "z$head" = "z$branch"'
+
+test_expect_success "fetch test remote HEAD change" '
+	cd "$D" &&
+	cd two &&
+	git switch -c other &&
+	git push -u origin other &&
+	git rev-parse --verify refs/remotes/origin/HEAD &&
+	git rev-parse --verify refs/remotes/origin/main &&
+	git rev-parse --verify refs/remotes/origin/other &&
+	git remote set-head origin other &&
+	git fetch &&
+	head=$(git rev-parse refs/remotes/origin/HEAD) &&
+	branch=$(git rev-parse refs/remotes/origin/other) &&
+	test "z$head" = "z$branch"'
+
 test_expect_success 'fetch --prune on its own works as expected' '
 	cd "$D" &&
 	git clone . prune &&
t/t5512-ls-remote.sh +2 -0
index 64b3491e4e..1b3865e154 100755
@@ -293,6 +293,8 @@ test_expect_success 'ls-remote with filtered symref (refname)' '
 	cat >expect <<-EOF &&
 	ref: refs/heads/main	HEAD
 	$rev	HEAD
+	ref: refs/remotes/origin/main	refs/remotes/origin/HEAD
+	$rev	refs/remotes/origin/HEAD
 	EOF
 	git ls-remote --symref . HEAD >actual &&
 	test_cmp expect actual
t/t5514-fetch-multiple.sh +15 -2
index 579872c258..e3482b27b2 100755
@@ -45,14 +45,17 @@ test_expect_success setup '
 '
 
 cat > test/expect << EOF
+  one/HEAD -> one/main
   one/main
   one/side
   origin/HEAD -> origin/main
   origin/main
   origin/side
+  three/HEAD -> three/main
   three/another
   three/main
   three/side
+  two/HEAD -> two/main
   two/another
   two/main
   two/side
@@ -97,6 +100,7 @@ cat > expect << EOF
   origin/HEAD -> origin/main
   origin/main
   origin/side
+  three/HEAD -> three/main
   three/another
   three/main
   three/side
@@ -112,8 +116,10 @@ test_expect_success 'git fetch --multiple (but only one remote)' '
 '
 
 cat > expect << EOF
+  one/HEAD -> one/main
   one/main
   one/side
+  two/HEAD -> two/main
   two/another
   two/main
   two/side
@@ -141,7 +147,7 @@ test_expect_success 'git fetch --multiple (bad remote names)' '
 
 test_expect_success 'git fetch --all (skipFetchAll)' '
 	(cd test4 &&
-	 for b in $(git branch -r)
+	 for b in $(git branch -r | grep -v HEAD)
 	 do
 		git branch -r -d $b || exit 1
 	 done &&
@@ -153,11 +159,14 @@ test_expect_success 'git fetch --all (skipFetchAll)' '
 '
 
 cat > expect << EOF
+  one/HEAD -> one/main
   one/main
   one/side
+  three/HEAD -> three/main
   three/another
   three/main
   three/side
+  two/HEAD -> two/main
   two/another
   two/main
   two/side
@@ -165,7 +174,7 @@ EOF
 
 test_expect_success 'git fetch --multiple (ignoring skipFetchAll)' '
 	(cd test4 &&
-	 for b in $(git branch -r)
+	 for b in $(git branch -r | grep -v HEAD)
 	 do
 		git branch -r -d $b || exit 1
 	 done &&
@@ -221,14 +230,17 @@ test_expect_success 'git fetch --multiple --jobs=0 picks a default' '
 
 create_fetch_all_expect () {
 	cat >expect <<-\EOF
+	  one/HEAD -> one/main
 	  one/main
 	  one/side
 	  origin/HEAD -> origin/main
 	  origin/main
 	  origin/side
+	  three/HEAD -> three/main
 	  three/another
 	  three/main
 	  three/side
+	  two/HEAD -> two/main
 	  two/another
 	  two/main
 	  two/side
@@ -265,6 +277,7 @@ test_expect_success 'git fetch (fetch all remotes with fetch.all = true)' '
 
 create_fetch_one_expect () {
 	cat >expect <<-\EOF
+	  one/HEAD -> one/main
 	  one/main
 	  one/side
 	  origin/HEAD -> origin/main
t/t5516-fetch-push.sh +2 -1
index 331778bd42..5a051aa0c7 100755
@@ -1395,7 +1395,8 @@ test_expect_success 'fetch follows tags by default' '
 		git tag -m "annotated" tag &&
 		git for-each-ref >tmp1 &&
 		sed -n "p; s|refs/heads/main$|refs/remotes/origin/main|p" tmp1 |
-		sort -k 3 >../expect
+		sed -n "p; s|refs/heads/main$|refs/remotes/origin/HEAD|p"  |
+		sort -k 4 >../expect
 	) &&
 	test_when_finished "rm -rf dst" &&
 	git init dst &&
t/t5527-fetch-odd-refs.sh +2 -1
index 98ece27c6a..d3996af6ee 100755
@@ -52,7 +52,8 @@ test_expect_success LONG_REF 'fetch handles extremely long refname' '
 	long
 	main
 	EOF
-	git for-each-ref --format="%(subject)" refs/remotes/long >actual &&
+	git for-each-ref --format="%(subject)" refs/remotes/long \
+		--exclude=refs/remotes/long/HEAD >actual &&
 	test_cmp expect actual
 '
 
t/t7900-maintenance.sh +2 -1
index c224c8450c..edb85b7145 100755
@@ -329,7 +329,8 @@ test_expect_success 'incremental-repack task' '
 
 	# Delete refs that have not been repacked in these packs.
 	git for-each-ref --format="delete %(refname)" \
-		refs/prefetch refs/tags refs/remotes >refs &&
+		refs/prefetch refs/tags refs/remotes \
+		--exclude=refs/remotes/*/HEAD >refs &&
 	git update-ref --stdin <refs &&
 
 	# Replace the object directory with this pack layout.
t/t9210-scalar.sh +3 -2
index a30b2c9f70..2237844550 100755
@@ -151,7 +151,8 @@ test_expect_success 'scalar clone' '
 			"$(pwd)" &&
 
 		git for-each-ref --format="%(refname)" refs/remotes/origin/ >actual &&
-		echo "refs/remotes/origin/parallel" >expect &&
+		echo "refs/remotes/origin/HEAD" >>expect &&
+		echo "refs/remotes/origin/parallel" >>expect &&
 		test_cmp expect actual &&
 
 		test_path_is_missing 1/2 &&
@@ -220,7 +221,7 @@ test_expect_success 'scalar reconfigure --all with includeIf.onbranch' '
 	done
 '
 
- test_expect_success 'scalar reconfigure --all with detached HEADs' '
+test_expect_success 'scalar reconfigure --all with detached HEADs' '
 	repos="two three four" &&
 	for num in $repos
 	do
t/t9211-scalar-clone.sh +3 -3
index c16ea67c1d..d9cb6b9a3e 100755
@@ -32,7 +32,7 @@ test_expect_success 'set up repository to clone' '
 	)
 '
 
-cleanup_clone () {
+cleanup_clone() {
 	rm -rf "$1"
 }
 
@@ -128,7 +128,7 @@ test_expect_success '--single-branch clones HEAD only' '
 	(
 		cd $enlistment/src &&
 		git for-each-ref refs/remotes/origin >out &&
-		test_line_count = 1 out &&
+		test_line_count = 2 out &&
 		grep "refs/remotes/origin/base" out
 	) &&
 
@@ -142,7 +142,7 @@ test_expect_success '--no-single-branch clones all branches' '
 	(
 		cd $enlistment/src &&
 		git for-each-ref refs/remotes/origin >out &&
-		test_line_count = 2 out &&
+		test_line_count = 3 out &&
 		grep "refs/remotes/origin/base" out &&
 		grep "refs/remotes/origin/parallel" out
 	) &&
t/t9902-completion.sh +65 -0

443 lines not on this page · raw message

Signature · 1 line
2.47.0.298.g52a96ec17b
#206 lore raw

Re: [PATCH v15 09/10] fetch: set remote/HEAD if it does not exist

Quoted 7 lines · Bence Ferdinandy

When cloning a repository remote/HEAD is created, but when the user creates a repository with git init, and later adds a remote, remote/HEAD is only created if the user explicitly runs a variant of "remote set-head". Attempt to set remote/HEAD during fetch, if the user does not have it already set. Silently ignore any errors.

At $DAYJOB, we noticed that this breaks git fetch --tags, although I haven't had a chance to figure out what causes the error just yet.

I was able to bisect down to this commit using Jonathan Tan's reproduction script:

rm -rf test_tag_1 test_tag_2 GIT=~/git/bin-wrappers/git
mkdir test_tag_1 && cd test_tag_1 REMOTE=$(pwd)
$GIT init .
touch foo.txt
$GIT add foo.txt
$GIT commit foo.txt -m "commit one"
$GIT tag foo
cd ..
mkdir test_tag_2 && cd test_tag_2
$GIT init .
echo fetch --tags
$GIT fetch --tags "file://$REMOTE"
echo regular fetch
$GIT fetch "file://$REMOTE" 'refs/tags/*:refs/tags/*' $GIT --version

Prior to this change, the first $GIT fetch --tags "file://$REMOTE" fetches the foo tag; with this change, it does not.

#207 lore raw

Re: [PATCH v15 09/10] fetch: set remote/HEAD if it does not exist

Quoted 34 lines · Josh Steadmon
Quoted 7 lines · Bence Ferdinandy

When cloning a repository remote/HEAD is created, but when the user creates a repository with git init, and later adds a remote, remote/HEAD is only created if the user explicitly runs a variant of "remote set-head". Attempt to set remote/HEAD during fetch, if the user does not have it already set. Silently ignore any errors.

At $DAYJOB, we noticed that this breaks git fetch --tags, although I haven't had a chance to figure out what causes the error just yet.

I was able to bisect down to this commit using Jonathan Tan's reproduction script:

rm -rf test_tag_1 test_tag_2 GIT=~/git/bin-wrappers/git
mkdir test_tag_1 && cd test_tag_1 REMOTE=$(pwd)
$GIT init .
touch foo.txt
$GIT add foo.txt
$GIT commit foo.txt -m "commit one"
$GIT tag foo
cd ..
mkdir test_tag_2 && cd test_tag_2
$GIT init .
echo fetch --tags
$GIT fetch --tags "file://$REMOTE"
echo regular fetch
$GIT fetch "file://$REMOTE" 'refs/tags/*:refs/tags/*' $GIT --version

Quoted 2 lines

Prior to this change, the first $GIT fetch --tags "file://$REMOTE" fetches the foo tag; with this change, it does not.

FWIW, moving this:

@@ -1643,6 +1703,8 @@ static int do_fetch(struct transport *transport,

                                    "refs/tags/");
        }

+ strvec_push(&transport_ls_refs_options.ref_prefixes, "HEAD");
+

to just above the prior if block fixes our issue and doesn't break any tests. However, I'm not sure yet why the order of ref_prefixes should matter here.

#208 lore raw

Re: [PATCH v15 09/10] fetch: set remote/HEAD if it does not exist

Quoted 48 lines · Josh Steadmon
Quoted 34 lines · Josh Steadmon
Quoted 7 lines · Bence Ferdinandy

When cloning a repository remote/HEAD is created, but when the user creates a repository with git init, and later adds a remote, remote/HEAD is only created if the user explicitly runs a variant of "remote set-head". Attempt to set remote/HEAD during fetch, if the user does not have it already set. Silently ignore any errors.

At $DAYJOB, we noticed that this breaks git fetch --tags, although I haven't had a chance to figure out what causes the error just yet.

I was able to bisect down to this commit using Jonathan Tan's reproduction script:

rm -rf test_tag_1 test_tag_2 GIT=~/git/bin-wrappers/git
mkdir test_tag_1 && cd test_tag_1 REMOTE=$(pwd)
$GIT init .
touch foo.txt
$GIT add foo.txt
$GIT commit foo.txt -m "commit one"
$GIT tag foo
cd ..
mkdir test_tag_2 && cd test_tag_2
$GIT init .
echo fetch --tags
$GIT fetch --tags "file://$REMOTE"
echo regular fetch
$GIT fetch "file://$REMOTE" 'refs/tags/*:refs/tags/*' $GIT --version

Quoted 2 lines

Prior to this change, the first $GIT fetch --tags "file://$REMOTE" fetches the foo tag; with this change, it does not.

FWIW, moving this:

builtin/fetch.c +2 -0
@@ -1643,6 +1703,8 @@ static int do_fetch(struct transport *transport,
                                    "refs/tags/");
        }
 
+       strvec_push(&transport_ls_refs_options.ref_prefixes, "HEAD");
+
 
Quoted 3 lines

to just above the prior if block fixes our issue and doesn't break any tests. However, I'm not sure yet why the order of ref_prefixes should matter here.

Thanks for looking into this! I think the issue is with

$GIT fetch --tags "file://$REMOTE"

instead of adding a proper remote. Tbh, I've never seen the above syntax before, so first I just ran your script, which reproduced the issue for me, but then I modified it to use a proper remote which works as expected:

rm -rf test_tag_1 test_tag_2
GIT=~/git/bin-wrappers/git
mkdir test_tag_1 && cd test_tag_1
REMOTE=$(pwd)
$GIT init .
touch foo.txt
$GIT add foo.txt
$GIT commit foo.txt -m "commit one"
$GIT tag foo
cd ..
mkdir test_tag_2 && cd test_tag_2
$GIT init .
$GIT remote add origin $REMOTE
echo fetch --tags
$GIT fetch origin --tags
echo regular fetch
$GIT fetch origin 'refs/tags/*:refs/tags/*'
$GIT --version

So I'm assuming this is why also the tests never caught this, since probably all of them are using git remote add.

#209 lore raw

Re: [PATCH v15 09/10] fetch: set remote/HEAD if it does not exist

Quoted 79 lines · Bence Ferdinandy
Quoted 48 lines · Josh Steadmon
Quoted 34 lines · Josh Steadmon
Quoted 7 lines · Bence Ferdinandy

When cloning a repository remote/HEAD is created, but when the user creates a repository with git init, and later adds a remote, remote/HEAD is only created if the user explicitly runs a variant of "remote set-head". Attempt to set remote/HEAD during fetch, if the user does not have it already set. Silently ignore any errors.

At $DAYJOB, we noticed that this breaks git fetch --tags, although I haven't had a chance to figure out what causes the error just yet.

I was able to bisect down to this commit using Jonathan Tan's reproduction script:

rm -rf test_tag_1 test_tag_2 GIT=~/git/bin-wrappers/git
mkdir test_tag_1 && cd test_tag_1 REMOTE=$(pwd)
$GIT init .
touch foo.txt
$GIT add foo.txt
$GIT commit foo.txt -m "commit one"
$GIT tag foo
cd ..
mkdir test_tag_2 && cd test_tag_2
$GIT init .
echo fetch --tags
$GIT fetch --tags "file://$REMOTE"
echo regular fetch
$GIT fetch "file://$REMOTE" 'refs/tags/*:refs/tags/*' $GIT --version

Quoted 2 lines

Prior to this change, the first $GIT fetch --tags "file://$REMOTE" fetches the foo tag; with this change, it does not.

FWIW, moving this:

builtin/fetch.c +2 -0
@@ -1643,6 +1703,8 @@ static int do_fetch(struct transport *transport,
                                    "refs/tags/");
        }
 
+       strvec_push(&transport_ls_refs_options.ref_prefixes, "HEAD");
+
 
Quoted 3 lines

to just above the prior if block fixes our issue and doesn't break any tests. However, I'm not sure yet why the order of ref_prefixes should matter here.

Thanks for looking into this! I think the issue is with

$GIT fetch --tags "file://$REMOTE"

instead of adding a proper remote. Tbh, I've never seen the above syntax before, so first I just ran your script, which reproduced the issue for me, but then I modified it to use a proper remote which works as expected:

rm -rf test_tag_1 test_tag_2
GIT=~/git/bin-wrappers/git
mkdir test_tag_1 && cd test_tag_1
REMOTE=$(pwd)
$GIT init .
touch foo.txt
$GIT add foo.txt
$GIT commit foo.txt -m "commit one"
$GIT tag foo
cd ..
mkdir test_tag_2 && cd test_tag_2
$GIT init .
$GIT remote add origin $REMOTE
echo fetch --tags
$GIT fetch origin --tags
echo regular fetch
$GIT fetch origin 'refs/tags/*:refs/tags/*'
$GIT --version

Quoted 3 lines

So I'm assuming this is why also the tests never caught this, since probably all of them are using git remote add.

Yeah, I think the issue is that we check the --tags flag first, but only add the ref_prefixes entry if it's not empty already. Then after that we unconditionally add HEAD. So that's why moving your strvec_push earlier fixes it. I'll send a fix + test patch in just a minute.

#210 lore raw

PATCH 1/1 Fix `git fetch --tags` in repo with no configured remote

In 3f763ddf28 (fetch: set remote/HEAD if it does not exist, 2024-11-22), git-fetch learned to opportunistically set $REMOTE/HEAD when fetching. However, this broke the logic for the --tags flag. Specifically, we now unconditionally add HEAD to the ref_prefixes list, but we did this *after* deciding whether we also need to explicitly request tags.

Fix this by adding HEAD to the ref_prefixes list prior to handling the --tags flag, and removing the now obsolete check whether ref_prefixes is empty or not.

---
 builtin/fetch.c  |  9 ++++-----
 t/t5510-fetch.sh | 17 +++++++++++++++++
 2 files changed, 21 insertions(+), 5 deletions(-)
builtin/fetch.c +4 -5
index b2a36a5d95..e7b0c79678 100644
@@ -1699,15 +1699,14 @@ static int do_fetch(struct transport *transport,
 		}
 	}
 
+	strvec_push(&transport_ls_refs_options.ref_prefixes, "HEAD");
+
 	if (tags == TAGS_SET || tags == TAGS_DEFAULT) {
 		must_list_refs = 1;
-		if (transport_ls_refs_options.ref_prefixes.nr)
-			strvec_push(&transport_ls_refs_options.ref_prefixes,
-				    "refs/tags/");
+		strvec_push(&transport_ls_refs_options.ref_prefixes,
+			    "refs/tags/");
 	}
 
-	strvec_push(&transport_ls_refs_options.ref_prefixes, "HEAD");
-
 	if (must_list_refs) {
 		trace2_region_enter("fetch", "remote_refs", the_repository);
 		remote_refs = transport_get_remote_refs(transport,
t/t5510-fetch.sh +17 -0
index 87698341f5..d7602333ff 100755
@@ -189,6 +189,23 @@ test_expect_success 'fetch --prune --tags with refspec prunes based on refspec'
 	git rev-parse sometag
 '
 
+test_expect_success 'fetch --tags gets tags even without a configured remote' '
+	REMOTE="$(pwd)/test_tag_1" &&
+	git init test_tag_1 &&
+	(
+		cd test_tag_1 &&
+		test_commit foo
+	) &&
+	git init test_tag_2 &&
+	(
+		cd test_tag_2 &&
+		git fetch --tags "file://$REMOTE" &&
+		echo "foo" >expect &&
+		git tag >actual &&
+		test_cmp expect actual
+	)
+'
+
 test_expect_success REFFILES 'fetch --prune fails to delete branches' '
 	cd "$D" &&
 	git clone . prune-fail &&

base-commit: 3f763ddf28d28fe63963991513c8db4045eabadc

Signature · 1 line
2.47.0.338.g60cca15819-goog
#211 lore raw

Re: [PATCH] Fix `git fetch --tags` in repo with no configured remote

Quoted 10 lines · Josh Steadmon

In 3f763ddf28 (fetch: set remote/HEAD if it does not exist, 2024-11-22), git-fetch learned to opportunistically set $REMOTE/HEAD when fetching. However, this broke the logic for the --tags flag. Specifically, we now unconditionally add HEAD to the ref_prefixes list, but we did this *after* deciding whether we also need to explicitly request tags.

Fix this by adding HEAD to the ref_prefixes list prior to handling the --tags flag, and removing the now obsolete check whether ref_prefixes is empty or not.

Quoted 5 lines
---
 builtin/fetch.c  |  9 ++++-----
 t/t5510-fetch.sh | 17 +++++++++++++++++
 2 files changed, 21 insertions(+), 5 deletions(-)

I see Bence is happy with the fix in a nearby message, so let me queue (and perhaps later amend it with Acked-by from Bence if we see one) the fix.

Thanks.

#212 lore raw

Re: [PATCH] Fix `git fetch --tags` in repo with no configured remote

In reply to Josh Steadmon
Quoted 24 lines · Josh Steadmon

In 3f763ddf28 (fetch: set remote/HEAD if it does not exist, 2024-11-22), git-fetch learned to opportunistically set $REMOTE/HEAD when fetching. However, this broke the logic for the --tags flag. Specifically, we now unconditionally add HEAD to the ref_prefixes list, but we did this *after* deciding whether we also need to explicitly request tags.

Fix this by adding HEAD to the ref_prefixes list prior to handling the --tags flag, and removing the now obsolete check whether ref_prefixes is empty or not.

---
 builtin/fetch.c  |  9 ++++-----
 t/t5510-fetch.sh | 17 +++++++++++++++++
 2 files changed, 21 insertions(+), 5 deletions(-)
builtin/fetch.c +0 -0
index b2a36a5d95..e7b0c79678 100644
@@ -1699,15 +1699,14 @@ static int do_fetch(struct transport *transport,
 		}
 	}
 

builtin/fetch.c static int do_fetch(struct transport *transport, · line 1708

Quoted 10 lines
builtin/fetch.c +4 -3
+	strvec_push(&transport_ls_refs_options.ref_prefixes, "HEAD");
+
 	if (tags == TAGS_SET || tags == TAGS_DEFAULT) {
 		must_list_refs = 1;
-		if (transport_ls_refs_options.ref_prefixes.nr)
-			strvec_push(&transport_ls_refs_options.ref_prefixes,
-				    "refs/tags/");
+		strvec_push(&transport_ls_refs_options.ref_prefixes,
+			    "refs/tags/");
 	}

Stepping back a bit, do we even need to learn where HEAD points at in the remote, when we are not doing the "opportunistically set $REMOTE/HEAD"? Your example is "in repo with no configured remote", which by definition means that we do not use any refs/remotes/*/ ref hierarchy to keep track of the remote-tracking branches for the remote we are fetching from. There is no place we record what we learn by running ls-remote HEAD against them, so should we even push "HEAD" to the ls-remote prefixes in such a case?

While this change may hide the breakage you saw in your set-up, we may be now asking to ls-remote HEAD even in cases we do not need to.

Quoted 3 lines

Fix this by adding HEAD to the ref_prefixes list prior to handling the --tags flag, and removing the now obsolete check whether ref_prefixes is empty or not.

And if we unconditionally add HEAD even when we do not need to, especially with the loss of the ref-prefixes condition that was there in order to implement "learn refs/tags/* hierarchy only when we are doing the default fetch", wouldn't it mean we may learn refs/tags/* even when we do not have to?

Quoted 60 lines
---
 builtin/fetch.c  |  9 ++++-----
 t/t5510-fetch.sh | 17 +++++++++++++++++
 2 files changed, 21 insertions(+), 5 deletions(-)
builtin/fetch.c +4 -5
index b2a36a5d95..e7b0c79678 100644
@@ -1699,15 +1699,14 @@ static int do_fetch(struct transport *transport,
 		}
 	}
 
+	strvec_push(&transport_ls_refs_options.ref_prefixes, "HEAD");
+
 	if (tags == TAGS_SET || tags == TAGS_DEFAULT) {
 		must_list_refs = 1;
-		if (transport_ls_refs_options.ref_prefixes.nr)
-			strvec_push(&transport_ls_refs_options.ref_prefixes,
-				    "refs/tags/");
+		strvec_push(&transport_ls_refs_options.ref_prefixes,
+			    "refs/tags/");
 	}
 
-	strvec_push(&transport_ls_refs_options.ref_prefixes, "HEAD");
-
 	if (must_list_refs) {
 		trace2_region_enter("fetch", "remote_refs", the_repository);
 		remote_refs = transport_get_remote_refs(transport,
t/t5510-fetch.sh +17 -0
index 87698341f5..d7602333ff 100755
@@ -189,6 +189,23 @@ test_expect_success 'fetch --prune --tags with refspec prunes based on refspec'
 	git rev-parse sometag
 '
 
+test_expect_success 'fetch --tags gets tags even without a configured remote' '
+	REMOTE="$(pwd)/test_tag_1" &&
+	git init test_tag_1 &&
+	(
+		cd test_tag_1 &&
+		test_commit foo
+	) &&
+	git init test_tag_2 &&
+	(
+		cd test_tag_2 &&
+		git fetch --tags "file://$REMOTE" &&
+		echo "foo" >expect &&
+		git tag >actual &&
+		test_cmp expect actual
+	)
+'
+
 test_expect_success REFFILES 'fetch --prune fails to delete branches' '
 	cd "$D" &&
 	git clone . prune-fail &&

base-commit: 3f763ddf28d28fe63963991513c8db4045eabadc

#213 lore raw

Re: [PATCH] Fix `git fetch --tags` in repo with no configured remote

Quoted 5 lines · Junio C Hamano

And if we unconditionally add HEAD even when we do not need to, especially with the loss of the ref-prefixes condition that was there in order to implement "learn refs/tags/* hierarchy only when we are doing the default fetch", wouldn't it mean we may learn refs/tags/* even when we do not have to?

Micro-correction. "We grab tags only when we are fetching something else as well" is what I should have said.

The general direction my comment leads to does not change, though. Because we ask for "HEAD" even when we do not need to, the additional change this patch names to unconditionally add "refs/tags/" would make us ask to list all the tags even when we do not need to see them.

Thanks.

#214 lore raw

Re* [PATCH] Fix `git fetch --tags` in repo with no configured remote

In reply to Junio C Hamano
Quoted 16 lines · Junio C Hamano

Stepping back a bit, do we even need to learn where HEAD points at in the remote, when we are not doing the "opportunistically set $REMOTE/HEAD"? Your example is "in repo with no configured remote", which by definition means that we do not use any refs/remotes/*/ ref hierarchy to keep track of the remote-tracking branches for the remote we are fetching from. There is no place we record what we learn by running ls-remote HEAD against them, so should we even push "HEAD" to the ls-remote prefixes in such a case?

While this change may hide the breakage you saw in your set-up, we may be now asking to ls-remote HEAD even in cases we do not need to.

Quoted 3 lines

Fix this by adding HEAD to the ref_prefixes list prior to handling the --tags flag, and removing the now obsolete check whether ref_prefixes is empty or not.

Quoted 5 lines

And if we unconditionally add HEAD even when we do not need to, especially with the loss of the ref-prefixes condition that was there in order to implement "learn refs/tags/* hierarchy only when we are doing the default fetch", wouldn't it mean we may learn refs/tags/* even when we do not have to?

In other words, what I think the "fix" should look like is more like the attached. It seems to pass your test, as well as existing tests Bence added and other tests about "git fetch".

One thing I am not happy about is the abstraction violation that is needed to make the uses_remote_tracking() helper aware of the "use the rs, the refspec given from the command line, or if it is empty, use the configured 'fetch' refspec from the remote" rule, which is primarily used by get_ref_map() that is much later called, but the layering violation started when we started limiting the ls-remote request with narrowing common prefixes, and it would take a larger surgery to fix, I would think.

0000---- >8 ----
Subject: [PATCH] fetch: do not ask for HEAD unnecessarily

In 3f763ddf28 (fetch: set remote/HEAD if it does not exist, 2024-11-22), git-fetch learned to opportunistically set $REMOTE/HEAD when fetching by always asking for remote HEAD, in the hope that it will help setting refs/remotes/<name>/HEAD if missing.

But it is not needed to always ask for remote HEAD. When we are fetching from a remote, for which we have remote-tracking branches, we do need to know about HEAD. But if we are doing one-shot fetch, e.g.,

  $ git fetch --tags https://github.com/git/git

we do not even know what sub-hierarchy of refs/remotes/<remote>/ we need to adjust the remote HEAD for. There is no need to ask for HEAD in such a case.

Incidentally, because the unconditional request to list "HEAD" affected the number of ref-prefixes requested in the ls-remote request, this affected how the requests for tags are added to the same ls-remote request, breaking "git fetch --tags $URL" performed against a URL that is not configured as a remote.

Make sure we ask to list "HEAD" from the remote only when we are fetching with configured remote for which we use remote-tracking branches.

[jc: tests are also borrowed from Josh's patch]

---

* Even though I borrowed some part of the commit log message from Josh's version, it not clear to me how "*after* deciding" led to whatever the observed breakage (which was not described in the log message), in the following part.

Specifically, we now unconditionally add HEAD to the ref_prefixes list, but we did this *after* deciding whether we also need to explicitly request tags.

Bence's change asks "HEAD" after "if we are fetching something, then also ask about refs/tags/" logic thought we are not fetching anything (i.e. ref_prefixes.nr == 0 at that point). But before Bence's series, the same refs/tags/ logic saw that (ref_prefix.nr == 0), didn't it? So that does not sound like a sufficient explanation on how the series regressed.

 builtin/fetch.c  | 20 +++++++++++++++++++-
 t/t5510-fetch.sh | 17 +++++++++++++++++
 2 files changed, 36 insertions(+), 1 deletion(-)
builtin/fetch.c +19 -1
index a64de4485f..3eb6f3acc9 100644
@@ -1643,6 +1643,21 @@ static int set_head(const struct ref *remote_refs)
 	return result;
 }
 
+static int uses_remote_tracking(struct transport *transport, struct refspec *rs)
+{
+	if (!remote_is_configured(transport->remote, 0))
+		return 0;
+
+	if (!rs->nr)
+		rs = &transport->remote->fetch;
+
+	for (int i = 0; i < rs->nr; i++)
+		if (rs->items[i].dst)
+			return 1;
+
+	return 0;
+}
+
 static int do_fetch(struct transport *transport,
 		    struct refspec *rs,
 		    const struct fetch_config *config)
@@ -1712,7 +1727,10 @@ static int do_fetch(struct transport *transport,
 				    "refs/tags/");
 	}
 
-	strvec_push(&transport_ls_refs_options.ref_prefixes, "HEAD");
+	if (uses_remote_tracking(transport, rs)) {
+		must_list_refs = 1;
+		strvec_push(&transport_ls_refs_options.ref_prefixes, "HEAD");
+	}
 
 	if (must_list_refs) {
 		trace2_region_enter("fetch", "remote_refs", the_repository);
t/t5510-fetch.sh +17 -0
index 87698341f5..d7602333ff 100755
@@ -189,6 +189,23 @@ test_expect_success 'fetch --prune --tags with refspec prunes based on refspec'
 	git rev-parse sometag
 '
 
+test_expect_success 'fetch --tags gets tags even without a configured remote' '
+	REMOTE="$(pwd)/test_tag_1" &&
+	git init test_tag_1 &&
+	(
+		cd test_tag_1 &&
+		test_commit foo
+	) &&
+	git init test_tag_2 &&
+	(
+		cd test_tag_2 &&
+		git fetch --tags "file://$REMOTE" &&
+		echo "foo" >expect &&
+		git tag >actual &&
+		test_cmp expect actual
+	)
+'
+
 test_expect_success REFFILES 'fetch --prune fails to delete branches' '
 	cd "$D" &&
 	git clone . prune-fail &&
#215 lore raw

Re* [PATCH] Fix `git fetch --tags` in repo with no configured remote

In reply to Junio C Hamano
Quoted 16 lines · Junio C Hamano

Stepping back a bit, do we even need to learn where HEAD points at in the remote, when we are not doing the "opportunistically set $REMOTE/HEAD"? Your example is "in repo with no configured remote", which by definition means that we do not use any refs/remotes/*/ ref hierarchy to keep track of the remote-tracking branches for the remote we are fetching from. There is no place we record what we learn by running ls-remote HEAD against them, so should we even push "HEAD" to the ls-remote prefixes in such a case?

While this change may hide the breakage you saw in your set-up, we may be now asking to ls-remote HEAD even in cases we do not need to.

Quoted 3 lines

Fix this by adding HEAD to the ref_prefixes list prior to handling the --tags flag, and removing the now obsolete check whether ref_prefixes is empty or not.

Quoted 5 lines

And if we unconditionally add HEAD even when we do not need to, especially with the loss of the ref-prefixes condition that was there in order to implement "learn refs/tags/* hierarchy only when we are doing the default fetch", wouldn't it mean we may learn refs/tags/* even when we do not have to?

In other words, what I think the "fix" should look like is more like the attached. It seems to pass your test, as well as existing tests Bence added and other tests about "git fetch".

One thing I am not happy about is the abstraction violation that is needed to make the uses_remote_tracking() helper aware of the "use the rs, the refspec given from the command line, or if it is empty, use the configured 'fetch' refspec from the remote" rule, which is primarily used by get_ref_map() that is much later called, but the layering violation started when we started limiting the ls-remote request with narrowing common prefixes, and it would take a larger surgery to fix, I would think.

---- >8 ----
Subject: [PATCH] fetch: do not ask for HEAD unnecessarily

In 3f763ddf28 (fetch: set remote/HEAD if it does not exist, 2024-11-22), git-fetch learned to opportunistically set $REMOTE/HEAD when fetching by always asking for remote HEAD, in the hope that it will help setting refs/remotes/<name>/HEAD if missing.

But it is not needed to always ask for remote HEAD. When we are fetching from a remote, for which we have remote-tracking branches, we do need to know about HEAD. But if we are doing one-shot fetch, e.g.,

  $ git fetch --tags https://github.com/git/git

we do not even know what sub-hierarchy of refs/remotes/<remote>/ we need to adjust the remote HEAD for. There is no need to ask for HEAD in such a case.

Incidentally, because the unconditional request to list "HEAD" affected the number of ref-prefixes requested in the ls-remote request, this affected how the requests for tags are added to the same ls-remote request, breaking "git fetch --tags $URL" performed against a URL that is not configured as a remote.

[jc: tests are also borrowed from Josh's patch]

---

* Even though I borrowed some part of the commit log message from Josh's version, it not clear to me how "*after* deciding" led to whatever the observed breakage (which was not described in the log message), in the following part.

Specifically, we now unconditionally add HEAD to the ref_prefixes list, but we did this *after* deciding whether we also need to explicitly request tags.

Bence's change asks "HEAD" after "if we are fetching something, then also ask about refs/tags/" logic thought we are not fetching anything (i.e. ref_prefixes.nr == 0 at that point). But before Bence's series, the same refs/tags/ logic saw that (ref_prefix.nr == 0), didn't it? So that does not sound like a sufficient explanation on how the series regressed.

---
 builtin/fetch.c  | 20 +++++++++++++++++++-
 t/t5510-fetch.sh | 17 +++++++++++++++++
 2 files changed, 36 insertions(+), 1 deletion(-)
builtin/fetch.c +19 -1
index a64de4485f..3eb6f3acc9 100644
@@ -1643,6 +1643,21 @@ static int set_head(const struct ref *remote_refs)
 	return result;
 }
 
+static int uses_remote_tracking(struct transport *transport, struct refspec *rs)
+{
+	if (!remote_is_configured(transport->remote, 0))
+		return 0;
+
+	if (!rs->nr)
+		rs = &transport->remote->fetch;
+
+	for (int i = 0; i < rs->nr; i++)
+		if (rs->items[i].dst)
+			return 1;
+
+	return 0;
+}
+
 static int do_fetch(struct transport *transport,
 		    struct refspec *rs,
 		    const struct fetch_config *config)
@@ -1712,7 +1727,10 @@ static int do_fetch(struct transport *transport,
 				    "refs/tags/");
 	}
 
-	strvec_push(&transport_ls_refs_options.ref_prefixes, "HEAD");
+	if (uses_remote_tracking(transport, rs)) {
+		must_list_refs = 1;
+		strvec_push(&transport_ls_refs_options.ref_prefixes, "HEAD");
+	}
 
 	if (must_list_refs) {
 		trace2_region_enter("fetch", "remote_refs", the_repository);
t/t5510-fetch.sh +17 -0
index 87698341f5..d7602333ff 100755
@@ -189,6 +189,23 @@ test_expect_success 'fetch --prune --tags with refspec prunes based on refspec'
 	git rev-parse sometag
 '
 
+test_expect_success 'fetch --tags gets tags even without a configured remote' '
+	REMOTE="$(pwd)/test_tag_1" &&
+	git init test_tag_1 &&
+	(
+		cd test_tag_1 &&
+		test_commit foo
+	) &&
+	git init test_tag_2 &&
+	(
+		cd test_tag_2 &&
+		git fetch --tags "file://$REMOTE" &&
+		echo "foo" >expect &&
+		git tag >actual &&
+		test_cmp expect actual
+	)
+'
+
 test_expect_success REFFILES 'fetch --prune fails to delete branches' '
 	cd "$D" &&
 	git clone . prune-fail &&
Signature · 1 line
2.47.1-576-g9f8c8eb655
#216 lore raw

Re: Re* [PATCH] Fix `git fetch --tags` in repo with no configured remote

Quoted 10 lines · Junio C Hamano
Quoted 8 lines · Junio C Hamano

Stepping back a bit, do we even need to learn where HEAD points at in the remote, when we are not doing the "opportunistically set $REMOTE/HEAD"? Your example is "in repo with no configured remote", which by definition means that we do not use any refs/remotes/*/ ref hierarchy to keep track of the remote-tracking branches for the remote we are fetching from. There is no place we record what we learn by running ls-remote HEAD against them, so should we even push "HEAD" to the ls-remote prefixes in such a case?

Yes, it probably doesn't make any sense to do that.

Quoted 66 lines
Quoted 8 lines

While this change may hide the breakage you saw in your set-up, we may be now asking to ls-remote HEAD even in cases we do not need to.

Quoted 3 lines

Fix this by adding HEAD to the ref_prefixes list prior to handling the --tags flag, and removing the now obsolete check whether ref_prefixes is empty or not.

Quoted 5 lines

And if we unconditionally add HEAD even when we do not need to, especially with the loss of the ref-prefixes condition that was there in order to implement "learn refs/tags/* hierarchy only when we are doing the default fetch", wouldn't it mean we may learn refs/tags/* even when we do not have to?

In other words, what I think the "fix" should look like is more like the attached. It seems to pass your test, as well as existing tests Bence added and other tests about "git fetch".

One thing I am not happy about is the abstraction violation that is needed to make the uses_remote_tracking() helper aware of the "use the rs, the refspec given from the command line, or if it is empty, use the configured 'fetch' refspec from the remote" rule, which is primarily used by get_ref_map() that is much later called, but the layering violation started when we started limiting the ls-remote request with narrowing common prefixes, and it would take a larger surgery to fix, I would think.

---- >8 ----
Subject: [PATCH] fetch: do not ask for HEAD unnecessarily

In 3f763ddf28 (fetch: set remote/HEAD if it does not exist, 2024-11-22), git-fetch learned to opportunistically set $REMOTE/HEAD when fetching by always asking for remote HEAD, in the hope that it will help setting refs/remotes/<name>/HEAD if missing.

But it is not needed to always ask for remote HEAD. When we are fetching from a remote, for which we have remote-tracking branches, we do need to know about HEAD. But if we are doing one-shot fetch, e.g.,

  $ git fetch --tags https://github.com/git/git

we do not even know what sub-hierarchy of refs/remotes/<remote>/ we need to adjust the remote HEAD for. There is no need to ask for HEAD in such a case.

Incidentally, because the unconditional request to list "HEAD" affected the number of ref-prefixes requested in the ls-remote request, this affected how the requests for tags are added to the same ls-remote request, breaking "git fetch --tags $URL" performed against a URL that is not configured as a remote.

[jc: tests are also borrowed from Josh's patch]

---

* Even though I borrowed some part of the commit log message from Josh's version, it not clear to me how "*after* deciding" led to whatever the observed breakage (which was not described in the log message), in the following part.

Specifically, we now unconditionally add HEAD to the ref_prefixes list, but we did this *after* deciding whether we also need to explicitly request tags.

Quoted 6 lines

Bence's change asks "HEAD" after "if we are fetching something, then also ask about refs/tags/" logic thought we are not fetching anything (i.e. ref_prefixes.nr == 0 at that point). But before Bence's series, the same refs/tags/ logic saw that (ref_prefix.nr == 0), didn't it? So that does not sound like a sufficient explanation on how the series regressed.

I did a bit of poking around on what is happening. For one I can confirm, that both before and after the set_head series transport_ls_refs_options.ref_prefixes.nr is 0. So the difference must be happening somewhere after that, and is not a side effect of calling set_head either, but I didn't manage to pin it down further.

I also checked what happens in set_head, just to be on the safe side: heads is empty so we reach the if where we check heads.nr which is 0. So at least no strange refs are created :)

Quoted 71 lines
---
 builtin/fetch.c  | 20 +++++++++++++++++++-
 t/t5510-fetch.sh | 17 +++++++++++++++++
 2 files changed, 36 insertions(+), 1 deletion(-)
builtin/fetch.c +19 -1
index a64de4485f..3eb6f3acc9 100644
@@ -1643,6 +1643,21 @@ static int set_head(const struct ref *remote_refs)
 	return result;
 }
 
+static int uses_remote_tracking(struct transport *transport, struct refspec *rs)
+{
+	if (!remote_is_configured(transport->remote, 0))
+		return 0;
+
+	if (!rs->nr)
+		rs = &transport->remote->fetch;
+
+	for (int i = 0; i < rs->nr; i++)
+		if (rs->items[i].dst)
+			return 1;
+
+	return 0;
+}
+
 static int do_fetch(struct transport *transport,
 		    struct refspec *rs,
 		    const struct fetch_config *config)
@@ -1712,7 +1727,10 @@ static int do_fetch(struct transport *transport,
 				    "refs/tags/");
 	}
 
-	strvec_push(&transport_ls_refs_options.ref_prefixes, "HEAD");
+	if (uses_remote_tracking(transport, rs)) {
+		must_list_refs = 1;
+		strvec_push(&transport_ls_refs_options.ref_prefixes, "HEAD");
+	}
 
 	if (must_list_refs) {
 		trace2_region_enter("fetch", "remote_refs", the_repository);
t/t5510-fetch.sh +17 -0
index 87698341f5..d7602333ff 100755
@@ -189,6 +189,23 @@ test_expect_success 'fetch --prune --tags with refspec prunes based on refspec'
 	git rev-parse sometag
 '
 
+test_expect_success 'fetch --tags gets tags even without a configured remote' '
+	REMOTE="$(pwd)/test_tag_1" &&
+	git init test_tag_1 &&
+	(
+		cd test_tag_1 &&
+		test_commit foo
+	) &&
+	git init test_tag_2 &&
+	(
+		cd test_tag_2 &&
+		git fetch --tags "file://$REMOTE" &&
+		echo "foo" >expect &&
+		git tag >actual &&
+		test_cmp expect actual
+	)
+'
+
 test_expect_success REFFILES 'fetch --prune fails to delete branches' '
 	cd "$D" &&
 	git clone . prune-fail &&
#217 lore raw

Re: Re* [PATCH] Fix `git fetch --tags` in repo with no configured remote

In reply to Junio C Hamano
Quoted 4 lines · Junio C Hamano
builtin/fetch.c +4 -0
+static int uses_remote_tracking(struct transport *transport, struct refspec *rs)
+{
+	if (!remote_is_configured(transport->remote, 0))
+		return 0;

builtin/fetch.c static int set_head(const struct ref *remote_refs) · line 1659

Quoted 10 lines
builtin/fetch.c +10 -0
+
+	if (!rs->nr)
+		rs = &transport->remote->fetch;
+
+	for (int i = 0; i < rs->nr; i++)
+		if (rs->items[i].dst)
+			return 1;
+
+	return 0;
+}

For the purpose of adjusting refs/remotes/<name>/HEAD, which is expected to point at something that we copy from their refs/heads/, it may be tempting to tighten the above logic to say, instead of "we have a refspec item that stores to somewhere", do something like

	for (int i = 0; i < rs->nr; i++) {
		struct refspec_item *item = &rs->items[i];

		if (item->dst &&
		    item->src && starts_with(item->src, "refs/heads/"))
			return 1;
	}

to make sure that we are tracking their branch refs, not some random hierarchy for which refs/remotes/<name>/HEAD does not matter.

I will leave the simplest "just check we have .dst" version (in other words, without any additional check on the .src side) in my tree, but if anybody wants to pursue "let's make sure that .src copies from somewhere in refs/heads/", please make sure that you consider "--mirror", where an equivalent of "refs/*:refs/*" is used as the refspec, i.e. there is a case where .src does not begin with "refs/heads/" but does cover the hierarchy and wants to learn about "HEAD".

Thanks.

#218 lore raw

Re: [PATCH v15 09/10] fetch: set remote/HEAD if it does not exist

In reply to Josh Steadmon

On Thu Dec 05, 2024 at 21:11, Josh Steadmon <[email protected]> wrote:
[snip]

Quoted 26 lines
Quoted 26 lines

Thanks for looking into this! I think the issue is with

$GIT fetch --tags "file://$REMOTE"

instead of adding a proper remote. Tbh, I've never seen the above syntax before, so first I just ran your script, which reproduced the issue for me, but then I modified it to use a proper remote which works as expected:

rm -rf test_tag_1 test_tag_2
GIT=~/git/bin-wrappers/git
mkdir test_tag_1 && cd test_tag_1
REMOTE=$(pwd)
$GIT init .
touch foo.txt
$GIT add foo.txt
$GIT commit foo.txt -m "commit one"
$GIT tag foo
cd ..
mkdir test_tag_2 && cd test_tag_2
$GIT init .
$GIT remote add origin $REMOTE
echo fetch --tags
$GIT fetch origin --tags
echo regular fetch
$GIT fetch origin 'refs/tags/*:refs/tags/*'
$GIT --version

Quoted 8 lines
Quoted 3 lines

So I'm assuming this is why also the tests never caught this, since probably all of them are using git remote add.

Yeah, I think the issue is that we check the --tags flag first, but only add the ref_prefixes entry if it's not empty already. Then after that we unconditionally add HEAD. So that's why moving your strvec_push earlier fixes it. I'll send a fix + test patch in just a minute.

Awesome, thanks!

#219 lore raw

PATCH v15 10/10 fetch set_head: handle mirrored bare repositories

In reply to Bence Ferdinandy

When adding a remote to bare repository with "git remote add --mirror", running fetch will fail to update HEAD to the remote's HEAD, since it does not know how to handle bare repositories. On the other hand HEAD already has content, since "git init --bare" has already set HEAD to whatever is the default branch set for the user. Unless this - by chance
- is the same as the remote's HEAD, HEAD will be pointing to a bad symref. Teach set_head to handle bare repositories, by overwriting HEAD so it mirrors the remote's HEAD.

Note, that in this case overriding the local HEAD reference is necessary, since HEAD will exist before fetch can be run, but this should not be an issue, since the whole purpose of --mirror is to be an exact mirror of the remote, so following any changes to HEAD makes sense.

Also note, that although "git remote set-head" also fails when trying to update the remote's locally tracked HEAD in a mirrored bare repository, the usage of the command does not make much sense after this patch:
fetch will update the remote HEAD correctly, and setting it manually to something else is antithetical to the concept of mirroring.

---

Notes:
state:
* last review: none

v10: - new patch
- handles the issue discovered in
https://lore.kernel.org/git/[email protected]/T/

v11: no change

v12: no change

v13: properly print the previous head if it was detached

v14: no change

v15: no change

 builtin/fetch.c   | 16 +++++++++++-----
 t/t5505-remote.sh | 10 ++++++++++
 2 files changed, 21 insertions(+), 5 deletions(-)
builtin/fetch.c +11 -5
index bbfaf50b63..2f416cf867 100644
@@ -1581,7 +1581,7 @@ static const char *strip_refshead(const char *name){
 
 static int set_head(const struct ref *remote_refs)
 {
-	int result = 0;
+	int result = 0, is_bare;
 	struct strbuf b_head = STRBUF_INIT, b_remote_head = STRBUF_INIT;
 	const char *remote = gtransport->remote->name;
 	char *head_name = NULL;
@@ -1613,15 +1613,21 @@ static int set_head(const struct ref *remote_refs)
 
 	if (!head_name)
 		goto cleanup;
-	strbuf_addf(&b_head, "refs/remotes/%s/HEAD", remote);
-	strbuf_addf(&b_remote_head, "refs/remotes/%s/%s", remote, head_name);
+	is_bare = is_bare_repository();
+	if (is_bare) {
+		strbuf_addstr(&b_head, "HEAD");
+		strbuf_addf(&b_remote_head, "refs/heads/%s", head_name);
+	} else {
+		strbuf_addf(&b_head, "refs/remotes/%s/HEAD", remote);
+		strbuf_addf(&b_remote_head, "refs/remotes/%s/%s", remote, head_name);
+	}
 		/* make sure it's valid */
-	if (!refs_ref_exists(refs, b_remote_head.buf)) {
+	if (!is_bare && !refs_ref_exists(refs, b_remote_head.buf)) {
 		result = 1;
 		goto cleanup;
 	}
 	if (refs_update_symref_extended(refs, b_head.buf, b_remote_head.buf,
-					"fetch", NULL, 1))
+					"fetch", NULL, !is_bare))
 		result = 1;
 
 cleanup:
t/t5505-remote.sh +10 -0
index afa261409f..2600add82a 100755
@@ -569,6 +569,16 @@ test_expect_success 'add --mirror && prune' '
 	)
 '
 
+test_expect_success 'add --mirror setting HEAD' '
+	mkdir headmirror &&
+	(
+		cd headmirror &&
+		git init --bare -b notmain &&
+		git remote add --mirror -f origin ../one &&
+		test "$(git symbolic-ref HEAD)" = "refs/heads/main"
+	)
+'
+
 test_expect_success 'add --mirror=fetch' '
 	mkdir mirror-fetch &&
 	git init -b main mirror-fetch/parent &&
Signature · 1 line
2.47.0.298.g52a96ec17b
#220 lore raw

PATCH 1/1 fetch: add configuration for set_head behaviour

In reply to Bence Ferdinandy

In the current implementatio, if refs/remotes/$remote/HEAD does not exist, running fetch will create it, but if it does exist it will not do anything, which is a somewhat safe and minimal approach. Unfortunately, for users who wish to NOT have refs/remotes/$remote/HEAD set for any reason (e.g. so that git rev-parse origin doesn't accidentally point them somewhere they do not want to), there is no way to remove this behaviour. On the other side of the spectrum, users may want fetch to automatically update HEAD or at least give them a warning if something changed on the remote.

Introduce a new setting, remote.$remote.followRemoteHEAD with four options:

- "never": do not ever do anything, not even create
- "create": the current behaviour, now the default behaviour
- "warn": print a message if remote and local HEAD is different
- "always": silently update HEAD on every change

---

Notes:
This patch is built off v15 of the set-head/fetch updates series:
https://lore.kernel.org/git/[email protected]/T/#m13a0c3e8919872188ef07fd9fd984c78e8aa35ba

The patch is sent separately, because the rest of the series building up to here seems ready, while this patch will likely trigger some discussion.

 Documentation/config/remote.txt |  11 ++++
 builtin/fetch.c                 |  46 ++++++++++++--
 remote.c                        |   9 +++
 remote.h                        |   9 +++
 t/t5510-fetch.sh                | 102 ++++++++++++++++++++++++++++++++
 5 files changed, 171 insertions(+), 6 deletions(-)
Documentation/config/remote.txt +11 -0
index 6d8b7d6c63..024f92befc 100644
@@ -101,6 +101,17 @@ remote.<name>.serverOption::
 	The default set of server options used when fetching from this remote.
 	These server options can be overridden by the `--server-option=` command
 	line arguments.
+
+remote.<name>.followRemoteHEAD::
+	How linkgit:git-fetch[1] should handle updates to `remotes/<name>/HEAD`.
+	The default value is "create", which will create `remotes/<name>/HEAD`
+	if it exists on the remote, but not locally, but will not touch an
+	already existing local reference.  Setting to "warn" will print
+	a message if the remote has a different value, than the local one and
+	in case there is no local reference, it behaves like "create". Setting
+	to "always" will silently update it to the value on the remote.
+	Finally, setting it to "never" will never change or create the local
+	reference.
 +
 This is a multi-valued variable, and an empty value can be used in a higher
 priority configuration file (e.g. `.git/config` in a repository) to clear
builtin/fetch.c +40 -6

84 lines not on this page · raw message

remote.c +9 -0
index 10104d11e3..5a768ddac2 100644
@@ -514,6 +514,15 @@ static int handle_config(const char *key, const char *value,
 	} else if (!strcmp(subkey, "serveroption")) {
 		return parse_transport_option(key, value,
 					      &remote->server_options);
+	} else if (!strcmp(subkey, "followremotehead")) {
+		if (!strcmp(value, "never"))
+			remote->follow_remote_head = -1;
+		else if (!strcmp(value, "create"))
+			remote->follow_remote_head = 0;
+		else if (!strcmp(value, "warn"))
+			remote->follow_remote_head = 1;
+		else if (!strcmp(value, "always"))
+			remote->follow_remote_head = 2;
 	}
 	return 0;
 }
remote.h +9 -0
index a7e5c4e07c..3ceadac820 100644
@@ -107,6 +107,15 @@ struct remote {
 	char *http_proxy_authmethod;
 
 	struct string_list server_options;
+
+	/*
+	 * The setting for whether to update HEAD for the remote.
+	 * -1 never update
+	 * 0 create only (default)
+	 * 1 warn on change
+	 * 2 always update
+	 */
+	int follow_remote_head;
 };
 
 /**
t/t5510-fetch.sh +102 -0

108 lines not on this page · raw message

Signature · 1 line
2.47.1.295.ge630f003e1.dirty
#221 lore raw

Re: [PATCH v1] fetch: add configuration for set_head behaviour

Quoted 7 lines · Bence Ferdinandy

Introduce a new setting, remote.$remote.followRemoteHEAD with four options:

- "never": do not ever do anything, not even create
- "create": the current behaviour, now the default behaviour
- "warn": print a message if remote and local HEAD is different
- "always": silently update HEAD on every change

That seems to be plenty of choices to please many classes of users.

Except for the one that I would want to use myself, which is "I understand their HEAD points at branch X right now; please warn when they flip their HEAD to a different branch, but until then please do nothing". That's somewhere between "never" and "warn".

builtin/fetch.c static int set_head(const struct ref *remote_refs) · line 1632

Quoted 6 lines
builtin/fetch.c +2 -0
@@ -1603,6 +1628,8 @@ static int set_head(const struct ref *remote_refs)
 		string_list_append(&heads, strip_refshead(ref->name));
 	}
 
+	if (follow_remote_head < 0)
+		goto cleanup;

There is some "magical" value(s) that is/are negative; we will find out what they are later.

builtin/fetch.c static int set_head(const struct ref *remote_refs) · line 1644

Quoted 5 lines
builtin/fetch.c +1 -0
@@ -1614,6 +1641,7 @@ static int set_head(const struct ref *remote_refs)
 	if (!head_name)
 		goto cleanup;
 	is_bare = is_bare_repository();
+	create_only = follow_remote_head == 2 ? 0 : !is_bare;

There is one more "magical" value that follow_remote_head can take.

builtin/fetch.c static int set_head(const struct ref *remote_refs) · line 1663

Quoted 13 lines
builtin/fetch.c +6 -2
@@ -1626,9 +1654,14 @@ static int set_head(const struct ref *remote_refs)
 		result = 1;
 		goto cleanup;
 	}
-	if (refs_update_symref_extended(refs, b_head.buf, b_remote_head.buf,
-					"fetch", NULL, !is_bare))
+	was_detached = refs_update_symref_extended(refs, b_head.buf, b_remote_head.buf,
+					"fetch", &b_local_head, create_only);
+	if (was_detached == -1) {
 		result = 1;
+		goto cleanup;
+	}
+	if (follow_remote_head == 1 && verbosity >= 0)

And there is one more.

Quoted 7 lines
remote.c +0 -0
index 10104d11e3..5a768ddac2 100644
@@ -514,6 +514,15 @@ static int handle_config(const char *key, const char *value,
 	} else if (!strcmp(subkey, "serveroption")) {
 		return parse_transport_option(key, value,

remote.c static int handle_config(const char *key, const char *value, · line 525

Quoted 10 lines
remote.c +9 -0
 					      &remote->server_options);
+	} else if (!strcmp(subkey, "followremotehead")) {
+		if (!strcmp(value, "never"))
+			remote->follow_remote_head = -1;
+		else if (!strcmp(value, "create"))
+			remote->follow_remote_head = 0;
+		else if (!strcmp(value, "warn"))
+			remote->follow_remote_head = 1;
+		else if (!strcmp(value, "always"))
+			remote->follow_remote_head = 2;

Use something like

/* The setting for whether to update HEAD for the remote. */ enum follow_remote_head {
FOLLOW_REMOTE_NEVER = -1, FOLLOW_REMOTE_CREATE = 0, FOLLOW_REMOTE_WARN = 1, FOLLOW_REMOTE_ALWAYS = 2,
};

or something? I have no strong preference between "enum" and "#define" myself, but moderately strong preference for anything symbolic over magic numbers.

Quoted 8 lines
remote.h +0 -0
index a7e5c4e07c..3ceadac820 100644
@@ -107,6 +107,15 @@ struct remote {
 	char *http_proxy_authmethod;
 
 	struct string_list server_options;

remote.h struct remote { · line 119

Quoted 10 lines
remote.h +9 -0
+
+	/*
+	 * The setting for whether to update HEAD for the remote.
+	 * -1 never update
+	 * 0 create only (default)
+	 * 1 warn on change
+	 * 2 always update
+	 */
+	int follow_remote_head;
 };

Other than that, looking good from a cursory read.

Thanks.

#222 lore raw

Re: [PATCH v1] fetch: add configuration for set_head behaviour

Quoted 10 lines · Junio C Hamano
Quoted 7 lines · Bence Ferdinandy

Introduce a new setting, remote.$remote.followRemoteHEAD with four options:

- "never": do not ever do anything, not even create
- "create": the current behaviour, now the default behaviour
- "warn": print a message if remote and local HEAD is different
- "always": silently update HEAD on every change

Quoted 6 lines

That seems to be plenty of choices to please many classes of users.

Except for the one that I would want to use myself, which is "I understand their HEAD points at branch X right now; please warn when they flip their HEAD to a different branch, but until then please do nothing". That's somewhere between "never" and "warn".

Just to clarify, an example: the remote's HEAD is set to "master", and you have
- git remote set-head origin next
- git config set remote.origin.followRemoteHead "manual"
- git config set remote.origin.followRemoteHeadManual "master"

you manually set these explicitly. As long as the remote's HEAD is still "master" you do not get a warning when running fetch, but if it changes to something else (even "next"), you _do_ get a warning, that is not silenced until you set followRemoteHeadManual to "next".

Would you expect git remote set-head origin next to set followRemoteHead to "manual" and to set the correct value for the followRemoteHeadManual to "master" if it actually changed the current refs/remote/origin/HEAD from master to next? Or is having this completely manual fine?

I toyed with the thought of rolling the two settings together (an unrecognized string would mean the reference for which we must be silent), but then you couldn't have a remote/HEAD called "create" for example, so I think we need to store that separately. I'm also not quite happy with "followRemoteHeadManual"
...

Quoted 63 lines

builtin/fetch.c static int set_head(const struct ref *remote_refs) · line 1632

Quoted 7 lines
builtin/fetch.c +2 -0
@@ -1603,6 +1628,8 @@ static int set_head(const struct ref *remote_refs)
 		string_list_append(&heads, strip_refshead(ref->name));
 	}
 
+	if (follow_remote_head < 0)
+		goto cleanup;

There is some "magical" value(s) that is/are negative; we will find out what they are later.

builtin/fetch.c static int set_head(const struct ref *remote_refs) · line 1644

Quoted 5 lines
builtin/fetch.c +1 -0
@@ -1614,6 +1641,7 @@ static int set_head(const struct ref *remote_refs)
 	if (!head_name)
 		goto cleanup;
 	is_bare = is_bare_repository();
+	create_only = follow_remote_head == 2 ? 0 : !is_bare;

There is one more "magical" value that follow_remote_head can take.

builtin/fetch.c static int set_head(const struct ref *remote_refs) · line 1663

Quoted 13 lines
builtin/fetch.c +6 -2
@@ -1626,9 +1654,14 @@ static int set_head(const struct ref *remote_refs)
 		result = 1;
 		goto cleanup;
 	}
-	if (refs_update_symref_extended(refs, b_head.buf, b_remote_head.buf,
-					"fetch", NULL, !is_bare))
+	was_detached = refs_update_symref_extended(refs, b_head.buf, b_remote_head.buf,
+					"fetch", &b_local_head, create_only);
+	if (was_detached == -1) {
 		result = 1;
+		goto cleanup;
+	}
+	if (follow_remote_head == 1 && verbosity >= 0)

And there is one more.

Quoted 7 lines
remote.c +0 -0
index 10104d11e3..5a768ddac2 100644
@@ -514,6 +514,15 @@ static int handle_config(const char *key, const char *value,
 	} else if (!strcmp(subkey, "serveroption")) {
 		return parse_transport_option(key, value,

remote.c static int handle_config(const char *key, const char *value, · line 525

Quoted 10 lines
remote.c +9 -0
 					      &remote->server_options);
+	} else if (!strcmp(subkey, "followremotehead")) {
+		if (!strcmp(value, "never"))
+			remote->follow_remote_head = -1;
+		else if (!strcmp(value, "create"))
+			remote->follow_remote_head = 0;
+		else if (!strcmp(value, "warn"))
+			remote->follow_remote_head = 1;
+		else if (!strcmp(value, "always"))
+			remote->follow_remote_head = 2;

Use something like

/* The setting for whether to update HEAD for the remote. */ enum follow_remote_head {
FOLLOW_REMOTE_NEVER = -1, FOLLOW_REMOTE_CREATE = 0, FOLLOW_REMOTE_WARN = 1, FOLLOW_REMOTE_ALWAYS = 2,
};

Quoted 3 lines

or something? I have no strong preference between "enum" and "#define" myself, but moderately strong preference for anything symbolic over magic numbers.

Ah, my mistake sorry. Magic number bad.

Best,
Bence

#223 lore raw

Re: [PATCH v1] fetch: add configuration for set_head behaviour

Quoted 9 lines · Bence Ferdinandy

Just to clarify, an example: the remote's HEAD is set to "master", and you have
- git remote set-head origin next
- git config set remote.origin.followRemoteHead "manual"
- git config set remote.origin.followRemoteHeadManual "master"

you manually set these explicitly. As long as the remote's HEAD is still "master" you do not get a warning when running fetch, but if it changes to something else (even "next"), you _do_ get a warning, that is not silenced until you set followRemoteHeadManual to "next".

Yup.

Quoted 4 lines

Would you expect git remote set-head origin next to set followRemoteHead to "manual" and to set the correct value for the followRemoteHeadManual to "master" if it actually changed the current refs/remote/origin/HEAD from master to next?

What I found missing is:

"I know this is the value I expect them to have, because it was the value I last observed there. Please let me know when they changed their mind; I want to reconsider my position when it happens, and your warning me would help me to do so."

My running "remote set-head" to manually change which of their branches I am interested in does not tell Git anything about what branch I expect them to be pointing at with HEAD. It may be the action after such "reconsideration" of my position, but there is 0 bit of information on what I expect their HEAD to be pointing at.

Quoted 1 line

Or is having this completely manual fine?

If it can be automated, that would be nicer, but I do not think a manual "remote set-head origin next" gives any information to help automating it.

Quoted 5 lines

I toyed with the thought of rolling the two settings together (an unrecognized string would mean the reference for which we must be silent), but then you couldn't have a remote/HEAD called "create" for example, so I think we need to store that separately. I'm also not quite happy with "followRemoteHeadManual"
...

Yeah, I was thinking a value like "warn-if-not-pointing-at-$branch" where $branch part would be a token like 'bc/set-head-symref', 'master', etc.

I do not think any of the above should block this topic. It can be added later without disrupting all other modes implemented there.

Thanks.

#224 lore raw

Re: [PATCH v1] fetch: add configuration for set_head behaviour

Quoted 26 lines · Junio C Hamano
Quoted 9 lines · Bence Ferdinandy

Just to clarify, an example: the remote's HEAD is set to "master", and you have
- git remote set-head origin next
- git config set remote.origin.followRemoteHead "manual"
- git config set remote.origin.followRemoteHeadManual "master"

you manually set these explicitly. As long as the remote's HEAD is still "master" you do not get a warning when running fetch, but if it changes to something else (even "next"), you _do_ get a warning, that is not silenced until you set followRemoteHeadManual to "next".

Yup.

Quoted 4 lines

Would you expect git remote set-head origin next to set followRemoteHead to "manual" and to set the correct value for the followRemoteHeadManual to "master" if it actually changed the current refs/remote/origin/HEAD from master to next?

What I found missing is:

"I know this is the value I expect them to have, because it was the value I last observed there. Please let me know when they changed their mind; I want to reconsider my position when it happens, and your warning me would help me to do so."

Quoted 5 lines

My running "remote set-head" to manually change which of their branches I am interested in does not tell Git anything about what branch I expect them to be pointing at with HEAD. It may be the action after such "reconsideration" of my position, but there is 0 bit of information on what I expect their HEAD to be pointing at.

Hmm. After a bit more thought: running remote set-head doesn't make much sense if you have "always" and makes a bit of sense if you have "warn". So maybe one thing set-head could do is _if_ you have always it could drop you to "warn" and "warn" could also include a line not just about following with "remote set-head" but something like "You can either follow with set-head, or you can turn this off but still get a warning if origin changes by setting ...".

Quoted 13 lines
Quoted 2 lines

Or is having this completely manual fine?

If it can be automated, that would be nicer, but I do not think a manual "remote set-head origin next" gives any information to help automating it.

Quoted 5 lines

I toyed with the thought of rolling the two settings together (an unrecognized string would mean the reference for which we must be silent), but then you couldn't have a remote/HEAD called "create" for example, so I think we need to store that separately. I'm also not quite happy with "followRemoteHeadManual"
...

Quoted 3 lines

Yeah, I was thinking a value like "warn-if-not-pointing-at-$branch" where $branch part would be a token like 'bc/set-head-symref', 'master', etc.

Good idea. Probably shorter though like "warn-if-not-$branch".

Quoted 3 lines

I do not think any of the above should block this topic. It can be added later without disrupting all other modes implemented there.

Ack, I'll send a respin with just enum fixed for now.

Thanks,
Bence

#225 lore raw

Re: [PATCH v1] fetch: add configuration for set_head behaviour

Quoted 2 lines · Bence Ferdinandy

Hmm. After a bit more thought: running remote set-head doesn't make much sense if you have "always" and makes a bit of sense if you have "warn".

Yeah, after "warn" tells the user what branch the remote points at, the user may decide to match. Or the user may decide to flip the remote-tracking HEAD to something unrelated. In the former case, the next and subsequent fetch will not warn, until the remote flips the HEAD again. In the latter case, the next and subsequent fetch will warn, until they happen to change their HEAD again and their choice happens to match ours.

Quoted 6 lines

So
maybe one thing set-head could do is _if_ you have always it could drop you to "warn" and "warn" could also include a line not just about following with "remote set-head" but something like "You can either follow with set-head, or you can turn this off but still get a warning if origin changes by setting ...".

Hmph, if you've been happy with "always", if we notice that the remote flipped its HEAD, we switch to "warn" and the next and subsequent fetch will not warn, until they flip again, and then we will keep warning.

If you do not want to follow (but still monitor origin), the above would not be sufficient, unless "by setting..." part gives a new choice via your .followRemoteHEADManual setting.

Quoted 5 lines
Quoted 3 lines

Yeah, I was thinking a value like "warn-if-not-pointing-at-$branch" where $branch part would be a token like 'bc/set-head-symref', 'master', etc.

Good idea. Probably shorter though like "warn-if-not-$branch".

Sorry, I wasn't offering an alternative I thought was better. It was "we could do this, if we really want to avoid two variables". But thinking about it further, not having to worry about two variables may be a huge benefit. If you set .followRemoteHEAD to "manual" and .followRemoteHEADManual to some value, and then changed .followRemoteHEAD to something else, you may be very likely to forget unsetting .followRemoteHEADManual that is no longer in effect.

Quoted 4 lines
Quoted 2 lines

I do not think any of the above should block this topic. It can be added later without disrupting all other modes implemented there.

Ack, I'll send a respin with just enum fixed for now.

Thanks.

#226 lore raw

cover set_head finishing touches

This series is intended to be the finishing touches on the fetch/remote set_head changes as in built off of bf/fetch-set-head-config <[email protected]>.

Patch 1/2 contains the missing setting Junio was missing, while 2/2 is an attempt to do the right thing.

I've marked them as RFC because I'm not sure about a couple of things:

- 1/2 contains a slight refactor by extracting the hints into a separate function to avoid writing it twice. It could be argued that this refactor could be in a separate patch.

- I'm not exactly sure what should be in this hint, I added the new "warn-if-not-$branch", but arguments could be made for adding "always", adding a generic "or see the other possible settings in the manual" or any other combination

- I've seen some examples of advice stuff in yellow that can be disabled, so maybe a complete explanation of possibilites could be done with that mechanism (I haven't yet looked into how that works)

- I'm also not entirely sure about 2/2. I think it's a safe move, that is very unlikely to be annoying, but it's hard to judge from my limited perspective on all the different possible workflows.

Bence Ferdinandy (2):
fetch set_head: add warn-if-not-$branch option remote set-head: set followRemoteHEAD to "warn" if "always"

 builtin/fetch.c   | 26 ++++++++++++++++++-------
 builtin/remote.c  | 12 +++++++++++-
 remote.c          |  5 +++++
 remote.h          |  6 ++++--
 t/t5505-remote.sh | 11 +++++++++++
 t/t5510-fetch.sh  | 49 +++++++++++++++++++++++++++++++++++++++++++++--
 6 files changed, 97 insertions(+), 12 deletions(-)
Signature · 1 line
2.47.1.297.g6455f89743
#227 lore raw

RFC PATCH 1/2 fetch set_head: add warn-if-not-$branch option

Currently if we want to have a remote/HEAD locally that is different from the one on the remote, but we still want to get a warning if remote changes HEAD, our only option is to have an indiscriminate warning with "follow_remote_head" set to "warn". Add a new option "warn-if-not-$branch", where $branch is a branch name we do not wish to get a warning about. If the remote HEAD is $branch do not warn, otherwise, behave as "warn".

E.g. let's assume, that our remote origin has HEAD
set to "master", but locally we have "git remote set-head origin seen". Setting 'remote.origin.followRemoteHEAD = "warn"' will always print a warning, even though the remote has not changed HEAD from "master". Setting 'remote.origin.followRemoteHEAD = "warn-if-not-master" will squelch the warning message, unless the remote changes HEAD from "master". Note, that should the remote change HEAD to "seen" (which we have locally), there will still be no warning.

Improve the advice message in report_set_head to also include silencing the warning message with "warn-if-not-$branch".

---
 builtin/fetch.c  | 26 ++++++++++++++++++-------
 remote.c         |  5 +++++
 remote.h         |  6 ++++--
 t/t5510-fetch.sh | 49 ++++++++++++++++++++++++++++++++++++++++++++++--
 4 files changed, 75 insertions(+), 11 deletions(-)
builtin/fetch.c +19 -7
index 88c5c5d781..fd7f3694cc 100644
@@ -1579,6 +1579,15 @@ static const char *strip_refshead(const char *name){
 	return name;
 }
 
+static void set_head_advice_msg(const char *remote, const char *head_name) {
+
+	printf("Run 'git remote set-head %s %s' to follow the change, or\n"
+		"'git config set remote.%s.warn-if-not-%s' to disable this warning\n"
+		"until the remote changes HEAD again.\n",
+		remote, head_name, remote, head_name);
+
+}
+
 static void report_set_head(const char *remote, const char *head_name,
 			struct strbuf *buf_prev, int updateres) {
 	struct strbuf buf_prefix = STRBUF_INIT;
@@ -1590,20 +1599,19 @@ static void report_set_head(const char *remote, const char *head_name,
 	if (prev_head && strcmp(prev_head, head_name)) {
 		printf("'HEAD' at '%s' is '%s', but we have '%s' locally.\n",
 			remote, head_name, prev_head);
-		printf("Run 'git remote set-head %s %s' to follow the change.\n",
-			remote, head_name);
+		set_head_advice_msg(remote, head_name);
 	}
 	else if (updateres && buf_prev->len) {
 		printf("'HEAD' at '%s' is '%s', "
 			"but we have a detached HEAD pointing to '%s' locally.\n",
 			remote, head_name, buf_prev->buf);
-		printf("Run 'git remote set-head %s %s' to follow the change.\n",
-			remote, head_name);
+		set_head_advice_msg(remote, head_name);
 	}
 	strbuf_release(&buf_prefix);
 }
 
-static int set_head(const struct ref *remote_refs, int follow_remote_head)
+static int set_head(const struct ref *remote_refs, int follow_remote_head,
+		const char *no_warn_branch)
 {
 	int result = 0, create_only, is_bare, was_detached;
 	struct strbuf b_head = STRBUF_INIT, b_remote_head = STRBUF_INIT,
@@ -1660,7 +1668,10 @@ static int set_head(const struct ref *remote_refs, int follow_remote_head)
 		result = 1;
 		goto cleanup;
 	}
-	if (follow_remote_head == FOLLOW_REMOTE_WARN && verbosity >= 0)
+	if ((follow_remote_head == FOLLOW_REMOTE_WARN ||
+		(follow_remote_head == FOLLOW_REMOTE_WARN_IF_NOT_BRANCH &&
+		strcmp(no_warn_branch, head_name))
+		) && verbosity >= 0)
 		report_set_head(remote, head_name, &b_local_head, was_detached);
 
 cleanup:
@@ -1889,7 +1900,8 @@ static int do_fetch(struct transport *transport,
 				  "you need to specify exactly one branch with the --set-upstream option"));
 		}
 	}
-	if (set_head(remote_refs, transport->remote->follow_remote_head))
+	if (set_head(remote_refs, transport->remote->follow_remote_head,
+		transport->remote->no_warn_branch))
 		;
 		/*
 		 * Way too many cases where this can go wrong
remote.c +5 -0
index 0b18840d43..f0e1b1b76a 100644
@@ -515,6 +515,7 @@ static int handle_config(const char *key, const char *value,
 		return parse_transport_option(key, value,
 					      &remote->server_options);
 	} else if (!strcmp(subkey, "followremotehead")) {
+		const char *no_warn_branch;
 		if (!strcmp(value, "never"))
 			remote->follow_remote_head = FOLLOW_REMOTE_NEVER;
 		else if (!strcmp(value, "create"))
@@ -523,6 +524,10 @@ static int handle_config(const char *key, const char *value,
 			remote->follow_remote_head = FOLLOW_REMOTE_WARN;
 		else if (!strcmp(value, "always"))
 			remote->follow_remote_head = FOLLOW_REMOTE_ALWAYS;
+		else if (skip_prefix(value, "warn-if-not-", &no_warn_branch)) {
+			remote->follow_remote_head = FOLLOW_REMOTE_WARN_IF_NOT_BRANCH;
+			remote->no_warn_branch = no_warn_branch;
+		}
 	}
 	return 0;
 }
remote.h +4 -2
index 184b35653d..75be3977ba 100644
@@ -62,8 +62,9 @@ struct remote_state *remote_state_new(void);
 	enum follow_remote_head_settings {
 		FOLLOW_REMOTE_NEVER = -1,
 		FOLLOW_REMOTE_CREATE = 0,
-		FOLLOW_REMOTE_WARN = 1,
-		FOLLOW_REMOTE_ALWAYS = 2,
+		FOLLOW_REMOTE_WARN_IF_NOT_BRANCH = 1,
+		FOLLOW_REMOTE_WARN = 2,
+		FOLLOW_REMOTE_ALWAYS = 3,
 	};
 
 struct remote {
@@ -116,6 +117,7 @@ struct remote {
 	struct string_list server_options;
 
 	enum follow_remote_head_settings follow_remote_head;
+	const char *no_warn_branch;
 };
 
 /**
t/t5510-fetch.sh +47 -2

67 lines not on this page · raw message

Signature · 1 line
2.47.1.297.g6455f89743
#228 lore raw

Re: [RFC PATCH v1 1/2] fetch set_head: add warn-if-not-$branch option

builtin/fetch.c static const char *strip_refshead(const char *name){ · line 1589

Quoted 8 lines · Bence Ferdinandy
builtin/fetch.c +8 -0
+static void set_head_advice_msg(const char *remote, const char *head_name) {
+
+	printf("Run 'git remote set-head %s %s' to follow the change, or\n"
+		"'git config set remote.%s.warn-if-not-%s' to disable this warning\n"
+		"until the remote changes HEAD again.\n",
+		remote, head_name, remote, head_name);
+
+}

Style. "{" that encloses the function body sits on a line of its own.

Perhaps use the advise_if_enabled(), so that those who already learned how to deal with the situation can squelch the "how to fix" message.

Quoted 7 lines
builtin/fetch.c +2 -1
-static int set_head(const struct ref *remote_refs, int follow_remote_head)
+static int set_head(const struct ref *remote_refs, int follow_remote_head,
+		const char *no_warn_branch)
 {
 	int result = 0, create_only, is_bare, was_detached;
 	struct strbuf b_head = STRBUF_INIT, b_remote_head = STRBUF_INIT,

builtin/fetch.c static int set_head(const struct ref *remote_refs, int follow_remote_head) · line 1674

Quoted 8 lines
builtin/fetch.c +4 -1
@@ -1660,7 +1668,10 @@ static int set_head(const struct ref *remote_refs, int follow_remote_head)
 		result = 1;
 		goto cleanup;
 	}
-	if (follow_remote_head == FOLLOW_REMOTE_WARN && verbosity >= 0)
+	if ((follow_remote_head == FOLLOW_REMOTE_WARN ||
+		(follow_remote_head == FOLLOW_REMOTE_WARN_IF_NOT_BRANCH &&
+		strcmp(no_warn_branch, head_name))
+		) && verbosity >= 0)

Reorder conditions combined with &&- to have more expensive ones later.

	if (verbosity >= 0 &&
            (follow_remote_head == FOLLOW_REMOTE_WARN ||
	    (follow_remote_head == FOLLOW_REMOTE_WARN_IF_NOT_BRANCH &&
	     strcmp(no_warn_branch, head_name)))

As human readers, we may know that no_warn_branch is always valid when (follow_remote_head == FOLLOW_REMOTE_WARN_IF_NOT_BRANCH), but semi clever compilers may not realize it and give a false warning about using no_warn_branch potentially uninitialized.

We could do without adding FOLLOW_REMOTE_WARN_IF_NOT_BRANCH and reuse FOLLOW_REMOTE_WARN, like so:

	if (verbosity >= 0 &&
            follow_remote_head == FOLLOW_REMOTE_WARN &&
	    (!no_warn_branch || strcmp(no_warn_branch, head_name)))

That is, "if set to remote-warn, always warn, or no_warn_branch is not NULL, only warn if the current head is different from it".

Quoted 13 lines
remote.c +1 -0
index 0b18840d43..f0e1b1b76a 100644
@@ -515,6 +515,7 @@ static int handle_config(const char *key, const char *value,
 		return parse_transport_option(key, value,
 					      &remote->server_options);
 	} else if (!strcmp(subkey, "followremotehead")) {
+		const char *no_warn_branch;
 		if (!strcmp(value, "never"))
 			remote->follow_remote_head = FOLLOW_REMOTE_NEVER;
 		else if (!strcmp(value, "create"))

remote.c static int handle_config(const char *key, const char *value, · line 530

Quoted 7 lines
remote.c +4 -0
@@ -523,6 +524,10 @@ static int handle_config(const char *key, const char *value,
 			remote->follow_remote_head = FOLLOW_REMOTE_WARN;
 		else if (!strcmp(value, "always"))
 			remote->follow_remote_head = FOLLOW_REMOTE_ALWAYS;
+		else if (skip_prefix(value, "warn-if-not-", &no_warn_branch)) {
+			remote->follow_remote_head = FOLLOW_REMOTE_WARN_IF_NOT_BRANCH;
+			remote->no_warn_branch = no_warn_branch;
+		}

If we were to do without FOLLOW_REMOTE_WARN_IF_NOT_BRANCH, then the above becomes

			remote->follow_remote_head = FOLLOW_REMOTE_WARN;
			remote->no_warn_branch = NULL;
		} else if (skip_prefix(value, "warn-if-not-", &no_warn_branch)) {
			remote->follow_remote_head = FOLLOW_REMOTE_WARN;
			remote->no_warn_branch = no_warn_branch;
		} else if (!strcmp(value, "always")) {
			remote->follow_remote_head = FOLLOW_REMOTE_ALWAYS;
		} else {
			warn(_("unrecognized followRemoteHEAD value '%s' ignored"),
			     value);
		}

We'd want the new choice documented before we graduate this topic out of the RFC status.

Thanks.

#229 lore raw

Re: [RFC PATCH v1 1/2] fetch set_head: add warn-if-not-$branch option

Quoted 50 lines · Junio C Hamano

builtin/fetch.c static const char *strip_refshead(const char *name){ · line 1589

Quoted 8 lines · Bence Ferdinandy
builtin/fetch.c +8 -0
+static void set_head_advice_msg(const char *remote, const char *head_name) {
+
+	printf("Run 'git remote set-head %s %s' to follow the change, or\n"
+		"'git config set remote.%s.warn-if-not-%s' to disable this warning\n"
+		"until the remote changes HEAD again.\n",
+		remote, head_name, remote, head_name);
+
+}

Style. "{" that encloses the function body sits on a line of its own.

Perhaps use the advise_if_enabled(), so that those who already learned how to deal with the situation can squelch the "how to fix" message.

Quoted 7 lines
builtin/fetch.c +2 -1
-static int set_head(const struct ref *remote_refs, int follow_remote_head)
+static int set_head(const struct ref *remote_refs, int follow_remote_head,
+		const char *no_warn_branch)
 {
 	int result = 0, create_only, is_bare, was_detached;
 	struct strbuf b_head = STRBUF_INIT, b_remote_head = STRBUF_INIT,

builtin/fetch.c static int set_head(const struct ref *remote_refs, int follow_remote_head) · line 1674

Quoted 8 lines
builtin/fetch.c +4 -1
@@ -1660,7 +1668,10 @@ static int set_head(const struct ref *remote_refs, int follow_remote_head)
 		result = 1;
 		goto cleanup;
 	}
-	if (follow_remote_head == FOLLOW_REMOTE_WARN && verbosity >= 0)
+	if ((follow_remote_head == FOLLOW_REMOTE_WARN ||
+		(follow_remote_head == FOLLOW_REMOTE_WARN_IF_NOT_BRANCH &&
+		strcmp(no_warn_branch, head_name))
+		) && verbosity >= 0)

Reorder conditions combined with &&- to have more expensive ones later.

	if (verbosity >= 0 &&
            (follow_remote_head == FOLLOW_REMOTE_WARN ||
	    (follow_remote_head == FOLLOW_REMOTE_WARN_IF_NOT_BRANCH &&
	     strcmp(no_warn_branch, head_name)))

As human readers, we may know that no_warn_branch is always valid when (follow_remote_head == FOLLOW_REMOTE_WARN_IF_NOT_BRANCH), but semi clever compilers may not realize it and give a false warning about using no_warn_branch potentially uninitialized.

We could do without adding FOLLOW_REMOTE_WARN_IF_NOT_BRANCH and reuse FOLLOW_REMOTE_WARN, like so:

Quoted 6 lines
	if (verbosity >= 0 &&
            follow_remote_head == FOLLOW_REMOTE_WARN &&
	    (!no_warn_branch || strcmp(no_warn_branch, head_name)))

That is, "if set to remote-warn, always warn, or no_warn_branch is not NULL, only warn if the current head is different from it".

Ah, nice, this has the added benefit of a bad configuration where "warn-if-not-" doesn't actually specify a branch to fall back to just "warn".

Quoted 41 lines
Quoted 14 lines
remote.c +1 -0
index 0b18840d43..f0e1b1b76a 100644
@@ -515,6 +515,7 @@ static int handle_config(const char *key, const char *value,
 		return parse_transport_option(key, value,
 					      &remote->server_options);
 	} else if (!strcmp(subkey, "followremotehead")) {
+		const char *no_warn_branch;
 		if (!strcmp(value, "never"))
 			remote->follow_remote_head = FOLLOW_REMOTE_NEVER;
 		else if (!strcmp(value, "create"))

remote.c static int handle_config(const char *key, const char *value, · line 530

Quoted 7 lines
remote.c +4 -0
@@ -523,6 +524,10 @@ static int handle_config(const char *key, const char *value,
 			remote->follow_remote_head = FOLLOW_REMOTE_WARN;
 		else if (!strcmp(value, "always"))
 			remote->follow_remote_head = FOLLOW_REMOTE_ALWAYS;
+		else if (skip_prefix(value, "warn-if-not-", &no_warn_branch)) {
+			remote->follow_remote_head = FOLLOW_REMOTE_WARN_IF_NOT_BRANCH;
+			remote->no_warn_branch = no_warn_branch;
+		}

If we were to do without FOLLOW_REMOTE_WARN_IF_NOT_BRANCH, then the above becomes

			remote->follow_remote_head = FOLLOW_REMOTE_WARN;
			remote->no_warn_branch = NULL;
		} else if (skip_prefix(value, "warn-if-not-", &no_warn_branch)) {
			remote->follow_remote_head = FOLLOW_REMOTE_WARN;
			remote->no_warn_branch = no_warn_branch;
		} else if (!strcmp(value, "always")) {
			remote->follow_remote_head = FOLLOW_REMOTE_ALWAYS;
		} else {
			warn(_("unrecognized followRemoteHEAD value '%s' ignored"),
			     value);
		}

We'd want the new choice documented before we graduate this topic out of the RFC status.

Thanks.

Signature · 1 line
bence.ferdinandy.com
#230 lore raw

RFC PATCH 2/2 remote set-head: set followRemoteHEAD to "warn" if "always"

In reply to Bence Ferdinandy

When running "remote set-head" manually it is unlikely, that the user would actually like to have "fetch" always update the remote/HEAD. On the contrary, it is more likely, that the user would expect remote/HEAD to stay the way they manually set it, and just forgot about having "followRemoteHEAD" set to "always".

When "followRemoteHEAD" is set to "always" make running "remote set-head" change the config to "warn".

---
 builtin/remote.c  | 12 +++++++++++-
 t/t5505-remote.sh | 11 +++++++++++
 2 files changed, 22 insertions(+), 1 deletion(-)
builtin/remote.c +11 -1
index 4a8b2ef678..9a30c17724 100644
@@ -1433,6 +1433,7 @@ static int set_head(int argc, const char **argv, const char *prefix)
 		b_local_head = STRBUF_INIT;
 	char *head_name = NULL;
 	struct ref_store *refs = get_main_ref_store(the_repository);
+	struct remote *remote;
 
 	struct option options[] = {
 		OPT_BOOL('a', "auto", &opt_a,
@@ -1443,8 +1444,10 @@ static int set_head(int argc, const char **argv, const char *prefix)
 	};
 	argc = parse_options(argc, argv, prefix, options,
 			     builtin_remote_sethead_usage, 0);
-	if (argc)
+	if (argc) {
 		strbuf_addf(&b_head, "refs/remotes/%s/HEAD", argv[0]);
+		remote = remote_get(argv[0]);
+	}
 
 	if (!opt_a && !opt_d && argc == 2) {
 		head_name = xstrdup(argv[1]);
@@ -1483,6 +1486,13 @@ static int set_head(int argc, const char **argv, const char *prefix)
 	}
 	if (opt_a)
 		report_set_head_auto(argv[0], head_name, &b_local_head, was_detached);
+	if (remote->follow_remote_head == FOLLOW_REMOTE_ALWAYS) {
+		struct strbuf config_name = STRBUF_INIT;
+		strbuf_addf(&config_name,
+			"remote.%s.followremotehead", remote->name);
+		git_config_set(config_name.buf, "warn");
+		strbuf_release(&config_name);
+	}
 
 cleanup:
 	free(head_name);
t/t5505-remote.sh +11 -0
index 2600add82a..76ffdb2ec9 100755
@@ -505,6 +505,17 @@ test_expect_success 'set-head --auto has no problem w/multiple HEADs' '
 	)
 '
 
+test_expect_success 'set-head changes followRemoteHEAD always to warn' '
+	(
+		cd test &&
+		git config set remote.origin.followRemoteHEAD "always" &&
+		git remote set-head --auto origin &&
+		git config get remote.origin.followRemoteHEAD >output &&
+		echo "warn" >expect &&
+		test_cmp expect output
+	)
+'
+
 cat >test/expect <<\EOF
 refs/remotes/origin/side2
 EOF
Signature · 1 line
2.47.1.297.g6455f89743
#231 lore raw

Re: [RFC PATCH v1 2/2] remote set-head: set followRemoteHEAD to "warn" if "always"

Quoted 8 lines · Bence Ferdinandy

When running "remote set-head" manually it is unlikely, that the user would actually like to have "fetch" always update the remote/HEAD. On the contrary, it is more likely, that the user would expect remote/HEAD to stay the way they manually set it, and just forgot about having "followRemoteHEAD" set to "always".

When "followRemoteHEAD" is set to "always" make running "remote set-head" change the config to "warn".

If we were to take [1/2] of this series, then another plausible option is to set it to whatever their current value is, but that would require an extra probing of the remote state, so "warn" is probably a reasonable choice here.

Thanks.

#232 lore raw

PATCH v2 1/3 fetch set_head: move warn advice into advise_if_enabled

In reply to Bence Ferdinandy

Advice about what to do when getting a warning is typed out explicitly twice and is printed as regular output. The output is also tested for. Extract the advice message into a single place and use a wrapper function, so if later the advice is made more chatty the signature only needs to be changed in once place. Remove the testing for the advice output in the tests.

---

Notes:
v2: new patch, with the refactor moved out from the following one

 advice.c         |  1 +
 advice.h         |  1 +
 builtin/fetch.c  | 17 +++++++++++++----
 t/t5510-fetch.sh |  2 --
 4 files changed, 15 insertions(+), 6 deletions(-)
advice.c +1 -0
index 6b879d805c..66461fdce9 100644
@@ -53,6 +53,7 @@ static struct {
 	[ADVICE_COMMIT_BEFORE_MERGE]			= { "commitBeforeMerge" },
 	[ADVICE_DETACHED_HEAD]				= { "detachedHead" },
 	[ADVICE_DIVERGING]				= { "diverging" },
+	[ADVICE_FETCH_SET_HEAD_WARN]			= { "fetchRemoteHEADWarn" },
 	[ADVICE_FETCH_SHOW_FORCED_UPDATES]		= { "fetchShowForcedUpdates" },
 	[ADVICE_FORCE_DELETE_BRANCH]			= { "forceDeleteBranch" },
 	[ADVICE_GRAFT_FILE_DEPRECATED]			= { "graftFileDeprecated" },
advice.h +1 -0
index d7466bc0ef..cf2284ec43 100644
@@ -20,6 +20,7 @@ enum advice_type {
 	ADVICE_COMMIT_BEFORE_MERGE,
 	ADVICE_DETACHED_HEAD,
 	ADVICE_DIVERGING,
+	ADVICE_FETCH_SET_HEAD_WARN,
 	ADVICE_FETCH_SHOW_FORCED_UPDATES,
 	ADVICE_FORCE_DELETE_BRANCH,
 	ADVICE_GRAFT_FILE_DEPRECATED,
builtin/fetch.c +13 -4
index 62769d1686..087beb4c92 100644
@@ -1579,6 +1579,17 @@ static const char *strip_refshead(const char *name){
 	return name;
 }
 
+static void set_head_advice_msg(const char *remote, const char *head_name)
+{
+	const char message_advice_set_head[] =
+	N_("Run 'git remote set-head %s %s' to follow the change, or set\n"
+	   "'remote.%s.followRemoteHEAD' configuration option to a different value\n"
+	   "if you do not want to see this message.");
+
+	advise_if_enabled(ADVICE_FETCH_SET_HEAD_WARN, _(message_advice_set_head),
+			remote, head_name, remote);
+}
+
 static void report_set_head(const char *remote, const char *head_name,
 			struct strbuf *buf_prev, int updateres) {
 	struct strbuf buf_prefix = STRBUF_INIT;
@@ -1590,15 +1601,13 @@ static void report_set_head(const char *remote, const char *head_name,
 	if (prev_head && strcmp(prev_head, head_name)) {
 		printf("'HEAD' at '%s' is '%s', but we have '%s' locally.\n",
 			remote, head_name, prev_head);
-		printf("Run 'git remote set-head %s %s' to follow the change.\n",
-			remote, head_name);
+		set_head_advice_msg(remote, head_name);
 	}
 	else if (updateres && buf_prev->len) {
 		printf("'HEAD' at '%s' is '%s', "
 			"but we have a detached HEAD pointing to '%s' locally.\n",
 			remote, head_name, buf_prev->buf);
-		printf("Run 'git remote set-head %s %s' to follow the change.\n",
-			remote, head_name);
+		set_head_advice_msg(remote, head_name);
 	}
 	strbuf_release(&buf_prefix);
 }
t/t5510-fetch.sh +0 -2
index ccb74428bc..5d7ee1b550 100755
@@ -123,7 +123,6 @@ test_expect_success "fetch test followRemoteHEAD warn no change" '
 		git fetch >output &&
 		echo "${SQ}HEAD${SQ} at ${SQ}origin${SQ} is ${SQ}main${SQ}," \
 			"but we have ${SQ}other${SQ} locally." >expect &&
-		echo "Run ${SQ}git remote set-head origin main${SQ} to follow the change." >>expect &&
 		test_cmp expect output &&
 		head=$(git rev-parse refs/remotes/origin/HEAD) &&
 		branch=$(git rev-parse refs/remotes/origin/other) &&
@@ -160,7 +159,6 @@ test_expect_success "fetch test followRemoteHEAD warn detached" '
 		echo "${SQ}HEAD${SQ} at ${SQ}origin${SQ} is ${SQ}main${SQ}," \
 			"but we have a detached HEAD pointing to" \
 			"${SQ}${HEAD}${SQ} locally." >expect &&
-		echo "Run ${SQ}git remote set-head origin main${SQ} to follow the change." >>expect &&
 		test_cmp expect output
 	)
 '
Signature · 1 line
2.47.1.398.g18e7475ebe
#233 lore raw

PATCH v2 2/3 fetch set_head: add warn-if-not-$branch option

Currently if we want to have a remote/HEAD locally that is different from the one on the remote, but we still want to get a warning if remote changes HEAD, our only option is to have an indiscriminate warning with "follow_remote_head" set to "warn". Add a new option "warn-if-not-$branch", where $branch is a branch name we do not wish to get a warning about. If the remote HEAD is $branch do not warn, otherwise, behave as "warn".

E.g. let's assume, that our remote origin has HEAD
set to "master", but locally we have "git remote set-head origin seen". Setting 'remote.origin.followRemoteHEAD = "warn"' will always print a warning, even though the remote has not changed HEAD from "master". Setting 'remote.origin.followRemoteHEAD = "warn-if-not-master" will squelch the warning message, unless the remote changes HEAD from "master". Note, that should the remote change HEAD to "seen" (which we have locally), there will still be no warning.

Improve the advice message in report_set_head to also include silencing the warning message with "warn-if-not-$branch".

---

Notes:
v2: reuse FOLLOW_REMOTE_WARN
add documentation

 Documentation/config/remote.txt |  8 ++++---
 builtin/fetch.c                 | 16 +++++++++-----
 remote.c                        | 13 +++++++++--
 remote.h                        |  1 +
 t/t5510-fetch.sh                | 38 +++++++++++++++++++++++++++++++++
 5 files changed, 66 insertions(+), 10 deletions(-)
Documentation/config/remote.txt +5 -3
index 024f92befc..4118c219c1 100644
@@ -106,10 +106,12 @@ remote.<name>.followRemoteHEAD::
 	How linkgit:git-fetch[1] should handle updates to `remotes/<name>/HEAD`.
 	The default value is "create", which will create `remotes/<name>/HEAD`
 	if it exists on the remote, but not locally, but will not touch an
-	already existing local reference.  Setting to "warn" will print
+	already existing local reference. Setting to "warn" will print
 	a message if the remote has a different value, than the local one and
-	in case there is no local reference, it behaves like "create". Setting
-	to "always" will silently update it to the value on the remote.
+	in case there is no local reference, it behaves like "create".
+	A variant on "warn" is "warn-if-not-$branch", which behaves like
+	"warn", but if `HEAD` on the remote is `$branch` it will be silent.
+	Setting to "always" will silently update it to the value on the remote.
 	Finally, setting it to "never" will never change or create the local
 	reference.
 +
builtin/fetch.c +11 -5
index 087beb4c92..b3f6793026 100644
@@ -1584,10 +1584,12 @@ static void set_head_advice_msg(const char *remote, const char *head_name)
 	const char message_advice_set_head[] =
 	N_("Run 'git remote set-head %s %s' to follow the change, or set\n"
 	   "'remote.%s.followRemoteHEAD' configuration option to a different value\n"
-	   "if you do not want to see this message.");
+	   "if you do not want to see this message. Specifically running\n"
+	   "'git config set remote.%s.followRemoteHEAD %s' will disable the warning\n"
+	   "until the remote changes HEAD to something else.");
 
 	advise_if_enabled(ADVICE_FETCH_SET_HEAD_WARN, _(message_advice_set_head),
-			remote, head_name, remote);
+			remote, head_name, remote, remote, head_name);
 }
 
 static void report_set_head(const char *remote, const char *head_name,
@@ -1612,7 +1614,8 @@ static void report_set_head(const char *remote, const char *head_name,
 	strbuf_release(&buf_prefix);
 }
 
-static int set_head(const struct ref *remote_refs, int follow_remote_head)
+static int set_head(const struct ref *remote_refs, int follow_remote_head,
+		const char *no_warn_branch)
 {
 	int result = 0, create_only, is_bare, was_detached;
 	struct strbuf b_head = STRBUF_INIT, b_remote_head = STRBUF_INIT,
@@ -1669,7 +1672,9 @@ static int set_head(const struct ref *remote_refs, int follow_remote_head)
 		result = 1;
 		goto cleanup;
 	}
-	if (follow_remote_head == FOLLOW_REMOTE_WARN && verbosity >= 0)
+	if (verbosity >= 0 &&
+		follow_remote_head == FOLLOW_REMOTE_WARN &&
+		(!no_warn_branch || strcmp(no_warn_branch, head_name)))
 		report_set_head(remote, head_name, &b_local_head, was_detached);
 
 cleanup:
@@ -1898,7 +1903,8 @@ static int do_fetch(struct transport *transport,
 				  "you need to specify exactly one branch with the --set-upstream option"));
 		}
 	}
-	if (set_head(remote_refs, transport->remote->follow_remote_head))
+	if (set_head(remote_refs, transport->remote->follow_remote_head,
+		transport->remote->no_warn_branch))
 		;
 		/*
 		 * Way too many cases where this can go wrong
remote.c +11 -2
index 0b18840d43..4ec5d3f47b 100644
@@ -515,14 +515,23 @@ static int handle_config(const char *key, const char *value,
 		return parse_transport_option(key, value,
 					      &remote->server_options);
 	} else if (!strcmp(subkey, "followremotehead")) {
+		const char *no_warn_branch;
 		if (!strcmp(value, "never"))
 			remote->follow_remote_head = FOLLOW_REMOTE_NEVER;
 		else if (!strcmp(value, "create"))
 			remote->follow_remote_head = FOLLOW_REMOTE_CREATE;
-		else if (!strcmp(value, "warn"))
+		else if (!strcmp(value, "warn")) {
 			remote->follow_remote_head = FOLLOW_REMOTE_WARN;
-		else if (!strcmp(value, "always"))
+			remote->no_warn_branch = NULL;
+		} else if (skip_prefix(value, "warn-if-not-", &no_warn_branch)) {
+			remote->follow_remote_head = FOLLOW_REMOTE_WARN;
+			remote->no_warn_branch = no_warn_branch;
+		} else if (!strcmp(value, "always")) {
 			remote->follow_remote_head = FOLLOW_REMOTE_ALWAYS;
+		} else {
+			warning(_("unrecognized followRemoteHEAD value '%s' ignored"),
+				value);
+		}
 	}
 	return 0;
 }
remote.h +1 -0
index 184b35653d..bda10dd5c8 100644
@@ -116,6 +116,7 @@ struct remote {
 	struct string_list server_options;
 
 	enum follow_remote_head_settings follow_remote_head;
+	const char *no_warn_branch;
 };
 
 /**
t/t5510-fetch.sh +38 -0
index 5d7ee1b550..5c9915b836 100755
@@ -181,6 +181,44 @@ test_expect_success "fetch test followRemoteHEAD warn quiet" '
 	)
 '
 
+test_expect_success "fetch test followRemoteHEAD warn-if-not-branch branch is same" '
+	test_when_finished "git config unset remote.origin.followRemoteHEAD" &&
+	(
+		cd "$D" &&
+		cd two &&
+		git rev-parse --verify refs/remotes/origin/other &&
+		git remote set-head origin other &&
+		git rev-parse --verify refs/remotes/origin/HEAD &&
+		git rev-parse --verify refs/remotes/origin/main &&
+		git config set remote.origin.followRemoteHEAD "warn-if-not-main" &&
+		output=$(git fetch) &&
+		test "z" = "z$output" &&
+		head=$(git rev-parse refs/remotes/origin/HEAD) &&
+		branch=$(git rev-parse refs/remotes/origin/other) &&
+		test "z$head" = "z$branch"
+	)
+'
+
+test_expect_success "fetch test followRemoteHEAD warn-if-not-branch branch is different" '
+	test_when_finished "git config unset remote.origin.followRemoteHEAD" &&
+	(
+		cd "$D" &&
+		cd two &&
+		git rev-parse --verify refs/remotes/origin/other &&
+		git remote set-head origin other &&
+		git rev-parse --verify refs/remotes/origin/HEAD &&
+		git rev-parse --verify refs/remotes/origin/main &&
+		git config set remote.origin.followRemoteHEAD "warn-if-not-some/different-branch" &&
+		git fetch >output &&
+		echo "${SQ}HEAD${SQ} at ${SQ}origin${SQ} is ${SQ}main${SQ}," \
+			"but we have ${SQ}other${SQ} locally." >expect &&
+		test_cmp expect output &&
+		head=$(git rev-parse refs/remotes/origin/HEAD) &&
+		branch=$(git rev-parse refs/remotes/origin/other) &&
+		test "z$head" = "z$branch"
+	)
+'
+
 test_expect_success "fetch test followRemoteHEAD always" '
 	test_when_finished "git config unset remote.origin.followRemoteHEAD" &&
 	(
Signature · 1 line
2.47.1.398.g18e7475ebe
#234 lore raw

PATCH v2 3/3 remote set-head: set followRemoteHEAD to "warn" if "always"

In reply to Bence Ferdinandy

When running "remote set-head" manually it is unlikely, that the user would actually like to have "fetch" always update the remote/HEAD. On the contrary, it is more likely, that the user would expect remote/HEAD to stay the way they manually set it, and just forgot about having "followRemoteHEAD" set to "always".

When "followRemoteHEAD" is set to "always" make running "remote set-head" change the config to "warn".

---
 builtin/remote.c  | 12 +++++++++++-
 t/t5505-remote.sh | 11 +++++++++++
 2 files changed, 22 insertions(+), 1 deletion(-)
builtin/remote.c +11 -1
index d5b81445f2..32d02ca4a0 100644
@@ -1438,6 +1438,7 @@ static int set_head(int argc, const char **argv, const char *prefix,
 		b_local_head = STRBUF_INIT;
 	char *head_name = NULL;
 	struct ref_store *refs = get_main_ref_store(the_repository);
+	struct remote *remote;
 
 	struct option options[] = {
 		OPT_BOOL('a', "auto", &opt_a,
@@ -1448,8 +1449,10 @@ static int set_head(int argc, const char **argv, const char *prefix,
 	};
 	argc = parse_options(argc, argv, prefix, options,
 			     builtin_remote_sethead_usage, 0);
-	if (argc)
+	if (argc) {
 		strbuf_addf(&b_head, "refs/remotes/%s/HEAD", argv[0]);
+		remote = remote_get(argv[0]);
+	}
 
 	if (!opt_a && !opt_d && argc == 2) {
 		head_name = xstrdup(argv[1]);
@@ -1488,6 +1491,13 @@ static int set_head(int argc, const char **argv, const char *prefix,
 	}
 	if (opt_a)
 		report_set_head_auto(argv[0], head_name, &b_local_head, was_detached);
+	if (remote->follow_remote_head == FOLLOW_REMOTE_ALWAYS) {
+		struct strbuf config_name = STRBUF_INIT;
+		strbuf_addf(&config_name,
+			"remote.%s.followremotehead", remote->name);
+		git_config_set(config_name.buf, "warn");
+		strbuf_release(&config_name);
+	}
 
 cleanup:
 	free(head_name);
t/t5505-remote.sh +11 -0
index 7411aa770d..daf70406be 100755
@@ -504,6 +504,17 @@ test_expect_success 'set-head --auto has no problem w/multiple HEADs' '
 	)
 '
 
+test_expect_success 'set-head changes followRemoteHEAD always to warn' '
+	(
+		cd test &&
+		git config set remote.origin.followRemoteHEAD "always" &&
+		git remote set-head --auto origin &&
+		git config get remote.origin.followRemoteHEAD >output &&
+		echo "warn" >expect &&
+		test_cmp expect output
+	)
+'
+
 cat >test/expect <<\EOF
 refs/remotes/origin/side2
 EOF
Signature · 1 line
2.47.1.398.g18e7475ebe
#235 lore raw

Re: [PATCH v2 3/3] remote set-head: set followRemoteHEAD to "warn" if "always"

Hi

t/t5505-remote.sh test_expect_success 'set-head --auto has no problem w/multiple HEADs' ' · line 509

Quoted 12 lines · Bence Ferdinandy
t/t5505-remote.sh +0 -0
index 7411aa770d..daf70406be 100755
@@ -504,6 +504,17 @@ test_expect_success 'set-head --auto has no

problem w/multiple HEADs' '

t/t5505-remote.sh +3 -0
 	)
 '
 
+test_expect_success 'set-head changes followRemoteHEAD always to warn' '
+	(
+		cd test &&

I think you need to cd in a subshell here. See t/README, “Don't chdir around in tests.”.

t/t5505-remote.sh test_expect_success 'set-head --auto has no problem w/multiple HEADs' ' · line 512

Quoted 3 lines
t/t5505-remote.sh +3 -0
+		git config set remote.origin.followRemoteHEAD "always" &&
+		git remote set-head --auto origin &&
+		git config get remote.origin.followRemoteHEAD >output &&

Nit: maybe actual instead of output? Just for uniformity.

#236 lore raw

Re: [PATCH v2 3/3] remote set-head: set followRemoteHEAD to "warn" if "always"

Quoted 10 lines · Kristoffer Haugsbakk
Quoted 9 lines · Bence Ferdinandy
t/t5505-remote.sh +0 -0
index 7411aa770d..daf70406be 100755
@@ -504,6 +504,17 @@ test_expect_success 'set-head --auto has no

problem w/multiple HEADs' '
)
'

Quoted 6 lines

t/t5505-remote.sh test_expect_success 'set-head --auto has no problem w/multiple HEADs' ' · line 509

Quoted 3 lines
t/t5505-remote.sh +3 -0
+test_expect_success 'set-head changes followRemoteHEAD always to warn' '
+	(
+		cd test &&

I think you need to cd in a subshell here. See t/README, “Don't chdir around in tests.”.

Puzzled. Isn't this inside a (subshell) already?

Quoted 5 lines

t/t5505-remote.sh test_expect_success 'set-head --auto has no problem w/multiple HEADs' ' · line 512

Quoted 3 lines
t/t5505-remote.sh +3 -0
+		git config set remote.origin.followRemoteHEAD "always" &&
+		git remote set-head --auto origin &&
+		git config get remote.origin.followRemoteHEAD >output &&

Nit: maybe actual instead of output? Just for uniformity.

t5505 is already heavily contaminated with the pattern to compare "expect" with "output", not with "actual", but that does not make it a good idea to make it even worse by adding more instances ;-)

#237 lore raw

Re: [PATCH v2 3/3] remote set-head: set followRemoteHEAD to "warn" if "always"

Quoted 12 lines · Junio C Hamano
Quoted 10 lines · Kristoffer Haugsbakk
Quoted 9 lines · Bence Ferdinandy
t/t5505-remote.sh +0 -0
index 7411aa770d..daf70406be 100755
@@ -504,6 +504,17 @@ test_expect_success 'set-head --auto has no

problem w/multiple HEADs' '
)
'

Quoted 8 lines
Quoted 6 lines

t/t5505-remote.sh test_expect_success 'set-head --auto has no problem w/multiple HEADs' ' · line 509

Quoted 3 lines
t/t5505-remote.sh +3 -0
+test_expect_success 'set-head changes followRemoteHEAD always to warn' '
+	(
+		cd test &&

I think you need to cd in a subshell here. See t/README, “Don't chdir around in tests.”.

Puzzled. Isn't this inside a (subshell) already?

Aha, then I didn’t read the context properly.

#238 lore raw

Re: [PATCH v2 3/3] remote set-head: set followRemoteHEAD to "warn" if "always"

Quoted 20 lines · Kristoffer Haugsbakk
Quoted 19 lines · Junio C Hamano
Quoted 16 lines · Kristoffer Haugsbakk

t/t5505-remote.sh test_expect_success 'set-head --auto has no problem w/multiple HEADs' ' · line 509

Quoted 12 lines · Bence Ferdinandy
t/t5505-remote.sh +0 -0
index 7411aa770d..daf70406be 100755
@@ -504,6 +504,17 @@ test_expect_success 'set-head --auto has no

problem w/multiple HEADs' '

t/t5505-remote.sh +3 -0
 	)
 '
 
+test_expect_success 'set-head changes followRemoteHEAD always to warn' '
+	(
+		cd test &&

I think you need to cd in a subshell here. See t/README, “Don't chdir around in tests.”.

Quoted 3 lines
Quoted 1 line

Puzzled. Isn't this inside a (subshell) already?

Aha, then I didn’t read the context properly.

Thanks for the review! What I gathered is that v3 needs an s/output/actual and we're good. I'll send that soonish.

Thanks,
Bence

#239 lore raw

PATCH v3 1/3 fetch set_head: move warn advice into advise_if_enabled

In reply to Kristoffer Haugsbakk

Advice about what to do when getting a warning is typed out explicitly twice and is printed as regular output. The output is also tested for. Extract the advice message into a single place and use a wrapper function, so if later the advice is made more chatty the signature only needs to be changed in once place. Remove the testing for the advice output in the tests.

---

Notes:
v2: new patch, with the refactor moved out from the following one

 advice.c         |  1 +
 advice.h         |  1 +
 builtin/fetch.c  | 17 +++++++++++++----
 t/t5510-fetch.sh |  2 --
 4 files changed, 15 insertions(+), 6 deletions(-)
advice.c +1 -0
index 6b879d805c..66461fdce9 100644
@@ -53,6 +53,7 @@ static struct {
 	[ADVICE_COMMIT_BEFORE_MERGE]			= { "commitBeforeMerge" },
 	[ADVICE_DETACHED_HEAD]				= { "detachedHead" },
 	[ADVICE_DIVERGING]				= { "diverging" },
+	[ADVICE_FETCH_SET_HEAD_WARN]			= { "fetchRemoteHEADWarn" },
 	[ADVICE_FETCH_SHOW_FORCED_UPDATES]		= { "fetchShowForcedUpdates" },
 	[ADVICE_FORCE_DELETE_BRANCH]			= { "forceDeleteBranch" },
 	[ADVICE_GRAFT_FILE_DEPRECATED]			= { "graftFileDeprecated" },
advice.h +1 -0
index d7466bc0ef..cf2284ec43 100644
@@ -20,6 +20,7 @@ enum advice_type {
 	ADVICE_COMMIT_BEFORE_MERGE,
 	ADVICE_DETACHED_HEAD,
 	ADVICE_DIVERGING,
+	ADVICE_FETCH_SET_HEAD_WARN,
 	ADVICE_FETCH_SHOW_FORCED_UPDATES,
 	ADVICE_FORCE_DELETE_BRANCH,
 	ADVICE_GRAFT_FILE_DEPRECATED,
builtin/fetch.c +13 -4
index 62769d1686..087beb4c92 100644
@@ -1579,6 +1579,17 @@ static const char *strip_refshead(const char *name){
 	return name;
 }
 
+static void set_head_advice_msg(const char *remote, const char *head_name)
+{
+	const char message_advice_set_head[] =
+	N_("Run 'git remote set-head %s %s' to follow the change, or set\n"
+	   "'remote.%s.followRemoteHEAD' configuration option to a different value\n"
+	   "if you do not want to see this message.");
+
+	advise_if_enabled(ADVICE_FETCH_SET_HEAD_WARN, _(message_advice_set_head),
+			remote, head_name, remote);
+}
+
 static void report_set_head(const char *remote, const char *head_name,
 			struct strbuf *buf_prev, int updateres) {
 	struct strbuf buf_prefix = STRBUF_INIT;
@@ -1590,15 +1601,13 @@ static void report_set_head(const char *remote, const char *head_name,
 	if (prev_head && strcmp(prev_head, head_name)) {
 		printf("'HEAD' at '%s' is '%s', but we have '%s' locally.\n",
 			remote, head_name, prev_head);
-		printf("Run 'git remote set-head %s %s' to follow the change.\n",
-			remote, head_name);
+		set_head_advice_msg(remote, head_name);
 	}
 	else if (updateres && buf_prev->len) {
 		printf("'HEAD' at '%s' is '%s', "
 			"but we have a detached HEAD pointing to '%s' locally.\n",
 			remote, head_name, buf_prev->buf);
-		printf("Run 'git remote set-head %s %s' to follow the change.\n",
-			remote, head_name);
+		set_head_advice_msg(remote, head_name);
 	}
 	strbuf_release(&buf_prefix);
 }
t/t5510-fetch.sh +0 -2
index ccb74428bc..5d7ee1b550 100755
@@ -123,7 +123,6 @@ test_expect_success "fetch test followRemoteHEAD warn no change" '
 		git fetch >output &&
 		echo "${SQ}HEAD${SQ} at ${SQ}origin${SQ} is ${SQ}main${SQ}," \
 			"but we have ${SQ}other${SQ} locally." >expect &&
-		echo "Run ${SQ}git remote set-head origin main${SQ} to follow the change." >>expect &&
 		test_cmp expect output &&
 		head=$(git rev-parse refs/remotes/origin/HEAD) &&
 		branch=$(git rev-parse refs/remotes/origin/other) &&
@@ -160,7 +159,6 @@ test_expect_success "fetch test followRemoteHEAD warn detached" '
 		echo "${SQ}HEAD${SQ} at ${SQ}origin${SQ} is ${SQ}main${SQ}," \
 			"but we have a detached HEAD pointing to" \
 			"${SQ}${HEAD}${SQ} locally." >expect &&
-		echo "Run ${SQ}git remote set-head origin main${SQ} to follow the change." >>expect &&
 		test_cmp expect output
 	)
 '
Signature · 1 line
2.47.1.398.g18e7475ebe
#240 lore raw

PATCH v3 2/3 fetch set_head: add warn-if-not-$branch option

Currently if we want to have a remote/HEAD locally that is different from the one on the remote, but we still want to get a warning if remote changes HEAD, our only option is to have an indiscriminate warning with "follow_remote_head" set to "warn". Add a new option "warn-if-not-$branch", where $branch is a branch name we do not wish to get a warning about. If the remote HEAD is $branch do not warn, otherwise, behave as "warn".

E.g. let's assume, that our remote origin has HEAD
set to "master", but locally we have "git remote set-head origin seen". Setting 'remote.origin.followRemoteHEAD = "warn"' will always print a warning, even though the remote has not changed HEAD from "master". Setting 'remote.origin.followRemoteHEAD = "warn-if-not-master" will squelch the warning message, unless the remote changes HEAD from "master". Note, that should the remote change HEAD to "seen" (which we have locally), there will still be no warning.

Improve the advice message in report_set_head to also include silencing the warning message with "warn-if-not-$branch".

---

Notes:
v2: reuse FOLLOW_REMOTE_WARN
add documentation

v3: s/output/actual in tests

 Documentation/config/remote.txt |  8 ++++---
 builtin/fetch.c                 | 16 +++++++++-----
 remote.c                        | 13 +++++++++--
 remote.h                        |  1 +
 t/t5510-fetch.sh                | 38 +++++++++++++++++++++++++++++++++
 5 files changed, 66 insertions(+), 10 deletions(-)
Documentation/config/remote.txt +5 -3
index 024f92befc..4118c219c1 100644
@@ -106,10 +106,12 @@ remote.<name>.followRemoteHEAD::
 	How linkgit:git-fetch[1] should handle updates to `remotes/<name>/HEAD`.
 	The default value is "create", which will create `remotes/<name>/HEAD`
 	if it exists on the remote, but not locally, but will not touch an
-	already existing local reference.  Setting to "warn" will print
+	already existing local reference. Setting to "warn" will print
 	a message if the remote has a different value, than the local one and
-	in case there is no local reference, it behaves like "create". Setting
-	to "always" will silently update it to the value on the remote.
+	in case there is no local reference, it behaves like "create".
+	A variant on "warn" is "warn-if-not-$branch", which behaves like
+	"warn", but if `HEAD` on the remote is `$branch` it will be silent.
+	Setting to "always" will silently update it to the value on the remote.
 	Finally, setting it to "never" will never change or create the local
 	reference.
 +
builtin/fetch.c +11 -5
index 087beb4c92..b3f6793026 100644
@@ -1584,10 +1584,12 @@ static void set_head_advice_msg(const char *remote, const char *head_name)
 	const char message_advice_set_head[] =
 	N_("Run 'git remote set-head %s %s' to follow the change, or set\n"
 	   "'remote.%s.followRemoteHEAD' configuration option to a different value\n"
-	   "if you do not want to see this message.");
+	   "if you do not want to see this message. Specifically running\n"
+	   "'git config set remote.%s.followRemoteHEAD %s' will disable the warning\n"
+	   "until the remote changes HEAD to something else.");
 
 	advise_if_enabled(ADVICE_FETCH_SET_HEAD_WARN, _(message_advice_set_head),
-			remote, head_name, remote);
+			remote, head_name, remote, remote, head_name);
 }
 
 static void report_set_head(const char *remote, const char *head_name,
@@ -1612,7 +1614,8 @@ static void report_set_head(const char *remote, const char *head_name,
 	strbuf_release(&buf_prefix);
 }
 
-static int set_head(const struct ref *remote_refs, int follow_remote_head)
+static int set_head(const struct ref *remote_refs, int follow_remote_head,
+		const char *no_warn_branch)
 {
 	int result = 0, create_only, is_bare, was_detached;
 	struct strbuf b_head = STRBUF_INIT, b_remote_head = STRBUF_INIT,
@@ -1669,7 +1672,9 @@ static int set_head(const struct ref *remote_refs, int follow_remote_head)
 		result = 1;
 		goto cleanup;
 	}
-	if (follow_remote_head == FOLLOW_REMOTE_WARN && verbosity >= 0)
+	if (verbosity >= 0 &&
+		follow_remote_head == FOLLOW_REMOTE_WARN &&
+		(!no_warn_branch || strcmp(no_warn_branch, head_name)))
 		report_set_head(remote, head_name, &b_local_head, was_detached);
 
 cleanup:
@@ -1898,7 +1903,8 @@ static int do_fetch(struct transport *transport,
 				  "you need to specify exactly one branch with the --set-upstream option"));
 		}
 	}
-	if (set_head(remote_refs, transport->remote->follow_remote_head))
+	if (set_head(remote_refs, transport->remote->follow_remote_head,
+		transport->remote->no_warn_branch))
 		;
 		/*
 		 * Way too many cases where this can go wrong
remote.c +11 -2
index 0b18840d43..4ec5d3f47b 100644
@@ -515,14 +515,23 @@ static int handle_config(const char *key, const char *value,
 		return parse_transport_option(key, value,
 					      &remote->server_options);
 	} else if (!strcmp(subkey, "followremotehead")) {
+		const char *no_warn_branch;
 		if (!strcmp(value, "never"))
 			remote->follow_remote_head = FOLLOW_REMOTE_NEVER;
 		else if (!strcmp(value, "create"))
 			remote->follow_remote_head = FOLLOW_REMOTE_CREATE;
-		else if (!strcmp(value, "warn"))
+		else if (!strcmp(value, "warn")) {
 			remote->follow_remote_head = FOLLOW_REMOTE_WARN;
-		else if (!strcmp(value, "always"))
+			remote->no_warn_branch = NULL;
+		} else if (skip_prefix(value, "warn-if-not-", &no_warn_branch)) {
+			remote->follow_remote_head = FOLLOW_REMOTE_WARN;
+			remote->no_warn_branch = no_warn_branch;
+		} else if (!strcmp(value, "always")) {
 			remote->follow_remote_head = FOLLOW_REMOTE_ALWAYS;
+		} else {
+			warning(_("unrecognized followRemoteHEAD value '%s' ignored"),
+				value);
+		}
 	}
 	return 0;
 }
remote.h +1 -0
index 184b35653d..bda10dd5c8 100644
@@ -116,6 +116,7 @@ struct remote {
 	struct string_list server_options;
 
 	enum follow_remote_head_settings follow_remote_head;
+	const char *no_warn_branch;
 };
 
 /**
t/t5510-fetch.sh +38 -0
index 5d7ee1b550..3885461ac1 100755
@@ -181,6 +181,44 @@ test_expect_success "fetch test followRemoteHEAD warn quiet" '
 	)
 '
 
+test_expect_success "fetch test followRemoteHEAD warn-if-not-branch branch is same" '
+	test_when_finished "git config unset remote.origin.followRemoteHEAD" &&
+	(
+		cd "$D" &&
+		cd two &&
+		git rev-parse --verify refs/remotes/origin/other &&
+		git remote set-head origin other &&
+		git rev-parse --verify refs/remotes/origin/HEAD &&
+		git rev-parse --verify refs/remotes/origin/main &&
+		git config set remote.origin.followRemoteHEAD "warn-if-not-main" &&
+		actual=$(git fetch) &&
+		test "z" = "z$actual" &&
+		head=$(git rev-parse refs/remotes/origin/HEAD) &&
+		branch=$(git rev-parse refs/remotes/origin/other) &&
+		test "z$head" = "z$branch"
+	)
+'
+
+test_expect_success "fetch test followRemoteHEAD warn-if-not-branch branch is different" '
+	test_when_finished "git config unset remote.origin.followRemoteHEAD" &&
+	(
+		cd "$D" &&
+		cd two &&
+		git rev-parse --verify refs/remotes/origin/other &&
+		git remote set-head origin other &&
+		git rev-parse --verify refs/remotes/origin/HEAD &&
+		git rev-parse --verify refs/remotes/origin/main &&
+		git config set remote.origin.followRemoteHEAD "warn-if-not-some/different-branch" &&
+		git fetch >actual &&
+		echo "${SQ}HEAD${SQ} at ${SQ}origin${SQ} is ${SQ}main${SQ}," \
+			"but we have ${SQ}other${SQ} locally." >expect &&
+		test_cmp expect actual &&
+		head=$(git rev-parse refs/remotes/origin/HEAD) &&
+		branch=$(git rev-parse refs/remotes/origin/other) &&
+		test "z$head" = "z$branch"
+	)
+'
+
 test_expect_success "fetch test followRemoteHEAD always" '
 	test_when_finished "git config unset remote.origin.followRemoteHEAD" &&
 	(
Signature · 1 line
2.47.1.398.g18e7475ebe
#241 lore raw

PATCH v3 2/3 fetch set_head: add warn-if-not-$branch option

builtin/fetch.c static void set_head_advice_msg(const char *remote, const char *head_name) · line 1593

Quoted 13 lines · Bence Ferdinandy
builtin/fetch.c +4 -2
@@ -1584,10 +1584,12 @@ static void set_head_advice_msg(const char *remote, const char *head_name)
 	const char message_advice_set_head[] =
 	N_("Run 'git remote set-head %s %s' to follow the change, or set\n"
 	   "'remote.%s.followRemoteHEAD' configuration option to a different value\n"
-	   "if you do not want to see this message.");
+	   "if you do not want to see this message. Specifically running\n"
+	   "'git config set remote.%s.followRemoteHEAD %s' will disable the warning\n"
+	   "until the remote changes HEAD to something else.");
 
 	advise_if_enabled(ADVICE_FETCH_SET_HEAD_WARN, _(message_advice_set_head),
-			remote, head_name, remote);
+			remote, head_name, remote, remote, head_name);
 

Seems like the config is unsupported to be feed a variable named "head_name"?

Thanks.

#242 lore raw

Re: [PATCH v3 2/3] fetch set_head: add warn-if-not-$branch option

Quoted 12 lines · Teng Long
Quoted 9 lines · Bence Ferdinandy
builtin/fetch.c +3 -1
@@ -1584,10 +1584,12 @@ static void set_head_advice_msg(const char *remote, const char *head_name)
 	const char message_advice_set_head[] =
 	N_("Run 'git remote set-head %s %s' to follow the change, or set\n"
 	   "'remote.%s.followRemoteHEAD' configuration option to a different value\n"
-	   "if you do not want to see this message.");
+	   "if you do not want to see this message. Specifically running\n"
+	   "'git config set remote.%s.followRemoteHEAD %s' will disable the warning\n"
+	   "until the remote changes HEAD to something else.");
 
Quoted 8 lines

builtin/fetch.c static void set_head_advice_msg(const char *remote, const char *head_name) · line 1594

Quoted 4 lines
builtin/fetch.c +1 -1
 	advise_if_enabled(ADVICE_FETCH_SET_HEAD_WARN, _(message_advice_set_head),
-			remote, head_name, remote);
+			remote, head_name, remote, remote, head_name);
 

Seems like the config is unsupported to be feed a variable named "head_name"?

Thanks.

Ah, yes the warning seems to be off, it should read

git config set remote.%s.followRemoteHEAD warn-if-not-%s

thanks for catching this mistake!

Best,
Bence