Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --file option. #170

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 18 additions & 1 deletion git-open
Original file line number Diff line number Diff line change
Expand Up @@ -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
"

Expand All @@ -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
Expand All @@ -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 "$*"; }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you redefining this?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The join_by function definition was cut and pasted, and could probably be removed.

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
Expand Down Expand Up @@ -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
Expand Down