Fix helper panic (#676)

Fix helper on get login struct

Reviewed-on: https://gitea.com/gitea/tea/pulls/676
Reviewed-by: techknowlogick <techknowlogick@noreply.gitea.com>
Co-authored-by: Matheus Sampaio Queiroga <srherobrine20@gmail.com>
Co-committed-by: Matheus Sampaio Queiroga <srherobrine20@gmail.com>
This commit is contained in:
Matheus Sampaio Queiroga 2025-02-26 16:09:06 +00:00 committed by techknowlogick
parent fe1928679a
commit c2ddda6800
2 changed files with 9 additions and 18 deletions

View File

@ -91,7 +91,9 @@ var CmdLoginHelper = cli.Command{
} }
userConfig := config.GetLoginByHost(wants["host"]) userConfig := config.GetLoginByHost(wants["host"])
if len(userConfig.Token) == 0 { if userConfig == nil {
log.Fatal("host not exists")
} else if len(userConfig.Token) == 0 {
log.Fatal("User no set") log.Fatal("User no set")
} }

View File

@ -23,35 +23,24 @@ func SetupHelper(login config.Login) (ok bool, err error) {
return false, fmt.Errorf("Invalid gitea url") return false, fmt.Errorf("Invalid gitea url")
} }
// get tea binary path
var binPath string
if binPath, err = os.Executable(); err != nil {
return
}
// get all helper to URL in git config // get all helper to URL in git config
var currentHelpers []byte var currentHelpers []byte
if currentHelpers, err = exec.Command("git", "config", "--global", "--get-all", fmt.Sprintf("credential.%s.helper", login.URL)).Output(); err != nil { if currentHelpers, err = exec.Command("git", "config", "--global", "--get-all", fmt.Sprintf("credential.%s.helper", login.URL)).Output(); err != nil {
return false, err currentHelpers = []byte{}
} }
// Check if ared added tea helper // Check if ared added tea helper
for _, line := range strings.Split(strings.ReplaceAll(string(currentHelpers), "\r", ""), "\n") { for _, line := range strings.Split(strings.ReplaceAll(string(currentHelpers), "\r", ""), "\n") {
if strings.TrimSpace(line) == "" { if strings.HasSuffix(strings.TrimSpace(line), "login helper") {
continue
} else if strings.HasPrefix(line, binPath) && strings.Contains(line[len(binPath):], "login helper") {
return false, nil return false, nil
} }
} }
// Check if tea path have space, if have add quotes
if strings.Contains(binPath, " ") {
binPath = fmt.Sprintf("%q", binPath)
}
// Add tea helper // Add tea helper
if _, err = exec.Command("git", "config", "--global", "--add", fmt.Sprintf("credential.%s.helper", login.URL), fmt.Sprintf("!%s login helper", binPath)).Output(); err != nil { if _, err = exec.Command("git", "config", "--global", fmt.Sprintf("credential.%s.helper", login.URL), "").Output(); err != nil {
return false, err return false, fmt.Errorf("git config --global %s, error: %s", fmt.Sprintf("credential.%s.helper", login.URL), err)
} else if _, err = exec.Command("git", "config", "--global", "--add", fmt.Sprintf("credential.%s.helper", login.URL), "!tea login helper").Output(); err != nil {
return false, fmt.Errorf("git config --global --add %s %s, error: %s", fmt.Sprintf("credential.%s.helper", login.URL), "!tea login helper", err)
} }
return true, nil return true, nil