66package cmd
77
88import (
9+ "context"
910 "fmt"
1011 "net/http"
1112 "net/url"
@@ -65,6 +66,21 @@ func setup(logPath string, debug bool) {
6566 if debug {
6667 setting .RunMode = "dev"
6768 }
69+
70+ // Check if setting.RepoRootPath exists. It could be the case that it doesn't exist, this can happen when
71+ // `[repository]` `ROOT` is a relative path and $GITEA_WORK_DIR isn't passed to the SSH connection.
72+ if _ , err := os .Stat (setting .RepoRootPath ); err != nil {
73+ if os .IsNotExist (err ) {
74+ _ = fail ("Incorrect configuration, no repository directory." , "Directory `[repository].ROOT` %q was not found, please check if $GITEA_WORK_DIR is passed to the SSH connection or make `[repository].ROOT` an absolute value." , setting .RepoRootPath )
75+ } else {
76+ _ = fail ("Incorrect configuration, repository directory is inaccessible" , "Directory `[repository].ROOT` %q is inaccessible. err: %v" , setting .RepoRootPath , err )
77+ }
78+ return
79+ }
80+
81+ if err := git .InitSimple (context .Background ()); err != nil {
82+ _ = fail ("Failed to init git" , "Failed to init git, err: %v" , err )
83+ }
6884}
6985
7086var (
@@ -80,12 +96,12 @@ var (
8096func fail (userMessage , logMessage string , args ... interface {}) error {
8197 // There appears to be a chance to cause a zombie process and failure to read the Exit status
8298 // if nothing is outputted on stdout.
83- fmt .Fprintln (os .Stdout , "" )
84- fmt .Fprintln (os .Stderr , "Gitea:" , userMessage )
99+ _ , _ = fmt .Fprintln (os .Stdout , "" )
100+ _ , _ = fmt .Fprintln (os .Stderr , "Gitea:" , userMessage )
85101
86102 if len (logMessage ) > 0 {
87103 if ! setting .IsProd {
88- fmt .Fprintf (os .Stderr , logMessage + "\n " , args ... )
104+ _ , _ = fmt .Fprintf (os .Stderr , logMessage + "\n " , args ... )
89105 }
90106 }
91107 ctx , cancel := installSignals ()
@@ -237,17 +253,6 @@ func runServ(c *cli.Context) error {
237253 }
238254 return fail ("Internal Server Error" , "%s" , err .Error ())
239255 }
240- os .Setenv (repo_module .EnvRepoIsWiki , strconv .FormatBool (results .IsWiki ))
241- os .Setenv (repo_module .EnvRepoName , results .RepoName )
242- os .Setenv (repo_module .EnvRepoUsername , results .OwnerName )
243- os .Setenv (repo_module .EnvPusherName , results .UserName )
244- os .Setenv (repo_module .EnvPusherEmail , results .UserEmail )
245- os .Setenv (repo_module .EnvPusherID , strconv .FormatInt (results .UserID , 10 ))
246- os .Setenv (repo_module .EnvRepoID , strconv .FormatInt (results .RepoID , 10 ))
247- os .Setenv (repo_module .EnvPRID , fmt .Sprintf ("%d" , 0 ))
248- os .Setenv (repo_module .EnvDeployKeyID , fmt .Sprintf ("%d" , results .DeployKeyID ))
249- os .Setenv (repo_module .EnvKeyID , fmt .Sprintf ("%d" , results .KeyID ))
250- os .Setenv (repo_module .EnvAppURL , setting .AppURL )
251256
252257 // LFS token authentication
253258 if verb == lfsAuthenticateVerb {
@@ -298,20 +303,29 @@ func runServ(c *cli.Context) error {
298303 gitcmd = exec .CommandContext (ctx , verb , repoPath )
299304 }
300305
301- // Check if setting.RepoRootPath exists. It could be the case that it doesn't exist, this can happen when
302- // `[repository]` `ROOT` is a relative path and $GITEA_WORK_DIR isn't passed to the SSH connection.
303- if _ , err := os .Stat (setting .RepoRootPath ); err != nil {
304- if os .IsNotExist (err ) {
305- return fail ("Incorrect configuration." ,
306- "Directory `[repository]` `ROOT` %s was not found, please check if $GITEA_WORK_DIR is passed to the SSH connection or make `[repository]` `ROOT` an absolute value." , setting .RepoRootPath )
307- }
308- }
309-
310306 process .SetSysProcAttribute (gitcmd )
311307 gitcmd .Dir = setting .RepoRootPath
312308 gitcmd .Stdout = os .Stdout
313309 gitcmd .Stdin = os .Stdin
314310 gitcmd .Stderr = os .Stderr
311+ gitcmd .Env = append (gitcmd .Env , os .Environ ()... )
312+ gitcmd .Env = append (gitcmd .Env ,
313+ repo_module .EnvRepoIsWiki + "=" + strconv .FormatBool (results .IsWiki ),
314+ repo_module .EnvRepoName + "=" + results .RepoName ,
315+ repo_module .EnvRepoUsername + "=" + results .OwnerName ,
316+ repo_module .EnvPusherName + "=" + results .UserName ,
317+ repo_module .EnvPusherEmail + "=" + results .UserEmail ,
318+ repo_module .EnvPusherID + "=" + strconv .FormatInt (results .UserID , 10 ),
319+ repo_module .EnvRepoID + "=" + strconv .FormatInt (results .RepoID , 10 ),
320+ repo_module .EnvPRID + "=" + fmt .Sprintf ("%d" , 0 ),
321+ repo_module .EnvDeployKeyID + "=" + fmt .Sprintf ("%d" , results .DeployKeyID ),
322+ repo_module .EnvKeyID + "=" + fmt .Sprintf ("%d" , results .KeyID ),
323+ repo_module .EnvAppURL + "=" + setting .AppURL ,
324+ )
325+ // to avoid breaking, here only use the minimal environment variables for the "gitea serv" command.
326+ // it could be re-considered whether to use the same git.CommonGitCmdEnvs() as "git" command later.
327+ gitcmd .Env = append (gitcmd .Env , git .CommonCmdServEnvs ()... )
328+
315329 if err = gitcmd .Run (); err != nil {
316330 return fail ("Internal error" , "Failed to execute git command: %v" , err )
317331 }
0 commit comments