Skip to content
This repository was archived by the owner on May 8, 2024. It is now read-only.

Commit ce68324

Browse files
committed
Ported simplified logic from #129
1 parent f82d56d commit ce68324

File tree

1 file changed

+25
-31
lines changed

1 file changed

+25
-31
lines changed

internal/executor/executor.go

+25-31
Original file line numberDiff line numberDiff line change
@@ -201,48 +201,42 @@ func (executor *Executor) PopulateCloneAndWorkingDirEnvironmentVariables(environ
201201
result[key] = value
202202
}
203203

204-
_, hasCloneDir := result["CIRRUS_CLONE_DIR"]
205-
_, hasWorkingDir := result["CIRRUS_WORKING_DIR"]
206-
207-
if hasCloneDir && !hasWorkingDir {
208-
// Only clone was overridden. Make sure $CIRRUS_WORKING_DIR is set
209-
result["CIRRUS_WORKING_DIR"] = "$CIRRUS_CLONE_DIR"
210-
}
211-
if !hasCloneDir && hasWorkingDir {
212-
// User did override working dir
213-
if !strings.Contains(result["CIRRUS_WORKING_DIR"], "CIRRUS_CLONE_DIR") {
214-
// If working dir doesn't depend on clone dir then we can default clone dir to the one provided by user
215-
result["CIRRUS_CLONE_DIR"] = "$CIRRUS_WORKING_DIR"
216-
hasCloneDir = true
204+
if _, ok := result["CIRRUS_WORKING_DIR"]; !ok {
205+
if executor.preCreatedWorkingDir != "" {
206+
result["CIRRUS_WORKING_DIR"] = executor.preCreatedWorkingDir
207+
} else {
208+
result["CIRRUS_WORKING_DIR"] = makeScratchDir(executor)
217209
}
218210
}
219211

220-
if !hasCloneDir && !hasWorkingDir {
221-
// none of the dirs are explicitly set. Make sure they'll be the same
222-
result["CIRRUS_WORKING_DIR"] = "$CIRRUS_CLONE_DIR"
223-
}
224-
225-
if !hasCloneDir && executor.preCreatedWorkingDir != "" {
226-
// none of the dirs are explicitly set. Make sure they'll be the same
227-
result["CIRRUS_CLONE_DIR"] = executor.preCreatedWorkingDir
228-
}
229-
230212
if _, ok := result["CIRRUS_CLONE_DIR"]; !ok {
231-
defaultTempDirPath := filepath.Join(os.TempDir(), "cirrus-ci-build")
232-
if _, err := os.Stat(defaultTempDirPath); os.IsNotExist(err) {
233-
result["CIRRUS_CLONE_DIR"] = filepath.ToSlash(defaultTempDirPath)
234-
} else if executor.commandFrom != "" {
235-
// Default folder exists and we continue execution. Therefore we need to use it.
236-
result["CIRRUS_CLONE_DIR"] = filepath.ToSlash(defaultTempDirPath)
213+
// Get the working directory here again after we've
214+
// dealt with it's potential absence above
215+
workingDir := result["CIRRUS_WORKING_DIR"]
216+
217+
if strings.Contains(workingDir, "CIRRUS_CLONE_DIR") {
218+
result["CIRRUS_CLONE_DIR"] = makeScratchDir(executor)
237219
} else {
238-
uniqueTempDirPath, _ := ioutil.TempDir(os.TempDir(), fmt.Sprintf("cirrus-task-%d", executor.taskIdentification.TaskId))
239-
result["CIRRUS_CLONE_DIR"] = filepath.ToSlash(uniqueTempDirPath)
220+
result["CIRRUS_CLONE_DIR"] = workingDir
240221
}
241222
}
242223

243224
return result
244225
}
245226

227+
func makeScratchDir(executor *Executor) string {
228+
defaultTempDirPath := filepath.Join(os.TempDir(), "cirrus-ci-build")
229+
if _, err := os.Stat(defaultTempDirPath); os.IsNotExist(err) {
230+
return filepath.ToSlash(defaultTempDirPath)
231+
} else if executor.commandFrom != "" {
232+
// Default folder exists and we continue execution. Therefore we need to use it.
233+
return filepath.ToSlash(defaultTempDirPath)
234+
} else {
235+
uniqueTempDirPath, _ := ioutil.TempDir(os.TempDir(), fmt.Sprintf("cirrus-task-%d", executor.taskIdentification.TaskId))
236+
return filepath.ToSlash(uniqueTempDirPath)
237+
}
238+
}
239+
246240
func (executor *Executor) performStep(env map[string]string, currentStep *api.Command) error {
247241
success := false
248242
signaledToExit := false

0 commit comments

Comments
 (0)