Skip to content

Commit 46a3137

Browse files
matloobgopherbot
authored andcommitted
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 <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 3afcd4e commit 46a3137

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

zip/zip.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,9 @@ func CreateFromDir(w io.Writer, m module.Version, dir string) (err error) {
573573
// VCS repository stored locally. The zip content is written to w.
574574
//
575575
// repoRoot must be an absolute path to the base of the repository, such as
576-
// "/Users/some-user/some-repo".
576+
// "/Users/some-user/some-repo". If the repository is a Git repository,
577+
// this path is expected to point to its worktree: it can't be a bare git
578+
// repo.
577579
//
578580
// revision is the revision of the repository to create the zip from. Examples
579581
// include HEAD or SHA sums for git repositories.

zip/zip_test.go

+15-5
Original file line numberDiff line numberDiff line change
@@ -1325,7 +1325,7 @@ func downloadVCSZip(t testing.TB, vcs, url, rev, subdir string) (repoDir string,
13251325
switch vcs {
13261326
case "git":
13271327
// Create a repository and download the revision we want.
1328-
if _, err := run(t, repoDir, "git", "init", "--bare"); err != nil {
1328+
if _, err := runWithGitDir(t, repoDir, repoDir, "git", "init", "--bare"); err != nil {
13291329
return "", nil, err
13301330
}
13311331
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,
13421342
if err := attrFile.Close(); err != nil {
13431343
return "", nil, err
13441344
}
1345-
if _, err := run(t, repoDir, "git", "remote", "add", "origin", "--", url); err != nil {
1345+
if _, err := runWithGitDir(t, repoDir, repoDir, "git", "remote", "add", "origin", "--", url); err != nil {
13461346
return "", nil, err
13471347
}
13481348
var refSpec string
@@ -1351,7 +1351,7 @@ func downloadVCSZip(t testing.TB, vcs, url, rev, subdir string) (repoDir string,
13511351
} else {
13521352
refSpec = fmt.Sprintf("%s:refs/dummy", rev)
13531353
}
1354-
if _, err := run(t, repoDir, "git", "fetch", "-f", "--depth=1", "origin", refSpec); err != nil {
1354+
if _, err := runWithGitDir(t, repoDir, repoDir, "git", "fetch", "-f", "--depth=1", "origin", refSpec); err != nil {
13551355
return "", nil, err
13561356
}
13571357

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

13691369
cmd := exec.Command("git", "-c", "core.autocrlf=input", "-c", "core.eol=lf", "archive", "--format=zip", "--prefix=prefix/", rev, "--", subdirArg)
13701370
cmd.Dir = repoDir
1371+
cmd.Env = append(cmd.Environ(), "GIT_DIR="+repoDir)
13711372
cmd.Stdout = tmpZipFile
13721373
stderr := new(strings.Builder)
13731374
cmd.Stderr = stderr
@@ -1425,17 +1426,20 @@ func downloadVCSFile(t testing.TB, vcs, repo, rev, file string) ([]byte, error)
14251426
t.Helper()
14261427
switch vcs {
14271428
case "git":
1428-
return run(t, repo, "git", "cat-file", "blob", rev+":"+file)
1429+
return runWithGitDir(t, repo, repo, "git", "cat-file", "blob", rev+":"+file)
14291430
default:
14301431
return nil, fmt.Errorf("vcs %q not supported", vcs)
14311432
}
14321433
}
14331434

1434-
func run(t testing.TB, dir string, name string, args ...string) ([]byte, error) {
1435+
func runWithGitDir(t testing.TB, gitDir, dir string, name string, args ...string) ([]byte, error) {
14351436
t.Helper()
14361437

14371438
cmd := exec.Command(name, args...)
14381439
cmd.Dir = dir
1440+
if gitDir != "" {
1441+
cmd.Env = append(cmd.Environ(), "GIT_DIR="+gitDir)
1442+
}
14391443
stderr := new(strings.Builder)
14401444
cmd.Stderr = stderr
14411445

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

1457+
func run(t testing.TB, dir string, name string, args ...string) ([]byte, error) {
1458+
t.Helper()
1459+
1460+
return runWithGitDir(t, "", dir, name, args...)
1461+
}
1462+
14531463
type zipFile struct {
14541464
name string
14551465
f *zip.File

0 commit comments

Comments
 (0)