From 5ce8749892adc10839ebc5d785f8217ce6147309 Mon Sep 17 00:00:00 2001 From: Brian Wellington Date: Tue, 25 Aug 2020 16:36:51 -0700 Subject: [PATCH 1/2] Add --file option. --- git-open | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/git-open b/git-open index 1fcc655..197b1d9 100755 --- a/git-open +++ b/git-open @@ -19,6 +19,7 @@ git open [remote] [branch] c,commit! open current commit i,issue! open issues page s,suffix= append this suffix +f,file= append this file p,print! just print the url " @@ -32,12 +33,14 @@ is_issue=0 protocol="https" print_only=0 suffix_flag="" +file_flag="" while test $# != 0; do case "$1" in --commit) is_commit=1;; --issue) is_issue=1;; --suffix=*) suffix_flag="$1";; + --file=*) file_flag="$1";; --print) print_only=1;; --) shift; break ;; esac @@ -49,6 +52,11 @@ IFS='=' read -ra suffix_flag <<< "$suffix_flag" function join_by { local IFS="$1"; shift; echo "$*"; } suffix=$(join_by "=" "${suffix_flag[@]:1}") +# parse file from file=value +IFS='=' read -ra file_flag <<< "$file_flag" +function join_by { local IFS="$1"; shift; echo "$*"; } +file=$(join_by "=" "${file_flag[@]:1}") + # are we in a git repo? if ! git rev-parse --is-inside-work-tree &>/dev/null; then echo "Not a git repository." 1>&2 @@ -240,11 +248,20 @@ openurl="$protocol://$domain/$urlpath" if (( is_commit )); then sha=$(git rev-parse HEAD) openurl="$openurl/commit/$sha" -elif [[ $remote_ref != "master" ]]; then +elif [[ $remote_ref != "master" || "$file" ]]; then # simplify URL for master openurl="$openurl$providerBranchRef" fi +if [ "$file" ]; then + absfile=$(git ls-tree --full-name --name-only $branch $file) + if [[ -z "$absfile" ]]; then + echo "File $file is not in repository" 1>&2 + exit 1 + fi + openurl="$openurl/$absfile" +fi + if [ "$suffix" ]; then openurl="$openurl/$suffix" fi From 8ca8e1bad29ebae935ba074c5628a51f1877867d Mon Sep 17 00:00:00 2001 From: Brian Wellington Date: Sat, 29 Aug 2020 14:29:15 -0700 Subject: [PATCH 2/2] Remove duplicate function definition. --- git-open | 1 - 1 file changed, 1 deletion(-) diff --git a/git-open b/git-open index 197b1d9..2f23b0b 100755 --- a/git-open +++ b/git-open @@ -54,7 +54,6 @@ suffix=$(join_by "=" "${suffix_flag[@]:1}") # parse file from file=value IFS='=' read -ra file_flag <<< "$file_flag" -function join_by { local IFS="$1"; shift; echo "$*"; } file=$(join_by "=" "${file_flag[@]:1}") # are we in a git repo?