On Wed Oct 23, 2024 at 23:56, Jeff King <peff@xxxxxxxx> wrote: > On Mon, Oct 21, 2024 at 10:09:38PM +0200, Bence Ferdinandy wrote: > >> > 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)' Thanks for running through this in such detail! This would be more widely useful for sure. I'd probably call the flag something like "--current", "--current-only" rather than "--is-head" though. "--is-head" sounds as if it would filter --list but not necessarily end up with a single entry. Anyway, thanks for the idea, I'll probably pick this up sooner or later. Best, Bence -- bence.ferdinandy.com