Преглед изворни кода

builder: explicitly set CWD for all git commands

Keep It Simple! Set the working directory for git commands by...setting
the git process's working directory. Git commands can be run in the
parent process's working directory by passing the empty string.

Signed-off-by: Cory Snider <csnider@mirantis.com>
Cory Snider пре 3 година
родитељ
комит
0f7b0897cc
2 измењених фајлова са 7 додато и 14 уклоњено
  1. 3 6
      builder/remotecontext/git/gitutils.go
  2. 4 8
      builder/remotecontext/git/gitutils_test.go

+ 3 - 6
builder/remotecontext/git/gitutils.go

@@ -192,12 +192,9 @@ func checkoutGit(root, ref, subdir string) (string, error) {
 }
 }
 
 
 func gitWithinDir(dir string, args ...string) ([]byte, error) {
 func gitWithinDir(dir string, args ...string) ([]byte, error) {
-	a := []string{"--work-tree", dir, "--git-dir", filepath.Join(dir, ".git")}
-	return git(append(a, args...)...)
-}
-
-func git(args ...string) ([]byte, error) {
-	return exec.Command("git", args...).CombinedOutput()
+	cmd := exec.Command("git", args...)
+	cmd.Dir = dir
+	return cmd.CombinedOutput()
 }
 }
 
 
 // isGitTransport returns true if the provided str is a git transport by inspecting
 // isGitTransport returns true if the provided str is a git transport by inspecting

+ 4 - 8
builder/remotecontext/git/gitutils_test.go

@@ -6,7 +6,6 @@ import (
 	"net/http/httptest"
 	"net/http/httptest"
 	"net/url"
 	"net/url"
 	"os"
 	"os"
-	"os/exec"
 	"path/filepath"
 	"path/filepath"
 	"runtime"
 	"runtime"
 	"strings"
 	"strings"
@@ -160,7 +159,7 @@ func TestCloneArgsGit(t *testing.T) {
 }
 }
 
 
 func gitGetConfig(name string) string {
 func gitGetConfig(name string) string {
-	b, err := git([]string{"config", "--get", name}...)
+	b, err := gitWithinDir("", "config", "--get", name)
 	if err != nil {
 	if err != nil {
 		// since we are interested in empty or non empty string,
 		// since we are interested in empty or non empty string,
 		// we can safely ignore the err here.
 		// we can safely ignore the err here.
@@ -191,7 +190,7 @@ func TestCheckoutGit(t *testing.T) {
 	}
 	}
 
 
 	gitDir := filepath.Join(root, "repo")
 	gitDir := filepath.Join(root, "repo")
-	must(git("-c", "init.defaultBranch=master", "init", gitDir))
+	must(gitWithinDir(root, "-c", "init.defaultBranch=master", "init", gitDir))
 	must(gitWithinDir(gitDir, "config", "user.email", "test@docker.com"))
 	must(gitWithinDir(gitDir, "config", "user.email", "test@docker.com"))
 	must(gitWithinDir(gitDir, "config", "user.name", "Docker test"))
 	must(gitWithinDir(gitDir, "config", "user.name", "Docker test"))
 	assert.NilError(t, os.WriteFile(filepath.Join(gitDir, "Dockerfile"), []byte("FROM scratch"), 0644))
 	assert.NilError(t, os.WriteFile(filepath.Join(gitDir, "Dockerfile"), []byte("FROM scratch"), 0644))
@@ -218,7 +217,7 @@ func TestCheckoutGit(t *testing.T) {
 
 
 	// set up submodule
 	// set up submodule
 	subrepoDir := filepath.Join(root, "subrepo")
 	subrepoDir := filepath.Join(root, "subrepo")
-	must(git("-c", "init.defaultBranch=master", "init", subrepoDir))
+	must(gitWithinDir(root, "-c", "init.defaultBranch=master", "init", subrepoDir))
 	must(gitWithinDir(subrepoDir, "config", "user.email", "test@docker.com"))
 	must(gitWithinDir(subrepoDir, "config", "user.email", "test@docker.com"))
 	must(gitWithinDir(subrepoDir, "config", "user.name", "Docker test"))
 	must(gitWithinDir(subrepoDir, "config", "user.name", "Docker test"))
 
 
@@ -227,10 +226,7 @@ func TestCheckoutGit(t *testing.T) {
 	must(gitWithinDir(subrepoDir, "add", "-A"))
 	must(gitWithinDir(subrepoDir, "add", "-A"))
 	must(gitWithinDir(subrepoDir, "commit", "-am", "Subrepo initial"))
 	must(gitWithinDir(subrepoDir, "commit", "-am", "Subrepo initial"))
 
 
-	cmd := exec.Command("git", "submodule", "add", subrepoDir, "sub") // this command doesn't work with --work-tree
-	cmd.Dir = gitDir
-	must(cmd.CombinedOutput())
-
+	must(gitWithinDir(gitDir, "submodule", "add", subrepoDir, "sub"))
 	must(gitWithinDir(gitDir, "add", "-A"))
 	must(gitWithinDir(gitDir, "add", "-A"))
 	must(gitWithinDir(gitDir, "commit", "-am", "With submodule"))
 	must(gitWithinDir(gitDir, "commit", "-am", "With submodule"))