Skip to content

Commit dbeab2a

Browse files
n0tooseGusted
authored andcommitted
chore: introduce gitNeeded bool in setup (go-gitea#7348)
There are various commands of the Forgejo CLI that do not actually need Git, because i.e. they only issue network requests. Matter of fact, most occurrences do not actually require Git. By removing the Git initialization, operations by e.g. the manager will not fail in the absence of a Git binary. This is mostly relevant for an in-the-works Landlock implementation, which aims to minimize access to paths depending on the situation. Although we should expect that Git will be installed on the same system that the user is running Forgejo from, it somewhat slows things down, whereas the same edge cases that we are trying to protect the user from _could_ be achieved by keeping the `setting.RepoRootPath` check. Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/7348 Reviewed-by: Gusted <[email protected]> Co-authored-by: Panagiotis "Ivory" Vasilopoulos <[email protected]> Co-committed-by: Panagiotis "Ivory" Vasilopoulos <[email protected]>
1 parent 10c8ca6 commit dbeab2a

File tree

5 files changed

+23
-20
lines changed

5 files changed

+23
-20
lines changed

cmd/hook.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ func runHookPreReceive(c *cli.Context) error {
168168
ctx, cancel := installSignals()
169169
defer cancel()
170170

171-
setup(ctx, c.Bool("debug"))
171+
setup(ctx, c.Bool("debug"), true)
172172

173173
if len(os.Getenv("SSH_ORIGINAL_COMMAND")) == 0 {
174174
if setting.OnlyAllowPushIfGiteaEnvironmentSet {
@@ -327,7 +327,7 @@ func runHookPostReceive(c *cli.Context) error {
327327
ctx, cancel := installSignals()
328328
defer cancel()
329329

330-
setup(ctx, c.Bool("debug"))
330+
setup(ctx, c.Bool("debug"), true)
331331

332332
// First of all run update-server-info no matter what
333333
if _, _, err := git.NewCommand(ctx, "update-server-info").RunStdString(nil); err != nil {
@@ -491,7 +491,7 @@ func runHookProcReceive(c *cli.Context) error {
491491
ctx, cancel := installSignals()
492492
defer cancel()
493493

494-
setup(ctx, c.Bool("debug"))
494+
setup(ctx, c.Bool("debug"), true)
495495

496496
if len(os.Getenv("SSH_ORIGINAL_COMMAND")) == 0 {
497497
if setting.OnlyAllowPushIfGiteaEnvironmentSet {

cmd/keys.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func runKeys(c *cli.Context) error {
7171
ctx, cancel := installSignals()
7272
defer cancel()
7373

74-
setup(ctx, c.Bool("debug"))
74+
setup(ctx, c.Bool("debug"), true)
7575

7676
authorizedString, extra := private.AuthorizedPublicKeyByContent(ctx, content)
7777
// do not use handleCliResponseExtra or cli.NewExitError, if it exists immediately, it breaks some tests like Test_CmdKeys

cmd/manager.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func runShutdown(c *cli.Context) error {
112112
ctx, cancel := installSignals()
113113
defer cancel()
114114

115-
setup(ctx, c.Bool("debug"))
115+
setup(ctx, c.Bool("debug"), false)
116116
extra := private.Shutdown(ctx)
117117
return handleCliResponseExtra(extra)
118118
}
@@ -121,7 +121,7 @@ func runRestart(c *cli.Context) error {
121121
ctx, cancel := installSignals()
122122
defer cancel()
123123

124-
setup(ctx, c.Bool("debug"))
124+
setup(ctx, c.Bool("debug"), false)
125125
extra := private.Restart(ctx)
126126
return handleCliResponseExtra(extra)
127127
}
@@ -130,7 +130,7 @@ func runReloadTemplates(c *cli.Context) error {
130130
ctx, cancel := installSignals()
131131
defer cancel()
132132

133-
setup(ctx, c.Bool("debug"))
133+
setup(ctx, c.Bool("debug"), false)
134134
extra := private.ReloadTemplates(ctx)
135135
return handleCliResponseExtra(extra)
136136
}
@@ -139,7 +139,7 @@ func runFlushQueues(c *cli.Context) error {
139139
ctx, cancel := installSignals()
140140
defer cancel()
141141

142-
setup(ctx, c.Bool("debug"))
142+
setup(ctx, c.Bool("debug"), false)
143143
extra := private.FlushQueues(ctx, c.Duration("timeout"), c.Bool("non-blocking"))
144144
return handleCliResponseExtra(extra)
145145
}
@@ -148,7 +148,7 @@ func runProcesses(c *cli.Context) error {
148148
ctx, cancel := installSignals()
149149
defer cancel()
150150

151-
setup(ctx, c.Bool("debug"))
151+
setup(ctx, c.Bool("debug"), false)
152152
extra := private.Processes(ctx, os.Stdout, c.Bool("flat"), c.Bool("no-system"), c.Bool("stacktraces"), c.Bool("json"), c.String("cancel"))
153153
return handleCliResponseExtra(extra)
154154
}

cmd/manager_logging.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ func runRemoveLogger(c *cli.Context) error {
199199
ctx, cancel := installSignals()
200200
defer cancel()
201201

202-
setup(ctx, c.Bool("debug"))
202+
setup(ctx, c.Bool("debug"), false)
203203
logger := c.String("logger")
204204
if len(logger) == 0 {
205205
logger = log.DEFAULT
@@ -214,7 +214,7 @@ func runAddConnLogger(c *cli.Context) error {
214214
ctx, cancel := installSignals()
215215
defer cancel()
216216

217-
setup(ctx, c.Bool("debug"))
217+
setup(ctx, c.Bool("debug"), false)
218218
vals := map[string]any{}
219219
mode := "conn"
220220
vals["net"] = "tcp"
@@ -244,7 +244,7 @@ func runAddFileLogger(c *cli.Context) error {
244244
ctx, cancel := installSignals()
245245
defer cancel()
246246

247-
setup(ctx, c.Bool("debug"))
247+
setup(ctx, c.Bool("debug"), false)
248248
vals := map[string]any{}
249249
mode := "file"
250250
if c.IsSet("filename") {
@@ -311,7 +311,7 @@ func runPauseLogging(c *cli.Context) error {
311311
ctx, cancel := installSignals()
312312
defer cancel()
313313

314-
setup(ctx, c.Bool("debug"))
314+
setup(ctx, c.Bool("debug"), false)
315315
userMsg := private.PauseLogging(ctx)
316316
_, _ = fmt.Fprintln(os.Stdout, userMsg)
317317
return nil
@@ -321,7 +321,7 @@ func runResumeLogging(c *cli.Context) error {
321321
ctx, cancel := installSignals()
322322
defer cancel()
323323

324-
setup(ctx, c.Bool("debug"))
324+
setup(ctx, c.Bool("debug"), false)
325325
userMsg := private.ResumeLogging(ctx)
326326
_, _ = fmt.Fprintln(os.Stdout, userMsg)
327327
return nil
@@ -331,7 +331,7 @@ func runReleaseReopenLogging(c *cli.Context) error {
331331
ctx, cancel := installSignals()
332332
defer cancel()
333333

334-
setup(ctx, c.Bool("debug"))
334+
setup(ctx, c.Bool("debug"), false)
335335
userMsg := private.ReleaseReopenLogging(ctx)
336336
_, _ = fmt.Fprintln(os.Stdout, userMsg)
337337
return nil
@@ -340,7 +340,7 @@ func runReleaseReopenLogging(c *cli.Context) error {
340340
func runSetLogSQL(c *cli.Context) error {
341341
ctx, cancel := installSignals()
342342
defer cancel()
343-
setup(ctx, c.Bool("debug"))
343+
setup(ctx, c.Bool("debug"), false)
344344

345345
extra := private.SetLogSQL(ctx, !c.Bool("off"))
346346
return handleCliResponseExtra(extra)

cmd/serv.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,19 +57,22 @@ var CmdServ = &cli.Command{
5757
},
5858
}
5959

60-
func setup(ctx context.Context, debug bool) {
60+
func setup(ctx context.Context, debug, gitNeeded bool) {
6161
if debug {
6262
setupConsoleLogger(log.TRACE, false, os.Stderr)
6363
} else {
6464
setupConsoleLogger(log.FATAL, false, os.Stderr)
6565
}
6666
setting.MustInstalled()
67+
// Sanity check to ensure path is not relative, see: https://github.com/go-gitea/gitea/pull/19317
6768
if _, err := os.Stat(setting.RepoRootPath); err != nil {
6869
_ = fail(ctx, "Unable to access repository path", "Unable to access repository path %q, err: %v", setting.RepoRootPath, err)
6970
return
7071
}
71-
if err := git.InitSimple(context.Background()); err != nil {
72-
_ = fail(ctx, "Failed to init git", "Failed to init git, err: %v", err)
72+
if gitNeeded {
73+
if err := git.InitSimple(context.Background()); err != nil {
74+
_ = fail(ctx, "Failed to init git", "Failed to init git, err: %v", err)
75+
}
7376
}
7477
}
7578

@@ -133,7 +136,7 @@ func runServ(c *cli.Context) error {
133136
defer cancel()
134137

135138
// FIXME: This needs to internationalised
136-
setup(ctx, c.Bool("debug"))
139+
setup(ctx, c.Bool("debug"), true)
137140

138141
if setting.SSH.Disabled {
139142
fmt.Println("Forgejo: SSH has been disabled")

0 commit comments

Comments
 (0)