Commit 3574f484 authored by Kamil Trzciński's avatar Kamil Trzciński 🔴

Make selection of forward or back slashes dependent by shell not by system

parent 683c14ca
......@@ -4,6 +4,7 @@ package helpers
import (
"fmt"
"strings"
)
/*
......@@ -125,3 +126,11 @@ func ShellEscape(str string) string {
return out
}
func ToBackslash(path string) string {
return strings.Replace(path, "/", "\\", -1)
}
func ToSlash(path string) string {
return strings.Replace(path, "\\", "/", -1)
}
......@@ -18,24 +18,24 @@ func (b *BashShell) GetName() string {
return "bash"
}
func (b *BashShell) writeCloneCmd(w io.Writer, build *common.Build) {
func (b *BashShell) writeCloneCmd(w io.Writer, build *common.Build, projectDir string) {
io.WriteString(w, "echo Clonning repository...\n")
io.WriteString(w, fmt.Sprintf("rm -rf %s\n", build.FullProjectDir()))
io.WriteString(w, fmt.Sprintf("mkdir -p %s\n", build.FullProjectDir()))
io.WriteString(w, fmt.Sprintf("git clone %s %s\n", build.RepoURL, build.FullProjectDir()))
io.WriteString(w, fmt.Sprintf("cd %s\n", build.FullProjectDir()))
io.WriteString(w, fmt.Sprintf("rm -rf %s\n", projectDir))
io.WriteString(w, fmt.Sprintf("mkdir -p %s\n", projectDir))
io.WriteString(w, fmt.Sprintf("git clone %s %s\n", build.RepoURL, projectDir))
io.WriteString(w, fmt.Sprintf("cd %s\n", projectDir))
}
func (b *BashShell) writeFetchCmd(w io.Writer, build *common.Build) {
io.WriteString(w, fmt.Sprintf("if [[ -d %s/.git ]]; then\n", build.FullProjectDir()))
func (b *BashShell) writeFetchCmd(w io.Writer, build *common.Build, projectDir string) {
io.WriteString(w, fmt.Sprintf("if [[ -d %s/.git ]]; then\n", projectDir))
io.WriteString(w, "echo Fetching changes...\n")
io.WriteString(w, fmt.Sprintf("cd %s\n", build.FullProjectDir()))
io.WriteString(w, fmt.Sprintf("cd %s\n", projectDir))
io.WriteString(w, fmt.Sprintf("git clean -fdx\n"))
io.WriteString(w, fmt.Sprintf("git reset --hard > /dev/null\n"))
io.WriteString(w, fmt.Sprintf("git remote set-url origin %s\n", build.RepoURL))
io.WriteString(w, fmt.Sprintf("git fetch origin\n"))
io.WriteString(w, fmt.Sprintf("else\n"))
b.writeCloneCmd(w, build)
b.writeCloneCmd(w, build, projectDir)
io.WriteString(w, fmt.Sprintf("fi\n"))
}
......@@ -48,6 +48,9 @@ func (b *BashShell) GenerateScript(build *common.Build, shellType common.ShellTy
var buffer bytes.Buffer
w := bufio.NewWriter(&buffer)
projectDir := build.FullProjectDir()
projectDir = helpers.ToSlash(projectDir)
io.WriteString(w, "#!/usr/bin/env bash\n")
io.WriteString(w, "\n")
if len(build.Hostname) != 0 {
......@@ -60,9 +63,9 @@ func (b *BashShell) GenerateScript(build *common.Build, shellType common.ShellTy
io.WriteString(w, "\n")
if build.AllowGitFetch {
b.writeFetchCmd(w, build)
b.writeFetchCmd(w, build, helpers.ShellEscape(projectDir))
} else {
b.writeCloneCmd(w, build)
b.writeCloneCmd(w, build, helpers.ShellEscape(projectDir))
}
b.writeCheckoutCmd(w, build)
......@@ -86,7 +89,7 @@ func (b *BashShell) GenerateScript(build *common.Build, shellType common.ShellTy
fmt.Sprintf("CI_BUILD_REPO=%s", build.RepoURL),
fmt.Sprintf("CI_PROJECT_ID=%d", build.ProjectID),
fmt.Sprintf("CI_PROJECT_DIR=%s", build.FullProjectDir()),
fmt.Sprintf("CI_PROJECT_DIR=%s", projectDir),
"CI=true",
"CI_SERVER=yes",
......
......@@ -7,7 +7,6 @@ import (
"gitlab.com/gitlab-org/gitlab-ci-multi-runner/common"
"gitlab.com/gitlab-org/gitlab-ci-multi-runner/helpers"
"io"
"path/filepath"
"runtime"
"strings"
)
......@@ -28,8 +27,7 @@ func (b *CmdShell) writeCommandChecked(w io.Writer, format string, args ...inter
b.writeCommand(w, "%s", "IF %errorlevel% NEQ 0 exit /b %errorlevel%")
}
func (b *CmdShell) writeCloneCmd(w io.Writer, build *common.Build) {
dir := filepath.FromSlash(build.FullProjectDir())
func (b *CmdShell) writeCloneCmd(w io.Writer, build *common.Build, dir string) {
b.writeCommand(w, "echo Clonning repository...")
b.writeCommandChecked(w, "rd /s /q \"%s\" 2> NUL 1>NUL", dir)
b.writeCommandChecked(w, "md \"%s\"", dir)
......@@ -37,8 +35,7 @@ func (b *CmdShell) writeCloneCmd(w io.Writer, build *common.Build) {
b.writeCommandChecked(w, "cd \"%s\"", dir)
}
func (b *CmdShell) writeFetchCmd(w io.Writer, build *common.Build) {
dir := filepath.FromSlash(build.FullProjectDir())
func (b *CmdShell) writeFetchCmd(w io.Writer, build *common.Build, dir string) {
b.writeCommand(w, "IF EXIST \"%s\\.git\" (", dir)
b.writeCommand(w, "echo Fetching changes...")
b.writeCommandChecked(w, "cd \"%s\"", dir)
......@@ -47,7 +44,7 @@ func (b *CmdShell) writeFetchCmd(w io.Writer, build *common.Build) {
b.writeCommandChecked(w, "git remote set-url origin \"%s\"", build.RepoURL)
b.writeCommandChecked(w, "git fetch origin")
b.writeCommand(w, ") ELSE (")
b.writeCloneCmd(w, build)
b.writeCloneCmd(w, build, dir)
b.writeCommand(w, ")")
}
......@@ -60,6 +57,9 @@ func (b *CmdShell) GenerateScript(build *common.Build, shellType common.ShellTyp
var buffer bytes.Buffer
w := bufio.NewWriter(&buffer)
projectDir := build.FullProjectDir()
projectDir = helpers.ToBackslash(projectDir)
b.writeCommand(w, "@echo off")
b.writeCommand(w, "echo.")
b.writeCommand(w, "setlocal enableextensions")
......@@ -71,9 +71,9 @@ func (b *CmdShell) GenerateScript(build *common.Build, shellType common.ShellTyp
}
if build.AllowGitFetch {
b.writeFetchCmd(w, build)
b.writeFetchCmd(w, build, projectDir)
} else {
b.writeCloneCmd(w, build)
b.writeCloneCmd(w, build, projectDir)
}
b.writeCheckoutCmd(w, build)
......@@ -101,7 +101,7 @@ func (b *CmdShell) GenerateScript(build *common.Build, shellType common.ShellTyp
fmt.Sprintf("CI_BUILD_REPO=%s", build.RepoURL),
fmt.Sprintf("CI_PROJECT_ID=%d", build.ProjectID),
fmt.Sprintf("CI_PROJECT_DIR=%s", filepath.FromSlash(build.FullProjectDir())),
fmt.Sprintf("CI_PROJECT_DIR=%s", projectDir),
"CI=true",
"CI_SERVER=yes",
......
......@@ -7,7 +7,6 @@ import (
"gitlab.com/gitlab-org/gitlab-ci-multi-runner/common"
"gitlab.com/gitlab-org/gitlab-ci-multi-runner/helpers"
"io"
"path/filepath"
"strings"
)
......@@ -26,8 +25,7 @@ func (b *PowerShell) writeCommandChecked(w io.Writer, format string, args ...int
b.writeCommand(w, format, args...)
}
func (b *PowerShell) writeCloneCmd(w io.Writer, build *common.Build) {
dir := filepath.FromSlash(build.FullProjectDir())
func (b *PowerShell) writeCloneCmd(w io.Writer, build *common.Build, dir string) {
b.writeCommand(w, "echo \"Clonning repository...\"")
b.writeCommandChecked(w, "if(Test-Path \"%s\") { Remove-Item -Force -Recurse \"%s\" }", dir, dir)
b.writeCommandChecked(w, "(Test-Path \"%s\") -or (New-Item \"%s\")", dir, dir)
......@@ -35,8 +33,7 @@ func (b *PowerShell) writeCloneCmd(w io.Writer, build *common.Build) {
b.writeCommandChecked(w, "cd \"%s\"", dir)
}
func (b *PowerShell) writeFetchCmd(w io.Writer, build *common.Build) {
dir := filepath.FromSlash(build.FullProjectDir())
func (b *PowerShell) writeFetchCmd(w io.Writer, build *common.Build, dir string) {
b.writeCommand(w, "if(Test-Path \"%s\\.git\") {", dir)
b.writeCommand(w, "echo \"Fetching changes...\"")
b.writeCommandChecked(w, "cd \"%s\"", dir)
......@@ -45,7 +42,7 @@ func (b *PowerShell) writeFetchCmd(w io.Writer, build *common.Build) {
b.writeCommandChecked(w, "git remote set-url origin \"%s\"", build.RepoURL)
b.writeCommandChecked(w, "git fetch origin")
b.writeCommand(w, "} else {")
b.writeCloneCmd(w, build)
b.writeCloneCmd(w, build, dir)
b.writeCommand(w, "}")
}
......@@ -58,6 +55,9 @@ func (b *PowerShell) GenerateScript(build *common.Build, shellType common.ShellT
var buffer bytes.Buffer
w := bufio.NewWriter(&buffer)
projectDir := build.FullProjectDir()
projectDir = helpers.ToBackslash(projectDir)
b.writeCommand(w, "$ErrorActionPreference = \"Stop\"")
if len(build.Hostname) != 0 {
......@@ -68,9 +68,9 @@ func (b *PowerShell) GenerateScript(build *common.Build, shellType common.ShellT
b.writeCommand(w, "")
if build.AllowGitFetch {
b.writeFetchCmd(w, build)
b.writeFetchCmd(w, build, projectDir)
} else {
b.writeCloneCmd(w, build)
b.writeCloneCmd(w, build, projectDir)
}
b.writeCheckoutCmd(w, build)
......@@ -99,7 +99,7 @@ func (b *PowerShell) GenerateScript(build *common.Build, shellType common.ShellT
fmt.Sprintf("CI_BUILD_REPO=%s", build.RepoURL),
fmt.Sprintf("CI_PROJECT_ID=%d", build.ProjectID),
fmt.Sprintf("CI_PROJECT_DIR=%s", filepath.FromSlash(build.FullProjectDir())),
fmt.Sprintf("CI_PROJECT_DIR=%s", projectDir),
"CI=true",
"CI_SERVER=yes",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment