Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion git-open
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ git open [remote] [branch]

Available options are
c,commit! open current commit
C,Commit= open specific commit
i,issue! open issues page
s,suffix= append this suffix
f,file= append this file
Expand All @@ -27,8 +28,11 @@ p,print! just print the url
# shellcheck source=/dev/null
SUBDIRECTORY_OK='Yes' . "$(git --exec-path)/git-sh-setup"

function join_by { local IFS="$1"; shift; echo "$*"; }

# Defaults
is_commit=0
specific_commit=""
is_issue=0
protocol="https"
print_only=0
Expand All @@ -38,6 +42,10 @@ file_flag=""
while test $# != 0; do
case "$1" in
--commit) is_commit=1;;
--Commit=*)
IFS='=' read -ra specific_commit_flag <<< "$1"
specific_commit=$(join_by "=" "${specific_commit_flag[@]:1}")
;;
--issue) is_issue=1;;
--suffix=*) suffix_flag="$1";;
--file=*) file_flag="$1";;
Expand Down Expand Up @@ -258,7 +266,14 @@ elif [[ "$domain" =~ cnb\.cool$ ]]; then
fi
openurl="$protocol://$domain/$urlpath"

if (( is_commit )); then
if [[ -n "$specific_commit" ]]; then
sha=$(git rev-parse --verify "$specific_commit" 2>/dev/null)
if [[ -z "$sha" ]]; then
echo "Commit $specific_commit not found" 1>&2
exit 1
fi
openurl="$openurl/commit/$sha"
elif (( is_commit )); then
sha=$(git rev-parse HEAD)
openurl="$openurl/commit/$sha"
elif [[ $remote_ref != "master" || "$file" ]]; then
Expand Down
38 changes: 38 additions & 0 deletions test/git-open.bats
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,44 @@ setup() {
assert_output "https://github.com/paulirish/git-open/commit/${sha}"
}

@test "gh: git open --Commit=sha (valid)" {
git remote set-url origin "github.com:paulirish/git-open.git"
# Create a new commit to have a specific SHA
echo "content" > testfile
git add testfile
git commit -m "specific commit"
sha=$(git rev-parse HEAD)
run ../git-open "--Commit=${sha}"
assert_output "https://github.com/paulirish/git-open/commit/${sha}"
}

@test "gh: git open -C sha (valid)" {
git remote set-url origin "github.com:paulirish/git-open.git"
echo "content2" > testfile2
git add testfile2
git commit -m "specific commit 2"
sha=$(git rev-parse HEAD)
run ../git-open "-C" "${sha}"
assert_output "https://github.com/paulirish/git-open/commit/${sha}"
}

@test "gh: git open --Commit=HEAD~1 (relative ref)" {
git remote set-url origin "github.com:paulirish/git-open.git"
sha_parent=$(git rev-parse HEAD)
echo "content3" > testfile3
git add testfile3
git commit -m "child commit"
run ../git-open "--Commit=HEAD~1"
assert_output "https://github.com/paulirish/git-open/commit/${sha_parent}"
}

@test "gh: git open --Commit=invalid (invalid ref)" {
git remote set-url origin "github.com:paulirish/git-open.git"
run ../git-open "--Commit=nonexistent_ref"
[ "$status" -eq 1 ]
assert_output "Commit nonexistent_ref not found"
}

@test "gh: git open --suffix anySuffix" {
run ../git-open "--suffix" "anySuffix"
assert_output "https://github.com/paulirish/git-open/anySuffix"
Expand Down
2 changes: 1 addition & 1 deletion test/test_helper/bats-assert
Submodule bats-assert updated 46 files
+37 −0 .github/workflows/release.yml
+25 −0 .github/workflows/sync-default-branch.yml
+9 −0 .github/workflows/test.yml
+4 −0 .gitignore
+0 −8 .travis.yml
+0 −39 CHANGELOG.md
+570 −188 README.md
+22 −0 docs/CONTRIBUTING.md
+23 −0 install.sh
+32 −0 load.bash
+36 −0 package-lock.json
+45 −4 package.json
+0 −6 script/install-bats.sh
+29 −707 src/assert.bash
+42 −0 src/assert_equal.bash
+86 −0 src/assert_failure.bash
+298 −0 src/assert_line.bash
+42 −0 src/assert_not_equal.bash
+247 −0 src/assert_output.bash
+54 −0 src/assert_regex.bash
+47 −0 src/assert_success.bash
+42 −0 src/refute.bash
+341 −0 src/refute_line.bash
+243 −0 src/refute_output.bash
+64 −0 src/refute_regex.bash
+0 −50 test/50-assert-12-assert_equal.bats
+0 −36 test/50-assert-13-assert_success.bats
+0 −69 test/50-assert-14-assert_failure.bats
+0 −242 test/50-assert-15-assert_output.bats
+8 −7 test/assert.bats
+62 −0 test/assert_equal.bats
+94 −0 test/assert_failure.bats
+183 −139 test/assert_line.bats
+57 −0 test/assert_not_equal.bats
+306 −0 test/assert_output.bats
+97 −0 test/assert_regex.bats
+308 −0 test/assert_stderr.bats
+374 −0 test/assert_stderr_line.bats
+58 −0 test/assert_success.bats
+7 −7 test/refute.bats
+170 −147 test/refute_line.bats
+126 −83 test/refute_output.bats
+108 −0 test/refute_regex.bats
+252 −0 test/refute_stderr.bats
+366 −0 test/refute_stderr_line.bats
+27 −7 test/test_helper.bash