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

Re: Syncing HEAD

From
Jeff King <peff@peff.net>
Date
Aug 14, 2018, 21:47 UTC
Message-ID
<20180814214723.GA667@sigill.intra.peff.net>
In-Reply-To
<20180814210616.GA32367@sigill.intra.peff.net>
On Tue, Aug 14, 2018 at 05:06:16PM -0400, Jeff King wrote:
Show 18 quoted lines
> On Tue, Aug 14, 2018 at 10:09:37PM +0200, Christian Couder wrote:
> 
> > When cloning with --mirror, the clone gets its HEAD initialized with
> > the value HEAD has in its origin remote. After that if HEAD changes in
> > origin there is no simple way to sync HEAD at the same time as the
> > refs are synced.
> > 
> > It looks like the simplest way to sync HEAD is:
> > 
> > 1) git remote show origin
> > 2) parse "HEAD branch: XXX" from the output of the above command
> > 3) git symbolic-ref HEAD refs/heads/XXX
> 
> How about:
> 
>   git remote set-head origin -a
> 
> ?

Reading your message again, I see you actually care less about the refs/remote placeholder and more about the actual HEAD in a bare repo.

In which case "git remote" isn't going to help, though its underlying code has the algorithm you would want.

> One tricky thing is that the name "refs/remotes/<remote>/HEAD" is only
> special by convention, and that convention is known on the writing side
> only by git-clone and git-remote. So obviously:

And so here the convention is simpler, because we're talking about the main HEAD. But we still have know if you want to do that, and not update some refs/remotes/ symref in a bare repo.

So all of this really implies to me that you want to be able to say "take this symref on the other side and update this one on the local side". I.e., some way to tell a refspec "don't update the value, update the symref destination". So imagine we made "~" the magic character for "just the symrefs" (I picked that because it's not allowed in a refname).

Then you could do what you want with:
  git config --add remote.origin.fetch ~HEAD:HEAD
and these two would be the same:
  git remote set-head origin -a
  git fetch origin ~HEAD:refs/remotes/origin/HEAD
And it would allow more exotic things, too, like:
  # always update the remote notion of HEAD on every fetch
  git config --add remote.origin.fetch ~HEAD:refs/remotes/origin/HEAD
  # update a non-HEAD symref we track for our own purposes
  git fetch origin ~refs/tags/LATEST:refs/tags/LATEST
  # or the same thing but using the usual refspec "dst defaults to src"
  # rule and dwim lookup magic
  git fetch origin ~LATEST

In protocol v0 we don't get symref reports from the other side over the git protocol (except for HEAD), but we could use the same logic we use for determining HEAD for older versions of Git: find a ref that points to the same tip. Though I would say that unlike the existing code in guess_remote_head(), we'd probably want to treat an ambiguity as an error, and not just default to refs/heads/master.

-Peff
Previous: Jeff KingNext: Christian Couder
Message 5 of 10 in “Syncing HEAD”
  1. Christian CouderAug 14, 2018
  2. Stefan BellerAug 14, 2018
  3. Brandon WilliamsAug 14, 2018
  4. Jeff KingAug 14, 2018
  5. Jeff KingAug 14, 2018
  6. Christian CouderAug 15, 2018
  7. Jeff KingAug 17, 2018
  8. Junio C HamanoAug 17, 2018
  9. Jeff KingAug 17, 2018
  10. Ævar Arnfjörð BjarmasonAug 14, 2018

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.