loading up the forgejo repo on tangled to test page performance
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

chore: introduce gitNeeded bool in setup (#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 <gusted@noreply.codeberg.org>
Co-authored-by: Panagiotis "Ivory" Vasilopoulos <git@n0toose.net>
Co-committed-by: Panagiotis "Ivory" Vasilopoulos <git@n0toose.net>

+23 -20
+3 -3
cmd/hook.go
··· 168 168 ctx, cancel := installSignals() 169 169 defer cancel() 170 170 171 - setup(ctx, c.Bool("debug")) 171 + setup(ctx, c.Bool("debug"), true) 172 172 173 173 if len(os.Getenv("SSH_ORIGINAL_COMMAND")) == 0 { 174 174 if setting.OnlyAllowPushIfGiteaEnvironmentSet { ··· 327 327 ctx, cancel := installSignals() 328 328 defer cancel() 329 329 330 - setup(ctx, c.Bool("debug")) 330 + setup(ctx, c.Bool("debug"), true) 331 331 332 332 // First of all run update-server-info no matter what 333 333 if _, _, err := git.NewCommand(ctx, "update-server-info").RunStdString(nil); err != nil { ··· 491 491 ctx, cancel := installSignals() 492 492 defer cancel() 493 493 494 - setup(ctx, c.Bool("debug")) 494 + setup(ctx, c.Bool("debug"), true) 495 495 496 496 if len(os.Getenv("SSH_ORIGINAL_COMMAND")) == 0 { 497 497 if setting.OnlyAllowPushIfGiteaEnvironmentSet {
+1 -1
cmd/keys.go
··· 71 71 ctx, cancel := installSignals() 72 72 defer cancel() 73 73 74 - setup(ctx, c.Bool("debug")) 74 + setup(ctx, c.Bool("debug"), true) 75 75 76 76 authorizedString, extra := private.AuthorizedPublicKeyByContent(ctx, content) 77 77 // do not use handleCliResponseExtra or cli.NewExitError, if it exists immediately, it breaks some tests like Test_CmdKeys
+5 -5
cmd/manager.go
··· 112 112 ctx, cancel := installSignals() 113 113 defer cancel() 114 114 115 - setup(ctx, c.Bool("debug")) 115 + setup(ctx, c.Bool("debug"), false) 116 116 extra := private.Shutdown(ctx) 117 117 return handleCliResponseExtra(extra) 118 118 } ··· 121 121 ctx, cancel := installSignals() 122 122 defer cancel() 123 123 124 - setup(ctx, c.Bool("debug")) 124 + setup(ctx, c.Bool("debug"), false) 125 125 extra := private.Restart(ctx) 126 126 return handleCliResponseExtra(extra) 127 127 } ··· 130 130 ctx, cancel := installSignals() 131 131 defer cancel() 132 132 133 - setup(ctx, c.Bool("debug")) 133 + setup(ctx, c.Bool("debug"), false) 134 134 extra := private.ReloadTemplates(ctx) 135 135 return handleCliResponseExtra(extra) 136 136 } ··· 139 139 ctx, cancel := installSignals() 140 140 defer cancel() 141 141 142 - setup(ctx, c.Bool("debug")) 142 + setup(ctx, c.Bool("debug"), false) 143 143 extra := private.FlushQueues(ctx, c.Duration("timeout"), c.Bool("non-blocking")) 144 144 return handleCliResponseExtra(extra) 145 145 } ··· 148 148 ctx, cancel := installSignals() 149 149 defer cancel() 150 150 151 - setup(ctx, c.Bool("debug")) 151 + setup(ctx, c.Bool("debug"), false) 152 152 extra := private.Processes(ctx, os.Stdout, c.Bool("flat"), c.Bool("no-system"), c.Bool("stacktraces"), c.Bool("json"), c.String("cancel")) 153 153 return handleCliResponseExtra(extra) 154 154 }
+7 -7
cmd/manager_logging.go
··· 199 199 ctx, cancel := installSignals() 200 200 defer cancel() 201 201 202 - setup(ctx, c.Bool("debug")) 202 + setup(ctx, c.Bool("debug"), false) 203 203 logger := c.String("logger") 204 204 if len(logger) == 0 { 205 205 logger = log.DEFAULT ··· 214 214 ctx, cancel := installSignals() 215 215 defer cancel() 216 216 217 - setup(ctx, c.Bool("debug")) 217 + setup(ctx, c.Bool("debug"), false) 218 218 vals := map[string]any{} 219 219 mode := "conn" 220 220 vals["net"] = "tcp" ··· 244 244 ctx, cancel := installSignals() 245 245 defer cancel() 246 246 247 - setup(ctx, c.Bool("debug")) 247 + setup(ctx, c.Bool("debug"), false) 248 248 vals := map[string]any{} 249 249 mode := "file" 250 250 if c.IsSet("filename") { ··· 311 311 ctx, cancel := installSignals() 312 312 defer cancel() 313 313 314 - setup(ctx, c.Bool("debug")) 314 + setup(ctx, c.Bool("debug"), false) 315 315 userMsg := private.PauseLogging(ctx) 316 316 _, _ = fmt.Fprintln(os.Stdout, userMsg) 317 317 return nil ··· 321 321 ctx, cancel := installSignals() 322 322 defer cancel() 323 323 324 - setup(ctx, c.Bool("debug")) 324 + setup(ctx, c.Bool("debug"), false) 325 325 userMsg := private.ResumeLogging(ctx) 326 326 _, _ = fmt.Fprintln(os.Stdout, userMsg) 327 327 return nil ··· 331 331 ctx, cancel := installSignals() 332 332 defer cancel() 333 333 334 - setup(ctx, c.Bool("debug")) 334 + setup(ctx, c.Bool("debug"), false) 335 335 userMsg := private.ReleaseReopenLogging(ctx) 336 336 _, _ = fmt.Fprintln(os.Stdout, userMsg) 337 337 return nil ··· 340 340 func runSetLogSQL(c *cli.Context) error { 341 341 ctx, cancel := installSignals() 342 342 defer cancel() 343 - setup(ctx, c.Bool("debug")) 343 + setup(ctx, c.Bool("debug"), false) 344 344 345 345 extra := private.SetLogSQL(ctx, !c.Bool("off")) 346 346 return handleCliResponseExtra(extra)
+7 -4
cmd/serv.go
··· 57 57 }, 58 58 } 59 59 60 - func setup(ctx context.Context, debug bool) { 60 + func setup(ctx context.Context, debug, gitNeeded bool) { 61 61 if debug { 62 62 setupConsoleLogger(log.TRACE, false, os.Stderr) 63 63 } else { 64 64 setupConsoleLogger(log.FATAL, false, os.Stderr) 65 65 } 66 66 setting.MustInstalled() 67 + // Sanity check to ensure path is not relative, see: https://github.com/go-gitea/gitea/pull/19317 67 68 if _, err := os.Stat(setting.RepoRootPath); err != nil { 68 69 _ = fail(ctx, "Unable to access repository path", "Unable to access repository path %q, err: %v", setting.RepoRootPath, err) 69 70 return 70 71 } 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 + } 73 76 } 74 77 } 75 78 ··· 133 136 defer cancel() 134 137 135 138 // FIXME: This needs to internationalised 136 - setup(ctx, c.Bool("debug")) 139 + setup(ctx, c.Bool("debug"), true) 137 140 138 141 if setting.SSH.Disabled { 139 142 fmt.Println("Forgejo: SSH has been disabled")