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
24 changes: 21 additions & 3 deletions git-open
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ git open [remote] [branch]
https://github.com/paulirish/git-open/

Available options are
c,commit! open current commit
c,commit? open current commit or specific hash
i,issue! open issues page
s,suffix= append this suffix
f,file= append this file
Expand All @@ -29,15 +29,33 @@ SUBDIRECTORY_OK='Yes' . "$(git --exec-path)/git-sh-setup"

# Defaults
is_commit=0
commit_hash=""
is_issue=0
protocol="https"
print_only=0
suffix_flag=""
file_flag=""

# parse arguments
# git-sh-setup's parseopt is very limited, so we handle commit hash manually if provided
# check if the first arg after options or if any arg looks like a hash when --commit is used
# but git-sh-setup already consumed some? no, we call it after.

# Actually, let's just use a simple loop before git-sh-setup if we want to be fancy,
# but git-sh-setup is meant to handle the OPTIONS_SPEC.

# Let's try to see if we can just use the arguments directly.
# git-sh-setup's `eval "$(echo "$OPTIONS_SPEC" | git rev-parse --parseopt -- "$@")"` is what people usually do.

while test $# != 0; do
case "$1" in
--commit) is_commit=1;;
--commit)
is_commit=1
;;
--commit=*)
is_commit=1
commit_hash="${1#*=}"
;;
--issue) is_issue=1;;
--suffix=*) suffix_flag="$1";;
--file=*) file_flag="$1";;
Expand Down Expand Up @@ -259,7 +277,7 @@ fi
openurl="$protocol://$domain/$urlpath"

if (( is_commit )); then
sha=$(git rev-parse HEAD)
sha=${commit_hash:-$(git rev-parse HEAD)}
openurl="$openurl/commit/$sha"
elif [[ $remote_ref != "master" || "$file" ]]; then
# simplify URL for master
Expand Down
6 changes: 6 additions & 0 deletions test/git-open.bats
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,12 @@ setup() {
assert_output "https://github.com/paulirish/git-open/commit/${sha}"
}

@test "gh: git open --commit=HASH" {
git remote set-url origin "github.com:paulirish/git-open.git"
run ../git-open "--commit=abcdef1234567890"
assert_output "https://github.com/paulirish/git-open/commit/abcdef1234567890"
}

@test "gh: git open --suffix anySuffix" {
run ../git-open "--suffix" "anySuffix"
assert_output "https://github.com/paulirish/git-open/anySuffix"
Expand Down