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

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

From
Johannes Schindelin <johannes.schindelin@gmx.de>
Date
Jun 10, 2022, 21:46 UTC
Message-ID
<nycvar.QRO.7.76.6.2206102327540.353@tvgsbejvaqbjf.bet>
In-Reply-To
<CANWRddNa1nB9shoppfXuA2yqmd2353HVBopoufkh0SevnVrngA@mail.gmail.com>
Hi Rodrigo,
On Thu, 9 Jun 2022, Rodrigo Silva Mendoza wrote:
Show 12 quoted lines
> On Thu, Jun 9, 2022 at 1:02 AM Johannes Schindelin
> <Johannes.Schindelin@gmx.de> wrote:
> >
> > 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
Previous: Rodrigo Silva MendozaNext: Junio C Hamano
Message 6 of 9 in “Best way to update `HEAD` in mirrored repos”
  1. Rodrigo Silva MendozaJun 8, 2022
  2. Junio C HamanoJun 8, 2022
  3. Rodrigo Silva MendozaJun 9, 2022
  4. Johannes SchindelinJun 9, 2022
  5. Rodrigo Silva MendozaJun 9, 2022
  6. Johannes SchindelinJun 10, 2022
  7. Junio C HamanoJun 10, 2022
  8. Rodrigo Silva MendozaJun 15, 2022
  9. Rodrigo Silva MendozaJun 15, 2022

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

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