7979# DEBUG_LEVEL 3 = " and show called command
8080# DEBUG_LEVEL 4 = " and show all other commands (=set +x)
8181# DEBUG_LEVEL 5 = Show All Commands, without Debug Messages or Application Calls
82-
83- readonly DEBUG_LEVEL=0
8482# ==============================================================================
8583
8684
@@ -92,16 +90,15 @@ set -o nounset # Exit script on use of an undefined variable, same as "set
9290set -o errexit # Exit script when a command exits with non-zero status, same as "set -e"
9391set -o pipefail # Makes pipeline return the exit status of the last command in the pipe that failed
9492
95- if [ " ${DEBUG_LEVEL} " -gt 2 ]; then
96- set -o xtrace # Similar to -v, but expands commands, same as "set -x"
97- fi
98-
9993declare g_bInsideGitRepo=false
10094declare g_bShowHelp=false
10195declare -a g_aErrorMessages
10296declare -i g_iExitCode=0
10397declare -i g_iErrorCount=0
98+
10499readonly g_sBranchPrefix=' split-file'
100+ readonly g_sColorDim=$( tput dim)
101+ readonly g_sColorRestore=$( tput sgr0)
105102# ==============================================================================
106103
107104
@@ -259,15 +256,25 @@ fullUsage() {
259256# ------------------------------------------------------------------------------
260257handleParams () {
261258
262- local sParam sRootFile sSplitDirectory
259+ local iDebugLevel sParam sRootFile sSplitDirectory sTargetDirectory
260+
261+ iDebugLevel=0
263262
264263 for sParam in " $@ " ; do
265264 if [[ " ${sParam} " = " --help" ]]; then
266265 g_bShowHelp=true
266+ elif [[ " ${sParam} " = " --verbose" || " ${sParam} " = " -v" ]]; then
267+ iDebugLevel=1
268+ elif [[ " ${sParam} " = " -vv" ]]; then
269+ iDebugLevel=2
270+ elif [[ " ${sParam} " = " -vvv" ]]; then
271+ iDebugLevel=2
267272 fi
268273 done
269274
270- if [ " ${g_bShowHelp} " = false ] && [ " $# " -ne 4 ]; then
275+ readonly DEBUG_LEVEL=" ${iDebugLevel} "
276+
277+ if [[ " ${g_bShowHelp} " = false && " $# " -lt 4 ]]; then
271278 g_iExitCode=65
272279 error ' This script expects four command-line arguments'
273280 elif [[ " ${g_bShowHelp} " = false ]]; then
@@ -310,6 +317,7 @@ handleParams() {
310317 error " The given split strategy '${g_sStrategy} ' is not one of supported DELETE | KEEP | MOVE"
311318 fi
312319 fi
320+
313321 return ${g_iExitCode}
314322}
315323# ==============================================================================
@@ -318,8 +326,11 @@ handleParams() {
318326# UTILITY FUNCTIONS
319327# ##############################################################################
320328printDebug () {
329+ local aCaller
330+
321331 if [[ " ${DEBUG_LEVEL} " -gt 0 && " ${DEBUG_LEVEL} " -lt 5 ]]; then
322- debug " ${1} "
332+ aCaller=($( caller) )
333+ printf " ${g_sColorDim} [DEBUG] (line %04d): %s${g_sColorRestore} \n" " ${aCaller[0]} " " $* " >&2
323334 fi
324335}
325336
@@ -465,6 +476,10 @@ splitFiles() {
465476 # printStatus "Skipping source file '${g_sSourceFileName}'"
466477 # else
467478 printTopic " Running split processing for file '${sFile} '"
479+
480+ printDebug " sFile = ${sFile} "
481+ printDebug " sFileName = ${sFileName} "
482+
468483 checkoutSplitBranch " ${sFile} "
469484 renameFile " ${sFile} "
470485 commitFileContent " ${sFile} "
@@ -474,7 +489,9 @@ splitFiles() {
474489
475490mergeSplitBranches () {
476491 local sFile
492+
477493 printTopic ' Merging all the split branches into the source branch'
494+
478495 checkoutSourceBranch
479496
480497 for sFile in ${g_sSplitDirectory} /* ; do
@@ -579,39 +596,49 @@ run() {
579596}
580597
581598finish () {
582- if [[ ! ${g_iExitCode} -eq 0 ]]; then
583599
584- printErrorMessages " ${g_aErrorMessages[*]} "
600+ if [[ ! " ${bFinished:- } " ]]; then
601+
602+ readonly bFinished=true
585603
586- if [[ ${g_iExitCode} -eq 65 ]]; then
587- shortUsage " ${@ } "
604+ if [[ ! ${g_iExitCode} -eq 0 ]]; then
605+ printErrorMessages " ${g_aErrorMessages[*]} "
606+
607+ if [[ ${g_iExitCode} -eq 65 ]]; then
608+ shortUsage " ${@ } "
609+ fi
588610 fi
589- fi
590611
591- printDebug " Working Directory : $( pwd) "
592- if [[ ${g_bInsideGitRepo} = true ]]; then
593- printDebug " Root branch : $g_sRootBranch "
594- printDebug " Current branch : $( getCurrentBranch) "
595- else
596- printDebug " Not in a git repo"
597- fi
612+ printDebug " Working Directory : $( pwd) "
613+ if [[ ${g_bInsideGitRepo} = true ]]; then
614+ printDebug " Root branch : $g_sRootBranch "
615+ printDebug " Current branch : $( getCurrentBranch) "
616+ else
617+ printDebug " Not in a git repo"
618+ fi
598619
599- if [[ ${g_bInsideGitRepo} = true && " ${g_sRootBranch} " != " $( getCurrentBranch) " ]]; then
600- checkoutRootBranch
601- fi
620+ if [[ ${g_bInsideGitRepo} = true && " ${g_sRootBranch} " != " $( getCurrentBranch) " ]]; then
621+ checkoutRootBranch
622+ fi
602623
603- runCleanup
624+ runCleanup
604625
605- printMessage ' Done.'
626+ printMessage ' Done.'
627+ fi
606628
607629 exit ${g_iExitCode}
608630}
609631
610- registerTraps () {
632+ function debugTrapMessage {
633+ printDebug " ${g_sColorDim} [${1} :${2} ] ${3}${g_sColorRestore} "
634+ }
611635
636+ registerTraps () {
612637 trap finish EXIT
613638 trap finish ERR
639+ }
614640
641+ registerDebugTrap () {
615642 if [[ " ${DEBUG_LEVEL} " -gt 1 && " ${DEBUG_LEVEL} " -lt 5 ]]; then
616643 # Trap function is defined inline so we get the correct line number
617644 # trap '(echo -e "#[DEBUG] [$(basename ${BASH_SOURCE[0]}):${LINENO[0]}] ${BASH_COMMAND}");' DEBUG
@@ -624,9 +651,18 @@ registerTraps() {
624651# ==============================================================================
625652# RUN LOGIC
626653# ------------------------------------------------------------------------------
654+ export PS4=' $(printf "%04d: " $LINENO)'
655+
627656registerTraps
657+
628658handleParams " ${@:- } "
629659
660+ registerDebugTrap
661+
662+ if [[ " ${DEBUG_LEVEL} " -gt 2 ]]; then
663+ set -o xtrace # Similar to -v, but expands commands, same as "set -x"
664+ fi
665+
630666if [[ ${g_iExitCode} -eq 0 ]]; then
631667
632668 if [[ " ${g_bShowHelp} " = true ]]; then
0 commit comments