Skip to content

Commit 1731d22

Browse files
authored
feat(3333): Add skip git clone variable [2] (#248)
1 parent df1acad commit 1731d22

14 files changed

+111
-83
lines changed

index.js

Lines changed: 98 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -692,7 +692,8 @@ class GithubScm extends Scm {
692692
// Set sparse option
693693
trimIndentJoin([
694694
'if [ ! -z "$GIT_SPARSE_CHECKOUT_PATH" ]; then',
695-
' export GIT_SPARSE_OPTION="--no-checkout";else',
695+
' export GIT_SPARSE_OPTION="--no-checkout";',
696+
'else',
696697
' export GIT_SPARSE_OPTION="";',
697698
'fi'
698699
])
@@ -746,9 +747,15 @@ class GithubScm extends Scm {
746747
'fi'
747748
]),
748749
// Set config
749-
'echo Setting user name and user email',
750-
`$SD_GIT_WRAPPER "git config --global user.name ${this.config.username}"`,
751-
`$SD_GIT_WRAPPER "git config --global user.email ${this.config.email}"`,
750+
trimIndentJoin([
751+
'if [ ! -z $SD_SKIP_REPOSITORY_CLONE ] && [ $SD_SKIP_REPOSITORY_CLONE = true ] && ! $SD_GIT_WRAPPER "git -v" >/dev/null 2>&1; then',
752+
` echo 'Skipping git config';`,
753+
'else',
754+
' echo "Setting user name and user email";',
755+
` $SD_GIT_WRAPPER "git config --global user.name ${this.config.username}";`,
756+
` $SD_GIT_WRAPPER "git config --global user.email ${this.config.email}";`,
757+
'fi'
758+
]),
752759
// Set final checkout dir, default to SD_SOURCE_DIR for backward compatibility
753760
'export SD_CHECKOUT_DIR_FINAL=$SD_SOURCE_DIR',
754761
trimIndentJoin([
@@ -778,35 +785,37 @@ class GithubScm extends Scm {
778785
]),
779786
// Git clone
780787
`export SD_CONFIG_DIR=${externalConfigDir}`,
781-
`echo 'Cloning external config repo ${parentCheckoutUrl}'`,
782788
trimIndentJoin([
783-
'if [ ! -z $GIT_SHALLOW_CLONE ] && [ $GIT_SHALLOW_CLONE = false ]; then',
784-
` $SD_GIT_WRAPPER "git clone $GIT_SPARSE_OPTION $GIT_RECURSIVE_OPTION --quiet --progress --branch '${escapedParentBranch}' $CONFIG_URL $SD_CONFIG_DIR";`,
789+
'if [ ! -z $SD_SKIP_REPOSITORY_CLONE ] && [ $SD_SKIP_REPOSITORY_CLONE = true ]; then',
790+
` echo 'Skipping cloning ${checkoutUrl}, on branch ${singleQuoteEscapedBranch}';`,
785791
'else',
786-
' if [ ! -z "$GIT_SHALLOW_CLONE_SINCE" ]; then',
787-
' export GIT_SHALLOW_CLONE_DEPTH_OPTION="--shallow-since=\'$GIT_SHALLOW_CLONE_SINCE\'";',
792+
` echo 'Cloning external config repo ${parentCheckoutUrl}';`,
793+
' if [ ! -z $GIT_SHALLOW_CLONE ] && [ $GIT_SHALLOW_CLONE = false ]; then',
794+
` $SD_GIT_WRAPPER "git clone $GIT_SPARSE_OPTION $GIT_RECURSIVE_OPTION --quiet --progress --branch '${escapedParentBranch}' $CONFIG_URL $SD_CONFIG_DIR";`,
788795
' else',
789-
' if [ -z $GIT_SHALLOW_CLONE_DEPTH ]; then',
790-
' export GIT_SHALLOW_CLONE_DEPTH=50;',
796+
' if [ ! -z "$GIT_SHALLOW_CLONE_SINCE" ]; then',
797+
' export GIT_SHALLOW_CLONE_DEPTH_OPTION="--shallow-since=\'$GIT_SHALLOW_CLONE_SINCE\'";',
798+
' else',
799+
' if [ -z $GIT_SHALLOW_CLONE_DEPTH ]; then',
800+
' export GIT_SHALLOW_CLONE_DEPTH=50;',
801+
' fi;',
802+
' export GIT_SHALLOW_CLONE_DEPTH_OPTION="--depth=$GIT_SHALLOW_CLONE_DEPTH";',
803+
' fi;',
804+
' export GIT_SHALLOW_CLONE_BRANCH="--no-single-branch";',
805+
' if [ "$GIT_SHALLOW_CLONE_SINGLE_BRANCH" = true ]; then',
806+
' export GIT_SHALLOW_CLONE_BRANCH="";',
791807
' fi;',
792-
' export GIT_SHALLOW_CLONE_DEPTH_OPTION="--depth=$GIT_SHALLOW_CLONE_DEPTH";',
808+
` $SD_GIT_WRAPPER "git clone $GIT_SHALLOW_CLONE_DEPTH_OPTION $GIT_SHALLOW_CLONE_BRANCH $GIT_SPARSE_OPTION $GIT_RECURSIVE_OPTION --quiet --progress --branch '${escapedParentBranch}' $CONFIG_URL $SD_CONFIG_DIR";`,
793809
' fi;',
794-
' export GIT_SHALLOW_CLONE_BRANCH="--no-single-branch";',
795-
' if [ "$GIT_SHALLOW_CLONE_SINGLE_BRANCH" = true ]; then',
796-
' export GIT_SHALLOW_CLONE_BRANCH="";',
810+
// Sparse Checkout
811+
' if [ ! -z "$GIT_SPARSE_CHECKOUT_PATH" ]; then',
812+
' $SD_GIT_WRAPPER "git sparse-checkout set $GIT_SPARSE_CHECKOUT_PATH" && $SD_GIT_WRAPPER "git checkout";',
797813
' fi;',
798-
` $SD_GIT_WRAPPER "git clone $GIT_SHALLOW_CLONE_DEPTH_OPTION $GIT_SHALLOW_CLONE_BRANCH $GIT_SPARSE_OPTION $GIT_RECURSIVE_OPTION --quiet --progress --branch '${escapedParentBranch}' $CONFIG_URL $SD_CONFIG_DIR";`,
799-
'fi'
800-
]),
801-
// Sparse Checkout
802-
trimIndentJoin([
803-
'if [ ! -z "$GIT_SPARSE_CHECKOUT_PATH" ];then',
804-
' $SD_GIT_WRAPPER "git sparse-checkout set $GIT_SPARSE_CHECKOUT_PATH" && $SD_GIT_WRAPPER "git checkout";',
814+
// Reset to SHA
815+
` $SD_GIT_WRAPPER "git -C $SD_CONFIG_DIR reset --hard ${config.parentConfig.sha} --";`,
816+
` echo Reset external config repo to ${config.parentConfig.sha};`,
805817
'fi'
806-
]),
807-
// Reset to SHA
808-
`$SD_GIT_WRAPPER "git -C $SD_CONFIG_DIR reset --hard ${config.parentConfig.sha} --"`,
809-
`echo Reset external config repo to ${config.parentConfig.sha}`
818+
])
810819
);
811820
}
812821

