-
Notifications
You must be signed in to change notification settings - Fork 2
/
GitHelper
executable file
·772 lines (626 loc) · 20.9 KB
/
GitHelper
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
#!/usr/bin/env bash
. "${BASH_SOURCE[0]%/*}/function.sh" script || exit
usage()
{
ScriptUsage "$1" "\
Usage: $(ScriptName) changes|clone|edit|github|gui|hide|hub|IsInstalled|profile|pull|push|remote|server|size|time|upstream [OPTION]...
Additional Git functionality.
Common options:
-w, --win use Windows git"
exit $1
}
init()
{
code="$CODE"
fork="/usr/local/bin/fork"
forkWin="$UADATA/Fork/Fork.exe"; IsPlatform win && fork="$forkWin"
forkGit="$UADATA/Fork/gitInstance/2.24.1/bin/git.exe"
forkTitle="Fork*"
gitExtensions="$P/GitExtensions/GitExtensions.exe"
gitExtensionsGit="$P/Git/cmd/git.exe"
gitKraken="/usr/bin/gitkraken"; IsPlatform mac && gitKraken="$P/GitKraken.app/Contents/MacOS/GitKraken"
gitKrakenNoPath="$gitKraken"; IsPlatform mac && gitKrakenNoPath="GitKraken"
gitKrakenWin="$UADATA/gitkraken/gitkraken.exe"
gitx="$P/GitX.app/Contents/Resources/gitx"
# find a Windows git executable
! IsPlatform win && return
gitWin="$(FindInPath "git.exe")" && return
[[ -f "$gitExtensionsGit" ]] && { gitWin="$gitExtensionsGit"; return; }
[[ -f "$forkGit" ]] && { gitWin="$forkGit"; return; }
gitWin="git"
}
opt()
{
case "$1" in
--win|-w) win="--win" code="$WIN_CODE";;
*) return 1;;
esac
}
argEnd() { setGit; }
#
# commands
#
isInstalledCommand() { InPath git; }
profileUsage() { ScriptUsageEcho "Usage: $(ScriptName) profile dir|SaveDir|save|restore [<profile name>|default](latest)\n$(ScriptName) configuration."; }
profileArgs() { profileArgs=( "$@" ); (( shift+=$# )); return 0; }
profileCommand() { profile --app "GitExtensions" --method "$UADATA/../Roaming/GitExtensions/GitExtensions" --files "GitExtensions.settings" "${profileArgs[@]}"; }
changesCommand()
{
local newline
# change summary
local changeSummary; changeSummary="$("$git" --no-pager diff --compact-summary)" || return
if [[ $changeSummary ]]; then
echo "$changeSummary"
newline='\n'
fi
# regular git status output
(( verboseLevel > 1 )) && { printf "$newline"; "$git" status || return; }
# new files
local newFiles; newFiles="$("$git" status --porcelain | ${G}grep -E "^(\?\?|A)" | cut -c 4-)"
if [[ $newFiles ]] && (( verboseLevel <= 1 )); then
printf "${newline}New files:\n"
"$git" status --porcelain | ${G}grep -E "^(\?\?|A)" | cut -c 4- | AddTab
newline='\n'
fi
# file mode changes
local fileModeChanges; fileModeChanges="$("$git" --no-pager diff --summary)" || return
if [[ $fileModeChanges ]]; then
(( verboseLevel <= 1 )) && printf "$newline"
echo "File mode changes:"
echo "$fileModeChanges" | AddTab
[[ $verbose ]] && { "$git" diff --summary | ${G}grep 'mode change ' | awk '{print "'$(GitRoot)'/"$6}' | ${G}xargs --no-run-if-empty ${G}stat -c '%A %a %n' | AddTab || return; }
fi
return 0
}
editUsage() { echot "Usage: $(ScriptName) edit\nEdit ~/.gitconfig. In Windows, copy it to \$WIN_HOME/.gitconfig removing everything after the '# not windows' comment."; }
editCommand()
{
local file="$HOME/.gitconfig" winFile="$WIN_HOME/.gitconfig"
! IsPlatform win && { TextEdit "$file"; return; }
sublime --wait "$file" || return
merge "$file" "$winFile" || return
ask "Do you want to replace '$(FileToDesc "$winFile")' with '$(FileToDesc "$file")'" --default-response "n" || return 0
awk '/# not windows/ { seen = 1} ! seen {print}' "$file" > "$winFile"
}
hideCommand()
{
{ ! IsPlatform win || ! drive IsWin .; } && return
repoCheck || return
FileHide ".git" || return
FileTouchAndHide ".gitignore" || return
[[ -f ".gitkeep" ]] && { attrib .gitkeep +h /s || return; }
return 0
}
pullUsage() { ScriptUsageEcho "Usage: $(ScriptName) pull [REMOTE...]\nPull from the specified remotes if they exist."; }
pullArgStart() { unset -v remotes; }
pullArgs() { remotes=( $@ ); shift="$#"; }
pullCommand()
{
local remote branch="$(GitBranch)"
for remote in "${remotes[@]}"; do
! git remote | ${G}grep -q "^${remote}$" && continue
hilight "Pulling $branch from $remote..."
RunLog git pull "$remote" "$branch" || return
done
}
pushUsage() { ScriptUsageEcho "Usage: $(ScriptName) push [REMOTE...]\nPush to the specified remotes if they exist."; }
pushArgStart() { unset -v remotes; }
pushArgs() { remotes=( $@ ); shift="$#"; }
pushCommand()
{
local remote
for remote in "${remotes[@]}"; do
! git remote | ${G}grep -q "^${remote}$" && continue
hilight "Pushing to $remote..."
git push "$remote" $force || return
done
}
serverUsage() { ScriptUsageEcho "Usage: $(ScriptName) server\nManage a Git server for a Nomad job."; }
serverCommand()
{
local i=0 stopSignal
serverCheck || return
# start service
echo "Git server manager pid is $$"
TimerOn || return
# traps
trap "serverSignal SIGINT" SIGINT
trap "serverSignal SIGTERM" SIGTERM
trap "serverExit" EXIT
# wait
while true; do
(( i % 60 == 0 )) && printf "\nThe Git server has been running for $(TimerOff)..." || printf "."; (( ++i ))
serverCheck || { printf "\nThe 'Get server stopped running...\n"; break; }
[[ ! $stopSignal ]] && sleep 5 &
wait; [[ $stopSignal ]] && break
done
}
serverExit()
{
echo "The Git server manager ran for $(TimerOff)"
}
serverSignal()
{
stopSignal="$1"
printf "\nReceived $stopSignal...\n"
}
serverCheck()
{
[[ ! -d "/git" ]] && { ScriptErr "Git repositories are not present in '/git"; return 1; }
! InPath "git" && { ScriptErr "Git is not installed"; return 1; }
return 0
}
# sizeCommand - from # http://stubbisms.wordpress.com/2009/07/10/git-script-to-show-largest-pack-objects-and-trim-your-waist-line/
sizeCommand()
{
repoCheck || return
# set the internal field spereator to line break, so that we can iterate easily over the verify-pack output
local IFS=$'\n';
# list all objects including their size, sort by size, take top 10
echo "Calculating object sizes..."
objects=`"$git" verify-pack -v .git/objects/pack/pack-*.idx | ${G}grep -v chain | sort -k3nr | head -n 50`
echo "All sizes are in kB's. Pack is the size of the object, compressed, inside the pack file."
output="size,pack,SHA,location"
for y in $objects
do
# extract the size in bytes
size=$((`echo $y | cut -f 5 -d ' '`/1024))
# extract the compressed size in bytes
compressedSize=$((`echo $y | cut -f 6 -d ' '`/1024))
# extract the SHA
sha=`echo $y | cut -f 1 -d ' '`
# find the objects location in the repository tree
other=`"$git" rev-list --all --objects | ${G}grep $sha`
#lineBreak=`echo -e "\n"`
output="${output}\n${size},${compressedSize},${other}"
done
echo -e $output | column -t -s ', '
}
upstreamUsage()
{
echot "Usage: $(ScriptName) upstream
Return the upstream status: up-to-date, behind, ahead, or diverged."
}
upstreamCommand()
{
local upstream='@{u}'
local local="$("$git" rev-parse @)"
local remote="$("$git" rev-parse "$upstream")"
local base=$("$git" merge-base @ "$upstream")
if [[ "$local" == "$remote" ]]; then echo "up-to-date"
elif [[ "$local" == "$base" ]]; then echo "behind"
elif [[ "$remote" == "$base" ]]; then echo "ahead"
else echo "diverged"
fi
}
#
# Clone Commands
#
cloneUsage()
{
echot "Usage: $(ScriptName) clone [bin|existing] URL
Clone a repository. URL is either a standard Git URL or a SERVER and REPO.
-d, --dest DIR repository destination directory, defaults to \$code
-w, --windows clone to the Windows code directory (\$WCODE)"
}
cloneArgStart() { unset -v dest repo server url; }
cloneArgs()
{
[[ "$command" != @(clone|cloneExisting) ]] && return
if IsUrl "$1"; then
ScriptArgGet "url" -- "$@"; shift
[[ $1 ]] && { ScriptArgGet "repo" -- "$@"; shift; }
else
ScriptArgGet "server" -- "$@"; shift
ScriptArgGet "repo" -- "$@"; shift
fi
return 0
}
cloneOpt()
{
case "$1" in
--dest|--dest=*|-d|-d=*) ScriptOptGet "dest" "$@"; dest="$(RemoveTrailingSlash "$dest")";;
*) return 1;;
esac
}
cloneCommand()
{
# set the url
[[ ! $url ]] && url="$(serverToRepo "$server" "$repo")"
# set the repo
[[ ! $repo ]] && repo="$(echo "$url" | RemoveTrailingSlash | GetFileName | RemoveEnd ".git")"
# set the destination
[[ ! $dest ]] && { dest="$code/$repo"; [[ $win ]] && dest="$WIN_CODE/$repo"; }
[[ ! $force && -d "$dest" ]] && return
setGit "$dest" || return
# clone
echo "Cloning $repo ($url) to $dest..."
RunLog "$git" clone "$url" "$dest" || return
[[ $test ]] && return
# change to the new repo directory
cd "$dest" || return
# Windows file system does not support file permissions
drive IsWin "$dest" && { RunLog "$git" config --local --add core.filemode false || return; }
return 0
}
cloneBinCommand()
{
local server; server="$(ConfigGetCurrent "GitServer")" || { ScriptErr "unable to find a Git server"; return 1; }
# platform bin
cd "$BIN" || return
cloneExisting "$server" "public-bin" || return
! IsDomainRestricted && { remoteAdd "gh" "https://github.com/JohnButare/platform-helper" || return; }
# user bin
local user="$USER"; IsPlatform win && user="${WIN_USER,,}"
cd "$UBIN" || return
cloneExisting "$server" "${user}-bin" || return
! IsDomainRestricted && { remoteAdd "gh" "https://github.com/JohnButare/personal-bin" || return; }
# Windows plink requires accepting the key manually
local port="$(serverToPort "$server")"
if IsPlatform win && IsAvailablePort "$server" "$port"; then
echo "Testing SSH connection to $server..."
start plink.exe -v -P "$port" "$(serverToUser "$server")$server" git --version
fi
# permissions
SyncLocalFiles permissions || return
# done
if [[ -d "$BIN/.git" && -d "$UBIN/.git" ]]; then
echo "Git is setup for the the bin directories"
return 0
fi
return 0
}
cloneExistingUsage()
{
EchoWrap "Usage: $(ScriptName) clone existing SERVER
Clone into the current directory. This is useful if the content from the cloned repositry already exists."
}
cloneExistingCommand() { cloneExisting "$server" "$repo"; }
cloneExisting()
{
local server="$1" repo="$2"
repo="$(serverToRepo "$server" "$repo")"
echo "Cloning $repo..."
[[ ! -d .git && ! -d hold/.git ]] && { git clone "$repo" hold || return; }
[[ -f hold/.gitignore ]] && { mv hold/.gitignore . || return; }
[[ -d hold/.git ]] && { mv hold/.git . || return; }
hideCommand || return
[[ -d hold ]] && { rm -fr hold || return; }
echo "Updating remote to $repo..."
"$git" remote set-url origin "$repo" || return # in case the repository existed
}
#
# GUI Command
#
guiUsage()
{
echot "Usage: $(ScriptName) gui [PATH]
Open the repository in the current or specified directory in a Git GUI program.
-w, --wait wait for the operation to complete"
}
guiArgStart() { unset -v path wait; }
guiArgs()
{
[[ ! $1 ]] && return
ScriptArgGet "path" -- "$@" && ScriptCheckDir "$path" && path="$(GetFullPath "$path")" && isGitRepo "$path";
}
guiOpt()
{
case "$1" in
-w|--wait) wait="true";;
*) return 1
esac
}
guiCommand()
{
drive IsWin "$path" && { guiWin && return; }
if [[ -f "$gitKraken" ]]; then guiGitKraken
elif [[ -f "$fork" ]]; then guiFork
elif [[ -f "$gitx" ]]; then guiGitx
elif InPath "gitg"; then guiGitg
else guiWin || guiNone
fi
}
guiWin()
{
if [[ -f "$gitKrakenWin" ]]; then guiGitKrakenWin
elif [[ -f "$forkWin" ]]; then guiForkWin
elif [[ -f "$gitExtensions" ]]; then guiGitExtensions
else return 1
fi
}
guiNone() { ScriptErr "could not find a Git GUI application"; return 1; }
guiGitg()
{
[[ $path ]] && { command cd "$path" || return; }
start $wait gitg "${otherArgs[@]}"
}
guiGitExtensions()
{
local args=( "${otherArgs[@]}" ); [[ $path ]] && args+=( browse "$path" )
start $wait "$gitExtensions" "${args[@]}" || return
}
guiFork()
{
local args=(); [[ $path ]] && args+=( "$path" )
start "$fork" "${args[@]}"
}
guiForkWin()
{
local args=(); [[ $path ]] && args+=( "$path" )
start "$forkWin" "${args[@]}"
}
guiGitKraken()
{
if [[ $path ]]; then
local args=(); [[ $path ]] && args+=( --path "$path" )
RunLog start $wait "$gitKraken" "${args[@]}" || return
else
RunLog start $wait "$gitKrakenNoPath" || return
fi
! IsPlatform win && return
WinSetState "GitKraken" --activate >& /dev/null
}
guiGitKrakenWin()
{
local args=(); [[ $path ]] && args+=( --path "$(utw "$path")" )
GitKrakenHelper cli -- "${args[@]}" &
}
guiGitx()
{
local args=( "${otherArgs[@]}" ); [[ $path ]] && args+=( -git-dir="$path" )
start $wait "$gitx" "${args[@]}"
}
#
# GitHub Commands
#
githubUsage() { ScriptUsageEcho "Usage: $(ScriptName) GitHub dir|clone|create"; }
githubCommand(){ usage; }
githubDirArgs() { ScriptArgGet "repo" -- "$@"; }
githubDirCommand() { repoDir "$repo"; }
#
# GitHub clone command
#
githubCloneArgs() { ScriptArgGet "repo" -- "$@"; }
githubCloneCommand()
{
local dir; dir="$(repoDir "$repo")" || return
[[ -d "$dir" ]] && { echo "$dir"; return; }
setGit "$(GetParentDir "$dir")" || return
cd "$code" && "$git" clone "$repo" 1>&2 && echo "$dir"
}
#
# GitHub create command
#
githubCreateUsage()
{
echot "Usage: $(ScriptName) GitHut create [REPO]
Create a GitHub repository from an existing repository."
}
githubCreateArgs() { ScriptArgGet "repo" -- "$@"; shift; }
githubCreateCommand()
{
local org="$USER"
local dir="$code"
[[ ! -d "$dir/$repo" ]] && { EchoErr "$repo is not a valid repository"; return 1; }
cd "$dir/$repo"
setGit "$dir/$repo" || return
header "$repo"
[[ ! -f .gitignore ]] && { addDefaultGitIgnore || return 1; }
[[ ! -f README.md ]] && { addDefaultReadMe || return 1; }
"$git" remote | ${G}grep origin > /dev/null || { createGitHubRepository || return 1; }
echo "Pushing repository to GitHub..."; "$git" push || return 1
}
createGitHubRepository()
{
echo "Creating GitHub repository...."
hub create $org/$repo -p -d "$repo project (converted from CSIS SVN repository)" || return 1
}
addDefaultGitIgnore()
{
echo "Adding default C# .gitignore...."
cp ../.gitignore . || return 1
"$git" add .gitignore || return 1
"$git" commit -m "Add default C# .gitignore" || return 1
}
addDefaultReadMe()
{
echo "Adding default README.md...."
cat <<-EOF > "README.md"
$repo
$(eval printf '=%.0s' {1..${#n}})
$repo project (converted from a CSIS SVN repository).
External Dependencies
---------------------
EOF
"$git" add README.md || return 1
"$git" commit -m "Add default README.md" || return 1
}
#
# Remote Commands
#
remoteUsage() { ScriptUsageEcho "Usage: $(ScriptName) remote [add|dir|init|ls|swap|to](ls)\nManipulate remote repositories."; }
remoteCommand() { remoteLsCommand; }
remoteArgStart() { unset -v repo server; }
remoteArgs()
{
[[ "$command" == @(remoteAdd|remoteDir|remoteInit|remoteLs) ]] && { ScriptArgGet "server" -- "$@"; shift; }
if [[ "$command" == @(remoteAdd|remoteDir|remoteInit) ]]; then
if (( $# > 0 )); then
ScriptArgGet "repo" -- "$@"; shift;
elif [[ "$command" != @(remoteDir) ]]; then
local url; url="$("$git" remote get-url origin)" || return
repo="$(echo "$url" | GetLastDir)"
fi
fi
return 0
}
remoteAddUsage() { ScriptUsageEcho "Usage: $(ScriptName) git remote add SERVER REPO\nAdd the repository as a remote."; }
remoteAddCommand() { "$git" remote add "$server" "$(serverToRepo "$server" "$repo")" && "$git" fetch "$server" && "$git" remote -v; }
remoteDirUsage() { ScriptUsageEcho "Usage: $(ScriptName) remote dir SERVER\nReturn the git directory for the specified server."; }
remoteDirCommand() { echo "$(serverToShare "$server")/$repo"; }
remoteLsUsage() { ScriptUsageEcho "Usage: $(ScriptName) remote ls SERVER\nList git repositories on the specified server."; }
remoteLsCommand() { hilight "$server repostiories: "; ssh "$(ConfigGetCurrent "user")@$server" ls "/$(serverToDir "$server")" | ${G}grep -Ev "eaDir"; }
remoteToUsage() { ScriptUsageEcho "Usage: $(ScriptName) git remote to http|ssh\nConvert the current repository to HTTP or SSH."; }
remoteToCommand() { usage; }
remoteInitUsage() { ScriptUsageEcho "Usage: $(ScriptName) git remote init SERVER REPO\nInitialize an empty repository on the specified server."; }
remoteInitCommand()
{
# change to server Git directory
local dir; dir="$(unc mount "$(serverToShare "$server")")" || return
cd "$dir" || return
[[ -e "$repo" ]] && { ScriptErr "repository $repo already exists on $server"; return 1; }
# create respository
${G}mkdir --parents "$repo" && setGit "$repo" && cd "$repo" && "$git" init --bare
}
remoteSwapUsage() { ScriptUsageEcho "Usage: $(ScriptName) git remote swap [remote1] [remote2](origin)\nSwap remote names."; }
remoteSwapArgStart() { unset -v remote1; remote2="origin"; }
remoteSwapArgs()
{
ScriptArgGet "remote1" -- "$@"; shift;
(( $# > 0 )) && { ScriptArgGet "remote2" -- "$@"; shift; }
return 0
}
remoteSwapCommand()
{
# validate
[[ "$remote1" == "$remote2" ]] && { ScriptErr "remote1 and remote2 must be different"; return 1; }
local remote1Url; remote1Url="$("$git" remote get-url "$remote1")" || return
local remote2Url; remote2Url="$("$git" remote get-url "$remote2")" || return
local remote1NewName="$remote2" remote2NewName="$remote1"
[[ "$remote1" == "origin" ]] && { remote1NewName="$(GetUriServer "$remote1Url")"; }
[[ "$remote2" == "origin" ]] && { remote2NewName="$(GetUriServer "$remote2Url")"; }
# original configuration
header "Original Configuration"
"$git" remote -v || return
echo
# swap
header "Swapping $remote1 and $remote2"
log1 "$remote1 ($remote1Url)->$remote1NewName"
log1 "$remote2 ($remote2Url)->$remote2NewName"
RunLog "$git" remote rename "$remote1" "$remote1.hold" || return
RunLog "$git" remote rename "$remote2" "$remote2NewName" || return
RunLog "$git" remote rename "$remote1.hold" "$remote1NewName" || return
[[ "$remote1" == "origin" || "$remote2" == "origin" ]] && { RunLog "$git" branch master --set-upstream-to origin/master || return; }
echo
# new configuration
header "New Configuration"
"$git" remote -v || return
}
remoteToHttpCommand()
{
"$git" remote set-url origin "https://$(getBaseUrl "origin")/"
"$git" remote --verbose
}
remoteToSshCommand()
{
local url="$(getBaseUrl "origin")"
local server="${url%%/*}"
local suffix="${url#*( )*/}"
"$git" remote set-url origin "$(serverToRepo "$server" "${suffix}")" || return
"$git" remote --verbose
}
remoteAdd() { remoteExists "$1" && return; git remote add "$1" "$2"; }
remoteExists() { git remote get-url "$1" >& /dev/null; }
#
# Time Command
#
timeUsage() { EchoWrap "Usage: $(ScriptName) time [-a|--all]\nSet modification time of Git files modified today to the modification time in the repository. This is useful to correct file modification time of newly checked out files."; }
timeArgStart() { unset -v all; }
timeOpt()
{
case "$1" in
-a|--all) all="--all";;
*) return 1
esac
}
timeCommand()
{
# all git files
local file files; IFS=$'\n' ArrayMakeC files git ls-tree -r --name-only HEAD
# files modified today
[[ ! $all ]] && { IFS=$'\n' ArrayMake files "$(command ${G}ls -al --time-style=+%D "${files[@]}" | ${G}grep "$(date +%D)" | rev | cut -d" " -f1 | rev)" || return; }
# update times
printf "times..."
for file in "${files[@]}"; do
local gitTime; gitTime="$("$git" log -1 --format="%at" -- "$file")"
local fileTime; fileTime="$(GetFileModSeconds "$file")"
[[ "$gitTime" == "$fileTime" ]] && { printf "."; continue; }
printf "$file..."; log1 "$file: $gitTime $fileTime $(( fileTime - gitTime ))"
RunLog ${G}touch "$file" --time=mtime --date "@$gitTime" || return
done
echo "done"
}
#
# helper
#
repoCheck() { [[ -d ".git" ]] && return; ScriptErr "not a git repository"; return; }
repoDir()
{
local repo="$1";
IsUrl "$repo" && { echo "$code/$(GetUriDirs "$1" | RemoveTrailingSlash | GetFileName)"; return; }
[[ -d "$code/$repo" ]] && { echo "$code/$repo"; return; }
ScriptErr "'$repo' is not a valid repository"; return 1
}
# getBaseUrl REMOTE - get repository base URL, i.e. https://server/path/ ssh://user@server:port/path/ -> server/path
getBaseUrl()
{
local url; url="$("$git" remote get-url "$1")" || return
url="$(echo "$url" | sed 's/https:\/\///')" # remove https://
url="$(echo "$url" | sed 's/ssh:\/\///' | sed 's/git@//' | sed 's/:[0-9]*\//\//')" # remove ssh://, user@, and :port
url="${url%%/}" # remove trailing /
echo "${url}"
}
# isGitRepo DIR - return 0 if the directory is in a Git repository
isGitRepo()
{
( cd "$path" && setGit "$path" && "$git" rev-parse --git-dir >& /dev/null; ) && return
ScriptErrQuiet "fatal: not a git repository (or any of the parent directories)"
}
# serverToRepo server name -> ssh://user@server:port/dir/name
serverToRepo()
{
local server="$1" name="$2"
[[ -d "$server" ]] && { echo "$server/$name"; return; }
local port="$(serverToPort "$server")"; [[ $port ]] && port=":$port"
echo "ssh://$(serverToUser "$1")$server$port/$(serverToDir "$1")/$name";
}
serverToDir()
{
case "$1" in
ados.sandia.gov) echo "NG/SysAdminTeam/_git";;
gitlab|gitlab.butare.net) echo "$USER";;
*) echo "git";;
esac
}
serverToPort()
{
case "$1" in
ados.sandia.gov) echo "22";;
gitlab|gitlab.butare.net) echo "10022";;
nas1|nas1.butare.net) echo "608";;
esac
}
serverToShare()
{
case "$1" in
nas1|nas1.butare.net) echo "//$server/git";;
*) echo "//$server/root/$(serverToDir "$server")"
esac
}
serverToUser()
{
case "$1" in
ados.sandia.gov) echo "";;
gitlab|gitlab.butare.net) echo "git@";;
*) echo "$(ConfigGetCurrent "user")@";;
esac
}
setGit()
{
local dir="${1:-.}"
git="git"
IsPlatform win && { [[ $win ]] || drive IsWin "$dir"; } && git="$gitWin"
log1 "git set to '$git'"
}
ScriptRun "$@"