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.

Use git.HOME_PATH for Git HOME directory (#20114)

* Add git.HOME_PATH

* add legacy file check

* Apply suggestions from code review

Co-authored-by: zeripath <art27@cantab.net>

* pass env GNUPGHOME to git command, move the existing .gitconfig to new home, make the fix for 1.17rc more clear.

* set git.HOME_PATH for docker images to default HOME

* Revert "set git.HOME_PATH for docker images to default HOME"

This reverts commit f120101ddc267cef74e4f4b92c783d5fc8e275a1.

* force Gitea to use a stable GNUPGHOME directory

* extra check to ensure only process dir or symlink for legacy files

* refactor variable name

* The legacy dir check (for 1.17-rc1) could be removed with 1.18 release, since users should have upgraded from 1.17-rc to 1.17-stable

* Update modules/git/git.go

Co-authored-by: Steven Kriegler <61625851+justusbunsi@users.noreply.github.com>

* remove initFixGitHome117rc

* Update git.go

* Update docs/content/doc/advanced/config-cheat-sheet.en-us.md

Co-authored-by: zeripath <art27@cantab.net>
Co-authored-by: 6543 <6543@obermui.de>
Co-authored-by: Steven Kriegler <61625851+justusbunsi@users.noreply.github.com>
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>

authored by

wxiaoguang
zeripath
Steven Kriegler
6543
Lunny Xiao
and committed by
GitHub
496b8e39 c273dea5

+63 -27
+4 -1
custom/conf/app.example.ini
··· 603 603 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 604 604 ;; 605 605 ;; The path of git executable. If empty, Gitea searches through the PATH environment. 606 - PATH = 606 + ;PATH = 607 + ;; 608 + ;; The HOME directory for Git 609 + ;HOME_PATH = %(APP_DATA_PATH)/home 607 610 ;; 608 611 ;; Disables highlight of added and removed changes 609 612 ;DISABLE_DIFF_HIGHLIGHT = false
+2
docs/content/doc/advanced/config-cheat-sheet.en-us.md
··· 948 948 ## Git (`git`) 949 949 950 950 - `PATH`: **""**: The path of Git executable. If empty, Gitea searches through the PATH environment. 951 + - `HOME_PATH`: **%(APP_DATA_PATH)/home**: The HOME directory for Git. 952 + This directory will be used to contain the `.gitconfig` and possible `.gnupg` directories that Gitea's git calls will use. If you can confirm Gitea is the only application running in this environment, you can set it to the normal home directory for Gitea user. 951 953 - `DISABLE_DIFF_HIGHLIGHT`: **false**: Disables highlight of added and removed changes. 952 954 - `MAX_GIT_DIFF_LINES`: **1000**: Max number of lines allowed of a single file in diff view. 953 955 - `MAX_GIT_DIFF_LINE_CHARACTERS`: **5000**: Max character count per line highlighted in diff view.
+4 -3
docs/content/doc/advanced/signing.en-us.md
··· 97 97 signing keys on a per-repository basis. However, this is clearly not an 98 98 ideal UI and therefore subject to change. 99 99 100 - **Since 1.17**, Gitea runs git in its own home directory `[repository].ROOT` and uses its own config `{[repository].ROOT}/.gitconfig`. 100 + **Since 1.17**, Gitea runs git in its own home directory `[git].HOME_PATH` (default to `%(APP_DATA_PATH)/home`) 101 + and uses its own config `{[git].HOME_PATH}/.gitconfig`. 101 102 If you have your own customized git config for Gitea, you should set these configs in system git config (aka `/etc/gitconfig`) 102 - or the Gitea internal git config `{[repository].ROOT}/.gitconfig`. 103 - Related home files for git command (like `.gnupg`) should also be put in Gitea's git home directory `[repository].ROOT`. 103 + or the Gitea internal git config `{[git].HOME_PATH}/.gitconfig`. 104 + Related home files for git command (like `.gnupg`) should also be put in Gitea's git home directory `[git].HOME_PATH`. 104 105 105 106 106 107 ### `INITIAL_COMMIT`
+2
models/unittest/testdb.go
··· 107 107 108 108 setting.Packages.Storage.Path = filepath.Join(setting.AppDataPath, "packages") 109 109 110 + setting.Git.HomePath = filepath.Join(setting.AppDataPath, "home") 111 + 110 112 if err = storage.Init(); err != nil { 111 113 fatalTestError("storage.Init: %v\n", err) 112 114 }
+23 -10
modules/git/command.go
··· 105 105 PipelineFunc func(context.Context, context.CancelFunc) error 106 106 } 107 107 108 - // CommonGitCmdEnvs returns the common environment variables for a "git" command. 109 - func CommonGitCmdEnvs() []string { 108 + func commonBaseEnvs() []string { 110 109 // at the moment, do not set "GIT_CONFIG_NOSYSTEM", users may have put some configs like "receive.certNonceSeed" in it 111 - return []string{ 112 - fmt.Sprintf("LC_ALL=%s", DefaultLocale), 113 - "GIT_TERMINAL_PROMPT=0", // avoid prompting for credentials interactively, supported since git v2.3 110 + envs := []string{ 111 + "HOME=" + HomeDir(), // make Gitea use internal git config only, to prevent conflicts with user's git config 114 112 "GIT_NO_REPLACE_OBJECTS=1", // ignore replace references (https://git-scm.com/docs/git-replace) 115 - "HOME=" + HomeDir(), // make Gitea use internal git config only, to prevent conflicts with user's git config 113 + } 114 + 115 + // some environment variables should be passed to git command 116 + passThroughEnvKeys := []string{ 117 + "GNUPGHOME", // git may call gnupg to do commit signing 118 + } 119 + for _, key := range passThroughEnvKeys { 120 + if val, ok := os.LookupEnv(key); ok { 121 + envs = append(envs, key+"="+val) 122 + } 116 123 } 124 + return envs 125 + } 126 + 127 + // CommonGitCmdEnvs returns the common environment variables for a "git" command. 128 + func CommonGitCmdEnvs() []string { 129 + return append(commonBaseEnvs(), []string{ 130 + "LC_ALL=" + DefaultLocale, 131 + "GIT_TERMINAL_PROMPT=0", // avoid prompting for credentials interactively, supported since git v2.3 132 + }...) 117 133 } 118 134 119 135 // CommonCmdServEnvs is like CommonGitCmdEnvs but it only returns minimal required environment variables for the "gitea serv" command 120 136 func CommonCmdServEnvs() []string { 121 - return []string{ 122 - "GIT_NO_REPLACE_OBJECTS=1", // ignore replace references (https://git-scm.com/docs/git-replace) 123 - "HOME=" + HomeDir(), // make Gitea use internal git config only, to prevent conflicts with user's git config 124 - } 137 + return commonBaseEnvs() 125 138 } 126 139 127 140 // Run runs the command with the RunOpts
+13 -9
modules/git/git.go
··· 11 11 "fmt" 12 12 "os" 13 13 "os/exec" 14 + "path/filepath" 14 15 "regexp" 15 16 "runtime" 16 17 "strings" ··· 19 20 20 21 "code.gitea.io/gitea/modules/log" 21 22 "code.gitea.io/gitea/modules/setting" 22 - 23 23 "github.com/hashicorp/go-version" 24 24 ) 25 25 ··· 126 126 } 127 127 128 128 func checkInit() error { 129 - if setting.RepoRootPath == "" { 130 - return errors.New("can not init Git's HomeDir (RepoRootPath is empty), the setting and git modules are not initialized correctly") 129 + if setting.Git.HomePath == "" { 130 + return errors.New("unable to init Git's HomeDir, incorrect initialization of the setting and git modules") 131 131 } 132 132 if DefaultContext != nil { 133 133 log.Warn("git module has been initialized already, duplicate init should be fixed") ··· 137 137 138 138 // HomeDir is the home dir for git to store the global config file used by Gitea internally 139 139 func HomeDir() string { 140 - if setting.RepoRootPath == "" { 140 + if setting.Git.HomePath == "" { 141 141 // strict check, make sure the git module is initialized correctly. 142 142 // attention: when the git module is called in gitea sub-command (serv/hook), the log module is not able to show messages to users. 143 143 // for example: if there is gitea git hook code calling git.NewCommand before git.InitXxx, the integration test won't show the real failure reasons. 144 - log.Fatal("can not get Git's HomeDir (RepoRootPath is empty), the setting and git modules are not initialized correctly") 144 + log.Fatal("Unable to init Git's HomeDir, incorrect initialization of the setting and git modules") 145 145 return "" 146 146 } 147 - return setting.RepoRootPath 147 + return setting.Git.HomePath 148 148 } 149 149 150 150 // InitSimple initializes git module with a very simple step, no config changes, no global command arguments. ··· 175 175 } 176 176 177 177 initOnce.Do(func() { 178 - err = InitSimple(ctx) 179 - if err != nil { 178 + if err = InitSimple(ctx); err != nil { 180 179 return 180 + } 181 + 182 + // when git works with gnupg (commit signing), there should be a stable home for gnupg commands 183 + if _, ok := os.LookupEnv("GNUPGHOME"); !ok { 184 + _ = os.Setenv("GNUPGHOME", filepath.Join(HomeDir(), ".gnupg")) 181 185 } 182 186 183 187 // Since git wire protocol has been released from git v2.18 ··· 206 210 // syncGitConfig only modifies gitconfig, won't change global variables (otherwise there will be data-race problem) 207 211 func syncGitConfig() (err error) { 208 212 if err = os.MkdirAll(HomeDir(), os.ModePerm); err != nil { 209 - return fmt.Errorf("unable to create directory %s, err: %w", setting.RepoRootPath, err) 213 + return fmt.Errorf("unable to prepare git home directory %s, err: %w", HomeDir(), err) 210 214 } 211 215 212 216 // Git requires setting user.name and user.email in order to commit changes - old comment: "if they're not set just add some defaults"
+3 -3
modules/git/git_test.go
··· 21 21 func testRun(m *testing.M) error { 22 22 _ = log.NewLogger(1000, "console", "console", `{"level":"trace","stacktracelevel":"NONE","stderr":true}`) 23 23 24 - repoRootPath, err := os.MkdirTemp(os.TempDir(), "repos") 24 + gitHomePath, err := os.MkdirTemp(os.TempDir(), "git-home") 25 25 if err != nil { 26 26 return fmt.Errorf("unable to create temp dir: %w", err) 27 27 } 28 - defer util.RemoveAll(repoRootPath) 29 - setting.RepoRootPath = repoRootPath 28 + defer util.RemoveAll(gitHomePath) 29 + setting.Git.HomePath = gitHomePath 30 30 31 31 if err = InitOnceWithSync(context.Background()); err != nil { 32 32 return fmt.Errorf("failed to call Init: %w", err)
+12 -1
modules/setting/git.go
··· 5 5 package setting 6 6 7 7 import ( 8 + "path/filepath" 8 9 "time" 9 10 10 11 "code.gitea.io/gitea/modules/log" ··· 13 14 // Git settings 14 15 var Git = struct { 15 16 Path string 17 + HomePath string 16 18 DisableDiffHighlight bool 17 19 MaxGitDiffLines int 18 20 MaxGitDiffLineCharacters int ··· 67 69 } 68 70 69 71 func newGit() { 70 - if err := Cfg.Section("git").MapTo(&Git); err != nil { 72 + sec := Cfg.Section("git") 73 + 74 + if err := sec.MapTo(&Git); err != nil { 71 75 log.Fatal("Failed to map Git settings: %v", err) 76 + } 77 + 78 + Git.HomePath = sec.Key("HOME_PATH").MustString("home") 79 + if !filepath.IsAbs(Git.HomePath) { 80 + Git.HomePath = filepath.Join(AppDataPath, Git.HomePath) 81 + } else { 82 + Git.HomePath = filepath.Clean(Git.HomePath) 72 83 } 73 84 }