@@ -854,47 +863,49 @@ class GithubScm extends Scm {
854863
const resetEchoSha = config.prRef ? singleQuoteEscapedBranch : config.sha;
855864

856865
command.push(
857-
// Git clone
858-
`echo 'Cloning ${checkoutUrl}, on branch ${singleQuoteEscapedBranch}'`,
859866
trimIndentJoin([
860-
'if [ ! -z $GIT_SHALLOW_CLONE ] && [ $GIT_SHALLOW_CLONE = false ]; then',
861-
` $SD_GIT_WRAPPER "git clone $GIT_SPARSE_OPTION $GIT_RECURSIVE_OPTION --quiet --progress --branch '${doubleQuoteEscapedBranch}' $SCM_URL $SD_CHECKOUT_DIR_FINAL";`,
867+
// Git clone
868+
'if [ ! -z $SD_SKIP_REPOSITORY_CLONE ] && [ $SD_SKIP_REPOSITORY_CLONE = true ]; then',
869+
` echo 'Skipping cloning ${checkoutUrl}, on branch ${singleQuoteEscapedBranch}';`,
862870
'else',
863-
' if [ ! -z "$GIT_SHALLOW_CLONE_SINCE" ]; then',
864-
' export GIT_SHALLOW_CLONE_DEPTH_OPTION="--shallow-since=\'$GIT_SHALLOW_CLONE_SINCE\'";',
871+
` echo 'Cloning ${checkoutUrl}, on branch ${singleQuoteEscapedBranch}';`,
872+
' if [ ! -z $GIT_SHALLOW_CLONE ] && [ $GIT_SHALLOW_CLONE = false ]; then',
873+
` $SD_GIT_WRAPPER "git clone $GIT_SPARSE_OPTION $GIT_RECURSIVE_OPTION --quiet --progress --branch '${doubleQuoteEscapedBranch}' $SCM_URL $SD_CHECKOUT_DIR_FINAL";`,
865874
' else',
866-
' if [ -z $GIT_SHALLOW_CLONE_DEPTH ]; then',
867-
' export GIT_SHALLOW_CLONE_DEPTH=50;',
875+
' if [ ! -z "$GIT_SHALLOW_CLONE_SINCE" ]; then',
876+
' export GIT_SHALLOW_CLONE_DEPTH_OPTION="--shallow-since=\'$GIT_SHALLOW_CLONE_SINCE\'";',
877+
' else',
878+
' if [ -z $GIT_SHALLOW_CLONE_DEPTH ]; then',
879+
' export GIT_SHALLOW_CLONE_DEPTH=50;',
880+
' fi;',
881+
' export GIT_SHALLOW_CLONE_DEPTH_OPTION="--depth=$GIT_SHALLOW_CLONE_DEPTH";',
882+
' fi;',
883+
' export GIT_SHALLOW_CLONE_BRANCH="--no-single-branch";',
884+
' if [ "$GIT_SHALLOW_CLONE_SINGLE_BRANCH" = true ]; then',
885+
' export GIT_SHALLOW_CLONE_BRANCH="";',
868886
' fi;',
869-
' export GIT_SHALLOW_CLONE_DEPTH_OPTION="--depth=$GIT_SHALLOW_CLONE_DEPTH";',
887+
` $SD_GIT_WRAPPER "git clone $GIT_SHALLOW_CLONE_DEPTH_OPTION $GIT_SHALLOW_CLONE_BRANCH $GIT_SPARSE_OPTION $GIT_RECURSIVE_OPTION --quiet --progress --branch '${doubleQuoteEscapedBranch}' $SCM_URL $SD_CHECKOUT_DIR_FINAL";`,
870888
' fi;',
871-
' export GIT_SHALLOW_CLONE_BRANCH="--no-single-branch";',
872-
' if [ "$GIT_SHALLOW_CLONE_SINGLE_BRANCH" = true ]; then',
873-
' export GIT_SHALLOW_CLONE_BRANCH="";',
889+
// Sparse Checkout
890+
' if [ ! -z "$GIT_SPARSE_CHECKOUT_PATH" ]; then',
891+
' $SD_GIT_WRAPPER "git sparse-checkout set $GIT_SPARSE_CHECKOUT_PATH" && $SD_GIT_WRAPPER "git checkout";',
874892
' fi;',
875-
` $SD_GIT_WRAPPER "git clone $GIT_SHALLOW_CLONE_DEPTH_OPTION $GIT_SHALLOW_CLONE_BRANCH $GIT_SPARSE_OPTION $GIT_RECURSIVE_OPTION --quiet --progress --branch '${doubleQuoteEscapedBranch}' $SCM_URL $SD_CHECKOUT_DIR_FINAL";`,
876-
'fi'
877-
]),
878-
// Sparse Checkout
879-
trimIndentJoin([
880-
'if [ ! -z "$GIT_SPARSE_CHECKOUT_PATH" ];then',
881-
' $SD_GIT_WRAPPER "git sparse-checkout set $GIT_SPARSE_CHECKOUT_PATH" && $SD_GIT_WRAPPER "git checkout";',
893+
// Reset to SHA
894+
...(!config.prRef
895+
? [
896+
trimIndentJoin([
897+
'if [ ! -z $GIT_SHALLOW_CLONE ] && [ $GIT_SHALLOW_CLONE = false ]; then',
898+
` $SD_GIT_WRAPPER "git fetch origin '${config.sha}'";`,
899+
'else',
900+
` $SD_GIT_WRAPPER "git fetch $GIT_SHALLOW_CLONE_DEPTH_OPTION origin '${config.sha}'";`,
901+
'fi;'
902+
])
903+
]
904+
: []),
905+
` $SD_GIT_WRAPPER "git reset --hard '${resetSha}' --";`,
906+
` echo 'Reset to ${resetEchoSha}';`,
882907
'fi'
883-
]),
884-
// Reset to SHA
885-
...(!config.prRef
886-
? [
887-
trimIndentJoin([
888-
'if [ ! -z $GIT_SHALLOW_CLONE ] && [ $GIT_SHALLOW_CLONE = false ]; then',
889-
` $SD_GIT_WRAPPER "git fetch origin '${config.sha}'";`,
890-
'else',
891-
` $SD_GIT_WRAPPER "git fetch $GIT_SHALLOW_CLONE_DEPTH_OPTION origin '${config.sha}'";`,
892-
'fi'
893-
])
894-
]
895-
: []),
896-
`$SD_GIT_WRAPPER "git reset --hard '${resetSha}' --"`,
897-
`echo 'Reset to ${resetEchoSha}'`
908+
])
898909
);
899910
}
900911

