Skip to content

Commit 2147fb4

Browse files
Hannes Hörlg-chauvel
andcommitted
Merge branch 'gchauvel/powershell-native-clone' into 'main'
Enable powershell native clone See merge request https://gitlab.com/gitlab-org/gitlab-runner/-/merge_requests/5577 Merged-by: Hannes Hörl <[email protected]> Approved-by: Hannes Hörl <[email protected]> Reviewed-by: Hannes Hörl <[email protected]> Co-authored-by: Guillaume CHAUVEL <[email protected]>
2 parents 613f805 + bb1eb67 commit 2147fb4

File tree

2 files changed

+81
-2
lines changed

2 files changed

+81
-2
lines changed

executors/shell/shell_integration_test.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1951,6 +1951,85 @@ func TestBuildInvokeBinaryHelper(t *testing.T) {
19511951
})
19521952
}
19531953

1954+
func TestGitCloneOrFetch(t *testing.T) {
1955+
if !test.CommandVersionIsAtLeast(t, "2.49.0", "git", "version") {
1956+
t.Skip("git version is not 2.49.0")
1957+
}
1958+
1959+
tests := map[string]struct {
1960+
revision string
1961+
sha string
1962+
depth int
1963+
assertError bool
1964+
}{
1965+
"main branch matching sha": {
1966+
revision: "main",
1967+
sha: "1ea27a9695f80d7816d9e8ce025d9b2df83d0dd7",
1968+
},
1969+
"main refs matching sha": {
1970+
revision: "refs/heads/main",
1971+
sha: "1ea27a9695f80d7816d9e8ce025d9b2df83d0dd7",
1972+
},
1973+
"main refs matching sha with depth 1": {
1974+
revision: "refs/heads/main",
1975+
sha: "1ea27a9695f80d7816d9e8ce025d9b2df83d0dd7",
1976+
depth: 1,
1977+
},
1978+
"main refs previous sha with depth 1": {
1979+
revision: "refs/heads/main",
1980+
sha: "035c3a26fadbc7bd2f4101c84812a8b6e722f562",
1981+
depth: 1,
1982+
assertError: true,
1983+
},
1984+
"main refs wrong sha": {
1985+
revision: "refs/heads/main",
1986+
sha: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
1987+
assertError: true,
1988+
},
1989+
}
1990+
1991+
for tn, tt := range tests {
1992+
t.Run(tn, func(t *testing.T) {
1993+
t.Parallel()
1994+
shellstest.OnEachShell(t, func(t *testing.T, shell string) {
1995+
t.Parallel()
1996+
buildtest.WithEachFeatureFlag(t, func(t *testing.T, setup buildtest.BuildSetupFn) {
1997+
t.Parallel()
1998+
1999+
jobResponse, err := common.GetRemoteBuildResponse(`echo "Hello World"`)
2000+
2001+
assert.NoError(t, err)
2002+
build := newBuild(t, jobResponse, shell)
2003+
2004+
setup(t, build)
2005+
2006+
build.GitInfo.Ref = tt.revision
2007+
build.GitInfo.Sha = tt.sha
2008+
if tt.depth > 0 {
2009+
build.GitInfo.Depth = tt.depth
2010+
}
2011+
2012+
out, err := buildtest.RunBuildReturningOutput(t, build)
2013+
if tt.assertError {
2014+
assert.Error(t, err)
2015+
return
2016+
}
2017+
2018+
assert.NoError(t, err)
2019+
2020+
if build.IsFeatureFlagOn(featureflags.UseGitNativeClone) {
2021+
assert.Contains(t, out, "Cloning into")
2022+
} else {
2023+
assert.Contains(t, out, "Fetching changes")
2024+
}
2025+
checkingOutHEAD := fmt.Sprintf("Checking out %s as detached HEAD", tt.sha[:8])
2026+
assert.Contains(t, out, checkingOutHEAD)
2027+
}, featureflags.UseGitNativeClone)
2028+
})
2029+
})
2030+
}
2031+
}
2032+
19542033
func TestBuildPwshHandlesSyntaxErrors(t *testing.T) {
19552034
helpers.SkipIntegrationTests(t, shells.SNPwsh)
19562035

shells/powershell.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -443,9 +443,9 @@ func (p *PsWriter) IfCmdWithOutput(cmd string, arguments ...string) {
443443
}
444444

445445
func (p *PsWriter) IfGitVersionIsAtLeast(version string) {
446-
p.Printf("Powershell does not support Git version detection")
447-
p.Linef("if($false) {")
446+
p.Line(`if ([Version]((git version | Select-String "git version (\d+(?:\.\d+)*)").Matches.Groups[1].Value) -ge [Version]"` + version + `") {`)
448447
p.Indent()
448+
p.Printf("Git version at least %q", version)
449449
}
450450

451451
func (p *PsWriter) ifInTryCatch(cmd string) {

0 commit comments

Comments
 (0)