-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpu.sh
executable file
·168 lines (156 loc) · 3.8 KB
/
pu.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# Author: Claudio Marforio
# E-mail: [email protected]
# Usage: pu.sh projects_dir
# | $0 | $1 |
#!/bin/bash
if [[ $# != 1 ]]; then
echo "Usage: pu.sh \"projects directory\""
echo "Example: pu.sh ~/Documents/projects/"
exit
fi
# return values for checking which versioning system is in use
RET_VALUE_SVN=250
RET_VALUE_GIT=251
# global variables for summary
declare -a SVN_UPDATE
declare -a SVN_WARNING
declare -a SVN_ERRORS
declare -a GIT_UPDATE
declare -a GIT_WARNING
declare -a GIT_ERRORS
REPO_CHECKED=0
working_chars=('|' '/' '-' '\')
# main dir
function main {
if [[ "$1" == "." || "$1" == "./" ]]; then
main "`pwd`/"
fi
if test -e "$1"; then
for i in `ls -F "$1"`; do
if test -d "$1$i"; then
check_repo "$1$i"
fi
done
else
echo "$1 doesn't exist, exiting"
exit
fi
}
# check_repo dir
function check_repo {
index=$(($REPO_CHECKED % ${#working_chars[@]}))
echo -n -e "\rscript is working: ${working_chars[$index]} checked $REPO_CHECKED repositories"
check_svn "$1"
if [[ $? == $RET_VALUE_SVN ]]; then
REPO_CHECKED=$(($REPO_CHECKED + 1))
#echo -e "\t------ SVN ------"
cd "$1"
changed=`svn st`
if [[ $changed == "" ]]; then
#echo -e "Updating: $1"
update_result=`svn update`
update_worked=`echo $update_result | grep "At revision"`
if [[ $update_worked == "" ]]; then
SVN_UPDATE[${#SVN_UPDATE[@]}]="$1"
fi
#echo $update_result
else
#echo -e "WARNING: $1 contains uncommited changes, please check it manually"
SVN_WARNING[${#SVN_WARNING[@]}]="$1"
fi
#echo -e "\t------ DONE ------\n"
return;
fi
check_git "$1"
if [[ $? == $RET_VALUE_GIT ]]; then
REPO_CHECKED=$(($REPO_CHECKED + 1))
#echo -e "\t------ GIT ------"
cd "$1"
# check if user hasn't changed anything
changed=`git st | grep "nothing to commit"`
if [[ $changed != "" ]]; then
#echo -e "Updating: $1"
pull_st=`git pull 2>/tmp/pu_status`
pull_result=$(</tmp/pu_status)
rm /tmp/pu_status
pull_worked=`echo $pull_st | grep "Already up-to-date."`
if [[ $pull_worked == "" ]]; then
pull_worked=`echo $pull_result | grep "fatal"`
if [[ $pull_worked != "" ]]; then
GIT_ERRORS[${#GIT_ERRORS[@]}]="$1"
else
GIT_UPDATE[${#GIT_UPDATE[@]}]="$1"
fi
fi
#echo $pull_result
else
#echo -e "WARNING: $1 contains uncommited changes, please check it manually"
GIT_WARNING[${#GIT_WARNING[@]}]="$1"
fi
#echo -e "\t------ DONE ------\n"
return;
fi
if [[ $? == 0 ]]; then
main "$1"
fi
}
# check_svn dir
function check_svn {
if test -e "$1/.svn"; then
return $RET_VALUE_SVN
else
return 0
fi
}
# check_git dir
function check_git {
if test -e "$1/.git"; then
return $RET_VALUE_GIT
else
return 0
fi
}
# summary
function summary {
echo -e "\r " #remove the "script is working:"
echo
echo "Repositories checked: $REPO_CHECKED"
echo
if [[ ${#SVN_UPDATE[@]} != 0 ]]; then
echo "-> SVN UPDATED"
for (( i=0; i<${#SVN_UPDATE[@]}; i++ )); do
echo -e "\t ${SVN_UPDATE[$i]}"
done
echo
fi
if [[ ${#GIT_UPDATE[@]} != 0 ]]; then
echo "-> GIT UPDATED"
for (( i=0; i<${#GIT_UPDATE[@]}; i++ )); do
echo -e "\t ${GIT_UPDATE[$i]}"
done
echo
fi
if [[ ${#SVN_WARNING[@]} != 0 ]]; then
echo "-> SVN WARNINGS (check folders, uncommited changes)"
for (( i=0; i<${#SVN_WARNING[@]}; i++ )); do
echo -e "\t ${SVN_WARNING[$i]}"
done
echo
fi
if [[ ${#GIT_WARNING[@]} != 0 ]]; then
echo "-> GIT WARNINGS (check folders, uncommited changes)"
for (( i=0; i<${#GIT_WARNING[@]}; i++ )); do
echo -e "\t ${GIT_WARNING[$i]}"
done
echo
fi
if [[ ${#GIT_ERRORS[@]} != 0 ]]; then
echo "-> GIT ERRORS (git repository non reachable or nonexistent?)"
for (( i=0; i<${#GIT_ERRORS[@]}; i++ )); do
echo -e "\t ${GIT_ERRORS[$i]}"
done
fi
echo
}
main $1 2>/dev/null
summary