@@ -908,14 +919,20 @@ class GithubScm extends Scm {
908919

909920
// Fetch a pull request
910921
command.push(
911-
`echo 'Fetching PR ${prRef}'`,
912-
`$SD_GIT_WRAPPER "git fetch origin ${prRef}"`,
913922
`export PR_BASE_BRANCH_NAME='${singleQuoteEscapedBranch}'`,
914923
`export PR_BRANCH_NAME='${baseRepo}/${singleQuoteEscapedPrBranch}'`,
915-
`echo 'Checking out the PR branch ${singleQuoteEscapedPrBranch}'`,
916-
`$SD_GIT_WRAPPER "git checkout ${LOCAL_BRANCH_NAME}"`,
917-
`$SD_GIT_WRAPPER "git merge '${doubleQuoteEscapedBranch}'"`,
918-
`export GIT_BRANCH=origin/refs/${prRef}`
924+
`export GIT_BRANCH=origin/refs/${prRef}`,
925+
trimIndentJoin([
926+
'if [ ! -z $SD_SKIP_REPOSITORY_CLONE ] && [ $SD_SKIP_REPOSITORY_CLONE = true ]; then',
927+
` echo 'Skipping fetching PR ${prRef}';`,
928+
'else',
929+
` echo 'Fetching PR ${prRef}';`,
930+
` $SD_GIT_WRAPPER "git fetch origin ${prRef}";`,
931+
` echo 'Checking out the PR branch ${singleQuoteEscapedPrBranch}';`,
932+
` $SD_GIT_WRAPPER "git checkout ${LOCAL_BRANCH_NAME}";`,
933+
` $SD_GIT_WRAPPER "git merge '${doubleQuoteEscapedBranch}'";`,
934+
'fi'
935+
])
919936
);
920937
} else {
921938
command.push(`export GIT_BRANCH='origin/${singleQuoteEscapedBranch}'`);
@@ -925,10 +942,14 @@ class GithubScm extends Scm {
925942
// Init & Update submodule only when sd-repo is not used
926943
command.push(
927944
trimIndentJoin([
928-
'if [ ! -z $GIT_RECURSIVE_CLONE ] && [ $GIT_RECURSIVE_CLONE = false ]; then',
929-
' $SD_GIT_WRAPPER "git submodule init";',
945+
'if [ ! -z $SD_SKIP_REPOSITORY_CLONE ] && [ $SD_SKIP_REPOSITORY_CLONE = true ]; then',
946+
` echo 'Skipping submodule init and update';`,
930947
'else',
931-
' $SD_GIT_WRAPPER "git submodule update --init --recursive";',
948+
' if [ ! -z $GIT_RECURSIVE_CLONE ] && [ $GIT_RECURSIVE_CLONE = false ]; then',
949+
' $SD_GIT_WRAPPER "git submodule init";',
950+
' else',
951+
' $SD_GIT_WRAPPER "git submodule update --init --recursive";',
952+
' fi;',
932953
'fi'
933954
])
934955
);
@@ -939,7 +960,14 @@ class GithubScm extends Scm {
939960
// The path is then wrapped in single quotes to safely change directories using the 'cd' command.
940961
const escapedRootDir = config.rootDir.replace(/'/g, "'\\''");
941962

942-
command.push(`cd '${escapedRootDir}'`);
963+
command.push(
964+
trimIndentJoin([
965+
'if [ ! -z $SD_SKIP_REPOSITORY_CLONE ] && [ $SD_SKIP_REPOSITORY_CLONE = true ]; then',
966+
` mkdir -p ${escapedRootDir};`,
967+
'fi'
968+
]),
969+
`cd '${escapedRootDir}'`
970+
);
943971
}
944972
}
945973

0 commit comments

Comments
 (0)