zip: set GIT_DIR in test when using bare repositories

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>
This commit is contained in:
Michael Matloob 2024-08-15 12:30:22 -04:00 коммит произвёл Gopher Robot
Родитель 3afcd4e90a
Коммит 46a3137dae
2 изменённых файлов: 18 добавлений и 6 удалений

Просмотреть файл

@ -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.

Просмотреть файл

@ -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 {
@ -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
@ -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
}
@ -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
@ -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
@ -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