Skip to content

Commit 43efd36

Browse files
committed
Exclude branches with partially matching name
ex: ignore `backup/feature` when matching `feature`
1 parent e47e5e6 commit 43efd36

File tree

2 files changed

+42
-5
lines changed

2 files changed

+42
-5
lines changed

git-context-graph

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,17 @@ branch_with_remotes() {
102102
return
103103
fi
104104

105-
local ref_pattern
106-
[[ -z $GIT_CG_SHOW_LOCAL_ONLY ]] &&
107-
ref_pattern="refs/**/$short_branch" ||
108-
ref_pattern="refs/heads/$short_branch"
105+
local ref_patterns=("refs/heads/$short_branch")
106+
if [[ -z $GIT_CG_SHOW_LOCAL_ONLY ]]; then
107+
local remote
108+
for remote in $(git remote); do
109+
ref_patterns+=("refs/remotes/${remote}/${short_branch}")
110+
done
111+
fi
109112

110113
# Branches with same name on all remotes
111114
local same_name_refs;
112-
same_name_refs=$(git for-each-ref --format="%(refname)" "$ref_pattern")
115+
same_name_refs=$(git for-each-ref --format="%(refname)" "${ref_patterns[@]}")
113116
echo "$same_name_refs"
114117

115118
[[ -n $GIT_CG_SHOW_LOCAL_ONLY ]] && return

test/git-context-graph.bats

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ git-context-graph() {
1919
setup_file() {
2020
cd "${BATS_TEST_DIRNAME}" || exit
2121

22+
[[ -d data ]] && rm -rf data
2223
mkdir data && cd data || exit
2324

2425
git init --bare -b main remote1
@@ -120,6 +121,39 @@ teardown() {
120121
)"
121122
}
122123

124+
@test "Patially matching branches are properly excluded" {
125+
git clone ./remote1 repo && cd repo
126+
127+
# Other branch with name containing 'feature-A'
128+
git switch -c backup/feature-A origin/feature-A --no-track
129+
git push -u origin backup/feature-A
130+
131+
# Other branch with name containing 'main'
132+
git switch -c old/main origin/main --no-track
133+
134+
git switch -c feature-A origin/feature-A
135+
136+
run git-context-graph --list
137+
refute_line "refs/heads/backup/feature-A"
138+
refute_line "refs/remotes/origin/backup/feature-A"
139+
refute_line "refs/heads/old/main"
140+
141+
run git-context-graph --list --short
142+
refute_line "backup/feature-A"
143+
refute_line "origin/backup/feature-A"
144+
refute_line "old/main"
145+
146+
run git-context-graph --list --local
147+
refute_line "refs/heads/backup/feature-A"
148+
refute_line "refs/heads/old/main"
149+
150+
run git-context-graph --list --no-default
151+
refute_line "refs/heads/backup/feature-A"
152+
refute_line "refs/remotes/origin/backup/feature-A"
153+
154+
git push origin --delete backup/feature-A
155+
}
156+
123157
@test "Branches to list can be passed as arguments" {
124158
git clone ./remote1 repo && cd repo
125159

0 commit comments

Comments
 (0)