-
Notifications
You must be signed in to change notification settings - Fork 5
/
rezipdoc-history-filter.sh
executable file
·284 lines (253 loc) · 8.25 KB
/
rezipdoc-history-filter.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
#!/usr/bin/env bash
# Copyright (c) 2019 Robin Vobruba <[email protected]>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# For info about this script, please refer to the `printUsage()` function below.
# Exit immediately on each error and unset variable;
# see: https://vaneyckt.io/posts/safer_bash_scripts_with_set_euxo_pipefail/
set -Eeuo pipefail
#set -Eeu
pwd_before=$(pwd)
this_script_file=$(basename "$0")
script_name="$this_script_file"
this_script_dir=$(cd "$(dirname "$0")"; pwd)
# Settings and default values
source_repo=""
target_repo=""
num_commits_max=1000
use_orig_commit="false"
branch="master"
repo_tool_url="https://raw.githubusercontent.com/hoijui/ReZipDoc/master/scripts/rezipdoc-repo-tool.sh"
repo_tool_file_name=$(basename "$repo_tool_url")
printUsage() {
echo "$script_name - Creates a local clone of a repo, and filters"
echo "the main branch with ReZip(Doc)."
echo
echo "Usage:"
echo " $script_name [OPTIONS]"
echo
echo "Options:"
echo " -h, --help show this help message"
echo " -b, --branch git branch to filter"
echo " -m, --max-commits maximum number of commits to filter into the new repo"
echo " -o, --orig use the original commit message (default: prefix with \"FILTERED - \"),"
echo " author, email and time"
echo " -s, --source [path|URL] the repo to read commits from"
echo " -t, --target [path] the repo to write commits to"
}
# Handle command line arguments
while [ $# -gt 0 ]
do
opName="$1"
shift # skip argument
case ${opName} in
-h|--help)
printUsage
exit 0
;;
-b|--branch)
branch="$1"
shift # past argument
;;
-m|--max-commits)
num_commits_max=$1
shift # past argument
;;
-o|--orig)
use_orig_commit="true"
;;
-s|--source)
source_repo="$1"
shift # past argument
;;
-t|--target)
target_repo="$1"
shift # past argument
;;
*)
# unknown option / not an option
>&2 echo "Unknown option '${opName}'!"
printUsage
exit 1
;;
esac
done
if ! git ls-remote "$source_repo" > /dev/null 2> /dev/null
then
>&2 echo "Source repo is not a valid git repository: '$source_repo'!"
exit 1
fi
if [ "$source_repo" = "$target_repo" ]
then
>&2 echo "Source and target repos can not be equal!"
exit 1
fi
if [ -e "$target_repo" ]
then
>&2 echo "Target repo can not be an existing path: '$target_repo'!"
exit 1
fi
# Check whether the source repo is a local directory or a URL
source_is_url="true"
[ -d "$source_repo" ] && source_is_url="false"
[ "$source_is_url" = "true" ] && source_type="URL" || source_type="local repo"
# If the source repo is a local directory, make the path to it absolute
[ "$source_is_url" != "true" ] && source_repo="$(cd "$source_repo"; pwd)"
echo "Source repo: '${source_repo}' ($source_type)"
echo "Branch: ${branch}"
echo "Max commits: ${num_commits_max}"
echo "Target repo: '${target_repo}'"
mkdir "$target_repo"
cd "$target_repo"
git init
git remote add source "$source_repo"
git fetch source
# Ensure we have a local filter installer script
if [ -e "$this_script_dir/$repo_tool_file_name" ]
then
repo_tool="$this_script_dir/$repo_tool_file_name"
else
rnd=$(od -A n -t d -N 1 /dev/urandom | tr -d ' ')
repo_tool="/tmp/$(basename --suffix='.sh' \"$repo_tool_file_name\")-${rnd}.sh"
curl -s "$repo_tool_url" -o "$repo_tool"
fi
# Install our filter if not yet installed
if ! ${repo_tool} check --commit --diff --renormalize > /dev/null 2> /dev/null
then
${repo_tool} install --commit --diff --renormalize \
|| ( >&2 echo "Failed installing filter!"; exit 2 )
fi
git checkout --orphan ${branch}_filtered
git commit --allow-empty --allow-empty-message -m ""
num_commits=$(git log -${num_commits_max} --format="%H" --reverse source/${branch} | wc -l)
i=0
for commit_hash in $(git log -${num_commits_max} --topo-order --format="%H" --reverse source/${branch})
do
i=$(expr ${i} + 1)
echo
echo "############################################################"
echo "Copying & filtering commit ${i}/${num_commits} - ${commit_hash} ..."
echo
commit_args=""
if [ "$use_orig_commit" = "true" ]
then
#commit_msg=$(git log -1 --format="%s%n%n%b" ${commit_hash})
commit_msg=""
commit_args="$commit_args --reuse-message=${commit_hash}"
else
commit_msg=$(git log -1 --format="FILTERED - %s%n%n orig=%h%n%n%b" ${commit_hash})
# We have to give the message through stdin,
# because otherwise the quoting somehow gets fucked up (by sh)
commit_args="$commit_args --file=-"
fi
set +e
echo "Cherry-picking ..."
git cherry-pick --strategy=recursive --strategy-option=theirs --allow-empty --mainline 1 --no-commit ${commit_hash}
last_status=$?
if [ ${last_status} -ne 0 ]
then
>&2 echo -e "\tfailed! (Cherry-picking for ${commit_hash})"
fi
echo "Removing ..."
git status | grep 'deleted by them:' | cut -d':' -f2 | xargs -t -I {} git rm "{}"
last_status=$?
if [ ${last_status} -ne 0 ]
then
>&2 echo -e "\tfailed! (Removing for ${commit_hash})"
fi
echo "Adding the 1st ..."
git add --all --force
last_status=$?
if [ ${last_status} -ne 0 ]
then
>&2 echo -e "\tfailed! (Adding the 1st for ${commit_hash})"
fi
if [ ${last_status} -eq 0 ]
then
echo "Adding the 2nd ..."
git add --all --force --renormalize
last_status=$?
if [ ${last_status} -ne 0 ]
then
>&2 echo -e "\tfailed! (Adding the 2nd for ${commit_hash})"
fi
fi
if [ ${last_status} -eq 0 ]
then
if output=$(git status --porcelain) && [ -z "${output}" ]
then
# Working directory clean (completely)
if [ ${last_status} -ne 0 ]
then
>&2 echo -e "WARNING: Nothing to commit for ${commit_hash} -> skipping"
fi
last_status=0
else
echo "Committing ..."
echo "$commit_msg" | git commit -v ${commit_args}
last_status=$?
if [ ${last_status} -ne 0 ]
then
>&2 echo -e "\tfailed! (Committing for ${commit_hash})"
fi
fi
fi
set -e
if [ ${last_status} -ne 0 ]
then
git status
echo "Failed!"
exit ${last_status}
fi
echo "############################################################"
echo
done
echo
echo "############################################################"
echo "############################################################"
echo
# Merge the first (empty) commit with the second one
echo "removing first (empty) commit ..."
git rebase --root
echo "Source repo: '${source_repo}' ($source_type)"
echo "Branch: ${branch}"
echo "Max commits: ${num_commits_max}"
echo "Target repo: '${target_repo}'"
# Check if the original and the filtered versions have the same final content
cd "${target_repo}"
# We add a new commit on top of the orig branch,
# that just clean-filters all the final content (with re-zip),
# and we use that one only for comparing final contents for equality,
# to be sure we messed nothing (serious) up, during filtering.
git checkout -b "filter-commit-on-orig" "source/${branch}"
# This applies the clean filter(s) on all the files in the repo,
# and thus re-zips everything, in our case.
git commit --all -m "Re-zipped binaries"
set +e
git diff --exit-code --stat --color --color-moved "${branch}_filtered" "filter-commit-on-orig"
last_status=$?
set -e
git checkout "${branch}_filtered"
git branch -D "filter-commit-on-orig"
if [ ${last_status} -ne 0 ]
then
>&2 echo "ERROR: Original and filtered repos final content differ!"
exit ${last_status}
fi
cd "$pwd_before"