gitea源码

env.go 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // Copyright 2025 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package gitcmd
  4. import (
  5. "fmt"
  6. "os/exec"
  7. "code.gitea.io/gitea/modules/log"
  8. "code.gitea.io/gitea/modules/setting"
  9. )
  10. var GitExecutable = "git" // the command name of git, will be updated to an absolute path during initialization
  11. // SetExecutablePath changes the path of git executable and checks the file permission and version.
  12. func SetExecutablePath(path string) error {
  13. // If path is empty, we use the default value of GitExecutable "git" to search for the location of git.
  14. if path != "" {
  15. GitExecutable = path
  16. }
  17. absPath, err := exec.LookPath(GitExecutable)
  18. if err != nil {
  19. return fmt.Errorf("git not found: %w", err)
  20. }
  21. GitExecutable = absPath
  22. return nil
  23. }
  24. // HomeDir is the home dir for git to store the global config file used by Gitea internally
  25. func HomeDir() string {
  26. if setting.Git.HomePath == "" {
  27. // strict check, make sure the git module is initialized correctly.
  28. // attention: when the git module is called in gitea sub-command (serv/hook), the log module might not obviously show messages to users/developers.
  29. // for example: if there is gitea git hook code calling NewCommand before git.InitXxx, the integration test won't show the real failure reasons.
  30. log.Fatal("Unable to init Git's HomeDir, incorrect initialization of the setting and git modules")
  31. return ""
  32. }
  33. return setting.Git.HomePath
  34. }