Saša Živkov | 40839ea | 2018-03-07 19:51:44 +0100 | [diff] [blame] | 1 | #!/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 | |
| 17 | usage() { |
| 18 | me=`basename "$0"` |
| 19 | echo >&2 "Usage: $me [open] [-b branch] [path]" |
| 20 | exit 1 |
| 21 | } |
| 22 | |
| 23 | cmd_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 | |
| 38 | URL=$(git config --get gitiles.url) |
| 39 | |
| 40 | if test -z "$URL" ; then |
| 41 | echo >&2 "gitiles.url must be set in .git/config" |
| 42 | exit 1 |
| 43 | fi |
| 44 | |
| 45 | while 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 |
| 64 | done |
| 65 | |
| 66 | if test -z "$CMD" ; then |
| 67 | CMD=echo |
| 68 | fi |
| 69 | |
| 70 | if test -z "$B" ; then |
| 71 | B=$(git rev-parse HEAD) |
| 72 | fi |
| 73 | |
| 74 | URL="$URL/+/$B" |
| 75 | |
| 76 | if test -z "$P" ; then |
| 77 | P=$(git rev-parse --show-prefix) |
| 78 | elif test ${P:0:2} = "./" ; then |
| 79 | P=$(git rev-parse --show-prefix)${P:2} |
| 80 | fi |
| 81 | |
| 82 | URL="$URL/$P" |
| 83 | |
| 84 | $CMD $URL |