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

Re: [RFC PATCH] object-name: add @{upstreamhead} shorthand

From
Jeff King <peff@peff.net>
Date
Oct 23, 2024, 21:56 UTC
Message-ID
<20241023215618.GA821188@coredump.intra.peff.net>
In-Reply-To
<D51R90BTHJMY.1C1XY5P4CHTWG@ferdinandy.com>
On Mon, Oct 21, 2024 at 10:09:38PM +0200, Bence Ferdinandy wrote:
Show 12 quoted lines
> > And possibly we could make it easier to just grab the remote name with a
> > single command.
> 
> As I was running this patch through my head yesterday I sort of distilled my
> argument in favour to "writing remote agnostic scripts are unnecessarily
> complicated", but I do agree, that if there were a git command that could
> return the remote for a branch without any extra scripting hacks would easily
> get you the same result, and may even be useful elsewhere.
> 
> I'm not sure where this would be the best. Maybe: 
> 	git branch --show-current-remote
> ?
I've been giving some thought to this.

You could argue it's about querying remotes, so "git remote". Although we don't really want to know anything about the remote except its name, so it's a little weird.

Or as you note, we're querying info about a branch. So "git branch" makes sense. But "--show-current-remote" feels kind of narrow there. Shouldn't we be able to ask about the configured remote for any branch?

In which case it is really just a single "git config" lookup away:
  git config branch.$branch.remote

You have to look up the current branch, of course. You can do that with symbolic-ref like:

  git config "branch.$(git symbolic-ref --short HEAD).remote"

You might get an error from symbolic-ref if we're on a detached HEAD, of course. You can either ignore that (in which case the lookup of "branch..remote" would show nothing), or a script can actually distinguish the two cases ("not on a branch" versus "there is no configured remote").

There's also another wrinkle we hadn't discussed: we have the concept of both an upstream remote for fetching and a push remote. And this would naturally extend there (you'd ask for .pushremote instead).

And finally, there's yet another way to access this information. ;) The for-each-ref formatter (which is also used for "branch --format") knows how to show remote names (and much more). So:

  git branch --list --format='%(upstream:remotename)' $branch

also gets you what you want. I don't think there's a good way to ask that command to show just the branch pointed to by HEAD, though. We recently added --include-root-refs to for-each-ref, but that's not quite what you want (you want just HEAD, and you really want to dereference it to show details of the branch it points to).

So I think rather than "branch --show-current-remote", we'd want some option to make "branch --list" show only the currently checked out branch, and then you could apply --format to it to get whatever information you wanted. Something like:

  git branch --list --is-head --format='%(upstream:remotename)'
-Peff
Previous: Taylor BlauNext: Bence Ferdinandy
Message 7 of 14 in “object-name: add @{upstreamhead} shorthand”
  1. object-name: add @{upstreamhead} shorthandBence Ferdinandy, Oct 20, 2024
  2. Kristoffer HaugsbakkOct 20, 2024
  3. Bence FerdinandyOct 20, 2024
  4. Jeff KingOct 21, 2024
  5. Bence FerdinandyOct 21, 2024
  6. Taylor BlauOct 21, 2024
  7. Jeff KingOct 23, 2024
  8. Bence FerdinandyOct 24, 2024
  9. Jeff KingOct 25, 2024
  10. Bence FerdinandyOct 27, 2024
  11. Taylor BlauOct 27, 2024
  12. Jeff KingOct 28, 2024
  13. Taylor BlauOct 21, 2024
  14. Bence FerdinandyOct 21, 2024

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.