From 2135af0304fbe72f30858d705a06645a4538855c Mon Sep 17 00:00:00 2001 From: 6543 <6543@noreply.gitea.io> Date: Fri, 17 Jul 2020 16:36:48 +0000 Subject: [PATCH] Fix Login Detection By Repo Param (#151) Fix login detection by Repo param Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://gitea.com/gitea/tea/pulls/151 Reviewed-by: lafriks Reviewed-by: Lunny Xiao --- cmd/config.go | 11 +++++++---- cmd/flags.go | 11 +++-------- modules/git/repo.go | 12 ++++++++++++ 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/cmd/config.go b/cmd/config.go index fd7b4c5..89255e3 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -189,10 +189,13 @@ func saveConfig(ymlPath string) error { return ioutil.WriteFile(ymlPath, bs, 0660) } -func curGitRepoPath() (*Login, string, error) { - repo, err := git.RepoForWorkdir() - if err != nil { - return nil, "", errors.New("No Gitea login found") +func curGitRepoPath(path string) (*Login, string, error) { + var err error + var repo *git.TeaRepo + if len(path) == 0 { + repo, err = git.RepoForWorkdir() + } else { + repo, err = git.RepoFromPath(path) } gitConfig, err := repo.Config() if err != nil { diff --git a/cmd/flags.go b/cmd/flags.go index 7737972..84b4fe5 100644 --- a/cmd/flags.go +++ b/cmd/flags.go @@ -81,15 +81,10 @@ var AllDefaultFlags = append([]cli.Flag{ // initCommand returns repository and *Login based on flags func initCommand() (*Login, string, string) { - login := initCommandLoginOnly() + login, repoPath, err := curGitRepoPath(repoValue) + if err != nil { + log.Fatal(err.Error()) - var err error - repoPath := repoValue - if repoPath == "" { - login, repoPath, err = curGitRepoPath() - if err != nil { - log.Fatal(err.Error()) - } } owner, repo := splitRepo(repoPath) diff --git a/modules/git/repo.go b/modules/git/repo.go index 9e12fce..e6f03e8 100644 --- a/modules/git/repo.go +++ b/modules/git/repo.go @@ -25,3 +25,15 @@ func RepoForWorkdir() (*TeaRepo, error) { return &TeaRepo{repo}, nil } + +// RepoFromPath tries to open the git repository by path +func RepoFromPath(path string) (*TeaRepo, error) { + repo, err := git.PlainOpenWithOptions(path, &git.PlainOpenOptions{ + DetectDotGit: true, + }) + if err != nil { + return nil, err + } + + return &TeaRepo{repo}, nil +}