blob: 3e603b9af900e3e2e2567de29a4395e4cec6ad3d [file] [log] [blame]
Saša Živkov40839ea2018-03-07 19:51:44 +01001#!/bin/bash
2#
3# Copyright (C) 2018 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17usage() {
18 me=`basename "$0"`
19 echo >&2 "Usage: $me [open] [-b branch] [path]"
20 exit 1
21}
22
23cmd_open() {
24 case "$(uname)" in
25 Darwin)
26 echo "open"
27 ;;
28 Linux)
29 echo "xdg-open"
30 ;;
31
32 *)
33 echo >&2 "Don't know how to open URLs on $(uname)"
34 exit 1
35 esac
36}
37
38URL=$(git config --get gitiles.url)
39
40if test -z "$URL" ; then
41 echo >&2 "gitiles.url must be set in .git/config"
42 exit 1
43fi
44
45while test $# -gt 0 ; do
46 case "$1" in
47 open)
48 CMD=$(cmd_open)
49 shift
50 ;;
51 -b|--branch)
52 shift
53 B=$1
54 shift
55 ;;
56 -h|--help)
57 usage
58 ;;
59
60 *)
61 P=$1
62 shift
63 esac
64done
65
66if test -z "$CMD" ; then
67 CMD=echo
68fi
69
70if test -z "$B" ; then
71 B=$(git rev-parse HEAD)
72fi
73
74URL="$URL/+/$B"
75
76if test -z "$P" ; then
77 P=$(git rev-parse --show-prefix)
78elif test ${P:0:2} = "./" ; then
79 P=$(git rev-parse --show-prefix)${P:2}
80fi
81
82URL="$URL/$P"
83
84$CMD $URL