-
Notifications
You must be signed in to change notification settings - Fork 32
/
git-subsplit.sh
executable file
·347 lines (291 loc) · 7.69 KB
/
git-subsplit.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
#!/usr/bin/env bash
#
# git-subsplit.sh: Automate and simplify the process of managing one-way
# read-only subtree splits.
#
# Copyright (C) 2012 Dragonfly Development Inc.
#
if [ $# -eq 0 ]; then
set -- -h
fi
OPTS_SPEC="\
git subsplit init url
git subsplit publish splits --heads=<heads> --tags=<tags> --splits=<splits>
git subsplit update
--
h,help show the help
q quiet
debug show plenty of debug output
n,dry-run do everything except actually send the updates
work-dir directory that contains the subsplit working directory
options for 'publish'
heads= only publish for listed heads instead of all heads
no-heads do not publish any heads
tags= only publish for listed tags instead of all tags
no-tags do not publish any tags
update fetch updates from repository before publishing
rebuild-tags rebuild all tags (as opposed to skipping tags that are already synced)
"
eval "$(echo "$OPTS_SPEC" | git rev-parse --parseopt -- "$@" || echo exit $?)"
# We can run this from anywhere.
NONGIT_OK=1
DEBUG=" :DEBUG >"
PATH=$PATH:$(git --exec-path)
. git-sh-setup
if [ "$(hash git-subtree &>/dev/null && echo OK)" = "" ]
then
die "Git subplit needs git subtree; install git subtree or upgrade git to >=1.7.11"
fi
ANNOTATE=
QUIET=
COMMAND=
SPLITS=
REPO_URL=
WORK_DIR="${PWD}/.subsplit"
HEADS=
NO_HEADS=
TAGS=
NO_TAGS=
REBUILD_TAGS=
DRY_RUN=
VERBOSE=
subsplit_main()
{
while [ $# -gt 0 ]; do
opt="$1"
shift
case "$opt" in
-q) QUIET=1 ;;
--debug) VERBOSE=1 ;;
--heads) HEADS="$1"; shift ;;
--no-heads) NO_HEADS=1 ;;
--tags) TAGS="$1"; shift ;;
--no-tags) NO_TAGS=1 ;;
--update) UPDATE=1 ;;
-n) DRY_RUN="--dry-run" ;;
--dry-run) DRY_RUN="--dry-run" ;;
--rebuild-tags) REBUILD_TAGS=1 ;;
--) break ;;
*) die "Unexpected option: $opt" ;;
esac
done
COMMAND="$1"
shift
case "$COMMAND" in
init)
if [ $# -lt 1 ]; then die "init command requires url to be passed as first argument"; fi
REPO_URL="$1"
shift
subsplit_init
;;
publish)
if [ $# -lt 1 ]; then die "publish command requires splits to be passed as first argument"; fi
SPLITS="$1"
shift
subsplit_publish
;;
update)
subsplit_update
;;
*) die "Unknown command '$COMMAND'" ;;
esac
}
say()
{
if [ -z "$QUIET" ];
then
echo "$@" >&2
fi
}
subsplit_require_work_dir()
{
if [ ! -e "$WORK_DIR" ]
then
die "Working directory not found at ${WORK_DIR}; please run init first"
fi
if [ -n "$VERBOSE" ];
then
echo "${DEBUG} pushd \"${WORK_DIR}\" >/dev/null"
fi
pushd "$WORK_DIR" >/dev/null
}
subsplit_init()
{
if [ -e "$WORK_DIR" ]
then
die "Working directory already found at ${WORK_DIR}; please remove or run update"
fi
say "Initializing subsplit from origin (${REPO_URL})"
if [ -n "$VERBOSE" ];
then
echo "${DEBUG} git clone -q \"${REPO_URL}\" \"${WORK_DIR}\""
fi
git clone -q "$REPO_URL" "$WORK_DIR" || die "Could not clone repository"
}
subsplit_publish()
{
subsplit_require_work_dir
if [ -n "$UPDATE" ];
then
subsplit_update
fi
if [ -z "$HEADS" ] && [ -z "$NO_HEADS" ]
then
# If heads are not specified and we want heads, discover them.
HEADS="$(git ls-remote origin 2>/dev/null | grep "refs/heads/" | cut -f3- -d/)"
if [ -n "$VERBOSE" ];
then
echo "${DEBUG} HEADS=\"${HEADS}\""
fi
fi
if [ -z "$TAGS" ] && [ -z "$NO_TAGS" ]
then
# If tags are not specified and we want tags, discover them.
TAGS="$(git ls-remote origin 2>/dev/null | grep -v "\^{}" | grep "refs/tags/" | cut -f3 -d/)"
if [ -n "$VERBOSE" ];
then
echo "${DEBUG} TAGS=\"${TAGS}\""
fi
fi
for SPLIT in $SPLITS
do
SUBPATH=$(echo "$SPLIT" | cut -f1 -d:)
REMOTE_URL=$(echo "$SPLIT" | cut -f2- -d:)
REMOTE_NAME=$(echo "$SPLIT" | git hash-object --stdin)
if [ -n "$VERBOSE" ];
then
echo "${DEBUG} SUBPATH=${SUBPATH}"
echo "${DEBUG} REMOTE_URL=${REMOTE_URL}"
echo "${DEBUG} REMOTE_NAME=${REMOTE_NAME}"
fi
if ! git remote | grep "^${REMOTE_NAME}$" >/dev/null
then
git remote add "$REMOTE_NAME" "$REMOTE_URL"
if [ -n "$VERBOSE" ];
then
echo "${DEBUG} git remote add \"${REMOTE_NAME}\" \"${REMOTE_URL}\""
fi
fi
say "Syncing ${SUBPATH} -> ${REMOTE_URL}"
for HEAD in $HEADS
do
if [ -n "$VERBOSE" ];
then
echo "${DEBUG} git show-ref --quiet --verify -- \"refs/remotes/origin/${HEAD}\""
fi
if ! git show-ref --quiet --verify -- "refs/remotes/origin/${HEAD}"
then
say " - skipping head '${HEAD}' (does not exist)"
continue
fi
LOCAL_BRANCH="${REMOTE_NAME}-branch-${HEAD}"
if [ -n "$VERBOSE" ];
then
echo "${DEBUG} LOCAL_BRANCH=\"${LOCAL_BRANCH}\""
fi
say " - syncing branch '${HEAD}'"
git checkout master >/dev/null 2>&1
git branch -D "$LOCAL_BRANCH" >/dev/null 2>&1
git branch -D "${LOCAL_BRANCH}-checkout" >/dev/null 2>&1
git checkout -b "${LOCAL_BRANCH}-checkout" "origin/${HEAD}" >/dev/null 2>&1
git subtree split -q --prefix="$SUBPATH" --branch="$LOCAL_BRANCH" "origin/${HEAD}" >/dev/null
RETURNCODE=$?
if [ -n "$VERBOSE" ];
then
echo "${DEBUG} git checkout master >/dev/null 2>&1"
echo "${DEBUG} git branch -D \"$LOCAL_BRANCH\" >/dev/null 2>&1"
echo "${DEBUG} git branch -D \"${LOCAL_BRANCH}-checkout\" >/dev/null 2>&1"
echo "${DEBUG} git checkout -b \"${LOCAL_BRANCH}-checkout\" \"origin/${HEAD}\" >/dev/null 2>&1"
echo "${DEBUG} git subtree split -q --prefix=\"$SUBPATH\" --branch=\"$LOCAL_BRANCH\" \"origin/${HEAD}\" >/dev/null"
fi
if [ $RETURNCODE -eq 0 ]
then
PUSH_CMD="git push -q ${DRY_RUN} --force $REMOTE_NAME ${LOCAL_BRANCH}:${HEAD}"
if [ -n "$VERBOSE" ];
then
echo "${DEBUG} $PUSH_CMD"
fi
if [ -n "$DRY_RUN" ]
then
echo \# $PUSH_CMD
$PUSH_CMD
else
$PUSH_CMD
fi
fi
done
for TAG in $TAGS
do
if [ -n "$VERBOSE" ];
then
echo "${DEBUG} git show-ref --quiet --verify -- \"refs/tags/${TAG}\""
fi
if ! git show-ref --quiet --verify -- "refs/tags/${TAG}"
then
say " - skipping tag '${TAG}' (does not exist)"
continue
fi
LOCAL_TAG="${REMOTE_NAME}-tag-${TAG}"
if [ -n "$VERBOSE" ];
then
echo "${DEBUG} LOCAL_TAG="${LOCAL_TAG}""
fi
if git branch | grep "${LOCAL_TAG}$" >/dev/null && [ -z "$REBUILD_TAGS" ]
then
say " - skipping tag '${TAG}' (already synced)"
continue
fi
if [ -n "$VERBOSE" ];
then
echo "${DEBUG} git branch | grep \"${LOCAL_TAG}$\" >/dev/null && [ -z \"${REBUILD_TAGS}\" ]"
fi
say " - syncing tag '${TAG}'"
say " - deleting '${LOCAL_TAG}'"
git branch -D "$LOCAL_TAG" >/dev/null 2>&1
if [ -n "$VERBOSE" ];
then
echo "${DEBUG} git branch -D \"${LOCAL_TAG}\" >/dev/null 2>&1"
fi
say " - subtree split for '${TAG}'"
git subtree split -q --annotate="${ANNOTATE}" --prefix="$SUBPATH" --branch="$LOCAL_TAG" "$TAG" >/dev/null
RETURNCODE=$?
if [ -n "$VERBOSE" ];
then
echo "${DEBUG} git subtree split -q --annotate=\"${ANNOTATE}\" --prefix=\"$SUBPATH\" --branch=\"$LOCAL_TAG\" \"$TAG\" >/dev/null"
fi
say " - subtree split for '${TAG}' [DONE]"
if [ $RETURNCODE -eq 0 ]
then
PUSH_CMD="git push -q ${DRY_RUN} --force ${REMOTE_NAME} ${LOCAL_TAG}:refs/tags/${TAG}"
if [ -n "$VERBOSE" ];
then
echo "${DEBUG} PUSH_CMD=\"${PUSH_CMD}\""
fi
if [ -n "$DRY_RUN" ]
then
echo \# $PUSH_CMD
$PUSH_CMD
else
$PUSH_CMD
fi
fi
done
done
popd >/dev/null
}
subsplit_update()
{
subsplit_require_work_dir
say "Updating subsplit from origin"
git fetch -q -t origin
git checkout master
git reset --hard origin/master
if [ -n "$VERBOSE" ];
then
echo "${DEBUG} git fetch -q -t origin"
echo "${DEBUG} git checkout master"
echo "${DEBUG} git reset --hard origin/master"
fi
popd >/dev/null
}
subsplit_main "$@"