-
Notifications
You must be signed in to change notification settings - Fork 2
/
git-sync.sh
198 lines (159 loc) · 5.39 KB
/
git-sync.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
set -euf +x -o pipefail
echo
echo Start `basename $0`
invoke_path="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "$invoke_path/util/set_env.sh" "$@"
echo
source "$path_git_sync_util/repo_create.sh"
cd "$path_sync_repo"
rm -f "$env_notify_del_file"
rm -f "$env_notify_solving_file"
source "$path_git_sync_util/restore-after-crash.sh"
echo
if [[ ! -f "$env_modifications_signal_file" ]]; then
source "$path_git_sync_util/change_detector.sh"
if (( $changes_detected != 1 )); then
echo '@' RESULT: Refs are the same. Exit.
echo
# !!! EXIT !!!
exit
fi
else
echo '@' RESULT: Synchronization requested.
remote_refs_1=$(<"$env_modifications_signal_file_1")
remote_refs_2=$(<"$env_modifications_signal_file_2")
rm -f "$env_modifications_signal_file"
rm -f "$env_modifications_signal_file_1"
rm -f "$env_modifications_signal_file_2"
fi
local_refs_1=$(git for-each-ref --format="%(objectname) %(refname)" "refs/remotes/$origin_1/")
local_refs_2=$(git for-each-ref --format="%(objectname) %(refname)" "refs/remotes/$origin_2/")
refspecs=$(awk \
-f "$path_git_sync_util/state_to_refspec.gawk" \
`# --lint` \
--assign must_exist_branch=$must_exist_branch \
--assign origin_1="$origin_1" \
--assign origin_2="$origin_2" \
--assign prefix_1="$prefix_1" \
--assign prefix_2="$prefix_2" \
--assign trace_on=1 \
<(echo "$remote_refs_1") \
<(echo "$remote_refs_2") \
<(echo "$local_refs_1") \
<(echo "$local_refs_2") \
)
mapfile -t refspec_list < <(echo "$refspecs")
del_spec="${refspec_list[1]}";
fetch_spec1="${refspec_list[2]}";
fetch_spec2="${refspec_list[3]}";
push_spec1="${refspec_list[4]}";
push_spec2="${refspec_list[5]}";
post_fetch_spec1="${refspec_list[6]}";
post_fetch_spec2="${refspec_list[7]}";
notify_del="${refspec_list[8]}";
notify_solving="${refspec_list[9]}";
mkdir -p "$path_async_output"
if [[ -n "$del_spec" ]]; then
echo $'\n>' Delete local branch
echo $del_spec
git branch --delete --force --remotes $del_spec
fi;
if [[ -n "$fetch_spec1" && -n "$fetch_spec2" ]]; then
git fetch --no-tags $origin_1 $fetch_spec1 > "$path_async_output/fetch1.txt" &
pid_fetch1=$!
git fetch --no-tags $origin_2 $fetch_spec2 > "$path_async_output/fetch2.txt" &
pid_fetch2=$!
fetch_report1="> Fetch $origin_1 "
wait $pid_fetch1 && fetch_report1+="(async success)" || fetch_report1+="(async failure)"
fetch_report2+="> Fetch $origin_2 "
wait $pid_fetch2 && fetch_report2+="(async success)" || fetch_report2+="(async failure)"
echo
echo $fetch_report1
echo $fetch_spec1
cat < "$path_async_output/fetch1.txt"
echo
echo $fetch_report2
echo $fetch_spec2
cat < "$path_async_output/fetch2.txt"
else
if [[ -n "$fetch_spec1" ]]; then
echo $'\n>' Fetch $origin_1
echo $fetch_spec1
git fetch --no-tags $origin_1 $fetch_spec1
fi;
if [[ -n "$fetch_spec2" ]]; then
echo $'\n>' Fetch $origin_2
echo $fetch_spec2
git fetch --no-tags $origin_2 $fetch_spec2
fi;
fi;
if [[ -n "$notify_del" ]]; then
install -D /dev/null "$env_notify_del_file"
echo > "$env_notify_del_file"
echo "$notify_del" >> "$env_notify_del_file"
fi;
if [[ -n "$notify_solving" ]]; then
install -D /dev/null "$env_notify_solving_file"
echo > "$env_notify_solving_file"
echo "$notify_solving" >> "$env_notify_solving_file"
fi;
if [[ -n "$push_spec1" && -n "$push_spec2" ]]; then
{ git push --verbose $origin_1 $push_spec1 || true; } > "$path_async_output/push1.txt" &
pid_push1=$!
{ git push --verbose $origin_2 $push_spec2 || true; } > "$path_async_output/push2.txt" &
pid_push2=$!
push_report1="> Push $origin_1 "
wait $pid_push1 && push_report1+="(async success)" || push_report1+="(async failure)"
push_report2+="> Push $origin_2 "
wait $pid_push2 && push_report2+="(async success)" || push_report2+="(async failure)"
echo
echo $push_report1
echo $push_spec1
cat < "$path_async_output/push1.txt"
echo
echo $push_report2
echo $push_spec2
cat < "$path_async_output/push2.txt"
else
if [[ -n "$push_spec1" ]]; then
echo $'\n>' Push $origin_1
echo $push_spec1
git push --verbose $origin_1 $push_spec1 || true
fi;
if [[ -n "$push_spec2" ]]; then
echo $'\n>' Push $origin_2
echo $push_spec2
git push --verbose $origin_2 $push_spec2 || true
fi;
fi;
if [[ -n "$post_fetch_spec1" && -n "$post_fetch_spec2" ]]; then
git fetch --no-tags $origin_1 $post_fetch_spec1 > "$path_async_output/post_fetch1.txt" &
pid_post_fetch1=$!
git fetch --no-tags $origin_2 $post_fetch_spec2 > "$path_async_output/post_fetch2.txt" &
pid_post_fetch2=$!
post_fetch_report1="> Post-fetch $origin_1 "
wait $pid_post_fetch1 && post_fetch_report1+="(async success)" || post_fetch_report1+="(async failure)"
post_fetch_report2+="> Post-fetch $origin_2 "
wait $pid_post_fetch2 && post_fetch_report2+="(async success)" || post_fetch_report2+="(async failure)"
echo
echo $post_fetch_report1
echo $post_fetch_spec1
cat < "$path_async_output/post_fetch1.txt"
echo
echo $post_fetch_report2
echo $post_fetch_spec2
cat < "$path_async_output/post_fetch2.txt"
else
if [[ -n "$post_fetch_spec1" ]]; then
echo $'\n>' Post-fetch $origin_1
echo $post_fetch_spec1
git fetch --no-tags $origin_1 $post_fetch_spec1
fi;
if [[ -n "$post_fetch_spec2" ]]; then
echo $'\n>' Post-fetch $origin_2
echo $post_fetch_spec2
git fetch --no-tags $origin_2 $post_fetch_spec2
fi;
fi;
echo
echo End `basename $0`