From 7f4b6893ec06c1a7f4dd585f208567aec37f0c97 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Fri, 24 Jun 2022 14:03:53 +0800 Subject: [PATCH 01/14] Add git.HOME_PATH --- custom/conf/app.example.ini | 5 ++++- .../content/doc/advanced/config-cheat-sheet.en-us.md | 1 + docs/content/doc/advanced/signing.en-us.md | 7 ++++--- models/unittest/testdb.go | 2 ++ modules/git/git.go | 12 ++++++------ modules/git/git_test.go | 6 +++--- modules/setting/git.go | 11 +++++++++++ 7 files changed, 31 insertions(+), 13 deletions(-) diff --git a/custom/conf/app.example.ini b/custom/conf/app.example.ini index 80017a11764ee..69e3cb316b0a1 100644 --- a/custom/conf/app.example.ini +++ b/custom/conf/app.example.ini @@ -617,7 +617,10 @@ ROUTER = console ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; ;; The path of git executable. If empty, Gitea searches through the PATH environment. -PATH = +;PATH = +;; +;; The HOME directory for Git +;HOME_PATH = %(APP_DATA_PATH)/home ;; ;; Disables highlight of added and removed changes ;DISABLE_DIFF_HIGHLIGHT = false diff --git a/docs/content/doc/advanced/config-cheat-sheet.en-us.md b/docs/content/doc/advanced/config-cheat-sheet.en-us.md index 15628a7def051..a1aabdda1649d 100644 --- a/docs/content/doc/advanced/config-cheat-sheet.en-us.md +++ b/docs/content/doc/advanced/config-cheat-sheet.en-us.md @@ -947,6 +947,7 @@ Default templates for project boards: ## Git (`git`) - `PATH`: **""**: The path of Git executable. If empty, Gitea searches through the PATH environment. +- `HOME_PATH`: **%(APP_DATA_PATH)/home**: The HOME directory for Git. - `DISABLE_DIFF_HIGHLIGHT`: **false**: Disables highlight of added and removed changes. - `MAX_GIT_DIFF_LINES`: **1000**: Max number of lines allowed of a single file in diff view. - `MAX_GIT_DIFF_LINE_CHARACTERS`: **5000**: Max character count per line highlighted in diff view. diff --git a/docs/content/doc/advanced/signing.en-us.md b/docs/content/doc/advanced/signing.en-us.md index 8ae2f94e9ece9..9ef94deb75ce4 100644 --- a/docs/content/doc/advanced/signing.en-us.md +++ b/docs/content/doc/advanced/signing.en-us.md @@ -97,10 +97,11 @@ repositories, `SIGNING_KEY=default` could be used to provide different signing keys on a per-repository basis. However, this is clearly not an ideal UI and therefore subject to change. -**Since 1.17**, Gitea runs git in its own home directory `[repository].ROOT` and uses its own config `{[repository].ROOT}/.gitconfig`. +**Since 1.17**, Gitea runs git in its own home directory `[git].HOME_PATH` (default to `%(APP_DATA_PATH)/home`) +and uses its own config `{[git].HOME_PATH}/.gitconfig`. If you have your own customized git config for Gitea, you should set these configs in system git config (aka `/etc/gitconfig`) -or the Gitea internal git config `{[repository].ROOT}/.gitconfig`. -Related home files for git command (like `.gnupg`) should also be put in Gitea's git home directory `[repository].ROOT`. +or the Gitea internal git config `{[git].HOME_PATH}/.gitconfig`. +Related home files for git command (like `.gnupg`) should also be put in Gitea's git home directory `[git].HOME_PATH`. ### `INITIAL_COMMIT` diff --git a/models/unittest/testdb.go b/models/unittest/testdb.go index baea46dbce68f..7f9af553747cb 100644 --- a/models/unittest/testdb.go +++ b/models/unittest/testdb.go @@ -107,6 +107,8 @@ func MainTest(m *testing.M, testOpts *TestOptions) { setting.Packages.Storage.Path = filepath.Join(setting.AppDataPath, "packages") + setting.Git.HomePath = filepath.Join(setting.AppDataPath, "home") + if err = storage.Init(); err != nil { fatalTestError("storage.Init: %v\n", err) } diff --git a/modules/git/git.go b/modules/git/git.go index 0652a75f9aeff..c792a798864d3 100644 --- a/modules/git/git.go +++ b/modules/git/git.go @@ -126,8 +126,8 @@ func VersionInfo() string { } func checkInit() error { - if setting.RepoRootPath == "" { - return errors.New("can not init Git's HomeDir (RepoRootPath is empty), the setting and git modules are not initialized correctly") + if setting.Git.HomePath == "" { + return errors.New("can not init Git's HomeDir, the setting and git modules are not initialized correctly") } if DefaultContext != nil { log.Warn("git module has been initialized already, duplicate init should be fixed") @@ -137,14 +137,14 @@ func checkInit() error { // HomeDir is the home dir for git to store the global config file used by Gitea internally func HomeDir() string { - if setting.RepoRootPath == "" { + if setting.Git.HomePath == "" { // strict check, make sure the git module is initialized correctly. // attention: when the git module is called in gitea sub-command (serv/hook), the log module is not able to show messages to users. // 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. - log.Fatal("can not get Git's HomeDir (RepoRootPath is empty), the setting and git modules are not initialized correctly") + log.Fatal("can not get Git's HomeDir, the setting and git modules are not initialized correctly") return "" } - return setting.RepoRootPath + return setting.Git.HomePath } // InitSimple initializes git module with a very simple step, no config changes, no global command arguments. @@ -206,7 +206,7 @@ func InitOnceWithSync(ctx context.Context) (err error) { // syncGitConfig only modifies gitconfig, won't change global variables (otherwise there will be data-race problem) func syncGitConfig() (err error) { if err = os.MkdirAll(HomeDir(), os.ModePerm); err != nil { - return fmt.Errorf("unable to create directory %s, err: %w", setting.RepoRootPath, err) + return fmt.Errorf("unable to prepare git home directory %s, err: %w", HomeDir(), err) } // Git requires setting user.name and user.email in order to commit changes - old comment: "if they're not set just add some defaults" diff --git a/modules/git/git_test.go b/modules/git/git_test.go index c1a9ec351aaa5..c5a63de0644c0 100644 --- a/modules/git/git_test.go +++ b/modules/git/git_test.go @@ -21,12 +21,12 @@ import ( func testRun(m *testing.M) error { _ = log.NewLogger(1000, "console", "console", `{"level":"trace","stacktracelevel":"NONE","stderr":true}`) - repoRootPath, err := os.MkdirTemp(os.TempDir(), "repos") + gitHomePath, err := os.MkdirTemp(os.TempDir(), "git-home") if err != nil { return fmt.Errorf("unable to create temp dir: %w", err) } - defer util.RemoveAll(repoRootPath) - setting.RepoRootPath = repoRootPath + defer util.RemoveAll(gitHomePath) + setting.Git.HomePath = gitHomePath if err = InitOnceWithSync(context.Background()); err != nil { return fmt.Errorf("failed to call Init: %w", err) diff --git a/modules/setting/git.go b/modules/setting/git.go index 9b2698f01e780..ff9e3c6ce6030 100644 --- a/modules/setting/git.go +++ b/modules/setting/git.go @@ -5,6 +5,7 @@ package setting import ( + "path/filepath" "time" "code.gitea.io/gitea/modules/log" @@ -13,6 +14,7 @@ import ( // Git settings var Git = struct { Path string + HomePath string DisableDiffHighlight bool MaxGitDiffLines int MaxGitDiffLineCharacters int @@ -67,7 +69,16 @@ var Git = struct { } func newGit() { + sec := Cfg.Section("git") + if err := Cfg.Section("git").MapTo(&Git); err != nil { log.Fatal("Failed to map Git settings: %v", err) } + + Git.HomePath = sec.Key("HOME_PATH").MustString("home") + if !filepath.IsAbs(Git.HomePath) { + Git.HomePath = filepath.Join(AppDataPath, Git.HomePath) + } else { + Git.HomePath = filepath.Clean(Git.HomePath) + } } From 780e858d5c3155193d1a372466d083be8d917318 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Mon, 27 Jun 2022 11:27:10 +0800 Subject: [PATCH 02/14] add legacy file check --- modules/git/git.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/modules/git/git.go b/modules/git/git.go index c792a798864d3..2d373f44c817d 100644 --- a/modules/git/git.go +++ b/modules/git/git.go @@ -11,6 +11,7 @@ import ( "fmt" "os" "os/exec" + "path/filepath" "regexp" "runtime" "strings" @@ -180,6 +181,24 @@ func InitOnceWithSync(ctx context.Context) (err error) { return } + // Gitea 1.17-rc uses "setting.RepoRootPath" for Git HOME, which is incorrect. + // 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. + var hasCheckErr bool + _ = os.Remove(filepath.Join(setting.RepoRootPath, ".gitconfig")) // remove the auto generated git config file + _ = os.Remove(filepath.Join(setting.RepoRootPath, ".ssh")) // remove the empty dummy ".ssh" directory + for _, wellKnownName := range []string{".ssh", ".gnupg"} { + checkLegacyFile := filepath.Join(setting.RepoRootPath, wellKnownName) + _, checkErr := os.Stat(checkLegacyFile) + if checkErr == nil || !errors.Is(checkErr, os.ErrNotExist) { + 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) + hasCheckErr = true + } + } + if hasCheckErr { + log.Fatal("Please fix errors above, remove legacy files") + } + // end of legacy Gitea 1.17-rc check + // Since git wire protocol has been released from git v2.18 if setting.Git.EnableAutoGitWireProtocol && CheckGitVersionAtLeast("2.18") == nil { globalCommandArgs = append(globalCommandArgs, "-c", "protocol.version=2") From f345b4c9dfe41f60f8b2c0f6240f603fe8d37dc8 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Mon, 4 Jul 2022 10:42:05 +0800 Subject: [PATCH 03/14] Apply suggestions from code review Co-authored-by: zeripath --- modules/git/git.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/git/git.go b/modules/git/git.go index 2d373f44c817d..8ee31e815e364 100644 --- a/modules/git/git.go +++ b/modules/git/git.go @@ -128,7 +128,7 @@ func VersionInfo() string { func checkInit() error { if setting.Git.HomePath == "" { - return errors.New("can not init Git's HomeDir, the setting and git modules are not initialized correctly") + return errors.New("unable to init Git's HomeDir, incorrect initialization of the setting and git modules") } if DefaultContext != nil { log.Warn("git module has been initialized already, duplicate init should be fixed") @@ -142,7 +142,7 @@ func HomeDir() string { // strict check, make sure the git module is initialized correctly. // attention: when the git module is called in gitea sub-command (serv/hook), the log module is not able to show messages to users. // 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. - log.Fatal("can not get Git's HomeDir, the setting and git modules are not initialized correctly") + log.Fatal("Unable to init Git's HomeDir, incorrect initialization of the setting and git modules") return "" } return setting.Git.HomePath From 1a386b7f6607f48058b9340eb1212ae8b05b3072 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Mon, 4 Jul 2022 12:49:19 +0800 Subject: [PATCH 04/14] pass env GNUPGHOME to git command, move the existing .gitconfig to new home, make the fix for 1.17rc more clear. --- modules/git/command.go | 33 +++++++++++++++------- modules/git/git.go | 64 +++++++++++++++++++++++++++++------------- 2 files changed, 68 insertions(+), 29 deletions(-) diff --git a/modules/git/command.go b/modules/git/command.go index d71497f1d791e..a1bacbb707a25 100644 --- a/modules/git/command.go +++ b/modules/git/command.go @@ -105,23 +105,36 @@ type RunOpts struct { PipelineFunc func(context.Context, context.CancelFunc) error } -// CommonGitCmdEnvs returns the common environment variables for a "git" command. -func CommonGitCmdEnvs() []string { +func commonBaseEnvs() []string { // at the moment, do not set "GIT_CONFIG_NOSYSTEM", users may have put some configs like "receive.certNonceSeed" in it - return []string{ - fmt.Sprintf("LC_ALL=%s", DefaultLocale), - "GIT_TERMINAL_PROMPT=0", // avoid prompting for credentials interactively, supported since git v2.3 - "GIT_NO_REPLACE_OBJECTS=1", // ignore replace references (https://git-scm.com/docs/git-replace) + envs := []string{ "HOME=" + HomeDir(), // make Gitea use internal git config only, to prevent conflicts with user's git config + "GIT_NO_REPLACE_OBJECTS=1", // ignore replace references (https://git-scm.com/docs/git-replace) + } + + // some environment variables should be passed to git command + passThroughEnvKeys := []string{ + "GNUPGHOME", // git may call gnupg to do commit signing + } + for _, key := range passThroughEnvKeys { + if val, ok := os.LookupEnv(key); ok { + envs = append(envs, key+"="+val) + } } + return envs +} + +// CommonGitCmdEnvs returns the common environment variables for a "git" command. +func CommonGitCmdEnvs() []string { + return append(commonBaseEnvs(), []string{ + "LC_ALL=" + DefaultLocale, + "GIT_TERMINAL_PROMPT=0", // avoid prompting for credentials interactively, supported since git v2.3 + }...) } // CommonCmdServEnvs is like CommonGitCmdEnvs but it only returns minimal required environment variables for the "gitea serv" command func CommonCmdServEnvs() []string { - return []string{ - "GIT_NO_REPLACE_OBJECTS=1", // ignore replace references (https://git-scm.com/docs/git-replace) - "HOME=" + HomeDir(), // make Gitea use internal git config only, to prevent conflicts with user's git config - } + return commonBaseEnvs() } // Run runs the command with the RunOpts diff --git a/modules/git/git.go b/modules/git/git.go index 8ee31e815e364..96e4efb486265 100644 --- a/modules/git/git.go +++ b/modules/git/git.go @@ -20,6 +20,7 @@ import ( "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" + "code.gitea.io/gitea/modules/util" "github.com/hashicorp/go-version" ) @@ -167,6 +168,47 @@ func InitSimple(ctx context.Context) error { var initOnce sync.Once +func initFixGitHome117rc() error { + // Gitea 1.17-rc uses "setting.RepoRootPath" for Git HOME, which is incorrect. + // 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. + + // remove the auto generated git config file (it will be moved to new home) + gitConfigNewPath := filepath.Join(HomeDir(), ".gitconfig") + gitConfigLegacyPath := filepath.Join(setting.RepoRootPath, ".gitconfig") + if ok, err := util.IsExist(gitConfigLegacyPath); ok && err == nil { + if err = os.MkdirAll(HomeDir(), os.ModePerm); err != nil { + return err + } + if ok, err = util.IsExist(gitConfigNewPath); !ok && err == nil { + err = util.CopyFile(gitConfigLegacyPath, gitConfigNewPath) + } else { + err = util.CopyFile(gitConfigLegacyPath, gitConfigNewPath+".bak") + } + if err != nil { + return err + } + _ = os.Remove(gitConfigLegacyPath) + } + + // remove the empty directories, if some directories are non-empty, warn users and exit + var hasCheckErr bool + for _, wellDirName := range []string{".ssh", ".gnupg"} { + checkLegacyDir := filepath.Join(setting.RepoRootPath, wellDirName) + _ = os.Remove(checkLegacyDir) // try to remove the empty dummy directory first + _, checkErr := os.Stat(checkLegacyDir) // if the directory is not empty, then it won't be removed, it should be handled manually + if checkErr == nil || !errors.Is(checkErr, os.ErrNotExist) { + 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) + hasCheckErr = true + } + } + + if hasCheckErr { + log.Fatal("Please fix errors above, remove legacy files.") + } + + return nil +} + // InitOnceWithSync initializes git module with version check and change global variables, sync gitconfig. // This method will update the global variables ONLY ONCE (just like git.CheckLFSVersion -- which is not ideal too), // otherwise there will be data-race problem at the moment. @@ -176,28 +218,12 @@ func InitOnceWithSync(ctx context.Context) (err error) { } initOnce.Do(func() { - err = InitSimple(ctx) - if err != nil { + if err = InitSimple(ctx); err != nil { return } - - // Gitea 1.17-rc uses "setting.RepoRootPath" for Git HOME, which is incorrect. - // 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. - var hasCheckErr bool - _ = os.Remove(filepath.Join(setting.RepoRootPath, ".gitconfig")) // remove the auto generated git config file - _ = os.Remove(filepath.Join(setting.RepoRootPath, ".ssh")) // remove the empty dummy ".ssh" directory - for _, wellKnownName := range []string{".ssh", ".gnupg"} { - checkLegacyFile := filepath.Join(setting.RepoRootPath, wellKnownName) - _, checkErr := os.Stat(checkLegacyFile) - if checkErr == nil || !errors.Is(checkErr, os.ErrNotExist) { - 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) - hasCheckErr = true - } - } - if hasCheckErr { - log.Fatal("Please fix errors above, remove legacy files") + if err = initFixGitHome117rc(); err != nil { + return } - // end of legacy Gitea 1.17-rc check // Since git wire protocol has been released from git v2.18 if setting.Git.EnableAutoGitWireProtocol && CheckGitVersionAtLeast("2.18") == nil { From f120101ddc267cef74e4f4b92c783d5fc8e275a1 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Mon, 4 Jul 2022 13:13:52 +0800 Subject: [PATCH 05/14] set git.HOME_PATH for docker images to default HOME --- docker/root/etc/templates/app.ini | 3 +++ docker/rootless/etc/templates/app.ini | 3 +++ 2 files changed, 6 insertions(+) diff --git a/docker/root/etc/templates/app.ini b/docker/root/etc/templates/app.ini index 7e8aa0bd0a62c..7eb14c49373ff 100644 --- a/docker/root/etc/templates/app.ini +++ b/docker/root/etc/templates/app.ini @@ -61,3 +61,6 @@ REQUIRE_SIGNIN_VIEW = $REQUIRE_SIGNIN_VIEW [lfs] PATH = /data/git/lfs + +[git] +HOME_PATH = /data/git diff --git a/docker/rootless/etc/templates/app.ini b/docker/rootless/etc/templates/app.ini index f106f01f5eb44..369cf517bab7f 100644 --- a/docker/rootless/etc/templates/app.ini +++ b/docker/rootless/etc/templates/app.ini @@ -57,3 +57,6 @@ REQUIRE_SIGNIN_VIEW = $REQUIRE_SIGNIN_VIEW [lfs] PATH = $GITEA_WORK_DIR/git/lfs + +[git] +HOME_PATH = $GITEA_WORK_DIR/git From b349d889a251a3c21e554e2558ac972f6686c302 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Mon, 4 Jul 2022 21:51:14 +0800 Subject: [PATCH 06/14] Revert "set git.HOME_PATH for docker images to default HOME" This reverts commit f120101ddc267cef74e4f4b92c783d5fc8e275a1. --- docker/root/etc/templates/app.ini | 3 --- docker/rootless/etc/templates/app.ini | 3 --- 2 files changed, 6 deletions(-) diff --git a/docker/root/etc/templates/app.ini b/docker/root/etc/templates/app.ini index 7eb14c49373ff..7e8aa0bd0a62c 100644 --- a/docker/root/etc/templates/app.ini +++ b/docker/root/etc/templates/app.ini @@ -61,6 +61,3 @@ REQUIRE_SIGNIN_VIEW = $REQUIRE_SIGNIN_VIEW [lfs] PATH = /data/git/lfs - -[git] -HOME_PATH = /data/git diff --git a/docker/rootless/etc/templates/app.ini b/docker/rootless/etc/templates/app.ini index 369cf517bab7f..f106f01f5eb44 100644 --- a/docker/rootless/etc/templates/app.ini +++ b/docker/rootless/etc/templates/app.ini @@ -57,6 +57,3 @@ REQUIRE_SIGNIN_VIEW = $REQUIRE_SIGNIN_VIEW [lfs] PATH = $GITEA_WORK_DIR/git/lfs - -[git] -HOME_PATH = $GITEA_WORK_DIR/git From 3749e27d2f50c9ad317bf42e5def305efce2740e Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Mon, 4 Jul 2022 23:06:50 +0800 Subject: [PATCH 07/14] force Gitea to use a stable GNUPGHOME directory --- modules/git/git.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/modules/git/git.go b/modules/git/git.go index 96e4efb486265..d69d5f37798b8 100644 --- a/modules/git/git.go +++ b/modules/git/git.go @@ -225,6 +225,11 @@ func InitOnceWithSync(ctx context.Context) (err error) { return } + // when git works with gnupg (commit signing), there should be a stable home for gnupg commands + if _, ok := os.LookupEnv("GNUPGHOME"); !ok { + _ = os.Setenv("GNUPGHOME", HomeDir()) + } + // Since git wire protocol has been released from git v2.18 if setting.Git.EnableAutoGitWireProtocol && CheckGitVersionAtLeast("2.18") == nil { globalCommandArgs = append(globalCommandArgs, "-c", "protocol.version=2") From c20205cba0066874766944d54ac27afcae812c2f Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Tue, 5 Jul 2022 01:08:09 +0800 Subject: [PATCH 08/14] extra check to ensure only process dir or symlink for legacy files --- modules/git/git.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/modules/git/git.go b/modules/git/git.go index d69d5f37798b8..0be3362a8e134 100644 --- a/modules/git/git.go +++ b/modules/git/git.go @@ -194,9 +194,13 @@ func initFixGitHome117rc() error { var hasCheckErr bool for _, wellDirName := range []string{".ssh", ".gnupg"} { checkLegacyDir := filepath.Join(setting.RepoRootPath, wellDirName) - _ = os.Remove(checkLegacyDir) // try to remove the empty dummy directory first - _, checkErr := os.Stat(checkLegacyDir) // if the directory is not empty, then it won't be removed, it should be handled manually - if checkErr == nil || !errors.Is(checkErr, os.ErrNotExist) { + st, err := os.Lstat(checkLegacyDir) // only process dir or symlink + if err != nil || (!st.IsDir() && st.Mode()&os.ModeSymlink != os.ModeSymlink) { + continue + } + _ = os.Remove(checkLegacyDir) // try to remove the empty dummy directory first + _, err = os.Stat(checkLegacyDir) // if the directory is not empty, then it won't be removed, it should be handled manually + if err == nil || !errors.Is(err, os.ErrNotExist) { 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) hasCheckErr = true } From 2c06e05244ecf28e63718875a2a5dd4612372b71 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Tue, 5 Jul 2022 01:11:35 +0800 Subject: [PATCH 09/14] refactor variable name --- modules/git/git.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/modules/git/git.go b/modules/git/git.go index 0be3362a8e134..701fffc54c0a8 100644 --- a/modules/git/git.go +++ b/modules/git/git.go @@ -192,16 +192,16 @@ func initFixGitHome117rc() error { // remove the empty directories, if some directories are non-empty, warn users and exit var hasCheckErr bool - for _, wellDirName := range []string{".ssh", ".gnupg"} { - checkLegacyDir := filepath.Join(setting.RepoRootPath, wellDirName) - st, err := os.Lstat(checkLegacyDir) // only process dir or symlink + for _, wellKnownDirName := range []string{".ssh", ".gnupg"} { + legacyDir := filepath.Join(setting.RepoRootPath, wellKnownDirName) + st, err := os.Lstat(legacyDir) // only process dir or symlink if err != nil || (!st.IsDir() && st.Mode()&os.ModeSymlink != os.ModeSymlink) { continue } - _ = os.Remove(checkLegacyDir) // try to remove the empty dummy directory first - _, err = os.Stat(checkLegacyDir) // if the directory is not empty, then it won't be removed, it should be handled manually + _ = os.Remove(legacyDir) // try to remove the empty dummy directory first + _, err = os.Stat(legacyDir) // if the directory is not empty, then it won't be removed, it should be handled manually if err == nil || !errors.Is(err, os.ErrNotExist) { - 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) + 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`, legacyDir) hasCheckErr = true } } From b0a3ef741d057596f9ba4eb4b252fb2f53afa734 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Tue, 5 Jul 2022 01:15:39 +0800 Subject: [PATCH 10/14] 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 --- modules/git/git.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/git/git.go b/modules/git/git.go index 701fffc54c0a8..23cc72ef5c0cc 100644 --- a/modules/git/git.go +++ b/modules/git/git.go @@ -170,7 +170,7 @@ var initOnce sync.Once func initFixGitHome117rc() error { // Gitea 1.17-rc uses "setting.RepoRootPath" for Git HOME, which is incorrect. - // 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. + // Do this check to make sure there is no legacy file in the RepoRootPath. This check might be able to be removed with 1.18 release. // remove the auto generated git config file (it will be moved to new home) gitConfigNewPath := filepath.Join(HomeDir(), ".gitconfig") From 2777d6370ffcd2b2aabcae7e0b01829ef2094f1b Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Tue, 5 Jul 2022 08:43:11 +0800 Subject: [PATCH 11/14] Update modules/git/git.go Co-authored-by: Steven Kriegler <61625851+justusbunsi@users.noreply.github.com> --- modules/git/git.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/git/git.go b/modules/git/git.go index 23cc72ef5c0cc..70613deab9f8e 100644 --- a/modules/git/git.go +++ b/modules/git/git.go @@ -231,7 +231,7 @@ func InitOnceWithSync(ctx context.Context) (err error) { // when git works with gnupg (commit signing), there should be a stable home for gnupg commands if _, ok := os.LookupEnv("GNUPGHOME"); !ok { - _ = os.Setenv("GNUPGHOME", HomeDir()) + _ = os.Setenv("GNUPGHOME", filepath.Join(HomeDir(), ".gnupg")) } // Since git wire protocol has been released from git v2.18 From 593d41e428fbc0913d235942401ec35b31f3e8ee Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Tue, 5 Jul 2022 15:34:02 +0800 Subject: [PATCH 12/14] remove initFixGitHome117rc --- modules/git/git.go | 50 ---------------------------------------------- 1 file changed, 50 deletions(-) diff --git a/modules/git/git.go b/modules/git/git.go index 70613deab9f8e..3bc08ff93b5fa 100644 --- a/modules/git/git.go +++ b/modules/git/git.go @@ -20,8 +20,6 @@ import ( "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" - "code.gitea.io/gitea/modules/util" - "github.com/hashicorp/go-version" ) @@ -168,51 +166,6 @@ func InitSimple(ctx context.Context) error { var initOnce sync.Once -func initFixGitHome117rc() error { - // Gitea 1.17-rc uses "setting.RepoRootPath" for Git HOME, which is incorrect. - // Do this check to make sure there is no legacy file in the RepoRootPath. This check might be able to be removed with 1.18 release. - - // remove the auto generated git config file (it will be moved to new home) - gitConfigNewPath := filepath.Join(HomeDir(), ".gitconfig") - gitConfigLegacyPath := filepath.Join(setting.RepoRootPath, ".gitconfig") - if ok, err := util.IsExist(gitConfigLegacyPath); ok && err == nil { - if err = os.MkdirAll(HomeDir(), os.ModePerm); err != nil { - return err - } - if ok, err = util.IsExist(gitConfigNewPath); !ok && err == nil { - err = util.CopyFile(gitConfigLegacyPath, gitConfigNewPath) - } else { - err = util.CopyFile(gitConfigLegacyPath, gitConfigNewPath+".bak") - } - if err != nil { - return err - } - _ = os.Remove(gitConfigLegacyPath) - } - - // remove the empty directories, if some directories are non-empty, warn users and exit - var hasCheckErr bool - for _, wellKnownDirName := range []string{".ssh", ".gnupg"} { - legacyDir := filepath.Join(setting.RepoRootPath, wellKnownDirName) - st, err := os.Lstat(legacyDir) // only process dir or symlink - if err != nil || (!st.IsDir() && st.Mode()&os.ModeSymlink != os.ModeSymlink) { - continue - } - _ = os.Remove(legacyDir) // try to remove the empty dummy directory first - _, err = os.Stat(legacyDir) // if the directory is not empty, then it won't be removed, it should be handled manually - if err == nil || !errors.Is(err, os.ErrNotExist) { - 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`, legacyDir) - hasCheckErr = true - } - } - - if hasCheckErr { - log.Fatal("Please fix errors above, remove legacy files.") - } - - return nil -} - // InitOnceWithSync initializes git module with version check and change global variables, sync gitconfig. // This method will update the global variables ONLY ONCE (just like git.CheckLFSVersion -- which is not ideal too), // otherwise there will be data-race problem at the moment. @@ -225,9 +178,6 @@ func InitOnceWithSync(ctx context.Context) (err error) { if err = InitSimple(ctx); err != nil { return } - if err = initFixGitHome117rc(); err != nil { - return - } // when git works with gnupg (commit signing), there should be a stable home for gnupg commands if _, ok := os.LookupEnv("GNUPGHOME"); !ok { From 701fa70bd4c154e1ece2dbeed978dcf720d2ede6 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Tue, 5 Jul 2022 16:07:41 +0800 Subject: [PATCH 13/14] Update git.go --- modules/setting/git.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/setting/git.go b/modules/setting/git.go index ff9e3c6ce6030..266bbc3c5aa3a 100644 --- a/modules/setting/git.go +++ b/modules/setting/git.go @@ -71,7 +71,7 @@ var Git = struct { func newGit() { sec := Cfg.Section("git") - if err := Cfg.Section("git").MapTo(&Git); err != nil { + if err := sec.MapTo(&Git); err != nil { log.Fatal("Failed to map Git settings: %v", err) } From e7fc548b0e8df32edac746c57543b8b05db069fd Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Fri, 8 Jul 2022 11:47:54 +0800 Subject: [PATCH 14/14] Update docs/content/doc/advanced/config-cheat-sheet.en-us.md --- docs/content/doc/advanced/config-cheat-sheet.en-us.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/content/doc/advanced/config-cheat-sheet.en-us.md b/docs/content/doc/advanced/config-cheat-sheet.en-us.md index 6073e02d5aa6d..84e3c6ae33df3 100644 --- a/docs/content/doc/advanced/config-cheat-sheet.en-us.md +++ b/docs/content/doc/advanced/config-cheat-sheet.en-us.md @@ -949,6 +949,7 @@ Default templates for project boards: - `PATH`: **""**: The path of Git executable. If empty, Gitea searches through the PATH environment. - `HOME_PATH`: **%(APP_DATA_PATH)/home**: The HOME directory for Git. + 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. - `DISABLE_DIFF_HIGHLIGHT`: **false**: Disables highlight of added and removed changes. - `MAX_GIT_DIFF_LINES`: **1000**: Max number of lines allowed of a single file in diff view. - `MAX_GIT_DIFF_LINE_CHARACTERS`: **5000**: Max character count per line highlighted in diff view.