Skip to content

Commit 92ed66a

Browse files
committed
tests/bud.bats: add git source
Signed-off-by: danishprakash <[email protected]>
1 parent e504396 commit 92ed66a

File tree

3 files changed

+48
-7
lines changed

3 files changed

+48
-7
lines changed

add.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"os"
1111
"path"
1212
"path/filepath"
13+
"regexp"
1314
"strconv"
1415
"strings"
1516
"sync"
@@ -75,14 +76,27 @@ type AddAndCopyOptions struct {
7576
StripStickyBit bool
7677
}
7778

79+
// gitURLFragmentSuffix matches fragments to use as Git reference and build
80+
// context from the Git repository e.g.
81+
//
82+
// github.com/containers/buildah.git
83+
// github.com/containers/buildah.git#main
84+
// github.com/containers/buildah.git#v1.35.0
85+
var gitURLFragmentSuffix = regexp.MustCompile(`\.git(?:#.+)?$`)
86+
7887
// sourceIsGit returns true if "source" is a git location.
7988
func sourceIsGit(source string) bool {
80-
return strings.HasPrefix(source, "git://") || strings.Contains(source, ".git")
89+
return isURL(source) && gitURLFragmentSuffix.MatchString(source)
90+
}
91+
92+
func isURL(url string) bool {
93+
return strings.HasPrefix(url, "http://") || strings.HasPrefix(url, "https://")
8194
}
8295

83-
// sourceIsRemote returns true if "source" is a remote location.
96+
// sourceIsRemote returns true if "source" is a remote location
97+
// and *not* a git repo. Certain github urls such as raw.github.* are allowed.
8498
func sourceIsRemote(source string) bool {
85-
return (strings.HasPrefix(source, "http://") || strings.HasPrefix(source, "https://")) && !strings.Contains(source, ".git")
99+
return isURL(source) && !gitURLFragmentSuffix.MatchString(source)
86100
}
87101

88102
// getURL writes a tar archive containing the named content

define/types.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -254,11 +254,11 @@ func parseGitBuildContext(url string) (string, string, string) {
254254
return gitBranchPart[0], gitSubdir, gitBranch
255255
}
256256

257-
func isGitTag(remote, ref, dir string) (bool, error) {
257+
func isGitTag(remote, ref string) bool {
258258
if _, err := exec.Command("git", "ls-remote", "--exit-code", remote, ref).Output(); err != nil {
259-
return true, nil
259+
return true
260260
}
261-
return false, nil
261+
return false
262262
}
263263

264264
func cloneToDirectory(url, dir string) ([]byte, string, error) {
@@ -279,7 +279,7 @@ func cloneToDirectory(url, dir string) ([]byte, string, error) {
279279
}
280280

281281
if gitRef != "" {
282-
if ok, _ := isGitTag(url, gitRef, dir); ok {
282+
if ok := isGitTag(url, gitRef); ok {
283283
gitRef += ":refs/tags/" + gitRef
284284
}
285285
}

tests/bud.bats

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6720,3 +6720,30 @@ _EOF
67206720
assert "$status" -eq 2 "exit code from ls"
67216721
expect_output --substring "No such file or directory"
67226722
}
6723+
6724+
@test "bud with ADD with git repository source" {
6725+
_prefetch alpine
6726+
6727+
local contextdir=${TEST_SCRATCH_DIR}/add-git
6728+
mkdir -p $contextdir
6729+
cat > $contextdir/Dockerfile << _EOF
6730+
FROM alpine
6731+
RUN apk add git
6732+
6733+
ADD https://github.com/containers/podman.git#v5.0 /podman-branch
6734+
ADD https://github.com/containers/podman.git#v5.0.0 /podman-tag
6735+
_EOF
6736+
6737+
run_buildah build -f $contextdir/Dockerfile -t git-image $contextdir
6738+
run_buildah from --quiet $WITH_POLICY_JSON --name testctr git-image
6739+
6740+
run_buildah run testctr -- sh -c 'cd podman-branch && git rev-parse HEAD'
6741+
local_head_hash=$output
6742+
run_buildah run testctr -- sh -c 'cd podman-branch && git ls-remote origin v5.0 | cut -f1'
6743+
assert "$output" = "$local_head_hash"
6744+
6745+
run_buildah run testctr -- sh -c 'cd podman-tag && git rev-parse HEAD'
6746+
local_head_hash=$output
6747+
run_buildah run testctr -- sh -c 'cd podman-tag && git ls-remote --tags origin v5.0.0^{} | cut -f1'
6748+
assert "$output" = "$local_head_hash"
6749+
}

0 commit comments

Comments
 (0)