Skip to content

Commit dd22397

Browse files
authored
Add --file option. (#170)
1 parent 5d1f938 commit dd22397

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

git-open

+17-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ git open [remote] [branch]
1919
c,commit! open current commit
2020
i,issue! open issues page
2121
s,suffix= append this suffix
22+
f,file= append this file
2223
p,print! just print the url
2324
"
2425

@@ -32,12 +33,14 @@ is_issue=0
3233
protocol="https"
3334
print_only=0
3435
suffix_flag=""
36+
file_flag=""
3537

3638
while test $# != 0; do
3739
case "$1" in
3840
--commit) is_commit=1;;
3941
--issue) is_issue=1;;
4042
--suffix=*) suffix_flag="$1";;
43+
--file=*) file_flag="$1";;
4144
--print) print_only=1;;
4245
--) shift; break ;;
4346
esac
@@ -49,6 +52,10 @@ IFS='=' read -ra suffix_flag <<< "$suffix_flag"
4952
function join_by { local IFS="$1"; shift; echo "$*"; }
5053
suffix=$(join_by "=" "${suffix_flag[@]:1}")
5154

55+
# parse file from file=value
56+
IFS='=' read -ra file_flag <<< "$file_flag"
57+
file=$(join_by "=" "${file_flag[@]:1}")
58+
5259
# are we in a git repo?
5360
if ! git rev-parse --is-inside-work-tree &>/dev/null; then
5461
echo "Not a git repository." 1>&2
@@ -246,11 +253,20 @@ openurl="$protocol://$domain/$urlpath"
246253
if (( is_commit )); then
247254
sha=$(git rev-parse HEAD)
248255
openurl="$openurl/commit/$sha"
249-
elif [[ $remote_ref != "master" ]]; then
256+
elif [[ $remote_ref != "master" || "$file" ]]; then
250257
# simplify URL for master
251258
openurl="$openurl$providerBranchRef"
252259
fi
253260

261+
if [ "$file" ]; then
262+
absfile=$(git ls-tree --full-name --name-only $branch $file)
263+
if [[ -z "$absfile" ]]; then
264+
echo "File $file is not in repository" 1>&2
265+
exit 1
266+
fi
267+
openurl="$openurl/$absfile"
268+
fi
269+
254270
if [ "$suffix" ]; then
255271
openurl="$openurl/$suffix"
256272
fi

0 commit comments

Comments
 (0)