forked from che-incubator/che-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rebase.sh
executable file
·449 lines (349 loc) · 14.6 KB
/
rebase.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
#!/bin/bash
#
# Copyright (c) 2021 Red Hat, Inc.
# This program and the accompanying materials are made
# available under the terms of the Eclipse Public License 2.0
# which is available at https://www.eclipse.org/legal/epl-2.0/
#
# SPDX-License-Identifier: EPL-2.0
#
#!/bin/bash
# This script will rebase upstream code to our subtree in code directory
set -e
set -u
# update $1 json file
# $2 is the formatting option
override_json_file() {
local filename=$1
local formattingOption=${2:-}
INDENT=("--indent" "2")
if [[ "$formattingOption" == "tab" ]]; then
INDENT=("--tab")
fi
# now apply override settings
local overrideFile=".rebase/override/$filename"
if [ -f "$overrideFile" ]; then
echo " ⚙️ applying override settings from $overrideFile..."
jq "${INDENT[@]}" -s '.[0] * .[1]' "$filename" "$overrideFile" > "$filename.tmp"
cat "$filename.tmp" > "$filename"
fi
# and now, add the values (not overriding)
local addFile=".rebase/add/$filename"
if [ -f "$addFile" ]; then
echo " ⚙️ adding values from $addFile..."
jq "${INDENT[@]}" -s '.[1] * .[0]' "$addFile" "$filename" > "$filename.tmp"
cat "$filename.tmp" > "$filename"
fi
# delete previous file
rm "$filename.tmp"
}
escape_litteral() {
escaped=${1//\$/\\$}
escaped=${escaped//\[/\\[}
escaped=${escaped//\]/\\]}
echo "$escaped"
}
# appy some string replace in $1 file
apply_replace() {
local -r filename=$1
local -r replaceSettings=".rebase/replace/$filename.json"
# get one replace instruction on each line
local -r replaceCommands=$(jq -c '.[]' "${replaceSettings}")
IFS=$'\n'
local from
local by
for replaceCommand in $replaceCommands; do
# need to replace from by the by
from=$(jq -n "$replaceCommand" | jq -r '.from')
by=$(jq -n "$replaceCommand" | jq -r '.by')
escape_from=$(escape_litteral "$from")
escape_by=$(escape_litteral "$by")
sed -i.bak -e "s|${escape_from}|${escape_by}|" "${filename}"
if diff "$filename" "$filename.bak" &> /dev/null; then
echo "Unable to perform the replace. Value is not present in the resulting file"
echo "Wanted to check ${from}"
echo "File content is"
cat "${filename}"
exit 1
fi
rm "$filename.bak"
done
}
# Apply changes on code/package.json file
apply_code_package_changes() {
echo " ⚙️ reworking code/package.json..."
# reset the file from what is upstream
git checkout --theirs code/package.json > /dev/null 2>&1
# now apply again the changes
override_json_file code/package.json
# resolve the change
git add code/package.json > /dev/null 2>&1
}
# Apply changes on code/remote/package.json file
apply_code_remote_package_changes() {
echo " ⚙️ reworking code/remote/package.json..."
# reset the file from what is upstream
git checkout --theirs code/remote/package.json > /dev/null 2>&1
# now apply again the changes
override_json_file code/remote/package.json
# resolve the change
git add code/remote/package.json > /dev/null 2>&1
}
# Apply changes on $1 package.json file
# A path to the file should be passed, for example: code/build/package.json
apply_package_changes_by_path() {
local filePath="$1"
if [ -z "$filePath" ]; then
echo "Can not apply changes for package.json file - the path was not passed"
exit 1;
fi
echo " ⚙️ reworking $filePath..."
# reset the file from what is upstream
git checkout --theirs $filePath > /dev/null 2>&1
# now apply again the changes
override_json_file $filePath
# resolve the change
git add $filePath > /dev/null 2>&1
}
# Apply changes on code/remote/yarn.lock file
apply_code_remote_yarn_lock_changes() {
echo " ⚙️ reworking code/remote/yarn.lock..."
# reset the file from what is upstream
git checkout --theirs code/remote/yarn.lock > /dev/null 2>&1
yarn add node-gyp@9
# update yarn lock
yarn --ignore-scripts --cwd code/remote
# resolve the change
git add code/remote/yarn.lock > /dev/null 2>&1
}
# Apply changes on code/product.json file
apply_code_product_changes() {
echo " ⚙️ reworking code/product.json..."
# reset the file from what is upstream
git checkout --theirs code/product.json > /dev/null 2>&1
# now apply again the changes
override_json_file code/product.json "tab"
# resolve the change
git add code/product.json > /dev/null 2>&1
}
# Apply changes on code/build/lib/mangle/index.js file
apply_mangle_index_js_changes() {
echo " ⚙️ reworking code/build/lib/mangle/index.js..."
# reset the file from what is upstream
git checkout --theirs code/build/lib/mangle/index.js > /dev/null 2>&1
# the actual changes are in the code/build/lib/mangle/index.ts file
(cd code/build && yarn compile)
# resolve the change
git add code/build/lib/mangle/index.js > /dev/null 2>&1
}
# Apply changes on code/build/lib/mangle/index.ts file
apply_mangle_index_ts_changes() {
echo " ⚙️ reworking code/build/lib/mangle/index.ts..."
# reset the file from what is upstream
git checkout --theirs code/build/lib/mangle/index.ts > /dev/null 2>&1
# now apply again the changes
apply_replace code/build/lib/mangle/index.ts
# apply changes for the code/build/lib/mangle/index.js file
(cd code/build && yarn compile)
# resolve the change
git add code/build/lib/mangle/index.ts > /dev/null 2>&1
git add code/build/lib/mangle/index.js > /dev/null 2>&1
}
# Apply changes on code/src/vs/platform/remote/browser/browserSocketFactory.ts file
apply_code_vs_platform_remote_browser_factory_changes() {
echo " ⚙️ reworking code/src/vs/platform/remote/browser/browserSocketFactory.ts..."
# reset the file from what is upstream
git checkout --theirs code/src/vs/platform/remote/browser/browserSocketFactory.ts > /dev/null 2>&1
# now apply again the changes
apply_replace code/src/vs/platform/remote/browser/browserSocketFactory.ts
# resolve the change
git add code/src/vs/platform/remote/browser/browserSocketFactory.ts > /dev/null 2>&1
}
# Apply changes on code/src/server-main.js file
apply_code_server-main_changes() {
echo " ⚙️ reworking code/src/server-main.js..."
# reset the file from what is upstream
git checkout --theirs code/src/server-main.js > /dev/null 2>&1
# now apply again the changes
apply_replace code/src/server-main.js
# resolve the change
git add code/src/server-main.js > /dev/null 2>&1
}
# Apply changes on code/src/vs/server/node/remoteExtensionHostAgentServer.ts file
apply_code_vs_server_node_remoteExtensionHostAgentServer_changes() {
echo " ⚙️ reworking code/src/vs/server/node/remoteExtensionHostAgentServer.ts..."
# reset the file from what is upstream
git checkout --theirs code/src/vs/server/node/remoteExtensionHostAgentServer.ts > /dev/null 2>&1
# now apply again the changes
apply_replace code/src/vs/server/node/remoteExtensionHostAgentServer.ts
# resolve the change
git add code/src/vs/server/node/remoteExtensionHostAgentServer.ts > /dev/null 2>&1
}
# Apply changes on code/src/vs/server/node/webClientServer.ts file
apply_code_vs_server_web_client_server_changes() {
echo " ⚙️ reworking code/src/vs/server/node/webClientServer.ts..."
# reset the file from what is upstream
git checkout --theirs code/src/vs/server/node/webClientServer.ts > /dev/null 2>&1
# now apply again the changes
apply_replace code/src/vs/server/node/webClientServer.ts
# resolve the change
git add code/src/vs/server/node/webClientServer.ts > /dev/null 2>&1
}
# Apply changes on code/src/vs/workbench/contrib/remote/browser/remote.ts file
apply_code_vs_workbench_contrib_remote_browser_remote_changes() {
echo " ⚙️ reworking code/src/vs/workbench/contrib/remote/browser/remote.ts..."
# reset the file from what is upstream
git checkout --theirs code/src/vs/workbench/contrib/remote/browser/remote.ts > /dev/null 2>&1
# now apply again the changes
apply_replace code/src/vs/workbench/contrib/remote/browser/remote.ts
# resolve the change
git add code/src/vs/workbench/contrib/remote/browser/remote.ts > /dev/null 2>&1
}
# Apply changes on code/src/vs/workbench/contrib/webview/browser/pre/index.html file
apply_code_vs_workbench_contrib_webview_browser_pre_index_html_changes() {
echo " ⚙️ reworking code/src/vs/workbench/contrib/webview/browser/pre/index.html..."
# reset the file from what is upstream
git checkout --theirs code/src/vs/workbench/contrib/webview/browser/pre/index.html > /dev/null 2>&1
# now apply again the changes
apply_replace code/src/vs/workbench/contrib/webview/browser/pre/index.html
# resolve the change
git add code/src/vs/workbench/contrib/webview/browser/pre/index.html > /dev/null 2>&1
}
# Apply changes on code/src/vs/workbench/contrib/webview/browser/pre/index-no-csp.html file
apply_code_vs_workbench_contrib_webview_browser_pre_index_no_csp_html_changes() {
echo " ⚙️ reworking code/src/vs/workbench/contrib/webview/browser/pre/index-no-csp.html..."
# reset the file from what is upstream
git checkout --theirs code/src/vs/workbench/contrib/webview/browser/pre/index-no-csp.html > /dev/null 2>&1
# now apply again the changes
apply_replace code/src/vs/workbench/contrib/webview/browser/pre/index-no-csp.html
# resolve the change
git add code/src/vs/workbench/contrib/webview/browser/pre/index-no-csp.html > /dev/null 2>&1
}
# Apply changes on code/src/vs/code/browser/workbench/workbench.ts file
apply_code_src_vs_code_browser_workbench_workbench_changes() {
echo " ⚙️ reworking code/src/vs/code/browser/workbench/workbench.ts..."
# reset the file from what is upstream
git checkout --theirs code/src/vs/code/browser/workbench/workbench.ts > /dev/null 2>&1
# now apply again the changes
apply_replace code/src/vs/code/browser/workbench/workbench.ts
# resolve the change
git add code/src/vs/code/browser/workbench/workbench.ts > /dev/null 2>&1
}
# Apply changes for the given file
apply_changes() {
local filePath="$1"
if [ -z "$filePath" ]; then
echo "Can not apply changes - the path was not passed"
exit 1;
fi
echo " ⚙️ reworking $filePath..."
# reset the file from what is upstream
git checkout --theirs "$filePath" > /dev/null 2>&1
# now apply again the changes
apply_replace "$filePath"
# resolve the change
git add "$filePath" > /dev/null 2>&1
}
# Will try to identify the conflicting files and for some of them it's easy to re-apply changes
resolve_conflicts() {
echo "⚠️ There are conflicting files, trying to solve..."
local -r conflictingFiles=$(git diff --name-only --diff-filter=U)
# iterate on all conflicting files
IFS=$'\n'
for conflictingFile in $conflictingFiles; do
echo " ➡️ Analyzing conflict for $conflictingFile"
if [[ "$conflictingFile" == "code/package.json" ]]; then
apply_code_package_changes
elif [[ "$conflictingFile" == "code/product.json" ]]; then
apply_code_product_changes
elif [[ "$conflictingFile" == "code/build/lib/mangle/index.js" ]]; then
apply_mangle_index_js_changes
elif [[ "$conflictingFile" == "code/build/lib/mangle/index.ts" ]]; then
apply_mangle_index_ts_changes
elif [[ "$conflictingFile" == "code/remote/package.json" ]]; then
apply_code_remote_package_changes
elif [[ "$conflictingFile" == "code/remote/yarn.lock" ]]; then
apply_code_remote_yarn_lock_changes
elif [[ "$conflictingFile" == "code/src/vs/platform/remote/browser/browserSocketFactory.ts" ]]; then
apply_code_vs_platform_remote_browser_factory_changes
elif [[ "$conflictingFile" == "code/src/vs/server/node/webClientServer.ts" ]]; then
apply_code_vs_server_web_client_server_changes
elif [[ "$conflictingFile" == "code/src/server-main.js" ]]; then
apply_code_server-main_changes
elif [[ "$conflictingFile" == "code/src/vs/server/node/remoteExtensionHostAgentServer.ts" ]]; then
apply_code_vs_server_node_remoteExtensionHostAgentServer_changes
elif [[ "$conflictingFile" == "code/src/vs/workbench/contrib/remote/browser/remote.ts" ]]; then
apply_code_vs_workbench_contrib_remote_browser_remote_changes
elif [[ "$conflictingFile" == "code/src/vs/workbench/contrib/webview/browser/pre/index.html" ]]; then
apply_code_vs_workbench_contrib_webview_browser_pre_index_html_changes
elif [[ "$conflictingFile" == "code/src/vs/workbench/contrib/webview/browser/pre/index-no-csp.html" ]]; then
apply_code_vs_workbench_contrib_webview_browser_pre_index_no_csp_html_changes
elif [[ "$conflictingFile" == "code/src/vs/code/browser/workbench/workbench.ts" ]]; then
apply_code_src_vs_code_browser_workbench_workbench_changes
elif [[ "$conflictingFile" == "code/src/vs/base/common/product.ts" ]]; then
apply_changes "$conflictingFile"
elif [[ "$conflictingFile" == "code/src/vs/workbench/contrib/welcomeGettingStarted/browser/gettingStarted.ts" ]]; then
apply_changes "$conflictingFile"
else
echo "$conflictingFile file cannot be automatically rebased. Aborting"
exit 1
fi
done
}
# $1 is the upstream sha1 on which we're rebasing
continue_merge() {
local -r conflictingFiles=$(git diff --name-only --diff-filter=U)
if [ -z "${conflictingFiles}" ]; then
echo "🚑 Conflicts have been solved. Continue to merge..."
# use an empty editor to not edit the message
GIT_EDITOR=: git merge --continue > /dev/null 2>&1
else
echo "Fail to resolve all conflicts. Exiting..."
exit 1
fi
}
# pull changes and if there are conflicts, resolve them
do_rebase() {
echo "Using git $(which git) $(git --version)"
# grab current upstream version
UPSTREAM_VERSION=$(git rev-parse upstream-code/main)
#UPSTREAM_VERSION=1.62.2
# Grab current version
if result=$(pull_changes "${UPSTREAM_VERSION}" 2>&1); then
echo "🎉 rebase operation done successfully"
else
stderr=$result
# do we have conflict ?
if [[ "$stderr" =~ "CONFLICT" ]]; then
resolve_conflicts
continue_merge "$UPSTREAM_VERSION"
echo "🚀 rebase successful after conflict solving"
else
echo "Unable to handle the error during rebase. Exiting..."
printf "\n"
echo "Error is :"
echo "${stderr}"
exit 1
fi
fi
}
# make sed -i portable
sed_in_place() {
SHORT_UNAME=$(uname -s)
if [ "$(uname)" == "Darwin" ]; then
sed -i '' "$@"
elif [ "${SHORT_UNAME:0:5}" == "Linux" ]; then
sed -i "$@"
fi
}
# $1 is the revision to pull
pull_changes() {
git subtree pull --prefix code upstream-code "$1" -m "$(get_commit_message "$1")"
}
# $1 is the revision
get_commit_message() {
echo "Rebase against the upstream ${1}"
echo "vscode-upstream-sha1: ${1}"
}
# perform rebase
do_rebase