Thread (4 messages) 4 messages, 2 authors, 2022-06-10

Re: Best way to update `HEAD` in mirrored repos

From: Johannes Schindelin <hidden>
Date: 2022-06-10 21:46:59
Subsystem: the rest · Maintainer: Linus Torvalds

Hi Rodrigo,

On Thu, 9 Jun 2022, Rodrigo Silva Mendoza wrote:
On Thu, Jun 9, 2022 at 1:02 AM Johannes Schindelin
[off-list ref] wrote:
quoted
The reason is that `set-head` expects options to come before arguments,
like so:

        git remote set-head -a origin
Hmm, that doesn't seem to work for me either - I get the same type of
error as before.
Here's a minimal repro:
    $ git clone git@github.com:git/gitscm-old.git --mirror
Here, you set this up as a fetch mirror. That is something I've missed
before. It is also important because it means that there is no
`refs/remotes/origin/HEAD`.
    $ cd gitscm-old.git
    $ git remote set-head -a origin
      error: Not a valid ref: refs/remotes/origin/master
... yet that's exactly what `set-head` assumes. The following patch fixes
it for me:

-- snip --
diff --git a/builtin/remote.c b/builtin/remote.c
index eddd40c8f87..fead15adb97 100644
--- a/builtin/remote.c
+++ b/builtin/remote.c
@@ -1344,7 +1344,7 @@ static int show(int argc, const char **argv)

 static int set_head(int argc, const char **argv)
 {
-	int i, opt_a = 0, opt_d = 0, result = 0;
+	int i, opt_a = 0, opt_d = 0, result = 0, is_mirror = 0;
 	struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT;
 	char *head_name = NULL;
@@ -1357,8 +1357,16 @@ static int set_head(int argc, const char **argv)
 	};
 	argc = parse_options(argc, argv, NULL, options, builtin_remote_sethead_usage,
 			     0);
-	if (argc)
-		strbuf_addf(&buf, "refs/remotes/%s/HEAD", argv[0]);
+	if (argc) {
+		struct remote *remote = remote_get(argv[0]);
+
+		if (!remote || !remote->mirror)
+			strbuf_addf(&buf, "refs/remotes/%s/HEAD", argv[0]);
+		else {
+			is_mirror = 1;
+			strbuf_addstr(&buf, "HEAD");
+		}
+	}

 	if (!opt_a && !opt_d && argc == 2) {
 		head_name = xstrdup(argv[1]);
@@ -1383,7 +1391,10 @@ static int set_head(int argc, const char **argv)
 		usage_with_options(builtin_remote_sethead_usage, options);

 	if (head_name) {
-		strbuf_addf(&buf2, "refs/remotes/%s/%s", argv[0], head_name);
+		if (is_mirror)
+			strbuf_addf(&buf2, "refs/heads/%s", head_name);
+		else
+			strbuf_addf(&buf2, "refs/remotes/%s/%s", argv[0], head_name);
 		/* make sure it's valid */
 		if (!ref_exists(buf2.buf))
 			result |= error(_("Not a valid ref: %s"), buf2.buf);

-- snap --
Would you have a chance to build Git with this patch and verify that it
works for you, too?

Ciao,
Johannes
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help