From 783ac7684a38a7b5be63e2587f245cf63e7f372a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C4=9Bj=20Cepl?= Date: Mon, 20 Apr 2026 19:39:42 +0000 Subject: [PATCH] fix(context): skip local repo detection for repo slugs (#960) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Treat explicit --repo slugs as remote targets so commands do not probe the current worktree. This avoids SHA256 repository failures when local git autodetection is unnecessary. --------- Co-authored-by: Lunny Xiao Reviewed-on: https://gitea.com/gitea/tea/pulls/960 Reviewed-by: Lunny Xiao Co-authored-by: Matěj Cepl Co-committed-by: Matěj Cepl --- modules/context/context.go | 29 ++++++++++--------- modules/context/context_test.go | 49 +++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 15 deletions(-) diff --git a/modules/context/context.go b/modules/context/context.go index 0fe55b3..7255c68 100644 --- a/modules/context/context.go +++ b/modules/context/context.go @@ -83,6 +83,8 @@ func InitCommand(cmd *cli.Command) (*TeaContext, error) { } if repoFlagPathExists { repoPath = repoFlag + } else { + c.RepoSlug = repoFlag } } @@ -90,12 +92,6 @@ func InitCommand(cmd *cli.Command) (*TeaContext, error) { remoteFlag = config.GetPreferences().FlagDefaults.Remote } - if repoPath == "" { - if repoPath, err = os.Getwd(); err != nil { - return nil, err - } - } - // Create env login before repo context detection so it participates in remote URL matching var extraLogins []config.Login envLogin := GetLoginByEnvVar() @@ -108,17 +104,20 @@ 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, // otherwise attempt PWD. if no repo is found, continue with default login - if c.LocalRepo, c.Login, c.RepoSlug, err = contextFromLocalRepo(repoPath, remoteFlag, extraLogins); err != nil { - if err == errNotAGiteaRepo || err == gogit.ErrRepositoryNotExists { - // we can deal with that, commands needing the optional values use ctx.Ensure() - } else { - return nil, err + if c.RepoSlug == "" { + if repoPath == "" { + if repoPath, err = os.Getwd(); err != nil { + return nil, err + } } - } - if len(repoFlag) != 0 && !repoFlagPathExists { - // if repoFlag is not a valid path, use it to override repoSlug - c.RepoSlug = repoFlag + if c.LocalRepo, c.Login, c.RepoSlug, err = contextFromLocalRepo(repoPath, remoteFlag, extraLogins); err != nil { + if err == errNotAGiteaRepo || err == gogit.ErrRepositoryNotExists { + // we can deal with that, commands needing the optional values use ctx.Ensure() + } else { + return nil, err + } + } } // If env vars are set, always use the env login (but repo slug was already diff --git a/modules/context/context_test.go b/modules/context/context_test.go index 9b73f88..2c8ea43 100644 --- a/modules/context/context_test.go +++ b/modules/context/context_test.go @@ -4,9 +4,14 @@ package context import ( + "os" + "os/exec" "testing" "code.gitea.io/tea/modules/config" + + "github.com/stretchr/testify/require" + "github.com/urfave/cli/v3" ) func Test_MatchLogins(t *testing.T) { @@ -65,3 +70,47 @@ func Test_MatchLogins(t *testing.T) { }) } } + +func TestInitCommand_WithRepoSlugSkipsLocalRepoDetection(t *testing.T) { + tmpDir := t.TempDir() + config.SetConfigForTesting(config.LocalConfig{ + Logins: []config.Login{{ + Name: "test-login", + URL: "https://gitea.example.com", + Token: "token", + User: "login-user", + Default: true, + }}, + }) + + cmd := exec.Command("git", "init", "--object-format=sha256", tmpDir) + cmd.Env = os.Environ() + require.NoError(t, cmd.Run()) + + oldWd, err := os.Getwd() + require.NoError(t, err) + require.NoError(t, os.Chdir(tmpDir)) + t.Cleanup(func() { + require.NoError(t, os.Chdir(oldWd)) + }) + + cliCmd := cli.Command{ + Name: "branches", + Flags: []cli.Flag{ + &cli.StringFlag{Name: "login"}, + &cli.StringFlag{Name: "repo"}, + &cli.StringFlag{Name: "remote"}, + &cli.StringFlag{Name: "output"}, + }, + } + require.NoError(t, cliCmd.Set("repo", "owner/repo")) + + ctx, err := InitCommand(&cliCmd) + require.NoError(t, err) + require.Equal(t, "owner", ctx.Owner) + require.Equal(t, "repo", ctx.Repo) + require.Equal(t, "owner/repo", ctx.RepoSlug) + require.Nil(t, ctx.LocalRepo) + require.NotNil(t, ctx.Login) + require.Equal(t, "test-login", ctx.Login.Name) +}