We often use gitiles links to point to some source code which is
relevant for a discussion or code review. It often requires 10-15
mouse clicks in order to navigate to a path in gitiles and generate a
link. This is cumbersome and may discourage providing links to gitiles.
This script intends to provide a fast way to generate and open gitiles
links. It requires a one time setup of gitiles.url in .git/config file:
  [gitiles]
    url = https://gerrit.googlesource.com/gerrit
Once gitiles.url is setup we can generate gitiles URLs easily.
Examples
========
* print gitiles URL for the current HEAD and current directory:
  $ gitiles
  https://gerrit.googlesource.com/gerrit/+/d685ac1193e086b896cfc019ef4504d1b7ce455b/
* print gitiles URL for current HEAD and Documentation directory:
  $ gitiles Documentation
* print gitiles URL for branch stable-2.15 and current directory:
  $ gitiles -b stable-2.15
* print gitiles URL for current HEAD and a path relative to the current directory:
  $ pwd
  gerrit-server
  $ gitiles ./src/main/resources
The script also supports "open" command which open gitiles URL using
OS's native way of opening URLs:
  $ gitiles open
  $ gitiles open -b stable-2.15 Documentation
Change-Id: Icd31723711535e886313d3d17afb57caa4a710b1
		
	
		
			
				
	
	
		
			85 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			Bash
		
	
	
		
			Executable File
		
	
	
	
	
			
		
		
	
	
			85 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			Bash
		
	
	
		
			Executable File
		
	
	
	
	
#!/bin/bash
 | 
						|
#
 | 
						|
# Copyright (C) 2018 The Android Open Source Project
 | 
						|
#
 | 
						|
# Licensed under the Apache License, Version 2.0 (the "License");
 | 
						|
# you may not use this file except in compliance with the License.
 | 
						|
# You may obtain a copy of the License at
 | 
						|
#
 | 
						|
# http://www.apache.org/licenses/LICENSE-2.0
 | 
						|
#
 | 
						|
# Unless required by applicable law or agreed to in writing, software
 | 
						|
# distributed under the License is distributed on an "AS IS" BASIS,
 | 
						|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
						|
# See the License for the specific language governing permissions and
 | 
						|
# limitations under the License.
 | 
						|
 | 
						|
usage() {
 | 
						|
  me=`basename "$0"`
 | 
						|
  echo >&2 "Usage: $me [open] [-b branch] [path]"
 | 
						|
  exit 1
 | 
						|
}
 | 
						|
 | 
						|
cmd_open() {
 | 
						|
  case "$(uname)" in
 | 
						|
    Darwin)
 | 
						|
      echo "open"
 | 
						|
      ;;
 | 
						|
    Linux)
 | 
						|
      echo "xdg-open"
 | 
						|
      ;;
 | 
						|
 | 
						|
    *)
 | 
						|
      echo >&2 "Don't know how to open URLs on $(uname)"
 | 
						|
      exit 1
 | 
						|
  esac
 | 
						|
}
 | 
						|
 | 
						|
URL=$(git config --get gitiles.url)
 | 
						|
 | 
						|
if test -z "$URL" ; then
 | 
						|
  echo >&2 "gitiles.url must be set in .git/config"
 | 
						|
  exit 1
 | 
						|
fi
 | 
						|
 | 
						|
while test $# -gt 0 ; do
 | 
						|
  case "$1" in
 | 
						|
  open)
 | 
						|
    CMD=$(cmd_open)
 | 
						|
    shift
 | 
						|
    ;;
 | 
						|
  -b|--branch)
 | 
						|
    shift
 | 
						|
    B=$1
 | 
						|
    shift
 | 
						|
    ;;
 | 
						|
  -h|--help)
 | 
						|
    usage
 | 
						|
    ;;
 | 
						|
 | 
						|
  *)
 | 
						|
    P=$1
 | 
						|
    shift
 | 
						|
  esac
 | 
						|
done
 | 
						|
 | 
						|
if test -z "$CMD" ; then
 | 
						|
  CMD=echo
 | 
						|
fi
 | 
						|
 | 
						|
if test -z "$B" ; then
 | 
						|
  B=$(git rev-parse HEAD)
 | 
						|
fi
 | 
						|
 | 
						|
URL="$URL/+/$B"
 | 
						|
 | 
						|
if test -z "$P" ; then
 | 
						|
  P=$(git rev-parse --show-prefix)
 | 
						|
elif test ${P:0:2} = "./" ; then
 | 
						|
  P=$(git rev-parse --show-prefix)${P:2}
 | 
						|
fi
 | 
						|
 | 
						|
URL="$URL/$P"
 | 
						|
 | 
						|
$CMD $URL
 |