refactor(git): move GIT env args for the method

This commit is contained in:
Denis Gukov 2024-12-21 12:52:13 +05:00
parent f882d20b0a
commit 4836b189a0
No known key found for this signature in database
GPG Key ID: 044381366A5D4731
4 changed files with 25 additions and 29 deletions

View File

@ -78,7 +78,23 @@ type AccessKeyInstallation struct {
Script string
}
func (key AccessKeyInstallation) Destroy() error {
func (key *AccessKeyInstallation) GetGitEnv() (env []string) {
env = make([]string, 0)
env = append(env, fmt.Sprintln("GIT_TERMINAL_PROMPT=0"))
if key.SSHAgent != nil {
env = append(env, fmt.Sprintf("SSH_AUTH_SOCK=%s", key.SSHAgent.SocketFile))
sshCmd := "ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"
if util.Config.SshConfigPath != "" {
sshCmd += " -F " + util.Config.SshConfigPath
}
env = append(env, fmt.Sprintf("GIT_SSH_COMMAND=%s", sshCmd))
}
return env
}
func (key *AccessKeyInstallation) Destroy() error {
if key.SSHAgent != nil {
return key.SSHAgent.Close()
}

View File

@ -2,7 +2,6 @@ package db_lib
import (
"fmt"
"os"
"os/exec"
"strings"
@ -17,16 +16,7 @@ type CmdGitClient struct {
func (c CmdGitClient) makeCmd(r GitRepository, targetDir GitRepositoryDirType, args ...string) *exec.Cmd {
cmd := exec.Command("git") //nolint: gas
cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, fmt.Sprintln("GIT_TERMINAL_PROMPT=0"))
if r.Repository.SSHKey.Type == db.AccessKeySSH {
cmd.Env = append(cmd.Env, fmt.Sprintf("SSH_AUTH_SOCK=%s", c.keyInstallation.SSHAgent.SocketFile))
sshCmd := "ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"
if util.Config.SshConfigPath != "" {
sshCmd += " -F " + util.Config.SshConfigPath
}
cmd.Env = append(cmd.Env, fmt.Sprintf("GIT_SSH_COMMAND=%s", sshCmd))
}
cmd.Env = append(getEnvironmentVars(), c.keyInstallation.GetGitEnv()...)
switch targetDir {
case GitRepositoryTmpPath:

View File

@ -3,9 +3,6 @@ package db_lib
import (
"errors"
"github.com/semaphoreui/semaphore/db"
"github.com/semaphoreui/semaphore/pkg/task_logger"
"github.com/semaphoreui/semaphore/util"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/plumbing"
@ -13,6 +10,9 @@ import (
"github.com/go-git/go-git/v5/plumbing/transport/http"
"github.com/go-git/go-git/v5/plumbing/transport/ssh"
"github.com/go-git/go-git/v5/storage/memory"
"github.com/semaphoreui/semaphore/db"
"github.com/semaphoreui/semaphore/pkg/task_logger"
"github.com/semaphoreui/semaphore/util"
ssh2 "golang.org/x/crypto/ssh"
)
@ -167,7 +167,7 @@ func (c GoGitClient) CanBePulled(r GitRepository) bool {
Auth: authMethod,
})
if err != nil && err != git.NoErrAlreadyUpToDate {
if err != nil && !errors.Is(err, git.NoErrAlreadyUpToDate) {
return false
}

View File

@ -865,16 +865,6 @@ func LookupDefaultApps() {
}
}
func PrintDebug() {
envs := os.Environ()
for _, e := range envs {
fmt.Println(e)
}
b, _ := Config.ToJSON()
fmt.Println(string(b))
}
func GetPublicAliasURL(scope string, alias string) string {
aliasURL := Config.WebHost
port := Config.Port