verbose by default
[infrastructure.git] / scripts / git-mirror.sh
1 #!/bin/bash
2
3 MIRROR_GIT="git://git.planet-lab.org"
4 MASTER_GIT="/git"
5 LOCAL_MIRROR_DIR="/git-mirror"
6 QUIET=0
7
8 function msg () {
9     if [ $QUIET -eq 0 ]
10     then
11         echo "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx $1"
12     fi
13 }
14
15 function error () {
16     echo "[ERROR] xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx $1"
17     exit 1
18 }
19
20 function run () {
21     if [ $QUIET -eq 1 ]
22     then
23         COMMAND="$1 &> /dev/null"
24     else
25         COMMAND="$1"
26     fi
27     REPO=$2
28
29     pushd ${REPO} > /dev/null
30     eval $COMMAND
31     popd > /dev/null
32 }
33
34 function mirror () {
35     for arg in "$@" ; do
36         NAME=$(echo ${arg} | sed 's/\/$//')
37         GIT_NAME=${NAME}.git
38         REPO_DIR=${LOCAL_MIRROR_DIR}/${NAME}
39         MIRROR_REPO=${MIRROR_GIT}/${GIT_NAME}
40         MASTER_REPO=${MASTER_GIT}/${GIT_NAME}
41
42         if [ -d ${REPO_DIR} ]
43         then
44             msg "pulling from ${REPO_NAME}"
45             run "git fetch origin --tags" ${REPO_DIR}
46             run "git fetch origin" ${REPO_DIR}
47             run "git merge origin/master" ${REPO_DIR}
48             if [ $? -ne 0 ]
49             then
50                 error "Can not fetch from ${MASTER_REPO}"
51             fi
52         else
53             msg "mirroring in ${REPO_NAME} for the first time"
54             run "git clone ${MIRROR_REPO}" ${LOCAL_MIRROR_DIR}
55             run "git remote add local_master ${MASTER_REPO}" ${REPO_DIR}
56         fi
57
58         msg "pushing ${REPO_NAME} to local master"
59         run "git fetch local_master --tags" ${REPO_DIR}
60         run "git fetch local_master" ${REPO_DIR}
61         run "git merge local_master/master" ${REPO_DIR}
62         if [ $? -ne 0 ]
63         then
64             error "Can not fetch from ${MIRROR_REPO}"
65         fi
66
67         run "git push local_master" ${REPO_DIR}
68         run "git push --tags local_master" ${REPO_DIR}
69     done
70 }
71
72
73 while getopts ":hq" opt
74 do
75   case $opt in
76       q)
77           QUIET=1
78           break
79           ;;
80       h)
81           echo "USAGE: $0 [-q] REPONAME*"
82           exit 1
83           ;;
84       \?)
85           echo "Invalid option: -$OPTARG" >&2
86           ;;
87   esac
88 done
89
90 shift $((OPTIND-1))
91 mirror $@
92