diff options
author | Sami Hiltunen <shiltunen@gitlab.com> | 2021-06-09 20:03:46 +0300 |
---|---|---|
committer | Sami Hiltunen <shiltunen@gitlab.com> | 2021-06-09 20:03:46 +0300 |
commit | 9e380186b45e7d46b731ab588d735a1ace2f68b1 (patch) | |
tree | e947fb5cb03441d7ba610095381fd68ff6bcb45d | |
parent | dc956109aa3e352cadc55ebba267b43fdd4fdc27 (diff) |
Extract helpers for GIT_SSH_COMMAND from Push
localrepo.Push contains helpers for setting the GIT_SSH_COMMAND to
use when communicating with the remote repository. This commit extracts
them as they'll be needed to set up SSH credentials for GetRemoteReferences.
-rw-r--r-- | internal/git/localrepo/remote.go | 6 | ||||
-rw-r--r-- | internal/git/localrepo/remote_test.go | 29 |
2 files changed, 24 insertions, 11 deletions
diff --git a/internal/git/localrepo/remote.go b/internal/git/localrepo/remote.go index d5f3b0bf0..bb659f6d2 100644 --- a/internal/git/localrepo/remote.go +++ b/internal/git/localrepo/remote.go @@ -274,6 +274,10 @@ func validateNotBlank(val, name string) error { return nil } +func envGitSSHCommand(cmd string) string { + return "GIT_SSH_COMMAND=" + cmd +} + // PushOptions are options that can be configured for a push. type PushOptions struct { // SSHCommand is the command line to use for git's SSH invocation. The command line is used @@ -293,7 +297,7 @@ func (repo *Repo) Push(ctx context.Context, remote string, refspecs []string, op var env []string if options.SSHCommand != "" { - env = append(env, "GIT_SSH_COMMAND="+options.SSHCommand) + env = append(env, envGitSSHCommand(options.SSHCommand)) } stderr := &bytes.Buffer{} diff --git a/internal/git/localrepo/remote_test.go b/internal/git/localrepo/remote_test.go index 33505f5f3..158f3ea49 100644 --- a/internal/git/localrepo/remote_test.go +++ b/internal/git/localrepo/remote_test.go @@ -438,13 +438,11 @@ func TestRepo_FetchRemote(t *testing.T) { }) } -func TestRepo_Push(t *testing.T) { - ctx, cancel := testhelper.Context() - defer cancel() - - cfg, sourceRepoPb, _ := testcfg.BuildWithRepo(t) - - tmpDir := testhelper.TempDir(t) +// captureGitSSHCommand returns a path to a script that wraps the git at the passed in path +// and records the GIT_SSH_COMMAND that was passed to it. It returns also a path to the file +// that contains the captured GIT_SSH_COMMAND value. +func captureGitSSHCommand(t testing.TB, gitBinaryPath string) (string, string) { + tmpDir := t.TempDir() gitPath := filepath.Join(tmpDir, "git-hook") envPath := filepath.Join(tmpDir, "GIT_SSH_PATH") @@ -455,12 +453,23 @@ func TestRepo_Push(t *testing.T) { if [ -z ${GIT_SSH_COMMAND+x} ];then rm -f %q ;else echo -n "$GIT_SSH_COMMAND" > %q; fi %s "$@"`, envPath, envPath, - cfg.Git.BinPath, + gitBinaryPath, )), os.ModePerm), ) - cfg.Git.BinPath = gitPath + return gitPath, envPath +} + +func TestRepo_Push(t *testing.T) { + ctx, cancel := testhelper.Context() + defer cancel() + + cfg, sourceRepoPb, _ := testcfg.BuildWithRepo(t) + + wrappedGit, gitSSHCommandFile := captureGitSSHCommand(t, cfg.Git.BinPath) + + cfg.Git.BinPath = wrappedGit sourceRepo := NewTestRepo(t, cfg, sourceRepoPb) setupPushRepo := func(t testing.TB) (*Repo, string, []git.ConfigPair) { @@ -563,7 +572,7 @@ if [ -z ${GIT_SSH_COMMAND+x} ];then rm -f %q ;else echo -n "$GIT_SSH_COMMAND" > } require.NoError(t, err) - gitSSHCommand, err := ioutil.ReadFile(envPath) + gitSSHCommand, err := ioutil.ReadFile(gitSSHCommandFile) if !os.IsNotExist(err) { require.NoError(t, err) } |