From c2ddda6800528e01023c477db380cf344c3a69e2 Mon Sep 17 00:00:00 2001 From: Matheus Sampaio Queiroga Date: Wed, 26 Feb 2025 16:09:06 +0000 Subject: [PATCH] Fix helper panic (#676) Fix helper on get login struct Reviewed-on: https://gitea.com/gitea/tea/pulls/676 Reviewed-by: techknowlogick Co-authored-by: Matheus Sampaio Queiroga Co-committed-by: Matheus Sampaio Queiroga --- cmd/login/helper.go | 4 +++- modules/task/login_create.go | 23 ++++++----------------- 2 files changed, 9 insertions(+), 18 deletions(-) diff --git a/cmd/login/helper.go b/cmd/login/helper.go index c749642..fd6d88d 100644 --- a/cmd/login/helper.go +++ b/cmd/login/helper.go @@ -91,7 +91,9 @@ var CmdLoginHelper = cli.Command{ } 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") } diff --git a/modules/task/login_create.go b/modules/task/login_create.go index cc3dd1d..a0aacc0 100644 --- a/modules/task/login_create.go +++ b/modules/task/login_create.go @@ -23,35 +23,24 @@ func SetupHelper(login config.Login) (ok bool, err error) { 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 var currentHelpers []byte 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 for _, line := range strings.Split(strings.ReplaceAll(string(currentHelpers), "\r", ""), "\n") { - if strings.TrimSpace(line) == "" { - continue - } else if strings.HasPrefix(line, binPath) && strings.Contains(line[len(binPath):], "login helper") { + if strings.HasSuffix(strings.TrimSpace(line), "login helper") { 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 - if _, err = exec.Command("git", "config", "--global", "--add", fmt.Sprintf("credential.%s.helper", login.URL), fmt.Sprintf("!%s login helper", binPath)).Output(); err != nil { - return false, err + if _, err = exec.Command("git", "config", "--global", fmt.Sprintf("credential.%s.helper", login.URL), "").Output(); err != nil { + 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