-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-split.sh
executable file
·382 lines (321 loc) · 11 KB
/
git-split.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
#!/bin/bash
# git-split.sh - hordp@cisco.com
#
# A script to transmogrify git repositories in various ways
# using git filter-branch and a clever gitignore-like keep/delete
# syntax. This attempt reads a single conf file and outputs
# different targets to the output.
#
#_____________________________________________________________________________
# SETUP
COLOR_START=`echo -e '\033['`
COLOR_END='m'
RED=`echo -e '\033[31m'`
GREEN=`echo -e '\033[32m'`
NORMAL=`echo -e '\033[0m'`
TMP_DIR=/tmp
#_____________________________________________________________________________
# CMDLINE
# Stop on errors
set -e
# Uncomment for script tracing
# set -x
verbose()
{
if [[ $VERBOSE > 0 ]] ; then
echo "$@"
fi
}
usage()
{
cat << EOF
usage: $0 options <config-file> [<repository>]
Divides a git repository into one or more new repositories by cloning the original repository
and removing unwanted files from the new repository(ies)
OPTIONS:
-h Show this message
-f Force overwrite of output repositories if they exist
-D DryRun mode: list which files will be removed/retained
-i Specify input repository (in options instead of at end)
-t path Specify target path
-v Verbose
<config-file> Config settings; a mostly-.gitignore-style list of files to keep/remove
<repository> Source repo. Optional. May be specified in the config file.
EOF
}
SOURCE_REPO=
FORCE=0
VERBOSE=0
DRYRUN=0
TESTSPLIT=0
SOURCE_MATCH=
TARGET_PATH=
options()
{
# Handles options received from multiple command lines
OPTIND=1
CONFIG_FILES=
while [ $OPTIND -le $# ] ; do
while getopts “hfi:t:TDv” OPTION
do
case $OPTION in
h)
usage
exit 1
;;
t)
TARGET_PATH="$OPTARG"
;;
i)
SOURCE_REPO="$OPTARG"
;;
f)
FORCE=1
;;
T)
# Undocumented experimental option. Only modifies workdir in new repo for fast testing.
TESTSPLIT=1
;;
D)
DRYRUN=1
;;
v)
VERBOSE=$(( $VERBOSE+1 ))
;;
?)
usage
exit
;;
esac
done
if [ $OPTIND -le $# ] ; then
shift $(( OPTIND-1 ))
CONFIG_FILES="${CONFIG_FILES} $1"
shift
OPTIND=1
fi
done
export SOURCE_REPO
export VERBOSE
export DRYRUN
export TESTSPLIT
export TARGET_PATH
export CONFIG_FILES
}
# Parse the command-line options
options $@
# Source file is required
if [[ -z "$CONFIG_FILES" ]] ; then
usage
exit
fi
if [[ ! -z "$CONFIG_FILES" ]] ; then
SOURCE_MATCH="$CONFIG_FILES"
fi
for SOURCE_MATCH in $CONFIG_FILES ; do
# Required options: complain if not on command line or in source file
if [[ -z $SOURCE_REPO ]]
then
usage
exit 1
fi
if [[ $VERBOSE > 0 ]] ; then
echo "SOURCE_REPO=$SOURCE_REPO"
echo "SOURCE_MATCH=$SOURCE_MATCH"
echo "TARGET_PATH=$TARGET_PATH"
echo "VERBOSE=$VERBOSE"
echo "DRYRUN=$DRYRUN"
echo "TESTSPLIT=$TESTSPLIT"
echo "-------------------------------"
fi
#_____________________________________________________________________________
# VALIDATE
if ! [ -e "$SOURCE_REPO" ] ; then
echo "Warning: Source repository '$SOURCE_REPO' not found"
fi
if ! [ -e "$SOURCE_MATCH" ] ; then
echo "Error: '$SOURCE_MATCH' not found"
usage
exit 1
fi
#_____________________________________________________________________________
# PROCESS CONFIG
if [[ $VERBOSE > 1 ]] ; then
echo "Extra verbose..."
set -x
fi
verbose "Converting $SOURCE_MATCH to a sed script"
BASE_PATH="${TMP_DIR}/${SOURCE_MATCH#*/}"
MATCH_FILE="${BASE_PATH}-match.sed"
RAW_FILES="${BASE_PATH}-raw.txt"
ALL_FILES="${BASE_PATH}-all.txt"
if [ -z "$TARGET_PATH" ] ; then
TARGET_PATH="."
fi
if ! [ -e "$TARGET_PATH" ] ; then
mkdir -p "$TARGET_PATH"
fi
# Convert the input globs into a sed script
cat $SOURCE_MATCH | sed -e '
1i\
x;s/.*/-/;x # Pre-load hold buffer with "-" for "no-match"
1i\
/^$/d # Skip blank lines in the files list
1i\
/^[ \t]*$/d
s/^[ \t]*// # Remove leading whitespace
s/#.*// # Remove comments
s/[ \t]*$// # Remove trailing whitespace
/^!*$/d # Remove blank lines
h # copy buffer to hold buffer
s/^\(!\)*\(!!\)*.*$/\1/ # collect odd leading ! if any
x # swap into hold buffer
s/^![ \t]*// # and remove from pattern buffer
/^--*>[ \t]*/ { # --> Pusher
# When we get here, hold buffer is loaded with + or - indicating keep or toss.
# If its a keeper we need to put our section tag on it.
# x # Swap +/- into pattern space
# \\|^[+]| { # If its a match
# a\ # append the tag to it
# foo
# G # Append matched string to this one
# s/\n/\t/g # Replace LF with TABs
# p;d # Print the string and be done
# }
s/--*>[ \t]*// # Dump the prefix
s!.*!#-- Push matches to &\nx\n \\|^[+]| {\n s/.*/&/\n G\n s/\\n/\\t/g\n p;d\n }\nx!
p;d
}
s-[.]-[.]-g # Convert . to [.] (extension marker, not any-char)
s-[+]-[+]-g # Convert + to [+]
s-\*-|-g # Temp: replace * with |
s-||-.*-g # Convert ** to path-spanning globber (.*)
s-|-[^/]*-g # Convert * to filename globber (^[/]*)
s-^[^/]-^\\(.*/\\)*&- # interpret paths: bar/foo/ == ^(.*/)*bar/foo/, /bar/foo == /bar/foo
s-[^/]$-&$- # interpret paths: bar/foo/ == ^(.*/)*bar/foo/, /bar/foo == /bar/foo$
s-^/-^- # Force ^/ paths to match from start-of-line
# Input line is converted to a glob matcher now. Emit as matcher address to next-sed:
s-.*-\\|&| {-p
x # Recover negation operator from hold buffer
s|!|-|;s|^$|+| # and replace with + or -
s/./\tx;s|.*|&|;x\n\t}/ # Put mark ("+" or "-") in hold buffer of next-sed
' > $MATCH_FILE
#_____________________________________________________________________________
# GET FILES
verbose "Gathering file list from $SOURCE_REPO"
if [[ $TESTSPLIT == 0 ]] ; then
# Grab all the filenames from the repo
GIT_DIR=$SOURCE_REPO git log --pretty=format: --name-only --diff-filter=A --all | sort - | uniq > $RAW_FILES
else
# Grab all the current filenames from the repo
GIT_DIR=$SOURCE_REPO git ls-tree -r --name-only HEAD| sort - | uniq > $RAW_FILES
fi
# Flag each file for keep/no-keep
sed -f $MATCH_FILE < $RAW_FILES > $ALL_FILES
#_____________________________________________________________________________
# SPLIT RESULTS
verbose "Splitting into groups"
TARGETS=$(cut "-d " $ALL_FILES -f1 | sort - | uniq |grep -e "^[^-]")
# Create the TARGET files for each new repository
for A in $TARGETS ; do
TARG="$TARGET_PATH/$A.gitrm"
verbose " --> $TARG "
sed -n -e "/^$A\t/d" -e "s/..*\t/0 0000000000000000000000000000000000000000\t/p" $ALL_FILES > $TARG
done
if [[ $DRYRUN > 0 ]] ; then
# If dry-run, colorize the file sets here and display them
colors=( 31 32 33 35 36 37 41 42 47 46 45 34 )
# TODO: Option to also list "-targets"
grep -e "^[^-]" $ALL_FILES | sed $(
i=0
for A in $TARGETS ; do
echo "-e /^$A\t/s/^/${COLOR_START}${colors[$i]}${COLOR_END}/"
i=$(( $i+1 ))
done
) - | less -R -
fi
#_____________________________________________________________________________
# REWORK
if [[ $DRYRUN == 0 ]] ; then
TMPSOURCE="$TARGET_PATH/.tmp_divide"
rm -rf $TMPSOURCE
if [[ $TESTSPLIT == 0 ]] ; then
# Use --mirror here so I can be sure to get everything (remote branches) as a base
git clone --mirror "$SOURCE_REPO" "$TMPSOURCE"
else
# Clone working directories here
git clone "$SOURCE_REPO" "$TMPSOURCE"
rm -rf "$TMPSOURCE/.git"
fi
FAIL=0
for TARG in $TARGETS ; do
TARGET_REPO=$TARGET_PATH/${TARG}.git
if [[ $TESTSPLIT > 0 ]] ; then
TARGET_REPO=$TARGET_PATH/${TARG}
fi
if [ -e $(readlink -f $TARGET_REPO) ] ; then
if [[ $(readlink -f $TARGET_REPO) != $(readlink -f $SOURCE_REPO) ]] ; then
if [[ $FORCE > 0 ]] ; then
rm -rf "$TARGET_REPO"
else
echo "Warning: $TARGET_REPO already exists. Use -f to force overwrite."
FAIL=1
fi
fi
fi
done
if [[ $FAIL == 0 ]] ; then
for TARG in $TARGETS ; do
TARGET_REPO=$TARGET_PATH/${TARG}.git
if [[ $TESTSPLIT > 0 ]] ; then
TARGET_REPO=$TARGET_PATH/${TARG}
fi
if [[ $(readlink -f $TARGET_REPO) != $(readlink -f $SOURCE_REPO) ]] ; then
echo "Cloning into $TARGET_REPO"
cp -alT "$TMPSOURCE" "$TARGET_REPO"
fi
done
fi
rm -rf $TMPSOURCE
if [[ $FAIL > 0 ]] ; then
exit
fi
for TARG in $TARGETS ; do
FILTER=$(readlink -f $TARGET_PATH/${TARG}.gitrm )
TARGET_REPO=$(readlink -f $TARGET_PATH/${TARG}.git)
if [[ $TESTSPLIT == 0 ]] ; then
verbose "Removing files from $TARGET_REPO ($FILTER)"
GIT_DIR=$TARGET_REPO git filter-branch -f --index-filter "git update-index --force-remove --index-info < $FILTER" \
--remap-to-ancestor --prune-empty --tag-name-filter cat -- --all
verbose "Cleaning up old objects"
rm -rf $TARGET_REPO/refs/original
GIT_DIR=$TARGET_REPO git reflog expire --expire=now --all
GIT_DIR=$TARGET_REPO git gc --prune=now
else
verbose "Removing files from ${TARGET_REPO%.git} ($FILTER)"
pushd ${TARGET_REPO%.git}
cut '-d ' -f2 $FILTER | xargs -i rm -f "{}"
find -depth -type d -empty -exec rmdir "{}" \;
popd
fi
done
# Display the resulting repository sizes
du -lsh $(
echo "$SOURCE_REPO"
for TARG in $TARGETS ; do
if [[ $TESTSPLIT == 0 ]] ; then
echo "$TARGET_PATH/${TARG}.git"
else
echo "$TARGET_PATH/${TARG}"
fi
done
) | sort -n
fi
done
# TODO: Consider mode where we use this:
# git --git-dir=../bootloader/bin.git/ ls-tree -r --full-name HEAD
# or git ls-files --stage
# to get index, pipe it through a sed script that rewrites only-as-needed,
# and output to
# git update-index --index-info
# This should allow us to rewrite filenames when needed, as well as delete filenames not wanted.