Fix notifications --mine outside git repositories (#1056)

Fixes #1055.

## Root cause
`tea notifications --mine` still initialized full repository context before checking the global notification scope, so it probed the current working directory with git and could select or fail on repository-derived context even though repository data is not needed.

## Changes
- Add an InitCommand option to skip local git repository discovery when a command does not need repository context.
- Use that option for notification list and mark-as operations when `--mine` is set.
- Add a regression test that makes `git` fail if invoked and verifies `notifications --mine` still uses the global notifications API.

## Tests
- `go test ./cmd/notifications ./modules/context`

Reviewed-on: https://gitea.com/gitea/tea/pulls/1056
This commit is contained in:
Lunny Xiao
2026-07-26 01:37:33 +00:00
parent cd93d8561b
commit 993eb37b57
4 changed files with 93 additions and 22 deletions
+2 -2
View File
@@ -63,12 +63,12 @@ func listNotifications(requestCtx stdctx.Context, cmd *cli.Command, status []git
var news []*gitea.NotificationThread
var err error
ctx, err := context.InitCommand(cmd)
all := cmd.Bool("mine")
ctx, err := context.InitCommandWithOptions(cmd, context.InitOptions{SkipLocalRepo: all})
if err != nil {
return err
}
client := ctx.Login.Client()
all := ctx.Bool("mine")
// This enforces pagination (see https://github.com/go-gitea/gitea/issues/16733)
listOpts := flags.GetListOptions(cmd)
+55
View File
@@ -0,0 +1,55 @@
// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package notifications
import (
stdctx "context"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"runtime"
"testing"
"gitea.dev/tea/modules/config"
"github.com/stretchr/testify/require"
"github.com/urfave/cli/v3"
)
func TestRunNotificationsListMineDoesNotProbeGitRepository(t *testing.T) {
gitPath := filepath.Join(t.TempDir(), "git")
gitScript := "#!/bin/sh\necho 'git should not be called' >&2\nexit 1\n"
if runtime.GOOS == "windows" {
gitPath += ".bat"
gitScript = "@echo git should not be called 1>&2\r\nexit /b 1\r\n"
}
require.NoError(t, os.WriteFile(gitPath, []byte(gitScript), 0o755))
t.Setenv("PATH", filepath.Dir(gitPath))
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
require.Equal(t, "/api/v1/notifications", r.URL.Path)
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`[]`))
}))
defer server.Close()
config.SetConfigForTesting(config.LocalConfig{
Logins: []config.Login{{
Name: "default",
URL: server.URL,
Token: "token",
User: "user",
Default: true,
}},
})
cmd := cli.Command{
Name: CmdNotificationsList.Name,
Flags: CmdNotificationsList.Flags,
}
require.NoError(t, cmd.Set("mine", "true"))
require.NoError(t, cmd.Set("output", "json"))
require.NoError(t, RunNotificationsList(stdctx.Background(), &cmd))
}
+4 -4
View File
@@ -24,7 +24,7 @@ var CmdNotificationsMarkRead = cli.Command{
ArgsUsage: "[all | <notification id>]",
Flags: flags.NotificationFlags,
Action: func(requestCtx stdctx.Context, cmd *cli.Command) error {
ctx, err := context.InitCommand(cmd)
ctx, err := context.InitCommandWithOptions(cmd, context.InitOptions{SkipLocalRepo: cmd.Bool("mine")})
if err != nil {
return err
}
@@ -48,7 +48,7 @@ var CmdNotificationsMarkUnread = cli.Command{
ArgsUsage: "[all | <notification id>]",
Flags: flags.NotificationFlags,
Action: func(requestCtx stdctx.Context, cmd *cli.Command) error {
ctx, err := context.InitCommand(cmd)
ctx, err := context.InitCommandWithOptions(cmd, context.InitOptions{SkipLocalRepo: cmd.Bool("mine")})
if err != nil {
return err
}
@@ -72,7 +72,7 @@ var CmdNotificationsMarkPinned = cli.Command{
ArgsUsage: "[all | <notification id>]",
Flags: flags.NotificationFlags,
Action: func(requestCtx stdctx.Context, cmd *cli.Command) error {
ctx, err := context.InitCommand(cmd)
ctx, err := context.InitCommandWithOptions(cmd, context.InitOptions{SkipLocalRepo: cmd.Bool("mine")})
if err != nil {
return err
}
@@ -95,7 +95,7 @@ var CmdNotificationsUnpin = cli.Command{
ArgsUsage: "[all | <notification id>]",
Flags: flags.NotificationFlags,
Action: func(requestCtx stdctx.Context, cmd *cli.Command) error {
ctx, err := context.InitCommand(cmd)
ctx, err := context.InitCommandWithOptions(cmd, context.InitOptions{SkipLocalRepo: cmd.Bool("mine")})
if err != nil {
return err
}