{"thread":{"id":"60517","subject":"Feature request: git status --branch-only","startedAt":"2023-11-14T10:16:34Z","lastAt":"2023-11-16T16:10:02Z","messageCount":7,"participants":["Ondra Medek","Phillip Wood","Jeff King","phillip.wood123@gmail.com"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"484855","messageId":"CAJsoDaFX7YdncsTy7UsjxaM1GCKs36-H5RhJ6kzgBUFBJyoGZQ@mail.gmail.com","threadId":"60517","inReplyTo":null,"subject":"Feature request: git status --branch-only","fromName":"Ondra Medek","fromEmail":"xmedeko@gmail.com","sentAt":"2023-11-14T10:16:19Z","receivedAt":"2023-11-14T10:16:34Z","isPatch":false,"sender":{"key":"xmedeko@gmail.com","avatar":"https://gravatar.com/avatar/09ec305680e81ade2e3617fd245cda7136f6d5e859b5120feb9436e0725f6e1d?d=mp&s=160"},"body":"Hello,\nI am working on a tol which should fetch changes from a remote\nrepository on a user click. I want to limit fetch on the current\nremote tracking branch (something like \"origin/master\"), but\nsurprisingly, it's hard to get it for all corner cases like a fresh\nclone of an empty repository or detached head, etc. E.g see this SO\nthread https://stackoverflow.com/questions/171550/find-out-which-remote-branch-a-local-branch-is-tracking/52896538\n\nThe most reliable way for me is to call\n\ngit status -b --no-ahead-behind --porcelain=v2\n\nand parse the \"# branch.upstream\" line output. However, it is a bit\nslow on large repos and yields unused output for me. So, I propose a\nnew switch \"git status --branch-only\" which would output branch status\nonly.\n\nNote: workaround is to specify some non-existing directory, like\n\ngit status -b --no-ahead-behind --porcelain=v2 .this-dir-does-not-exists\n\nThanks\nOndra Medek\n"},{"id":"484859","messageId":"47fa8400-1e5f-437f-84b8-50bb09580325@gmail.com","threadId":"60517","inReplyTo":"CAJsoDaFX7YdncsTy7UsjxaM1GCKs36-H5RhJ6kzgBUFBJyoGZQ@mail.gmail.com","subject":"Re: Feature request: git status --branch-only","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2023-11-14T12:28:14Z","receivedAt":"2023-11-14T12:28:20Z","isPatch":false,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"Hi Ondra\n\nOn 14/11/2023 10:16, Ondra Medek wrote:\n> Hello,\n> I am working on a tol which should fetch changes from a remote\n> repository on a user click. I want to limit fetch on the current\n> remote tracking branch (something like \"origin/master\"), but\n> surprisingly, it's hard to get it for all corner cases like a fresh\n> clone of an empty repository or detached head, etc. E.g see this SO\n> thread https://stackoverflow.com/questions/171550/find-out-which-remote-branch-a-local-branch-is-tracking/52896538\n\nI think you can do this by calling\n\n\tgit symbolic-ref --quiet HEAD\n\nto get the full refname of the current branch. If HEAD is detached it \nwill print nothing and exit with exit code 1. Then you can call\n\n\tgit for-each-ref --format=\"%(upstream:short)\" $refname\n\nto get the upstream branch\n\nBest Wishes\n\nPhillip\n"},{"id":"484860","messageId":"CAJsoDaHX3t9bViq0F7gmJPD+PoE-ZqmJS5h=u-W900x9KEMmYA@mail.gmail.com","threadId":"60517","inReplyTo":"47fa8400-1e5f-437f-84b8-50bb09580325@gmail.com","subject":"Re: Feature request: git status --branch-only","fromName":"Ondra Medek","fromEmail":"xmedeko@gmail.com","sentAt":"2023-11-14T12:40:19Z","receivedAt":"2023-11-14T12:40:34Z","isPatch":false,"sender":{"key":"xmedeko@gmail.com","avatar":"https://gravatar.com/avatar/09ec305680e81ade2e3617fd245cda7136f6d5e859b5120feb9436e0725f6e1d?d=mp&s=160"},"body":"Hi Phillip,\n\nit does not work for a fresh clone of an empty repository\n\n    git for-each-ref --format=\"%(upstream:short)\" refs/heads/master\n\noutputs nothing, while\n\n    git status -b --no-ahead-behind --porcelain=v2\n\noutputs\n\n# branch.oid (initial)\n# branch.head master\n# branch.upstream origin/master\n\nI.e. it outputs a proper upstream branch.\n\nBest regards\nOndra\n\nOndra Medek\n\n\nOn Tue, 14 Nov 2023 at 13:28, Phillip Wood <phillip.wood123@gmail.com> wrote:\n>\n> Hi Ondra\n>\n> On 14/11/2023 10:16, Ondra Medek wrote:\n> > Hello,\n> > I am working on a tol which should fetch changes from a remote\n> > repository on a user click. I want to limit fetch on the current\n> > remote tracking branch (something like \"origin/master\"), but\n> > surprisingly, it's hard to get it for all corner cases like a fresh\n> > clone of an empty repository or detached head, etc. E.g see this SO\n> > thread https://stackoverflow.com/questions/171550/find-out-which-remote-branch-a-local-branch-is-tracking/52896538\n>\n> I think you can do this by calling\n>\n>         git symbolic-ref --quiet HEAD\n>\n> to get the full refname of the current branch. If HEAD is detached it\n> will print nothing and exit with exit code 1. Then you can call\n>\n>         git for-each-ref --format=\"%(upstream:short)\" $refname\n>\n> to get the upstream branch\n>\n> Best Wishes\n>\n> Phillip\n"},{"id":"484862","messageId":"00033c86-dbd7-4c88-bfbd-8f6766cd66c9@gmail.com","threadId":"60517","inReplyTo":"CAJsoDaHX3t9bViq0F7gmJPD+PoE-ZqmJS5h=u-W900x9KEMmYA@mail.gmail.com","subject":"Re: Feature request: git status --branch-only","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2023-11-14T15:02:04Z","receivedAt":"2023-11-14T15:02:08Z","isPatch":false,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"Hi Ondra\n\nOn 14/11/2023 12:40, Ondra Medek wrote:\n> Hi Phillip,\n> \n> it does not work for a fresh clone of an empty repository\n> \n>      git for-each-ref --format=\"%(upstream:short)\" refs/heads/master\n> \n> outputs nothing, while\n\nOh dear, that's a shame. I wonder if it is a bug because the \ndocumentation says that\n\n\t--format=\"%(upstream:track)\"\n\nshould print \"[gone]\" whenever an unknown upstream ref is encountered \nbut trying that on a clone of an empty repository gives no output.\n\nBest Wishes\n\nPhillip\n\n\n>      git status -b --no-ahead-behind --porcelain=v2\n> \n> outputs\n> \n> # branch.oid (initial)\n> # branch.head master\n> # branch.upstream origin/master\n> \n> I.e. it outputs a proper upstream branch.\n> \n> Best regards\n> Ondra\n> \n> Ondra Medek\n> \n> \n> On Tue, 14 Nov 2023 at 13:28, Phillip Wood <phillip.wood123@gmail.com> wrote:\n>>\n>> Hi Ondra\n>>\n>> On 14/11/2023 10:16, Ondra Medek wrote:\n>>> Hello,\n>>> I am working on a tol which should fetch changes from a remote\n>>> repository on a user click. I want to limit fetch on the current\n>>> remote tracking branch (something like \"origin/master\"), but\n>>> surprisingly, it's hard to get it for all corner cases like a fresh\n>>> clone of an empty repository or detached head, etc. E.g see this SO\n>>> thread https://stackoverflow.com/questions/171550/find-out-which-remote-branch-a-local-branch-is-tracking/52896538\n>>\n>> I think you can do this by calling\n>>\n>>          git symbolic-ref --quiet HEAD\n>>\n>> to get the full refname of the current branch. If HEAD is detached it\n>> will print nothing and exit with exit code 1. Then you can call\n>>\n>>          git for-each-ref --format=\"%(upstream:short)\" $refname\n>>\n>> to get the upstream branch\n>>\n>> Best Wishes\n>>\n>> Phillip\n> \n"},{"id":"484871","messageId":"CAJsoDaG71Jrt=M7iTn8zTvevv5FA4iuDjd2otDNy4tS5RCyX-w@mail.gmail.com","threadId":"60517","inReplyTo":"00033c86-dbd7-4c88-bfbd-8f6766cd66c9@gmail.com","subject":"Re: Feature request: git status --branch-only","fromName":"Ondra Medek","fromEmail":"xmedeko@gmail.com","sentAt":"2023-11-14T19:44:11Z","receivedAt":"2023-11-14T19:44:27Z","isPatch":false,"sender":{"key":"xmedeko@gmail.com","avatar":"https://gravatar.com/avatar/09ec305680e81ade2e3617fd245cda7136f6d5e859b5120feb9436e0725f6e1d?d=mp&s=160"},"body":"Hi Phillip,\n\neven \"[gone]\" would not be much of help for me. I would need something\nlike \"origin/master [gone]\" i.e. what \"git status -b --porcelain\"\nprints. (Note, I've written about \"git status -b --porcelain=v2\"\nbefore because v2 is better documented and parseable.)\n\nRegards\nOndra Medek\n\nOn Tue, 14 Nov 2023 at 16:02, Phillip Wood <phillip.wood123@gmail.com> wrote:\n>\n> Hi Ondra\n>\n> On 14/11/2023 12:40, Ondra Medek wrote:\n> > Hi Phillip,\n> >\n> > it does not work for a fresh clone of an empty repository\n> >\n> >      git for-each-ref --format=\"%(upstream:short)\" refs/heads/master\n> >\n> > outputs nothing, while\n>\n> Oh dear, that's a shame. I wonder if it is a bug because the\n> documentation says that\n>\n>         --format=\"%(upstream:track)\"\n>\n> should print \"[gone]\" whenever an unknown upstream ref is encountered\n> but trying that on a clone of an empty repository gives no output.\n>\n> Best Wishes\n>\n> Phillip\n>\n>\n> >      git status -b --no-ahead-behind --porcelain=v2\n> >\n> > outputs\n> >\n> > # branch.oid (initial)\n> > # branch.head master\n> > # branch.upstream origin/master\n> >\n> > I.e. it outputs a proper upstream branch.\n> >\n> > Best regards\n> > Ondra\n> >\n> > Ondra Medek\n> >\n> >\n> > On Tue, 14 Nov 2023 at 13:28, Phillip Wood <phillip.wood123@gmail.com> wrote:\n> >>\n> >> Hi Ondra\n> >>\n> >> On 14/11/2023 10:16, Ondra Medek wrote:\n> >>> Hello,\n> >>> I am working on a tol which should fetch changes from a remote\n> >>> repository on a user click. I want to limit fetch on the current\n> >>> remote tracking branch (something like \"origin/master\"), but\n> >>> surprisingly, it's hard to get it for all corner cases like a fresh\n> >>> clone of an empty repository or detached head, etc. E.g see this SO\n> >>> thread https://stackoverflow.com/questions/171550/find-out-which-remote-branch-a-local-branch-is-tracking/52896538\n> >>\n> >> I think you can do this by calling\n> >>\n> >>          git symbolic-ref --quiet HEAD\n> >>\n> >> to get the full refname of the current branch. If HEAD is detached it\n> >> will print nothing and exit with exit code 1. Then you can call\n> >>\n> >>          git for-each-ref --format=\"%(upstream:short)\" $refname\n> >>\n> >> to get the upstream branch\n> >>\n> >> Best Wishes\n> >>\n> >> Phillip\n> >\n"},{"id":"484884","messageId":"20231114201808.GE2092538@coredump.intra.peff.net","threadId":"60517","inReplyTo":"00033c86-dbd7-4c88-bfbd-8f6766cd66c9@gmail.com","subject":"Re: Feature request: git status --branch-only","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2023-11-14T20:18:08Z","receivedAt":"2023-11-14T20:18:10Z","isPatch":false,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Tue, Nov 14, 2023 at 03:02:04PM +0000, Phillip Wood wrote:\n\n> Hi Ondra\n> \n> On 14/11/2023 12:40, Ondra Medek wrote:\n> > Hi Phillip,\n> > \n> > it does not work for a fresh clone of an empty repository\n> > \n> >      git for-each-ref --format=\"%(upstream:short)\" refs/heads/master\n> > \n> > outputs nothing, while\n> \n> Oh dear, that's a shame. I wonder if it is a bug because the documentation\n> says that\n> \n> \t--format=\"%(upstream:track)\"\n> \n> should print \"[gone]\" whenever an unknown upstream ref is encountered but\n> trying that on a clone of an empty repository gives no output.\n\nI think it would print \"gone\" if the upstream branch went missing. But\nin this case the actual local branch is missing. And for-each-ref will\nnot show an entry at all for a ref that does not exist. The\n\"refs/heads/master\" on your command line is not a ref, but a pattern,\nand that pattern does not match anything. So it's working as intended.\n\nI think a more direct tool would be:\n\n  git rev-parse --symbolic-full-name master@{upstream}\n\nThat convinces branch_get_upstream() to return the value we want, but\nsadly it seems to get lost somewhere in the resolution process, and we\nspit out an error. Arguably that is a bug (with --symbolic or\n--symbolic-full-name, I think it would be OK to resolve names even if\nthey don't point to something, but it's possible that would have other\nunexpected side effects).\n\n-Peff\n"},{"id":"484978","messageId":"7b4c76c3-4a1c-4d78-85bf-7a160a286dc5@gmail.com","threadId":"60517","inReplyTo":"20231114201808.GE2092538@coredump.intra.peff.net","subject":"Re: Feature request: git status --branch-only","fromName":"","fromEmail":"phillip.wood123@gmail.com","sentAt":"2023-11-16T16:09:58Z","receivedAt":"2023-11-16T16:10:02Z","isPatch":false,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"Hi Peff\n\nOn 14/11/2023 20:18, Jeff King wrote:\n> On Tue, Nov 14, 2023 at 03:02:04PM +0000, Phillip Wood wrote:\n> \n>> Hi Ondra\n>>\n>> On 14/11/2023 12:40, Ondra Medek wrote:\n>>> Hi Phillip,\n>>>\n>>> it does not work for a fresh clone of an empty repository\n>>>\n>>>       git for-each-ref --format=\"%(upstream:short)\" refs/heads/master\n>>>\n>>> outputs nothing, while\n>>\n>> Oh dear, that's a shame. I wonder if it is a bug because the documentation\n>> says that\n>>\n>> \t--format=\"%(upstream:track)\"\n>>\n>> should print \"[gone]\" whenever an unknown upstream ref is encountered but\n>> trying that on a clone of an empty repository gives no output.\n> \n> I think it would print \"gone\" if the upstream branch went missing. But\n> in this case the actual local branch is missing. And for-each-ref will\n> not show an entry at all for a ref that does not exist. The\n> \"refs/heads/master\" on your command line is not a ref, but a pattern,\n> and that pattern does not match anything. So it's working as intended.\n\nOh of course, I'd somehow forgotten that \"refs/heads/master\" did not \nexist so it makes sense that for-each-ref does not print anything.\n\n> I think a more direct tool would be:\n> \n>    git rev-parse --symbolic-full-name master@{upstream}\n> \n> That convinces branch_get_upstream() to return the value we want, but\n> sadly it seems to get lost somewhere in the resolution process, and we\n> spit out an error. Arguably that is a bug (with --symbolic or\n> --symbolic-full-name, I think it would be OK to resolve names even if\n> they don't point to something, but it's possible that would have other\n> unexpected side effects).\n\nYeah, maybe we should look at fixing that - I didn't suggest it because \nI knew it did not work on an unborn branch but as you say there is no \nobvious reason why it shouldn't\n\nBest Wishes\n\nPhillip\n\n> -Peff\n"}]}