-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathup.sh
executable file
·99 lines (88 loc) · 2.09 KB
/
up.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/bin/bash
TMPLOG=$(mktemp /tmp/up.XXXXXXXXXX)
if [[ $1 == "-v" ]]; then
VERBOSE=1
shift
fi
function _msg {
if [[ $VERBOSE -eq 1 ]]; then
echo -e $@
fi
}
function _run {
if [[ $2 -eq 1 ]]; then
$(eval $1)
else
:> $TMPLOG
eval $1 > $TMPLOG 2>&1
if [ $? -ne 0 ]; then
echo $(cat $TMPLOG)
fi
fi
}
function run {
RUNCMD=$1
RUNDIR=$2
_msg "Running '$RUNCMD' on $(pwd)/$RUNDIR"
_run 'pushd $RUNDIR'
_run 'eval $RUNCMD'
_run 'popd'
}
function git_branch () {
pushd $1 > /dev/null
gitbranch=$(git symbolic-ref HEAD 2> /dev/null)
gitbranch=${gitbranch#refs/heads/}
popd > /dev/null
echo $gitbranch
}
function update_directory() {
DIR=$1
IS_REPOSITORY=1
echo "#### Working on ${PWD}/${DIR} ####"
if [ -f "${DIR}/.git/refs/remotes/git-svn" ]; then
run "git svn rebase" ${DIR}
elif [ -d "${DIR}/.git/refs/remotes/origin" ]; then
CUR_BRANCH=$(git_branch ${DIR})
if [[ x$CUR_BRANCH != "xmaster" ]]; then
_msg "Changing current branch from ${CUR_BRANCH} to master"
run "git checkout master" ${DIR}
fi
run "git pull" ${DIR}
if [[ x$CUR_BRANCH != "xmaster" ]]; then
run "git checkout $CUR_BRANCH" ${DIR}
run "git pull" ${DIR}
fi
elif [ -d "${DIR}/.hg/" ]; then
run "hg pull" ${DIR}
run "hg up" ${DIR}
elif [ -d "${DIR}/.svn/" ]; then
run "svn up" ${DIR}
else
IS_REPOSITORY=0
fi
return $IS_REPOSITORY
}
function update_directory_rec() {
if [ $# -eq 0 ]; then
return
fi
local head=$1
shift 1
local tail=$@
_msg -------------------------------
_msg head=$head
_msg tail=$tail
if [ -d $head ]; then
update_directory $head
if [ $? -ne 1 ]; then
pushd $head > /dev/null
update_directory_rec $(ls)
popd > /dev/null
fi
fi
update_directory_rec $tail
}
if update_directory '.'; then
update_directory_rec $(ls)
fi
rm -f $TMPLOG