From 9b7cb36c2f7251d9eb1d4ca47351d03c20844534 Mon Sep 17 00:00:00 2001 From: Sam Saccone Date: Thu, 12 Feb 2026 15:49:52 -0800 Subject: [PATCH] feat: add support for opening a specific commit with --Commit/-C Co-authored-by: Sam Bo --- git-open | 17 +++++++++++++++- test/git-open.bats | 38 +++++++++++++++++++++++++++++++++++ test/test_helper/bats-assert | 2 +- test/test_helper/bats-support | 2 +- 4 files changed, 56 insertions(+), 3 deletions(-) diff --git a/git-open b/git-open index 7dcae92..342a93e 100755 --- a/git-open +++ b/git-open @@ -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";; @@ -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 diff --git a/test/git-open.bats b/test/git-open.bats index 887d6a7..251cf1a 100755 --- a/test/git-open.bats +++ b/test/git-open.bats @@ -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" diff --git a/test/test_helper/bats-assert b/test/test_helper/bats-assert index 9f88b42..697471b 160000 --- a/test/test_helper/bats-assert +++ b/test/test_helper/bats-assert @@ -1 +1 @@ -Subproject commit 9f88b4207da750093baabc4e3f41bf68f0dd3630 +Subproject commit 697471b7a89d3ab38571f38c6c7c4b460d1f5e35 diff --git a/test/test_helper/bats-support b/test/test_helper/bats-support index 004e707..0954abb 160000 --- a/test/test_helper/bats-support +++ b/test/test_helper/bats-support @@ -1 +1 @@ -Subproject commit 004e707638eedd62e0481e8cdc9223ad471f12ee +Subproject commit 0954abb9925cad550424cebca2b99255d4eabe96