-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrezipdoc-create-archives-repo.sh
executable file
·213 lines (188 loc) · 6.28 KB
/
rezipdoc-create-archives-repo.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
#!/usr/bin/env bash
# Copyright (c) 2019 Robin Vobruba <hoijui.quaero@gmail.com>
#
# 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
add_archive_bin="false"
add_archive_src="true"
num_commits_max=1000
branch=master
source_repo=$(cd "${this_script_dir}"; cd ..; pwd)
rnd=$(od -A n -t d -N 1 /dev/urandom | tr -d ' ')
target_repo="/tmp/rezipdoc-archives-repo-${rnd}"
# We use this one for building the binaries
tmp_repo="/tmp/rezipdoc-tmp-repo-${rnd}"
printUsage() {
echo "$script_name - This creates a git repository with a lot of archives as content,"
echo "focusing on archives that contain mostly plain-text content that changes"
echo "non-radically over time."
echo
echo "Practically, it will build the main JAR of this project for every commit"
echo "of this repo, plus some text files, and commit each change, if there was one."
echo
echo "Usage:"
echo " $script_name [OPTIONS]"
echo
echo "Options:"
echo " -h, --help show this help message"
echo " --no-src-archive do not add the sources ZIP to each commit"
echo " --bin-archive add the binary archive (JAR) to each commit (NOTE this will take a long time)"
echo " -m, --max-commits maximum number of commits to transcribe into the new repo"
echo " -s, --source [path|URL] the repo to transcribe from"
echo " -t, --target [path] the repo to transcribe to"
echo " --tmp [path] the repo to use for temporary checkout and binary building"
}
# Handle command line arguments
while [ $# -gt 0 ]
do
opName="$1"
case ${opName} in
-h|--help)
printUsage
exit 0
;;
--no-src-archive)
add_archive_src="false"
;;
--bin-archive)
add_archive_bin="true"
;;
-m|--max-commits)
num_commits_max=$2
shift # past argument
;;
-s|--source)
source_repo="$2"
shift # past argument
;;
-t|--target)
target_repo="$2"
shift # past argument
;;
--tmp)
tmp_repo="$2"
shift # past argument
;;
*)
# unknown option / not an option
>&2 echo "Unknown option '${opName}'!"
printUsage
exit 1
;;
esac
shift # next argument or value
done
if [ "$add_archive_bin" != "true" ] && [ "$add_archive_src" != "true" ]
then
>&2 echo "Please include at least one of binary and source archive!"
exit 1
fi
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 [ -e "$tmp_repo" ]
then
>&2 echo "Temporary repo can not be an existing path: '$tmp_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
echo "Source repo: '${source_repo}'"
echo "Tmp-checkout repo: '${tmp_repo}'"
echo "Max commits: ${num_commits_max}"
echo "Target repo: '${target_repo}'"
git clone "$source_repo" "$tmp_repo"
mkdir "$target_repo"
cd "$target_repo"
git init
# This disables the global git-ignore file, which might otherwise prevent us
# from adding binaries (like archives).
git config core.excludesfile 'some-file-that-does-not-exist'
cd "$tmp_repo"
num_commits=$(git log -${num_commits_max} --format="%H" --reverse origin/${branch} | wc -l)
i=0
for commit_hash in $(git log -${num_commits_max} --format="%H" --reverse origin/${branch})
do
i=$((i + 1))
echo
echo "############################################################"
echo "Building commit ${i}/${num_commits} - ${commit_hash} ..."
echo
cd "$tmp_repo"
git checkout "$commit_hash"
if [ "${add_archive_bin}" = "true" ]
then
rm -f target/*.jar
mvn package -DskipTests
fi
commit_msg=$(git log -1 --format="ARCH - %s%n%n orig=%h%n%n%b" "$commit_hash")
cd "$target_repo"
find . -type f | grep -v "\.git" | xargs rm -Rf
# Add some Project-global text files/sources
cp "$tmp_repo/README"* ./ 2> /dev/null
cp "$tmp_repo/LICENSE"* ./ 2> /dev/null
cp "$tmp_repo/pom.xml" ./ 2> /dev/null
cp -r "$tmp_repo/src"* ./
# Add archive(s)
if [ "$add_archive_bin" = "true" ]
then
# uncompressing this is probably less interesting/useful,
# because it contains mainly class files, which are binary,
# and thus might not play much nicer with git
# then the compressed archive
cp "$tmp_repo/target/"*".jar" ./
fi
if [ "$add_archive_src" = "true" ]
then
# As this probably contains mostly text files,
# it should play much nicer with git when uncompressed.
(cd "$tmp_repo"; zip --quiet -r "$target_repo/src.zip" src)
fi
git add --all --force
git commit -m "${commit_msg}"
cd "$tmp_repo"
echo "############################################################"
echo
done
# Make sure a potential global value might be used again
git config --unset core.excludesfile
echo "Source repo: '${source_repo}'"
echo "Tmp-checkout repo: '${tmp_repo}'"
echo "Max commits: ${num_commits_max}"
echo "Target repo: '${target_repo}'"
cd "$pwd_before"