@ -20,6 +20,7 @@ import (
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"
"github.com/hashicorp/go-version"
"github.com/hashicorp/go-version"
)
)
@ -167,37 +168,62 @@ func InitSimple(ctx context.Context) error {
var initOnce sync . Once
var initOnce sync . Once
// InitOnceWithSync initializes git module with version check and change global variables, sync gitconfig.
func initFixGitHome117rc ( ) error {
// This method will update the global variables ONLY ONCE (just like git.CheckLFSVersion -- which is not ideal too),
// Gitea 1.17-rc uses "setting.RepoRootPath" for Git HOME, which is incorrect.
// otherwise there will be data-race problem at the moment.
// 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.
func InitOnceWithSync ( ctx context . Context ) ( err error ) {
if err = checkInit ( ) ; err != nil {
// 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
return err
}
}
if ok , err = util . IsExist ( gitConfigNewPath ) ; ! ok && err == nil {
initOnce . Do ( func ( ) {
err = util . CopyFile ( gitConfigLegacyPath , gitConfigNewPath )
err = InitSimple ( ctx )
} else {
err = util . CopyFile ( gitConfigLegacyPath , gitConfigNewPath + ".bak" )
}
if err != nil {
if err != nil {
return
return err
}
_ = os . Remove ( gitConfigLegacyPath )
}
}
// Gitea 1.17-rc uses "setting.RepoRootPath" for Git HOME, which is incorrect.
// remove the empty directories, if some directories are non-empty, warn users and exit
// 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
var hasCheckErr bool
_ = os . Remove ( filepath . Join ( setting . RepoRootPath , ".gitconfig" ) ) // remove the auto generated git config file
for _ , wellDirName := range [ ] string { ".ssh" , ".gnupg" } {
_ = os . Remove ( filepath . Join ( setting . RepoRootPath , ".ssh" ) ) // remove the empty dummy ".ssh" directory
checkLegacyDir := filepath . Join ( setting . RepoRootPath , wellDirName )
for _ , wellKnownName := range [ ] string { ".ssh" , ".gnupg" } {
_ = os . Remove ( checkLegacyDir ) // try to remove the empty dummy directory first
checkLegacyFile := filepath . Join ( setting . RepoRootPath , wellKnownName )
_ , checkErr := os . Stat ( checkLegacyDir ) // if the directory is not empty, then it won't be removed, it should be handled manually
_ , checkErr := os . Stat ( checkLegacyFile )
if checkErr == nil || ! errors . Is ( checkErr , os . ErrNotExist ) {
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 )
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
hasCheckErr = true
}
}
}
}
if hasCheckErr {
if hasCheckErr {
log . Fatal ( "Please fix errors above, remove legacy files" )
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.
func InitOnceWithSync ( ctx context . Context ) ( err error ) {
if err = checkInit ( ) ; err != nil {
return err
}
initOnce . Do ( func ( ) {
if err = InitSimple ( ctx ) ; err != nil {
return
}
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
// Since git wire protocol has been released from git v2.18
if setting . Git . EnableAutoGitWireProtocol && CheckGitVersionAtLeast ( "2.18" ) == nil {
if setting . Git . EnableAutoGitWireProtocol && CheckGitVersionAtLeast ( "2.18" ) == nil {