-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgit.functions
More file actions
449 lines (344 loc) · 10.3 KB
/
git.functions
File metadata and controls
449 lines (344 loc) · 10.3 KB
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
# This file is part of shellfire git. It is subject to the licence terms in the COPYRIGHT file found in the top-level directory of this distribution and at https://raw.githubusercontent.com/shellfire-dev/git/master/COPYRIGHT. No part of shellfire git, including this file, may be copied, modified, propagated, or distributed except according to the terms contained in the COPYRIGHT file.
# Copyright © 2014-2015 The developers of shellfire git. See the COPYRIGHT file in the top-level directory of this distribution and at https://raw.githubusercontent.com/shellfire-dev/git/master/COPYRIGHT.
core_dependency_requires '*' git
git_withOutputSilencedIfQuiet()
{
local verbosityLevel=$1
local gitCommand="$2"
shift 2
if [ "$(core_init_verbosity)" -lt $verbosityLevel ]; then
set -- -q "$@"
else
set -- "$@"
fi
git "$gitCommand" "$@"
}
git_inRepository()
{
local repositoryFolderPath="$1"
shift 1
pushd "$repositoryFolderPath"
"$@"
popd
}
git_inRepositorySilently()
{
local repositoryFolderPath="$1"
shift 1
git_inRepository "$repositoryFolderPath" git_withOutputSilencedIfQuiet 2 "$@"
}
core_dependency_requires '*' git
git_doesNotHaveTagPattern()
{
local repositoryFolderPath="$1"
local tagName="$2"
local tags="$(git_inRepository "$repositoryFolderPath" git tag --list "$tagName")"
if [ -z "$tags" ]; then
return 0
else
return 1
fi
}
git_checkout()
{
local repositoryFolderPath="$1"
shift 1
git_inRepositorySilently "$repositoryFolderPath" checkout "$@"
}
git_push()
{
local repositoryFolderPath="$1"
shift 1
if core_variable_isSet _git_pushDryRun; then
if $_git_pushDryRun; then
core_message WARN "Dry run of git push $@"
return 0
fi
fi
git_inRepositorySilently "$repositoryFolderPath" push "$@"
}
git_pull()
{
local repositoryFolderPath="$1"
shift 1
git_inRepositorySilently "$repositoryFolderPath" pull "$@"
}
git_commit()
{
local repositoryFolderPath="$1"
shift 1
git_inRepositorySilently "$repositoryFolderPath" commit "$@"
}
git_branch()
{
local repositoryFolderPath="$1"
shift 1
git_inRepositorySilently "$repositoryFolderPath" branch "$@"
}
core_dependency_requires '*' git
git_mostRecentCommit()
{
local repositoryFolderPath="$1"
git_mostRecent "$repositoryFolderPath" '%H'
}
git_mostRecent()
{
local repositoryFolderPath="$1"
local format="$2"
git_inRepository "$repositoryFolderPath" git log -n 1 --format="format:$format"
}
core_dependency_requires '*' git
git_commitToTagOrCommit()
{
local repositoryFolderPath="$1"
local commit="$2"
pushd "$repositoryFolderPath"
set +e
local tag="$(git describe --tags --exact-match "$commit" 2>/dev/null)"
set -e
popd
if [ -n "$tag" ]; then
printf '%s' "$tag"
else
printf '%s' "$commit"
fi
}
git_clean()
{
local repositoryFolderPath="$1"
local repositoryCommit="$2"
git_inRepositorySilently "$repositoryFolderPath" clean -d -f -x
git_inRepositorySilently "$repositoryFolderPath" reset --hard "$repositoryCommit"
pushd "$repositoryFolderPath"
local submodule
while IFS= read -r submodule
do
if [ -n "$submodule" ]; then
git_clean "$submodule" HEAD
fi
done <<-EOF
$(git submodule foreach -q 'echo "$path"')
EOF
popd
}
core_dependency_requires '*' git
git_tag()
{
local repositoryFolderPath="$1"
local tagName="$2"
local tagMessage="$3"
if git_doesNotHaveTagPattern "$repositoryFolderPath" "$tagName"; then
git_inRepository "$repositoryFolderPath" git tag -a -m "$tagMessage" "$tagName"
fi
git_push "$repositoryFolderPath" origin "$tagName"
}
git_tagUsingBaseName()
{
local repositoryFolderPath="$1"
local tagBaseName="$2"
local tagMessage="$3"
local tagName
local count=1
while true
do
tagName="$tagBaseName"-$count
if git_doesNotHaveTagPattern "$repositoryFolderPath" "$tagName"; then
break
fi
count=$((count+1))
done
git_tag "$repositoryFolderPath" "$tagName" "$tagMessage"
_git_tagNameChosen="$tagName"
}
core_dependency_requires '*' git
git_tagUpstream()
{
local repositoryFolderPath="$1"
local upstreamTagName="upstream-$(git_mostRecent "$repositoryFolderPath" '%at-%H')"
local longCommitHash="$(git_mostRecentCommit "$repositoryFolderPath")"
local iso8601Date="$(git_mostRecent "$repositoryFolderPath" '%aI')"
local upstreamTagMessage="Imported from upstream at '$iso8601Date' from revision '$longCommitHash'"
git_tag "$repositoryFolderPath" "$upstreamTagName" "$upstreamTagMessage"
}
core_dependency_requires '*' cat
git_patchAm()
{
local repositoryFolderPath="$1"
local patchFilePath="$2"
cat "$patchFilePath" | git_inRepositorySilently "$repositoryFolderPath" am --3way
}
git_addAll()
{
local repositoryFolderPath="$1"
shift 1
git_inRepository "$repositoryFolderPath" git add -A .
}
git_cherrypicks()
{
local repositoryFolderPath="$1"
local rationale="$2"
shift 2
git_inRepositorySilently "$repositoryFolderPath" cherry-pick --strategy=recursive -X theirs "$@"
}
git_addGitHubClone()
{
local repositoryParentPath="$1"
local repositoryUser="$2"
local repositoryName="$3"
local repositoryFolderPath="$repositoryParentPath"/"$repositoryName"
if [ -d "$repositoryFolderPath"/.git ]; then
return 0
fi
if [ -f "$repositoryFolderPath"/.git ]; then
core_exitError $core_commandLine_exitCode_CANTCREAT "Clone '$repositoryName' already exists as git submodule (not repository) in path '$repositoryParentPath'"
fi
if [ -e "$repositoryFolderPath" ]; then
core_exitError $core_commandLine_exitCode_CANTCREAT "Clone '$repositoryName' already exists as something (not repository) in path '$repositoryParentPath'"
fi
pushd "$repositoryParentPath"
git_withOutputSilencedIfQuiet 2 clone git@github.com:"$repositoryUser"/"$repositoryName".git
popd
git_inRepositorySilently "$repositoryFolderPath" submodule update --init --recursive
}
git_addGitHubSubmodule()
{
local repositoryParentPath="$1"
local repositoryUser="$2"
local repositoryName="$3"
local repositoryFolderPath="$repositoryParentPath"/"$repositoryName"
if [ -f "$repositoryFolderPath"/.git ]; then
return 0
fi
if [ -d "$repositoryFolderPath"/.git ]; then
core_exitError $core_commandLine_exitCode_CANTCREAT "Submodule '$repositoryName' already exists as git repository (not submodule) in path '$repositoryParentPath'"
fi
if [ -e "$repositoryFolderPath" ]; then
core_exitError $core_commandLine_exitCode_CANTCREAT "Submodule '$repositoryName' already exists as something (not submodule) in path '$repositoryParentPath'"
fi
pushd "$repositoryParentPath"
git_withOutputSilencedIfQuiet 2 submodule add https://github.com/"$repositoryUser"/"$repositoryName".git
popd
git_inRepositorySilently "$repositoryFolderPath" submodule update --init --recursive
}
git_addGitHubInit()
{
local repositoryParentPath="$1"
local repositoryUser="$2"
local repositoryName="$3"
pushd "$repositoryParentPath"
if [ -d "$repositoryName"/.git ]; then
git_addOriginRemoteIdempotently "$repositoryParentPath"/"$repositoryName" git@github.com:"$repositoryUser"/"$repositoryName".git
return 0
fi
if [ -f "$repositoryName"/.git ]; then
core_exitError $core_commandLine_exitCode_CANTCREAT "Init '$repositoryName' already exists as git submodule (not submodule) in path '$repositoryParentPath'"
fi
if [ -e "$repositoryName" ]; then
core_exitError $core_commandLine_exitCode_CANTCREAT "Init '$repositoryName' already exists as something in path '$repositoryParentPath'"
fi
git_withOutputSilencedIfQuiet 2 init "$repositoryName"
popd
git_addOriginRemoteIdempotently "$repositoryParentPath"/"$repositoryName" git@github.com:"$repositoryUser"/"$repositoryName".git
}
core_dependency_requires '*' git
git_addUpstreamRemoteIdempotently()
{
local repositoryFolderPath="$1"
local upstreamRemote="$2"
git_inRepository "$repositoryFolderPath" git remote add -f --tags upstream "$upstreamRemote" 1>/dev/null 2>/dev/null || git_fetchFromUpstreamRemote "$repositoryFolderPath"
}
core_dependency_requires '*' git
git_addOriginRemoteIdempotently()
{
local repositoryFolderPath="$1"
local originRemote="$2"
git_inRepository "$repositoryFolderPath" git remote add -f --tags origin "$originRemote" 1>/dev/null 2>/dev/null || git_fetchFromOriginRemote "$repositoryFolderPath"
}
git_fetchFromUpstreamRemote()
{
local repositoryFolderPath="$1"
git_fetchFromRemote "$repositoryFolderPath" upstream
}
git_fetchFromOriginRemote()
{
local repositoryFolderPath="$1"
git_fetchFromRemote "$repositoryFolderPath" origin
}
git_fetchFromRemote()
{
local repositoryFolderPath="$1"
local remote="$2"
git_inRepositorySilently "$repositoryFolderPath" fetch "$remote"
}
core_dependency_requires '*' git
git_removeAllLocalTags()
{
local repositoryFolderPath="$1"
pushd "$repositoryFolderPath"
# Note, absence of quotes is correct
# 2>/dev/null because there is no -q option
git tag -d $(git tag) 2>/dev/null
local remote
while IFS= read -r remote
do
if [ -n "$remote" ]; then
git fetch --tags "$remote"
fi
done <<-EOF
$(git remote)
EOF
popd
}
core_dependency_requires '*' cat git awk sed touch env
git_changeTimestampsToLastCommit()
{
local repositoryFolderPath="$1"
pushd "$repositoryFolderPath"
local submodule
while IFS= read -r submodule
do
if [ -n "$submodule" ]; then
git_changeTimestampsToLastCommit "$submodule"
fi
done <<-EOF
$(git submodule foreach -q 'echo "$path"')
EOF
local TMP_FILE
core_temporaryFiles_newFileToRemoveOnExit
local _git_changeTimestampsToLastCommit_awkScript="$TMP_FILE"
cat >"$_git_changeTimestampsToLastCommit_awkScript" <<EOF
BEGIN {
FS="\n"
RS=""
ORS=""
filesCount=0
$(git ls-files | sed 's/^\(.*\)$/files["\1"]=1;filesCount++/')
}
EOF
cat >>"$_git_changeTimestampsToLastCommit_awkScript" <<'EOF'
{
timestamp=$1
touch=""
n=2
while(n<=NF) {
if (files[$n] == 1) {
files[$n]=2
filesCount--
touch=touch " '" $n "'"
}
n++
}
if (length(touch)>0) {
# awk system() call invokes the shell which is (a) a security risk (b) slow (happens each time) and (c) requires daemons to have a shell
# Instead we pass the output to sh, which means one invocation and execution happens as-we-go
print "touch -d '" timestamp "'" touch " 2>/dev/null" " || true \n"
}
if(filesCount == 0)
{
exit 0
}
}
EOF
git whatchanged --format=format:%cI --name-only | awk -f "$_git_changeTimestampsToLastCommit_awkScript" - | env sh -s
popd
}