Skip to content

Commit

Permalink
tests/bud.bats: add git source
Browse files Browse the repository at this point in the history
Signed-off-by: danishprakash <[email protected]>
  • Loading branch information
danishprakash committed Apr 23, 2024
1 parent e504396 commit 92ed66a
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 7 deletions.
20 changes: 17 additions & 3 deletions add.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"os"
"path"
"path/filepath"
"regexp"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -75,14 +76,27 @@ type AddAndCopyOptions struct {
StripStickyBit bool
}

// gitURLFragmentSuffix matches fragments to use as Git reference and build
// context from the Git repository e.g.
//
// github.com/containers/buildah.git
// github.com/containers/buildah.git#main
// github.com/containers/buildah.git#v1.35.0
var gitURLFragmentSuffix = regexp.MustCompile(`\.git(?:#.+)?$`)

// sourceIsGit returns true if "source" is a git location.
func sourceIsGit(source string) bool {
return strings.HasPrefix(source, "git://") || strings.Contains(source, ".git")
return isURL(source) && gitURLFragmentSuffix.MatchString(source)
}

func isURL(url string) bool {
return strings.HasPrefix(url, "http://") || strings.HasPrefix(url, "https://")
}

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

// getURL writes a tar archive containing the named content
Expand Down
8 changes: 4 additions & 4 deletions define/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,11 +254,11 @@ func parseGitBuildContext(url string) (string, string, string) {
return gitBranchPart[0], gitSubdir, gitBranch
}

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

func cloneToDirectory(url, dir string) ([]byte, string, error) {
Expand All @@ -279,7 +279,7 @@ func cloneToDirectory(url, dir string) ([]byte, string, error) {
}

if gitRef != "" {
if ok, _ := isGitTag(url, gitRef, dir); ok {
if ok := isGitTag(url, gitRef); ok {
gitRef += ":refs/tags/" + gitRef
}
}
Expand Down
27 changes: 27 additions & 0 deletions tests/bud.bats
Original file line number Diff line number Diff line change
Expand Up @@ -6720,3 +6720,30 @@ _EOF
assert "$status" -eq 2 "exit code from ls"
expect_output --substring "No such file or directory"
}

@test "bud with ADD with git repository source" {
_prefetch alpine

local contextdir=${TEST_SCRATCH_DIR}/add-git
mkdir -p $contextdir
cat > $contextdir/Dockerfile << _EOF
FROM alpine
RUN apk add git
ADD https://github.com/containers/podman.git#v5.0 /podman-branch
ADD https://github.com/containers/podman.git#v5.0.0 /podman-tag
_EOF

run_buildah build -f $contextdir/Dockerfile -t git-image $contextdir
run_buildah from --quiet $WITH_POLICY_JSON --name testctr git-image

run_buildah run testctr -- sh -c 'cd podman-branch && git rev-parse HEAD'
local_head_hash=$output
run_buildah run testctr -- sh -c 'cd podman-branch && git ls-remote origin v5.0 | cut -f1'
assert "$output" = "$local_head_hash"

run_buildah run testctr -- sh -c 'cd podman-tag && git rev-parse HEAD'
local_head_hash=$output
run_buildah run testctr -- sh -c 'cd podman-tag && git ls-remote --tags origin v5.0.0^{} | cut -f1'
assert "$output" = "$local_head_hash"
}

0 comments on commit 92ed66a

Please sign in to comment.