Skip to content

Commit 1a386b7

Browse files
committed
pass env GNUPGHOME to git command, move the existing .gitconfig to new home, make the fix for 1.17rc more clear.
1 parent f345b4c commit 1a386b7

File tree

2 files changed

+68
-29
lines changed

2 files changed

+68
-29
lines changed

modules/git/command.go

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -105,23 +105,36 @@ type RunOpts struct {
105105
PipelineFunc func(context.Context, context.CancelFunc) error
106106
}
107107

108-
// CommonGitCmdEnvs returns the common environment variables for a "git" command.
109-
func CommonGitCmdEnvs() []string {
108+
func commonBaseEnvs() []string {
110109
// 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
114-
"GIT_NO_REPLACE_OBJECTS=1", // ignore replace references (https://git-scm.com/docs/git-replace)
110+
envs := []string{
115111
"HOME=" + HomeDir(), // make Gitea use internal git config only, to prevent conflicts with user's git config
112+
"GIT_NO_REPLACE_OBJECTS=1", // ignore replace references (https://git-scm.com/docs/git-replace)
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+
}
116123
}
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+
}...)
117133
}
118134

119135
// CommonCmdServEnvs is like CommonGitCmdEnvs but it only returns minimal required environment variables for the "gitea serv" command
120136
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()
125138
}
126139

127140
// Run runs the command with the RunOpts

modules/git/git.go

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020

2121
"code.gitea.io/gitea/modules/log"
2222
"code.gitea.io/gitea/modules/setting"
23+
"code.gitea.io/gitea/modules/util"
2324

2425
"github.com/hashicorp/go-version"
2526
)
@@ -167,6 +168,47 @@ func InitSimple(ctx context.Context) error {
167168

168169
var initOnce sync.Once
169170

171+
func initFixGitHome117rc() error {
172+
// Gitea 1.17-rc uses "setting.RepoRootPath" for Git HOME, which is incorrect.
173+
// Do this check to make sure there is no legacy file in the RepoRootPath. This check might be able to be removed with 1.19 release.
174+
175+
// remove the auto generated git config file (it will be moved to new home)
176+
gitConfigNewPath := filepath.Join(HomeDir(), ".gitconfig")
177+
gitConfigLegacyPath := filepath.Join(setting.RepoRootPath, ".gitconfig")
178+
if ok, err := util.IsExist(gitConfigLegacyPath); ok && err == nil {
179+
if err = os.MkdirAll(HomeDir(), os.ModePerm); err != nil {
180+
return err
181+
}
182+
if ok, err = util.IsExist(gitConfigNewPath); !ok && err == nil {
183+
err = util.CopyFile(gitConfigLegacyPath, gitConfigNewPath)
184+
} else {
185+
err = util.CopyFile(gitConfigLegacyPath, gitConfigNewPath+".bak")
186+
}
187+
if err != nil {
188+
return err
189+
}
190+
_ = os.Remove(gitConfigLegacyPath)
191+
}
192+
193+
// remove the empty directories, if some directories are non-empty, warn users and exit
194+
var hasCheckErr bool
195+
for _, wellDirName := range []string{".ssh", ".gnupg"} {
196+
checkLegacyDir := filepath.Join(setting.RepoRootPath, wellDirName)
197+
_ = os.Remove(checkLegacyDir) // try to remove the empty dummy directory first
198+
_, checkErr := os.Stat(checkLegacyDir) // if the directory is not empty, then it won't be removed, it should be handled manually
199+
if checkErr == nil || !errors.Is(checkErr, os.ErrNotExist) {
200+
log.Error(`Git HOME has been moved to [git].HOME_PATH, but there are legacy file in old place. Please backup and remove the legacy files %q`, checkLegacyDir)
201+
hasCheckErr = true
202+
}
203+
}
204+
205+
if hasCheckErr {
206+
log.Fatal("Please fix errors above, remove legacy files.")
207+
}
208+
209+
return nil
210+
}
211+
170212
// InitOnceWithSync initializes git module with version check and change global variables, sync gitconfig.
171213
// This method will update the global variables ONLY ONCE (just like git.CheckLFSVersion -- which is not ideal too),
172214
// otherwise there will be data-race problem at the moment.
@@ -176,28 +218,12 @@ func InitOnceWithSync(ctx context.Context) (err error) {
176218
}
177219

178220
initOnce.Do(func() {
179-
err = InitSimple(ctx)
180-
if err != nil {
221+
if err = InitSimple(ctx); err != nil {
181222
return
182223
}
183-
184-
// Gitea 1.17-rc uses "setting.RepoRootPath" for Git HOME, which is incorrect.
185-
// Do this check to make sure there is no legacy file in the RepoRootPath. This check might be able to be removed with 1.19 release.
186-
var hasCheckErr bool
187-
_ = os.Remove(filepath.Join(setting.RepoRootPath, ".gitconfig")) // remove the auto generated git config file
188-
_ = os.Remove(filepath.Join(setting.RepoRootPath, ".ssh")) // remove the empty dummy ".ssh" directory
189-
for _, wellKnownName := range []string{".ssh", ".gnupg"} {
190-
checkLegacyFile := filepath.Join(setting.RepoRootPath, wellKnownName)
191-
_, checkErr := os.Stat(checkLegacyFile)
192-
if checkErr == nil || !errors.Is(checkErr, os.ErrNotExist) {
193-
log.Error(`Git HOME has been moved to [git].HOME_PATH, but there are legacy file in old place. Please backup and remove the legacy files %q`, checkLegacyFile)
194-
hasCheckErr = true
195-
}
196-
}
197-
if hasCheckErr {
198-
log.Fatal("Please fix errors above, remove legacy files")
224+
if err = initFixGitHome117rc(); err != nil {
225+
return
199226
}
200-
// end of legacy Gitea 1.17-rc check
201227

202228
// Since git wire protocol has been released from git v2.18
203229
if setting.Git.EnableAutoGitWireProtocol && CheckGitVersionAtLeast("2.18") == nil {

0 commit comments

Comments
 (0)