Skip to content

Commit 3281b3f

Browse files
committed
Merge branch 'tests'
2 parents 45bc7ce + 46a7690 commit 3281b3f

File tree

8 files changed

+351
-7
lines changed

8 files changed

+351
-7
lines changed

.github/workflows/test.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: Tests
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
7+
build:
8+
name: build
9+
runs-on: ubuntu-latest
10+
steps:
11+
12+
- name: Setup BATS
13+
uses: mig4/setup-bats@v1
14+
with:
15+
bats-version: 1.8.2
16+
17+
- name: Check out code
18+
uses: actions/checkout@v3
19+
with:
20+
submodules: recursive
21+
22+
- name: Test
23+
run: bats ./test

.gitmodules

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[submodule "test/test_helper/bats-support"]
2+
path = test/test_helper/bats-support
3+
url = https://github.com/bats-core/bats-support.git
4+
[submodule "test/test_helper/bats-assert"]
5+
path = test/test_helper/bats-assert
6+
url = https://github.com/bats-core/bats-assert.git

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# git-context-graph
22

3+
[![Tests](https://github.com/Djuuu/git-context-graph/actions/workflows/test.yml/badge.svg)](https://github.com/Djuuu/git-context-graph/actions/workflows/test.yml)
4+
[![License](https://img.shields.io/badge/license-Beerware%20%F0%9F%8D%BA-yellow)](https://web.archive.org/web/20160322002352/http://www.cs.trincoll.edu/hfoss/wiki/Chris_Fei:_Beerware_License)
5+
36
Show graph log of branch with its remote counterparts and default repository branch.
47

58
This is a shortcut to `git log --graph` which provides a middle ground between

git-context-graph

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -320,18 +320,18 @@ while [[ $# -gt 0 ]]; do
320320
-l|--list) GIT_CG_LIST=1; shift ;;
321321
-s|--short) GIT_CG_LIST=1; GIT_CG_LIST_SHORT=1; shift ;;
322322

323-
-A|--config-add) GIT_CG_CONFIGURE=1; GIT_CG_CONFIG_ADD=1; GIT_CG_CONFIG_CLEAR=0; shift ;;
324-
-C|--config-clear) GIT_CG_CONFIGURE=1; GIT_CG_CONFIG_ADD=0; GIT_CG_CONFIG_CLEAR=1; shift ;;
323+
-A|--config-add) GIT_CG_CONFIGURE="add"; GIT_CG_CONFIG_ADD=1; shift ;;
324+
-C|--config-clear) GIT_CG_CONFIGURE="clear"; GIT_CG_CONFIG_CLEAR=1; shift ;;
325325

326326
-h|--usage) GIT_CONTEXT_GRAPH_HELP=1; shift ;;
327327

