akaros/scripts/git/git-track
<<
>>
Prefs
   1#!/bin/bash
   2# Barret Rhoden (brho@cs.berkeley.edu)
   3# Copyright 2016 Google Inc
   4#
   5# Tracks a branch in a remote repo.
   6#
   7# If the remote is already set up, then you only need to pass it remote/branch.
   8# If the remote is new, you will need to pass the URL for the repo.
   9
  10
  11usage()
  12{
  13        echo "$0 <remote>/<branch> [<url>]"
  14        exit -1
  15}
  16
  17if [ $# -lt 1 ]
  18then
  19        usage
  20fi
  21
  22REMOTE=`echo $1 | cut -f 1 -d '/'`
  23BRANCH=`echo $1 | cut -f 2- -d '/'`
  24
  25git remote | grep $REMOTE > /dev/null
  26if [ $? -ne 0 ]
  27then
  28        if [ $# -ne 2 ]
  29        then
  30                echo "Remote $REMOTE not tracked yet and no URL passed"
  31                echo "Try again with the repo's URL or add the remote manually"
  32                usage
  33        fi
  34        URL=$2
  35        git remote add -t $BRANCH $REMOTE $URL
  36else
  37        git remote set-branches --add $REMOTE $BRANCH
  38fi
  39
  40git fetch $REMOTE
  41