-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync_git.sh
executable file
·151 lines (135 loc) · 4.47 KB
/
sync_git.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
#!/bin/bash
# * @author Rainy Sia <[email protected]>
# * @createTime 2016-10-14 13:35:43
# * @lastChange 2016-11-18 19:04:55
#*
set +e
tmp_file='/tmp/_gitfolderarr.txt'
if [ -z "$1" ]; then
current_path=`pwd`'/'
else
pre_path=$1
if [ "${pre_path:0:1}" = "/" ]; then
current_path=$1
else
current_path=`pwd`/$1
fi
fi
declare git_proj_folders git_folder_branch git_remote_project
git_proj_folders[1]='project1'
git_proj_folders[2]='project2'
git_proj_folders[3]='project3'
git_proj_folders[4]='project4'
git_folder_branch[1]='master'
git_folder_branch[2]='develop'
git_folder_branch[3]='stable'
git_folder_branch[4]='master'
git_remote_project[1]='origin'
git_remote_project[2]='origin'
git_remote_project[3]='upstream'
git_remote_project[4]='upstream'
# Default to git pull with FF merge in quiet mode
git_command_pull="git pull --quiet"
git_command_update="git remote update"
git_command_merge="git merge "
git_command_stash_save="git stash save"
git_command_stash_pop="git stash pop"
git_command_checkout="git checkout "
git_command_fetch="git fetch "
git_command_br="git branch | awk '{ print NR=$2 }'"
git_local_sha="git rev-parse --verify HEAD"
git_remote_sha="git rev-parse --verify FETCH_HEAD"
git_command_space=" "
# User messages
GU_ERR_PROJ="The_git_project_don't_exist."
GU_ERR_NO_GIT="This_directory_has_not_been_initialized_with_Git."
GU_ERR_FETCH_FAIL="Unable_to_fetch_the_remote_repository."
GU_ERR_UPDATE_FAIL="Unable_to_update_the_local_repository."
GU_ERR_UNKNOWN="Unknown_error"
GU_ERR_NO_RIGHT="Sorry,_but_we_got_Permission_denied."
GU_INFO_CURRENT="The_local_repository_is_current._No_update_is_needed."
GU_INFO_SUCCESS="Update_complete."
pull_all() {
if [[ -f $tmp_file ]]; then
rm -f $tmp_file
fi
for i in "${!git_proj_folders[@]}"
do
#printf "%s\t%s\n" "$i" "${git_proj_folders[$i]}"
proj_name="${git_proj_folders[$i]}"
proj_folder="$current_path""${proj_name}"
proj_info_no=0
proj_info=''
proj_style=''
if [ ! -d "$proj_folder" ]; then
proj_info_no=1
else
cd "$proj_folder"
if [[ ! -d ".git" ]]; then
proj_info_no=2
else
cur_branch=`git branch | awk '{ print NR=$2 }'`
cur_err=`$git_command_stash_save 2>&1 /dev/null`
if [ $? ]; then
if [ $(expr substr "$cur_err" 1 2) == "No" ]; then
${git_command_fetch}${git_remote_project[$i]}${git_command_space}${git_folder_branch[$i]}
${git_command_checkout}${git_folder_branch[$i]}
git_run
${git_command_checkout}${cur_branch}
${git_command_stash_pop}
elif [ $(expr substr "$cur_err" 1 2) == "fa" ]; then
proj_info_no=4
else
proj_info_no=5
fi
else
proj_info_no=10
fi
fi
cd $current_path
fi
if [[ "$proj_info_no" -eq 1 ]]; then
proj_info=${GU_ERR_PROJ}
proj_style="\033[1;31m[%s]\e[0m: %s\n"
elif [[ "$proj_info_no" -eq 2 ]]; then
proj_info=${GU_ERR_NO_GIT}
proj_style="\033[1;31m[%s]\e[0m: %s\n"
elif [[ "$proj_info_no" -eq 3 ]]; then
proj_info=${GU_INFO_CURRENT}
proj_style="\033[1;32m[%s]\e[0m: %s\n"
elif [[ "$proj_info_no" -eq 4 ]]; then
proj_info=${GU_ERR_NO_RIGHT}
proj_style="\033[1;31m[%s]\e[0m: %s\n"
elif [[ "$proj_info_no" -eq 5 ]]; then
proj_info=${GU_ERR_UNKNOWN}
proj_style="\033[1;31m[%s]\e[0m: %s\n"
elif [[ "$proj_info_no" -eq 6 ]]; then
proj_info=${GU_ERR_UPDATE_FAIL}
proj_style="\033[1;32m[%s]\e[0m: %s\n"
else
proj_info=${GU_INFO_SUCCESS}
proj_style="\033[1;32m[%s]\e[0m: %s\n"
fi
printf "${proj_style}" ${proj_name} ${proj_info}
done
}
echon() {
echo -e "\n"
}
git_run() {
LOCAL_SHA=$(git rev-parse --verify HEAD)
REMOTE_SHA=$(git rev-parse --verify FETCH_HEAD)
if [ $LOCAL_SHA = $REMOTE_SHA ]; then
proj_info_no=3
else
$git_command_merge${git_remote_project[$i]}/${git_folder_branch[$i]}
if (( $? )); then
proj_info_no=6
else
proj_info_no=10
fi
fi
}
echon
pull_all
echon