f37 -> f39
[infrastructure.git] / scripts / git-update.sh
1 #!/bin/bash
2
3 # uses the directory where the command lies, so just symlink this script wherever it's needed
4 #
5 # based on auto-update that historically was guessing the scm in use
6 # this one is git-specific, it does the usual git pull 
7 # but also exposes all remote branches as local
8
9
10 ####################
11 COMMAND=$(basename $0)
12 # without an argument, use the place where the command is stored
13 if [[ -z "$@" ]] ; then
14     DIRNAME=$(dirname $0)
15     DIRNAME=$(cd $DIRNAME; pwd -P)
16 else
17     DIRNAME="$1" ; shift
18 fi
19
20 ##########
21 cd $DIRNAME
22
23 ### xxx ideally we should not need to git checkout anything
24 # which would in addition remove the need for a non-bare repo
25 # this would take a bit more time though
26
27 # make sure we keep track of the current branch upon exit
28 function current_branch () {
29     git branch | grep '^\*' | cut -d' ' -f2
30 }
31
32 function all_branches () {
33     remote=$1; shift
34     git branch -r | grep $remote | fgrep -v -- '->' | sed "s/.*\///g"
35 }
36     
37
38 function git_update () {
39     current_branch=$(current_branch)
40     echo Running $COMMAND
41     remote=origin
42     for branch in $(all_branches $remote); do
43         # create the branch as a tracking branch if not yet existing
44         git branch | grep -q ' '$branch'$' || git branch --track $branch $remote/$branch
45         git checkout $branch
46         git merge --ff $remote/$branch
47     done
48     git checkout $current_branch
49 }
50
51 git_update >& .git-update.log