1
1
#! /usr/bin/env bash
2
2
3
- # / ==============================================================================
3
+ # = ==============================================================================
4
4
# @TODO: Use named parameters instead of relying on parameter order
5
5
# ------------------------------------------------------------------------------
6
6
# @TODO: Add parameter to set author
12
12
# @FIXME: Add "aggressive" mode that creates a commit on the source branch (?before/after? merge)
13
13
# of the source file with all of the lines from the split file removed.
14
14
# - Using grep?
15
- # - Programatically creating a git patch?
15
+ # - Programmatically creating a git patch?
16
16
# - Doing a "reverse" patch? (http://stackoverflow.com/questions/16059771/reverse-apply-a-commit-to-working-copy)
17
17
# - Creating a reverse diff? (http://stackoverflow.com/a/3902431/153049)
18
18
# - Use patch created with GNU diff and use patch --reverse? (http://www.gnu.org/software/diffutils/manual/html_node/Reversed-Patches.html)
19
19
# So many options.
20
20
#
21
21
# grep -Fvxf <remove> <all-lines>
22
- # / ==============================================================================
22
+ # = ==============================================================================
23
23
24
24
# /==============================================================================
25
25
# / GIT SPLIT FILE
37
37
# # Call --help for more details
38
38
# #
39
39
# / Usually when you want to split a file into several files under git, you would
40
- # / lose the git history of this file. Often this is not desirable. The goal of
40
+ # / loose the git history of this file. Often this is not desirable. The goal of
41
41
# / this script is to enable splitting one file under Git revision control into
42
- # / multiple files whilst keeping the files git history intact.
42
+ # / multiple files whilst keeping the file's git history intact.
43
43
# /
44
44
# / For this script to work, you need to first create a folder that contains the
45
45
# / end result you want. This means you need to manually split the content of the
56
56
# / ------------------------------------------------------------------------------
57
57
# / The following ExitCodes are used:
58
58
# /
59
- # / 0 : Everything OK
60
- # / 64 : Undefined Error
59
+ # / 0 : Everything OK
60
+ # / 64 : Undefined Error
61
61
# /
62
62
# / 65 : Not enough parameters given
63
63
# / 66 : The given root file does not exist
@@ -128,7 +128,7 @@ error() {
128
128
# ==============================================================================
129
129
# sed -l basically makes sed replace and buffer through stdin to stdout
130
130
# so you get updates while the command runs and dont wait for the end
131
- # e.g. npm install | indent
131
+ # e.g. npm install
132
132
# ------------------------------------------------------------------------------
133
133
indent () {
134
134
# if an arg is given it's a flag indicating we shouldn't indent the first line,
@@ -233,6 +233,8 @@ shortUsage() {
233
233
# ------------------------------------------------------------------------------
234
234
fullUsage () {
235
235
grep ' ^#/' < " $0 " | cut -c4-
236
+
237
+ shortUsage
236
238
}
237
239
# ==============================================================================
238
240
@@ -342,7 +344,7 @@ getCurrentBranch() {
342
344
343
345
commit () {
344
346
printStatus ' Creating commit'
345
- git commit --author=" ${g_sAuthor} " --message=" ${1} ." | indent
347
+ git commit --author=" ${g_sAuthor} " --message=" ${1} ."
346
348
}
347
349
348
350
createBranch () {
@@ -351,8 +353,8 @@ createBranch() {
351
353
sBranchName=" ${1} "
352
354
sStartBranch=" ${2} "
353
355
354
- # git checkout -b "${sBranchName}" "${sStartBranch}" | indent
355
- git branch " ${sBranchName} " " ${sStartBranch} " | indent
356
+ # git checkout -b "${sBranchName}" "${sStartBranch}"
357
+ git branch " ${sBranchName} " " ${sStartBranch} "
356
358
}
357
359
358
360
createSourceBranch () {
@@ -375,7 +377,7 @@ createSplitBranch() {
375
377
376
378
checkoutBranch () {
377
379
printStatus " Switching to ${2} branch"
378
- git checkout " ${1} " | indent
380
+ git checkout " ${1} "
379
381
}
380
382
381
383
checkoutSplitBranch () {
@@ -408,16 +410,19 @@ mergeSplitBranch() {
408
410
# shellcheck disable=SC2086
409
411
if [[ -n " $( git show-ref refs/heads/${sBranchName} ) " ]]; then
410
412
printStatus " Branch '${sBranchName} ' exists"
411
- git merge --no-ff --no-edit -X theirs " ${sBranchName} " | indent || iResult=" $? "
412
413
413
- if [[ " ${iResult} " -eq 0 ]]; then
414
- printStatus ' No merge conflict'
415
- else
414
+ (
415
+ git merge --no-ff --no-edit -X theirs " ${sBranchName} " \
416
+ && printStatus ' No merge conflict'
417
+ ) || (
416
418
printStatus ' Merge conflict occurred. Attempting to resolve.'
417
- git add -- " ${g_sSourceFilePath} " | indent
418
-
419
- commit " Merging split file '${g_sSourceFileName} '"
420
- fi
419
+ git add -- " ${g_sSourceFilePath} " \
420
+ && commit " Merging split file '${g_sSourceFileName} '"
421
+ ) || (
422
+ printStatus ' Merge conflict remains. Attempting to resolve more aggressively.'
423
+ git add $( cat git-status.log | grep -o -E ' added by us: .*' | cut -d ' :' -f 2) \
424
+ && commit " Merging split file '${g_sSourceFileName} '"
425
+ )
421
426
else
422
427
printStatus " Branch does not exist. No need to merge"
423
428
fi
@@ -430,7 +435,7 @@ renameFile() {
430
435
431
436
if [[ ! -f " ${g_sSourceFilePath} " ]]; then
432
437
printStatus " File '${g_sSourceFilePath} ' does not exist. Checking out from '${g_sRootBranch} '"
433
- git checkout " ${g_sRootBranch} " -- " ${g_sSourceFilePath} " | indent
438
+ git checkout " ${g_sRootBranch} " -- " ${g_sSourceFilePath} "
434
439
fi
435
440
436
441
if [[ ! -d " ${g_sTargetDirectory} " ]]; then
@@ -443,7 +448,7 @@ renameFile() {
443
448
printStatus " File is root file '${g_sSourceFileName} ', no need to rename"
444
449
else
445
450
printStatus " Creating separate file for '${sFile} '"
446
- git mv " ${g_sSourceFilePath} " " ${g_sTargetDirectory} /${sFile} " | indent
451
+ git mv " ${g_sSourceFilePath} " " ${g_sTargetDirectory} /${sFile} "
447
452
commit " Adds separate file for '${sFile} '"
448
453
fi
449
454
}
@@ -464,15 +469,15 @@ commitFileContent() {
464
469
fi
465
470
466
471
cat " ${g_sSplitDirectory} /${sFile} " > " ${sTargetFile} "
467
- git add " ${sTargetFile} " | indent
472
+ git add " ${sTargetFile} "
468
473
commit " ${sMessage} "
469
474
}
470
475
471
476
createSubBranches () {
472
477
local sFile
473
478
474
479
printTopic ' Creating sub-branches'
475
- for sFile in ${g_sSplitDirectory} /* ; do
480
+ for sFile in " ${g_sSplitDirectory} /" * ; do
476
481
# if [[ "${sFile}" = "${g_sSourceFileName}" ]];then
477
482
# printStatus "Skipping branch for source file '${g_sSourceFileName}'"
478
483
# else
@@ -484,11 +489,11 @@ createSubBranches() {
484
489
splitFiles () {
485
490
local sFile sFileName
486
491
487
- for sFile in ${g_sSplitDirectory} /* ; do
492
+ for sFile in " ${g_sSplitDirectory} /" * ; do
488
493
sFileName=$( basename " ${sFile} " )
489
- # if [[ "${sFile }" = "${g_sSourceFileName}" ]];then
490
- # printStatus "Skipping source file '${g_sSourceFileName}'"
491
- # else
494
+ if [[ " ${sFileName } " = " ${g_sSourceFileName} " ]]; then
495
+ printTopic " Skipping source file '${g_sSourceFileName} '"
496
+ else
492
497
printTopic " Running split processing for file '${sFile} '"
493
498
494
499
printDebug " sFile = ${sFile} "
@@ -497,7 +502,7 @@ splitFiles() {
497
502
checkoutSplitBranch " ${sFile} "
498
503
renameFile " ${sFileName} "
499
504
commitFileContent " ${sFileName} "
500
- # fi
505
+ fi
501
506
done
502
507
}
503
508
@@ -508,8 +513,8 @@ mergeSplitBranches() {
508
513
509
514
checkoutSourceBranch
510
515
511
- for sFile in ${g_sSplitDirectory} /* ; do
512
- if [[ " ${sFile} " = " ${g_sSourceFileName} " ]]; then
516
+ for sFile in " ${g_sSplitDirectory} /" * ; do
517
+ if [[ " $( basename $ {sFile}) " = " $( basename $ {g_sSourceFileName}) " ]]; then
513
518
printTopic " Skipping source file '${g_sSourceFileName} '"
514
519
else
515
520
printTopic " Running merge processing for file '${sFile} '"
@@ -524,33 +529,38 @@ runCleanup() {
524
529
local sBranchName sFile
525
530
526
531
if [[ " ${g_sSourceBranch:- } " && " ${g_sSplitDirectory:- } " ]]; then
527
- read -n1 -p ' Remove all created branches ? (y/n) ' sContinue
532
+ read -n1 -p ' Cleanup all the things ? (y/n) ' sContinue
528
533
echo " "
529
534
530
535
if [[ " ${sContinue} " = ' y' ]]; then
531
536
printStatus ' Removing all the split branches that were created'
532
537
533
- git branch -D " ${g_sSourceBranch} " | indent
538
+ if [[ ${g_bInsideGitRepo} = true && " ${g_sRootBranch} " != " $( getCurrentBranch) " ]]; then
539
+ git merge --abort
540
+ checkoutRootBranch
541
+ fi
542
+
543
+ git branch -D " ${g_sSourceBranch} "
534
544
535
- for sFile in ${g_sSplitDirectory} /* ; do
545
+ for sFile in " ${g_sSplitDirectory} /" * ; do
536
546
sBranchName=$( createBranchName " ${sFile} " )
537
547
538
548
# shellcheck disable=SC2086
539
549
if [[ -n " $( git show-ref refs/heads/${sBranchName} ) " ]]; then
540
550
# Branch exists
541
- git branch -D " ${sBranchName} " | indent
551
+ git branch -D " ${sBranchName} "
542
552
fi
543
553
done
544
554
sBranchName=$( createBranchName " ${g_sSourceFileName} " )
545
555
546
556
# shellcheck disable=SC2086
547
557
if [[ -n " $( git show-ref refs/heads/${sBranchName} ) " ]]; then
548
558
# Branch exists
549
- git branch -D " ${sBranchName} " | indent
559
+ git branch -D " ${sBranchName} "
550
560
fi
551
561
552
562
else
553
- printStatus ' Leaving all branches in place .'
563
+ printStatus ' Leaving everything as-is .'
554
564
fi
555
565
fi
556
566
@@ -559,7 +569,7 @@ runCleanup() {
559
569
560
570
printHeader () {
561
571
562
- printMessage " running $0 "
572
+ printMessage " running $( basename $0 ) "
563
573
printMessage " for source file ${g_sSourceFilePath} "
564
574
printMessage " with source directory ${g_sSplitDirectory} "
565
575
printMessage " to target directory ${g_sTargetDirectory} "
@@ -591,21 +601,27 @@ run() {
591
601
# Process all non-source files
592
602
createSubBranches
593
603
splitFiles
594
-
595
- # ---
596
- # @TODO: Utilize the strategy flag.
597
- # ---
598
- # printTopic "Running split process for source file '${g_sSourceFileName}'"
599
- # checkoutSourceBranch
600
- # commitFileContent "${g_sSourceFileName}"
601
-
602
604
mergeSplitBranches
603
- # @NOTE: If a branch for g_sSourceFileName is present we need to merge that last
605
+
606
+ # Process the source file
607
+ printTopic " Running split process for source file '${g_sSourceFileName} '"
608
+ checkoutSourceBranch
609
+
610
+ if [[ ${g_sStrategy} = ' KEEP' ]]; then
611
+ echo ' @CHECKME: Nothing to do?'
612
+ elif [[ ${g_sStrategy} = ' MOVE' ]]; then
613
+ commitFileContent " ${g_sSourceFileName} "
614
+ elif [[ ${g_sStrategy} = ' DELETE' ]]; then
615
+ git rm " ${g_sSourceFilePath} "
616
+ commit " Removes '${g_sSourceFilePath} ' file that has been split."
617
+ else
618
+ error " Unsupported merge strategy '${g_sStrategy} '" 70
619
+ fi
604
620
mergeSplitBranch " ${g_sSourceFileName} "
605
621
606
622
printTopic " Merging source branch '${g_sSourceBranch} ' into the root branch '${g_sRootBranch} '"
607
623
checkoutRootBranch
608
- git merge --no-ff --no-edit " ${g_sSourceBranch} " | indent
624
+ git merge --no-ff --no-edit " ${g_sSourceBranch} "
609
625
else
610
626
printMessage ' Aborting.'
611
627
fi
@@ -633,10 +649,6 @@ finish() {
633
649
printDebug " Not in a git repo"
634
650
fi
635
651
636
- if [[ ${g_bInsideGitRepo} = true && " ${g_sRootBranch} " != " $( getCurrentBranch) " ]]; then
637
- checkoutRootBranch
638
- fi
639
-
640
652
runCleanup
641
653
642
654
printMessage ' Done.'
@@ -671,7 +683,7 @@ export PS4='$(printf "%04d: " $LINENO)'
671
683
672
684
registerTraps
673
685
674
- handleParams " ${@:- } "
686
+ handleParams " ${@ } "
675
687
676
688
registerDebugTrap
677
689
0 commit comments