Skip to content

Commit

Permalink
zip: set GIT_DIR in test when using bare repositories
Browse files Browse the repository at this point in the history
If git has safe.bareRepository=explicit set, operations on bare git
repos will fail unless --git-dir or GIT_DIR is set. Set GIT_DIR in the
parts of the zip test that use bare repos to allow the tests to pass in
those circumstances.

See CL 489915 for the change setting GIT_DIR for git operations on bare
repositories in cmd/go.

Change-Id: I1f8ae9ed2b687a58d533fa605ed9ad4b5cbb8549
Reviewed-on: https://go-review.googlesource.com/c/mod/+/605937
Auto-Submit: Michael Matloob <matloob@golang.org>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
  • Loading branch information
matloob authored and gopherbot committed Aug 16, 2024
1 parent 3afcd4e commit 46a3137
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
4 changes: 3 additions & 1 deletion zip/zip.go
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,9 @@ func CreateFromDir(w io.Writer, m module.Version, dir string) (err error) {
// VCS repository stored locally. The zip content is written to w.
//
// repoRoot must be an absolute path to the base of the repository, such as
// "/Users/some-user/some-repo".
// "/Users/some-user/some-repo". If the repository is a Git repository,
// this path is expected to point to its worktree: it can't be a bare git
// repo.
//
// revision is the revision of the repository to create the zip from. Examples
// include HEAD or SHA sums for git repositories.
Expand Down
20 changes: 15 additions & 5 deletions zip/zip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1325,7 +1325,7 @@ func downloadVCSZip(t testing.TB, vcs, url, rev, subdir string) (repoDir string,
switch vcs {
case "git":
// Create a repository and download the revision we want.
if _, err := run(t, repoDir, "git", "init", "--bare"); err != nil {
if _, err := runWithGitDir(t, repoDir, repoDir, "git", "init", "--bare"); err != nil {
return "", nil, err
}
if err := os.MkdirAll(filepath.Join(repoDir, "info"), 0777); err != nil {
Expand All @@ -1342,7 +1342,7 @@ func downloadVCSZip(t testing.TB, vcs, url, rev, subdir string) (repoDir string,
if err := attrFile.Close(); err != nil {
return "", nil, err
}
if _, err := run(t, repoDir, "git", "remote", "add", "origin", "--", url); err != nil {
if _, err := runWithGitDir(t, repoDir, repoDir, "git", "remote", "add", "origin", "--", url); err != nil {
return "", nil, err
}
var refSpec string
Expand All @@ -1351,7 +1351,7 @@ func downloadVCSZip(t testing.TB, vcs, url, rev, subdir string) (repoDir string,
} else {
refSpec = fmt.Sprintf("%s:refs/dummy", rev)
}
if _, err := run(t, repoDir, "git", "fetch", "-f", "--depth=1", "origin", refSpec); err != nil {
if _, err := runWithGitDir(t, repoDir, repoDir, "git", "fetch", "-f", "--depth=1", "origin", refSpec); err != nil {
return "", nil, err
}

Expand All @@ -1368,6 +1368,7 @@ func downloadVCSZip(t testing.TB, vcs, url, rev, subdir string) (repoDir string,

cmd := exec.Command("git", "-c", "core.autocrlf=input", "-c", "core.eol=lf", "archive", "--format=zip", "--prefix=prefix/", rev, "--", subdirArg)
cmd.Dir = repoDir
cmd.Env = append(cmd.Environ(), "GIT_DIR="+repoDir)
cmd.Stdout = tmpZipFile
stderr := new(strings.Builder)
cmd.Stderr = stderr
Expand Down Expand Up @@ -1425,17 +1426,20 @@ func downloadVCSFile(t testing.TB, vcs, repo, rev, file string) ([]byte, error)
t.Helper()
switch vcs {
case "git":
return run(t, repo, "git", "cat-file", "blob", rev+":"+file)
return runWithGitDir(t, repo, repo, "git", "cat-file", "blob", rev+":"+file)
default:
return nil, fmt.Errorf("vcs %q not supported", vcs)
}
}

func run(t testing.TB, dir string, name string, args ...string) ([]byte, error) {
func runWithGitDir(t testing.TB, gitDir, dir string, name string, args ...string) ([]byte, error) {
t.Helper()

cmd := exec.Command(name, args...)
cmd.Dir = dir
if gitDir != "" {
cmd.Env = append(cmd.Environ(), "GIT_DIR="+gitDir)
}
stderr := new(strings.Builder)
cmd.Stderr = stderr

Expand All @@ -1450,6 +1454,12 @@ func run(t testing.TB, dir string, name string, args ...string) ([]byte, error)
return out, err
}

func run(t testing.TB, dir string, name string, args ...string) ([]byte, error) {
t.Helper()

return runWithGitDir(t, "", dir, name, args...)
}

type zipFile struct {
name string
f *zip.File
Expand Down

0 comments on commit 46a3137

Please sign in to comment.