mirror of
https://gitea.com/gitea/tea.git
synced 2026-06-05 18:58:43 +02:00
fix(context): improve local repo detection logic and test (#999)
Fix https://gitea.com/gitea/tea/issues/995 Reviewed-on: https://gitea.com/gitea/tea/pulls/999 Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com> Co-authored-by: Minjie Fang <wingsallen@gmail.com> Co-committed-by: Minjie Fang <wingsallen@gmail.com>
This commit is contained in:
+13
-11
@@ -103,21 +103,23 @@ func InitCommand(cmd *cli.Command) (*TeaContext, error) {
|
|||||||
|
|
||||||
// try to read local git repo & extract context: if repoFlag specifies a valid path, read repo in that dir,
|
// try to read local git repo & extract context: if repoFlag specifies a valid path, read repo in that dir,
|
||||||
// otherwise attempt PWD. if no repo is found, continue with default login
|
// otherwise attempt PWD. if no repo is found, continue with default login
|
||||||
if c.RepoSlug == "" {
|
if repoPath == "" {
|
||||||
if repoPath == "" {
|
if repoPath, err = os.Getwd(); err != nil {
|
||||||
if repoPath, err = os.Getwd(); err != nil {
|
return nil, err
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if c.LocalRepo, c.Login, c.RepoSlug, err = contextFromLocalRepo(repoPath, remoteFlag, extraLogins); err != nil {
|
var localSlug string
|
||||||
if err == errNotAGiteaRepo || err == git.ErrRepositoryNotExists {
|
if c.LocalRepo, c.Login, localSlug, err = contextFromLocalRepo(repoPath, remoteFlag, extraLogins); err != nil {
|
||||||
// we can deal with that, commands needing the optional values use ctx.Ensure()
|
if err == errNotAGiteaRepo || err == git.ErrRepositoryNotExists {
|
||||||
} else {
|
// we can deal with that, commands needing the optional values use ctx.Ensure()
|
||||||
return nil, err
|
} else {
|
||||||
}
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if c.RepoSlug == "" && localSlug != "" {
|
||||||
|
c.RepoSlug = localSlug
|
||||||
|
}
|
||||||
|
|
||||||
// If env vars are set, always use the env login (but repo slug was already
|
// If env vars are set, always use the env login (but repo slug was already
|
||||||
// resolved by contextFromLocalRepo with the env login in the match list)
|
// resolved by contextFromLocalRepo with the env login in the match list)
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ import (
|
|||||||
teacontext "gitea.dev/tea/modules/context"
|
teacontext "gitea.dev/tea/modules/context"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestInitCommand_WithRepoSlugSkipsLocalRepoDetection(t *testing.T) {
|
func TestInitCommand_WithRepoSlugKeepsLocalRepoDetection(t *testing.T) {
|
||||||
tmpDir := t.TempDir()
|
tmpDir := t.TempDir()
|
||||||
config.SetConfigForTesting(config.LocalConfig{
|
config.SetConfigForTesting(config.LocalConfig{
|
||||||
Logins: []config.Login{{
|
Logins: []config.Login{{
|
||||||
@@ -54,7 +54,26 @@ func TestInitCommand_WithRepoSlugSkipsLocalRepoDetection(t *testing.T) {
|
|||||||
require.Equal(t, "owner", ctx.Owner)
|
require.Equal(t, "owner", ctx.Owner)
|
||||||
require.Equal(t, "repo", ctx.Repo)
|
require.Equal(t, "repo", ctx.Repo)
|
||||||
require.Equal(t, "owner/repo", ctx.RepoSlug)
|
require.Equal(t, "owner/repo", ctx.RepoSlug)
|
||||||
require.Nil(t, ctx.LocalRepo)
|
require.NotNil(t, ctx.LocalRepo)
|
||||||
|
require.NotNil(t, ctx.Login)
|
||||||
|
require.Equal(t, "test-login", ctx.Login.Name)
|
||||||
|
|
||||||
|
cliCmd = cli.Command{
|
||||||
|
Name: "pr",
|
||||||
|
Flags: []cli.Flag{
|
||||||
|
&cli.StringFlag{Name: "repo"},
|
||||||
|
&cli.StringFlag{Name: "base"},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
require.NoError(t, cliCmd.Set("repo", "owner/repo"))
|
||||||
|
require.NoError(t, cliCmd.Set("base", "main"))
|
||||||
|
|
||||||
|
ctx, err = teacontext.InitCommand(&cliCmd)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, "repo", ctx.Repo)
|
||||||
|
require.Equal(t, "owner/repo", ctx.RepoSlug)
|
||||||
|
require.Equal(t, "main", ctx.String("base"))
|
||||||
|
require.NotNil(t, ctx.LocalRepo)
|
||||||
require.NotNil(t, ctx.Login)
|
require.NotNil(t, ctx.Login)
|
||||||
require.Equal(t, "test-login", ctx.Login.Name)
|
require.Equal(t, "test-login", ctx.Login.Name)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user