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