Re: [PATCH v14 1/6] branch: add --forked filter for --list mode
- From
- Phillip Wood <phillip.wood123@gmail.com>
- Date
- Jun 15, 2026, 09:46 UTC
- Message-ID
- <16bb2c32-224d-4c8a-a696-d0b9ca9d304c@gmail.com>
- In-Reply-To
- <7383872f4b2f422ec36b11ab5fb31cce08e6106a.1780999917.git.gitgitgadget@gmail.com>
Hi Harald
On 09/06/2026 11:11, Harald Nordgren via GitGitGadget wrote:
Show 25 quoted lines
> From: Harald Nordgren <haraldnordgren@gmail.com> > > Add a --forked option to "git branch" list mode that lists only > branches whose configured upstream matches <branch>. The argument > can be a ref (e.g. "origin/main", "master") or a shell glob > (e.g. "origin/*"), and may be repeated to widen the filter. > > It is an ordinary list filter, so it combines with the others: > > git branch --merged origin/main --forked 'origin/*' > > lists branches forked from origin that are already merged into > origin/main, and --no-merged inverts the question. > > This is the building block for --prune-merged, which deletes the > listed branches once they have landed on their upstream. > > Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com> > --- > Documentation/git-branch.adoc | 10 +++- > builtin/branch.c | 18 ++++++- > ref-filter.c | 70 ++++++++++++++++++++++++++ > ref-filter.h | 10 ++++ > t/t3200-branch.sh | 92 +++++++++++++++++++++++++++++++++++ > 5 files changed, 197 insertions(+), 3 deletions(-)
It's nice to see that moving the code into the ref-filter.c has reduced the overall number of additions by ~50 lines. The documentation and implementation look fine though I have a couple of thoughts:
- Previous iterations supported "origin" as a short hand for the branch
origin/HEAD points to. That was nice because it means we can use the
same syntax for "git checkout -b" and "git branch --forked". It
would probably be a good idea to support it. - We could probably be a bit smarter about the way we handle patterns
by copying what dwim_ref() does to support things like
remotes/origin/* but I don't think we need to do that now.Show 20 quoted lines
> diff --git a/t/t3200-branch.sh b/t/t3200-branch.sh > index e7829c2c4b..4e7deddc04 100755 > --- a/t/t3200-branch.sh > +++ b/t/t3200-branch.sh > @@ -1717,4 +1717,96 @@ test_expect_success 'errors if given a bad branch name' ' > test_cmp expect actual > ' > > +test_expect_success '--forked: setup' ' > + test_create_repo forked-upstream && > + test_commit -C forked-upstream base && > + git -C forked-upstream branch one base && > + git -C forked-upstream branch two base && > + > + test_create_repo forked-other && > + test_commit -C forked-other other-base && > + git -C forked-other branch foreign other-base && > + > + git clone forked-upstream forked && > + git -C forked remote add other ../forked-other &&
We can use "add -f" to fetch here rather than doing it separately.
Show 6 quoted lines
> + git -C forked fetch other && > + git -C forked branch local-base && > + git -C forked branch --track local-one origin/one && > + git -C forked branch --track local-two origin/two && > + git -C forked branch --track local-foreign other/foreign && > + git -C forked branch detached &&
Normally we use "detached" to mean no branch, lets read on and see how this is used ...
Show 5 quoted lines
> + git -C forked branch --track local-trunk local-base > +' > + > +test_expect_success '--forked <upstream-tracking-branch> filters by upstream' ' > + git -C forked branch --forked origin/one --format="%(refname:short)" >actual &&
origin/one and origin/two point to the same commit, so this demonstrates that we're checking the branch names, not the topology which is good. All of the local branches point at their upstream which isn't very realistic - I wonder if we should add some local commits?
The tests all look sensible, but there is no coverage for combining --forked with branch names as in
git branch --forked <arg> <branch>
Thanks
Phillip
Show 70 quoted lines
> + echo local-one >expect && > + test_cmp expect actual > +' > + > +test_expect_success '--forked <glob> filters by wildmatch' ' > + git -C forked branch --forked "origin/*" --format="%(refname:short)" >actual && > + cat >expect <<-\EOF && > + local-one > + local-two > + main > + EOF > + test_cmp expect actual > +' > + > +test_expect_success '--forked <local-branch> matches branches with local upstream' ' > + git -C forked branch --forked local-base --format="%(refname:short)" >actual && > + echo local-trunk >expect && > + test_cmp expect actual > +' > + > +test_expect_success '--forked can be repeated to widen the filter' ' > + git -C forked branch --forked origin/one --forked other/foreign --format="%(refname:short)" >actual && > + cat >expect <<-\EOF && > + local-foreign > + local-one > + EOF > + test_cmp expect actual > +' > + > +test_expect_success '--forked combines literal and glob arguments' ' > + git -C forked branch --forked local-base --forked "other/*" --format="%(refname:short)" >actual && > + cat >expect <<-\EOF && > + local-foreign > + local-trunk > + EOF > + test_cmp expect actual > +' > + > +test_expect_success '--forked "*/*" covers every remote-tracking upstream' ' > + git -C forked branch --forked "*/*" --format="%(refname:short)" >actual && > + cat >expect <<-\EOF && > + local-foreign > + local-one > + local-two > + main > + EOF > + test_cmp expect actual > +' > + > +test_expect_success '--forked composes with --no-merged' ' > + test_when_finished "git -C forked checkout detached" && > + git -C forked checkout local-one && > + test_commit -C forked local-only && > + git -C forked branch --forked "origin/*" --no-merged origin/one \ > + --format="%(refname:short)" >actual && > + echo local-one >expect && > + test_cmp expect actual > +' > + > +test_expect_success '--forked rejects unknown branch/pattern' ' > + test_must_fail git -C forked branch --forked nope 2>err && > + test_grep "not a valid branch or pattern" err > +' > + > +test_expect_success '--forked requires a value' ' > + test_must_fail git -C forked branch --forked 2>err && > + test_grep "requires a value" err > +' > + > test_done