@@ -69,21 +69,56 @@ git_setup_local_mirrored_repo() {
69
69
error_exit " Failed: git clone --mirror '${GIT_URL} ' '${local_mirrored_repo} '"
70
70
}
71
71
72
+ git_checkout_into_temp_dir () {
73
+ # Depends on $local_mirrored_repo
74
+ # Accepts two args:
75
+ # $1 - dir to deploy to
76
+ # $2 - refspec to deploy - a git tagname, branch, or commit hash.
77
+ # $3 - project subdirectory inside the repository to deploy.
78
+
79
+ # The '^0' at the end of the refspec
80
+ # populates HEAD with the SHA of the commit
81
+ # rather than potentially using a git branch name.
82
+ # Also see http://stackoverflow.com/a/13293010/5419599
83
+ # and https://github.com/cfengine/core/pull/2465#issuecomment-173656475
84
+
85
+ # Checkout with optional subdirectory if not empty
86
+ if [ -n " $3 " ]; then
87
+ # checkout subdirectory only
88
+ git --git-dir=" ${local_mirrored_repo} " --work-tree=" ${temp_stage} " checkout -q -f " ${2} ^0" -- " $3 " ||
89
+ error_exit " Failed to checkout subdirectory '$3 ' from '$2 '"
90
+ # move contents of subdirectory to the temp_stage root
91
+ # include dotfile matching in wildcard expansions to move them as well
92
+ shopt -s dotglob
93
+ mv " ${temp_stage} /${3} " /* " ${temp_stage} /"
94
+ # disable dotfile matching
95
+ shopt -u dotglob
96
+ # remove empty subdirectory from the temp_stage
97
+ rmdir " ${temp_stage} /${3} "
98
+ else
99
+ # checkout the whole repository
100
+ git --git-dir=" ${local_mirrored_repo} " --work-tree=" ${temp_stage} " checkout -q -f " ${2} ^0" ||
101
+ error_exit " Failed to checkout '$2 ' from '${local_mirrored_repo} '"
102
+ fi
103
+ }
104
+
72
105
git_deploy_refspec () {
73
106
# Contributed by Mike Weilgart
74
107
75
108
# Depends on $local_mirrored_repo
76
109
# Accepts two args:
77
110
# $1 - dir to deploy to
78
111
# $2 - refspec to deploy - a git tagname, branch, or commit hash.
112
+ # $3 - project subdirectory inside the repository to deploy.
79
113
80
114
# This function
81
115
# 1. creates an empty temp dir,
82
116
# 2. checks out the refspec into the empty temp dir,
83
117
# (including populating .git/HEAD in the temp dir),
84
- # 3. sets appropriate permissions on the policy set,
85
- # 4. validates the policy set using cf-promises,
86
- # 5. moves the temp dir policy set into the given deploy dir,
118
+ # 3. if the project subdirectory is set, it checks it out and moves files from it to the root of the temp dir.
119
+ # 4. sets appropriate permissions on the policy set,
120
+ # 5. validates the policy set using cf-promises,
121
+ # 6. moves the temp dir policy set into the given deploy dir,
87
122
# avoiding triggering policy updates unnecessarily
88
123
# by comparing the cf_promises_validated flag file.
89
124
# (See long comment at end of function def.)
@@ -105,13 +140,8 @@ git_deploy_refspec() {
105
140
trap ' rm -rf "$temp_stage"' EXIT
106
141
107
142
# ######################### 2. CHECKOUT INTO TEMP DIR
108
- # The '^0' at the end of the refspec
109
- # populates HEAD with the SHA of the commit
110
- # rather than potentially using a git branch name.
111
- # Also see http://stackoverflow.com/a/13293010/5419599
112
- # and https://github.com/cfengine/core/pull/2465#issuecomment-173656475
113
- git --git-dir=" ${local_mirrored_repo} " --work-tree=" ${temp_stage} " checkout -q -f " ${2} ^0" ||
114
- error_exit " Failed to checkout '$2 ' from '${local_mirrored_repo} '"
143
+ git_checkout_into_temp_dir " $1 " " $2 " " $3 "
144
+
115
145
# Grab HEAD so it can be used to populate cf_promises_release_id
116
146
mkdir -p " ${temp_stage} /.git"
117
147
cp " ${local_mirrored_repo} /HEAD" " ${temp_stage} /.git/"
@@ -180,15 +210,17 @@ git_cfbs_deploy_refspec() {
180
210
# Accepts two args:
181
211
# $1 - dir to deploy to
182
212
# $2 - refspec to deploy - a git tagname, branch, or commit hash.
213
+ # $3 - project subdirectory inside the repository to deploy.
183
214
184
215
# This function
185
216
# 1. creates an empty temp dir,
186
217
# 2. checks out the refspec into the empty temp dir,
187
218
# (including populating .git/HEAD in the temp dir),
188
- # 3. builds policy with cfbs
189
- # 4. sets appropriate permissions on the policy set,
190
- # 5. validates the policy set using cf-promises,
191
- # 6. moves the temp dir policy set into the given deploy dir,
219
+ # 3. if the project subdirectory is set, it checks it out and moves files from it to the root of the temp dir.
220
+ # 4. builds policy with cfbs
221
+ # 5. sets appropriate permissions on the policy set,
222
+ # 6. validates the policy set using cf-promises,
223
+ # 7. moves the temp dir policy set into the given deploy dir,
192
224
# avoiding triggering policy updates unnecessarily
193
225
# by comparing the cf_promises_validated flag file.
194
226
# (See long comment at end of function def.)
@@ -222,13 +254,7 @@ git_cfbs_deploy_refspec() {
222
254
trap ' rm -rf "$temp_stage"' EXIT
223
255
224
256
# ######################### 2. CHECKOUT INTO TEMP DIR
225
- # The '^0' at the end of the refspec
226
- # populates HEAD with the SHA of the commit
227
- # rather than potentially using a git branch name.
228
- # Also see http://stackoverflow.com/a/13293010/5419599
229
- # and https://github.com/cfengine/core/pull/2465#issuecomment-173656475
230
- git --git-dir=" ${local_mirrored_repo} " --work-tree=" ${temp_stage} " checkout -q -f " ${2} ^0" ||
231
- error_exit " Failed to checkout '$2 ' from '${local_mirrored_repo} '"
257
+ git_checkout_into_temp_dir " $1 " " $2 " " $3 "
232
258
233
259
# ######################### 3. cfbs build
234
260
# Remember what directory we were in when we started.
@@ -382,7 +408,7 @@ git_stage_policy_channels() {
382
408
383
409
git_masterstage () {
384
410
# $1 -- whether to check only [true/false; optional]
385
- # Depends on $GIT_URL, $ROOT, $MASTERDIR, $GIT_REFSPEC
411
+ # Depends on $GIT_URL, $ROOT, $MASTERDIR, $GIT_REFSPEC, $PROJECT_SUBDIRECTORY
386
412
check_git_installed
387
413
git_setup_local_mirrored_repo " $( dirname " $ROOT " ) "
388
414
if [ " x$1 " = " xtrue" ]; then
@@ -392,8 +418,8 @@ git_masterstage() {
392
418
return 0 # not in sync => update available
393
419
fi
394
420
fi
395
- git_deploy_refspec " $MASTERDIR " " ${GIT_REFSPEC} "
396
- echo " Successfully deployed '${GIT_REFSPEC} ' from '${GIT_URL} ' to '${MASTERDIR} ' on $( date) "
421
+ git_deploy_refspec " $MASTERDIR " " ${GIT_REFSPEC} " " ${PROJECT_SUBDIRECTORY} "
422
+ echo " Successfully deployed '${GIT_REFSPEC} ' from '${GIT_URL} '$( [[ -n " ${PROJECT_SUBDIRECTORY} " ]] && echo " subdirectory: ' ${PROJECT_SUBDIRECTORY} ' " ) to '${MASTERDIR} ' on $( date) "
397
423
}
398
424
399
425
git_cfbs_masterstage () {
@@ -409,8 +435,8 @@ git_cfbs_masterstage() {
409
435
return 0 # not in sync => update available
410
436
fi
411
437
fi
412
- git_cfbs_deploy_refspec " $MASTERDIR " " ${GIT_REFSPEC} "
413
- echo " Successfully built and deployed '${GIT_REFSPEC} ' from '${GIT_URL} ' to '${MASTERDIR} ' on $( date) with cfbs"
438
+ git_cfbs_deploy_refspec " $MASTERDIR " " ${GIT_REFSPEC} " " ${PROJECT_SUBDIRECTORY} "
439
+ echo " Successfully built and deployed '${GIT_REFSPEC} ' from '${GIT_URL} '$( [[ -n " ${PROJECT_SUBDIRECTORY} " ]] && echo " subdirectory: ' ${PROJECT_SUBDIRECTORY} ' " ) to '${MASTERDIR} ' on $( date) with cfbs"
414
440
}
415
441
416
442
svn_branch () {
0 commit comments