try starting from scratch for every sync
[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 "mirroring in ${REPO_NAME} for the first time"
60         run "rm -rf ${REPO_DIR}" ${LOCAL_MIRROR_DIR}
61         run "git clone ${MIRROR_REPO}" ${LOCAL_MIRROR_DIR}
62         run "git remote add local_master ${MASTER_REPO}" ${REPO_DIR}
63
64         msg "pushing ${REPO_NAME} to local master"
65         run "git fetch local_master --tags" ${REPO_DIR}
66         run "git fetch local_master" ${REPO_DIR}
67         run "git merge --ff local_master/master" ${REPO_DIR}
68         if [ $? -ne 0 ]
69         then
70             error "Can not fetch from ${MIRROR_REPO}"
71         fi
72
73         run "git push local_master" ${REPO_DIR}
74         run "git push --tags local_master" ${REPO_DIR}
75     done
76 }
77
78
79 while getopts ":hq" opt
80 do
81   case $opt in
82       q)
83           QUIET=1
84           break
85           ;;
86       h)
87           echo "USAGE: $0 [-q] REPONAME*"
88           exit 1
89           ;;
90       \?)
91           echo "Invalid option: -$OPTARG" >&2
92           ;;
93   esac
94 done
95
96 shift $((OPTIND-1))
97 mirror $@
98