tweaked subject layout
[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 }
18
19 function run () {
20     if [ $QUIET -eq 1 ]
21     then
22         COMMAND="$1 &> /dev/null"
23     else
24         COMMAND="$1"
25         msg $COMMAND
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=$(basename ${arg} | sed s/.git$//g)
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 there is no remote repository it may be that we only have
43         # the repository locally and don't need to mirror
44         git ls-remote $MIRROR_REPO &> /dev/null
45         if [ $? -eq 0 ]
46         then
47             if [ -d ${REPO_DIR} ]
48             then
49                 msg "pulling from ${NAME}"
50                 run "git fetch origin --tags" ${REPO_DIR}
51                 run "git fetch origin" ${REPO_DIR}
52                 run "git merge --ff origin/master" ${REPO_DIR}
53                 if [ $? -ne 0 ]
54                 then
55                     error "Can not fetch from ${MASTER_REPO}"
56                 fi
57             else
58                 msg "mirroring ${NAME} for the first time"
59                 run "git clone ${MIRROR_REPO}" ${LOCAL_MIRROR_DIR}
60                 run "git remote add local_master ${MASTER_REPO}" ${REPO_DIR}
61             fi
62
63             msg "pushing ${NAME} to local master"
64             run "git fetch local_master --tags" ${REPO_DIR}
65             run "git fetch local_master" ${REPO_DIR}
66             run "git merge --ff local_master/master" ${REPO_DIR}
67             if [ $? -ne 0 ]
68             then
69                 error "Can not fetch from ${MIRROR_REPO}"
70             else
71                 run "git push local_master" ${REPO_DIR}
72                 run "git push --tags local_master" ${REPO_DIR}
73             fi
74         fi
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