328328
--) GIT_LOG_PATHS+=("$1"); shift ;;
329329
-*) GIT_LOG_OPTIONS+=("$1"); shift ;;
330330
*)
331-
if [[ ${#GIT_LOG_PATHS[@]} -gt 0 ]]; then GIT_LOG_PATHS+=("$1") # git-log path argument (after '--')
332-
elif [[ $GIT_CG_CONFIG_ADD -eq 1 ]]; then GIT_CG_ADD_BRANCHES+=("$1") # additional branch to add to config
333-
elif [[ $GIT_CG_CONFIG_CLEAR -eq 1 ]]; then GIT_CG_CLEAR_BRANCHES+=("$1") # additional branch to remove to config
334-
else GIT_CG_BRANCHES+=("$1") # git-context-graph branch argument
331+
if [[ ${#GIT_LOG_PATHS[@]} -gt 0 ]]; then GIT_LOG_PATHS+=("$1") # git-log path argument (after '--')
332+
elif [[ $GIT_CG_CONFIGURE == "add" ]]; then GIT_CG_ADD_BRANCHES+=("$1") # additional branch to add to config
333+
elif [[ $GIT_CG_CONFIGURE == "clear" ]]; then GIT_CG_CLEAR_BRANCHES+=("$1") # additional branch to remove from config
334+
else GIT_CG_BRANCHES+=("$1") # git-context-graph branch argument
335335
fi; shift ;;
336336
esac
337337
done
@@ -356,7 +356,9 @@ fi
356356

357357
# Show graph
358358
# shellcheck disable=SC2046 # use word splitting for GIT_CG_BRANCHES
359-
git log --color --graph --abbrev-commit --decorate --pretty=oneline \
359+
git \
360+
-c log.excludeDecoration='refs/remotes/*/HEAD' \
361+
log --color --graph --abbrev-commit --decorate --pretty=oneline \
360362
"${GIT_LOG_OPTIONS[@]}" \
361363
$(context_branches "${GIT_CG_BRANCHES[@]}") \
362364
"${GIT_LOG_PATHS[@]}"

test/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
data

test/git-context-graph.bats

Lines changed: 307 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,307 @@
1+
#!/usr/bin/env bats
2+
3+
load "test_helper/bats-support/load"
4+
load "test_helper/bats-assert/load"
5+
6+
7+
git() {
8+
command git \
9+
-c init.defaultBranch=main \
10+
11+
-c user.name=Test \
12+
"$@"
13+
}
14+
15+
git-context-graph() {
16+
"${BATS_TEST_DIRNAME}"/../git-context-graph "$@"
17+
}
18+
19+
setup_file() {
20+
cd "${BATS_TEST_DIRNAME}" || exit
21+
22+
mkdir data && cd data || exit
23+
24+
git init --bare -b main remote1
25+
git init --bare -b main remote2
26+
git init --bare -b custom remote3
27+
28+
git init repo && cd repo || exit
29+
30+
git remote add r1 ../remote1
31+
git remote add r2 ../remote2
32+
git remote add r3 ../remote3
33+
34+
git switch -c main
35+
git commit --allow-empty -m "Main 1"
36+
git push r1 main
37+
git push r2 main
38+
git push r3 main:custom
39+
40+
git switch -c feature-A
41+
git commit --allow-empty -m "Feature A - 1"
42+
git push r1 feature-A
43+
git push r2 feature-A
44+
45+
git switch -c feature-B main
46+
git commit --allow-empty -m "Feature B - 1"
47+
git push r1 feature-B
48+
git push r2 feature-B
49+
50+
git switch -c feature-C main
51+
git commit --allow-empty -m "Feature C - 1"
52+
git push r1 feature-C
53+
git push r3 feature-C
54+
55+
git switch -c epic/big-feature main
56+
git commit --allow-empty -m "Epic B - 1"
57+
git push r1 epic/big-feature
58+
git push r2 epic/big-feature
59+
60+
cd ..
61+
rm -rf repo
62+
}
63+
64+
teardown_file() {
65+
cd "${BATS_TEST_DIRNAME}" || exit
66+
rm -rf data
67+
}
68+
69+
setup() {
70+
cd "${BATS_TEST_DIRNAME}/data" || exit
71+
}
72+
73+
teardown() {
74+
cd "${BATS_TEST_DIRNAME}/data" || exit
75+
rm -rf repo*
76+
}
77+
78+
79+
@test "Command fails outside a Git repository" {
80+
cd /tmp
81+
run git-context-graph --list
82+
assert_failure
83+
}
84+
85+
@test "Local and default branches are shown with remotes" {
86+
git clone ./remote1 repo && cd repo
87+
88+
git switch -c feature-A origin/feature-A
89+
90+
run git-context-graph --list
91+
assert_output "$(cat <<- EOF
92+
refs/heads/feature-A
93+
refs/heads/main
94+
refs/remotes/origin/feature-A
95+
refs/remotes/origin/main
96+
EOF
97+
)"
98+
99+
run git-context-graph --list --short
100+
assert_output "$(cat <<- EOF
101+
feature-A
102+
main
103+
origin/feature-A
104+
origin/main
105+
EOF
106+
)"
107+
108+
run git-context-graph --list --local
109+
assert_output "$(cat <<- EOF
110+
refs/heads/feature-A
111+
refs/heads/main
112+
EOF
113+
)"
114+
115+
run git-context-graph --list --no-default
116+
assert_output "$(cat <<- EOF
117+
refs/heads/feature-A
118+
refs/remotes/origin/feature-A
119+
EOF
120+
)"
121+
}
122+
123+
@test "Branches to list can be passed as arguments" {
124+
git clone ./remote1 repo && cd repo
125+
126+
git switch -c feature-A origin/feature-A
127+
git switch -c feature-B origin/feature-B
128+
129+
run git-context-graph --list --no-default >&3
130+
assert_output "$(cat <<- EOF
131+
refs/heads/feature-B
132+
refs/remotes/origin/feature-B
133+
EOF
134+
)"
135+
136+
run git-context-graph feature-A --list --no-default >&3
137+
assert_output "$(cat <<- EOF
138+
refs/heads/feature-A
139+
refs/remotes/origin/feature-A
140+
EOF
141+
)"
142+
143+
run git-context-graph --add feature-A --list --no-default >&3
144+
assert_output "$(cat <<- EOF
145+
refs/heads/feature-A
146+
refs/heads/feature-B
147+
refs/remotes/origin/feature-A
148+
refs/remotes/origin/feature-B
149+
EOF
150+
)"
151+
}
152+
153+
@test "Default branch is determined from cloned remote HEAD" {
154+
git clone ./remote3 repo && cd repo
155+
git switch -c feature-C origin/feature-C
156+
157+
run git-context-graph --list
158+
assert_line "refs/heads/custom"
159+
assert_line "refs/remotes/origin/custom"
160+
}
161+
162+
@test "Default branch determination falls back to standard names" {
163+
# 'main' is a standard default branch
164+
(
165+
git init -b main repo1 && cd repo1
166+
git commit --allow-empty -m "Main 1"
167+
git switch -c feature
168+
git commit --allow-empty -m "feature 1"
169+
170+
run git-context-graph --list
171+
assert_line "refs/heads/main"
172+
)
173+
174+
# 'master' is a standard default branch
175+
(
176+
git init -b master repo2 && cd repo2
177+
git commit --allow-empty -m "Master 1"
178+
git switch -c feature
179+
git commit --allow-empty -m "feature 1"
180+
181+
run git-context-graph --list
182+
assert_line "refs/heads/master"
183+
)
184+
185+
# No standard default branch identified
186+
(
187+
git init -b custom repo3 && cd repo3
188+
git commit --allow-empty -m "Custom 1"
189+
git switch -c feature
190+
git commit --allow-empty -m "feature 1"
191+
192+
run git-context-graph --list
193+
assert_output "$(cat <<- EOF
194+
refs/heads/feature
195+
EOF
196+
)"
197+
)
198+
}
199+
200+
@test "All remotes are considered" {
201+
git clone ./remote1 repo && cd repo
202+
git remote add other ../remote2
203+
git fetch other
204+
205+
git switch -c feature-A origin/feature-A
206+
207+
run git-context-graph --list
208+
assert_output "$(cat <<- EOF
209+
refs/heads/feature-A
210+
refs/heads/main
211+
refs/remotes/origin/feature-A
212+
refs/remotes/origin/main
213+
refs/remotes/other/feature-A
214+
refs/remotes/other/main
215+
EOF
216+
)"
217+
}
218+
219+
@test "Tracking branches with different names are considered" {
220+
git clone ./remote1 repo && cd repo
221+
git remote add other ../remote2
222+
git fetch other
223+
224+
git switch -c feature-A-local origin/feature-A
225+
226+
run git-context-graph --list --no-default
227+
assert_output "$(cat <<- EOF
228+
refs/heads/feature-A-local
229+
refs/remotes/origin/feature-A
230+
EOF
231+
)"
232+
}
233+
234+
@test "Git-log options are passed to git-log" {
235+
git clone ./remote1 repo && cd repo
236+
237+
git switch -c feature-A origin/feature-A
238+
239+
run git-context-graph --all --pretty=oneline --no-color
240+
assert_output --partial "(origin/epic/big-feature) Epic B - 1"
241+
assert_output --partial "(HEAD -> feature-A, origin/feature-A) Feature A - 1"
242+
assert_output --partial "(origin/feature-B) Feature B - 1"
243+
assert_output --partial "(origin/main, main) Main 1"
244+
}
245+
246+
@test "Persistent additional context branches can be configured" {
247+
git clone ./remote1 repo && cd repo
248+
249+
git switch -c feature-A origin/feature-A
250+
git switch -c feature-B origin/feature-B
251+
git switch -c feature-C origin/feature-C
252+
git switch -c epic/big-feature origin/epic/big-feature
253+
254+
git switch feature-A
255+
256+
run git-context-graph --config-add unknown-branch
257+
assert_output "$(cat <<- EOF
258+
No additional context branches for feature-A.
259+
EOF
260+
)"
261+
262+
run git-context-graph --config-add epic/big-feature
263+
assert_output "$(cat <<- EOF
264+
Additional context branches for feature-A:
265+
epic/big-feature
266+
EOF
267+
)"
268+
269+
run git-context-graph feature-A --list --short --local --no-default
270+
assert_output "$(cat <<- EOF
271+
epic/big-feature
272+
feature-A
273+
EOF
274+
)"
275+
276+
git switch main
277+
278+
run git-context-graph feature-A --config-add feature-B feature-C --config-clear epic/big-feature
279+
assert_output "$(cat <<- EOF
280+
Additional context branches for feature-A:
281+
feature-B
282+
feature-C
283+
EOF
284+
)"
285+
286+
run git-context-graph feature-A --list --short --local --no-default
287+
assert_output "$(cat <<- EOF
288+
feature-A
289+
feature-B
290+
feature-C
291+
EOF
292+
)"
293+
294+
git switch feature-A
295+
296+
run git-context-graph --config-clear
297+
assert_output "$(cat <<- EOF
298+
Cleared additional context branches for feature-A.
299+
EOF
300+
)"
301+
302+
run git-context-graph --list --short --local --no-default
303+
assert_output "$(cat <<- EOF
304+
feature-A
305+
EOF
306+
)"
307+
}

test/test_helper/bats-assert

Submodule bats-assert added at 78fa631

test/test_helper/bats-support

Submodule bats-support added at 3c8fadc

0 commit comments

Comments
 (0)