From 0564817aef979c5ddee758f3a956e0dddcb7ad97 Mon Sep 17 00:00:00 2001 From: Domenico Gemoli Date: Fri, 28 Jun 2019 11:06:47 +0200 Subject: [PATCH] Adding a --commit option. I've only tested this with gitlab seeing as I needed it there. If this could be useful I can integrate any feedback you might have to get this proposal merged. --- git-open | 10 ++++++++-- git-open.1.md | 14 +++++++++++++- test/git-open.bats | 7 +++++++ 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/git-open b/git-open index 86c4dd3..490dbf8 100755 --- a/git-open +++ b/git-open @@ -16,6 +16,7 @@ git open [remote] [branch] https://github.com/paulirish/git-open/ Available options are +c,commit! open current commit i,issue! open issues page p,print! just print the url " @@ -25,12 +26,14 @@ p,print! just print the url SUBDIRECTORY_OK='Yes' . "$(git --exec-path)/git-sh-setup" # Defaults +is_commit=0 is_issue=0 protocol="https" print_only=0 while test $# != 0; do case "$1" in + --commit) is_commit=1;; --issue) is_issue=1;; --print) print_only=1;; --) shift; break ;; @@ -227,8 +230,11 @@ fi openurl="$protocol://$domain/$urlpath" -# simplify URL for master -if [[ $remote_ref != "master" ]]; then +if (( is_commit )); then + sha=$(git rev-parse HEAD) + openurl="$openurl/commit/$sha" +elif [[ $remote_ref != "master" ]]; then + # simplify URL for master openurl="$openurl$providerBranchRef" fi diff --git a/git-open.1.md b/git-open.1.md index b0a32ca..cb04504 100644 --- a/git-open.1.md +++ b/git-open.1.md @@ -3,7 +3,7 @@ ## SYNOPSIS -`git open` [--issue] [remote-name] [branch-name] +`git open` [--issue] [--commit] [remote-name] [branch-name] ## DESCRIPTION @@ -14,6 +14,10 @@ git hosting services are supported. ## OPTIONS +`-c`, `--commit` + Open the current commit. See `EXAMPLES` for more information. + Only tested with GitHub & GitLab. + `-i`, `--issue` Open the current issue. When the name of the current branch matches the right pattern, it will open the webpage with that issue. See `EXAMPLES` for more information. @@ -58,6 +62,14 @@ git open --print It prints the URL https://github.com/TRACKED_REMOTE_USER/CURRENT_REPO/ +```sh +git open --commit +``` + +Supposing that the current sha is `2ddc8d4548d0cee3d714dcf0068dbec5b168a9b2`, it opens +https://github.com/TRACKED_REMOTE_USER/CURRENT_REPO/commit/2ddc8d4548d0cee3d714dcf0068dbec5b168a9b2 + + ## SUPPORTED GIT HOSTING SERVICES git-open can automatically guess the corresponding repository page for remotes diff --git a/test/git-open.bats b/test/git-open.bats index 7d03216..d8dfadc 100755 --- a/test/git-open.bats +++ b/test/git-open.bats @@ -160,6 +160,13 @@ setup() { assert_output "https://github.com/paulirish/git-open/issues/38" } +@test "gh: git open --commit" { + git remote set-url origin "github.com:paulirish/git-open.git" + sha=$(git rev-parse HEAD) + run ../git-open "--commit" + assert_output "https://github.com/paulirish/git-open/commit/${sha}" +} + @test "gh: gist" { git remote set-url origin "git@gist.github.com:2d84a6db1b41b4020685.git" run ../git-open