revert last
[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         msg $COMMAND
27     fi
28     REPO=$2
29
30     pushd ${REPO} > /dev/null
31     eval $COMMAND
32     popd > /dev/null
33 }
34
35 function mirror () {
36     for arg in "$@" ; do
37         NAME=$(echo ${arg} | sed 's/\/$//')
38         GIT_NAME=${NAME}.git
39         REPO_DIR=${LOCAL_MIRROR_DIR}/${NAME}
40         MIRROR_REPO=${MIRROR_GIT}/${GIT_NAME}
41         MASTER_REPO=${MASTER_GIT}/${GIT_NAME}
42
43         if [ -d ${REPO_DIR} ]
44         then
45             msg "pulling from ${REPO_NAME}"
46             run "git fetch origin --tags" ${REPO_DIR}
47             run "git fetch origin" ${REPO_DIR}
48             run "git merge --ff origin/master" ${REPO_DIR}
49             if [ $? -ne 0 ]
50             then
51                 error "Can not fetch from ${MASTER_REPO}"
52             fi
53         else
54             msg "mirroring in ${REPO_NAME} for the first time"
55             run "git clone ${MIRROR_REPO}" ${LOCAL_MIRROR_DIR}
56             run "git remote add local_master ${MASTER_REPO}" ${REPO_DIR}
57         fi
58
59         msg "pushing ${REPO_NAME} to local master"
60         run "git fetch local_master --tags" ${REPO_DIR}
61         run "git fetch local_master" ${REPO_DIR}
62         run "git merge --ff local_master/master" ${REPO_DIR}
63         if [ $? -ne 0 ]
64         then
65             error "Can not fetch from ${MIRROR_REPO}"
66         fi
67
68         run "git push local_master" ${REPO_DIR}
69         run "git push --tags local_master" ${REPO_DIR}
70     done
71 }
72
73
74 while getopts ":hq" opt
75 do
76   case $opt in
77       q)
78           QUIET=1
79           break
80           ;;
81       h)
82           echo "USAGE: $0 [-q] REPONAME*"
83           exit 1
84           ;;
85       \?)
86           echo "Invalid option: -$OPTARG" >&2
87           ;;
88   esac
89 done
90
91 shift $((OPTIND-1))
92 mirror $@
93