This commit is contained in:
Sam Saccone 2026-06-16 17:09:58 +00:00 committed by GitHub
commit 9978fa140a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 56 additions and 3 deletions

View file

@ -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
@ -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
@ -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";;
@ -267,7 +275,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

View file

@ -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"

@ -1 +1 @@
Subproject commit 9f88b4207da750093baabc4e3f41bf68f0dd3630
Subproject commit 697471b7a89d3ab38571f38c6c7c4b460d1f5e35

@ -1 +1 @@
Subproject commit 004e707638eedd62e0481e8cdc9223ad471f12ee
Subproject commit 0954abb9925cad550424cebca2b99255d4eabe96