Remove legacy git code (ver < 2.0), fine tune markup tests (#19930)

* clean git support for ver < 2.0

* fine tune tests for markup (which requires git module)

* remove unnecessary comments

* try to fix tests

* try test again

* use const for GitVersionRequired instead of var

* try to fix integration test

* Refactor CheckAttributeReader to make a *git.Repository version

* update document for commit signing with Gitea's internal gitconfig

* update document for commit signing with Gitea's internal gitconfig

Co-authored-by: Andrew Thornton <art27@cantab.net>
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
This commit is contained in:
wxiaoguang 2022-06-16 23:47:44 +08:00 committed by GitHub
parent 70ce051f1a
commit 157b405753
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
19 changed files with 178 additions and 226 deletions

View file

@ -7,10 +7,10 @@ package git
import (
"context"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"sync"
@ -22,20 +22,16 @@ import (
"github.com/hashicorp/go-version"
)
var (
// GitVersionRequired is the minimum Git version required
// At the moment, all code for git 1.x are not changed, if some users want to test with old git client
// or bypass the check, they still have a chance to edit this variable manually.
// If everything works fine, the code for git 1.x could be removed in a separate PR before 1.17 frozen.
GitVersionRequired = "2.0.0"
// GitVersionRequired is the minimum Git version required
const GitVersionRequired = "2.0.0"
var (
// GitExecutable is the command name of git
// Could be updated to an absolute path while initialization
GitExecutable = "git"
// DefaultContext is the default context to run git commands in
// will be overwritten by InitXxx with HammerContext
DefaultContext = context.Background()
// DefaultContext is the default context to run git commands in, must be initialized by git.InitXxx
DefaultContext context.Context
// SupportProcReceive version >= 2.29.0
SupportProcReceive bool
@ -128,36 +124,43 @@ func VersionInfo() string {
return fmt.Sprintf(format, args...)
}
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 DefaultContext != nil {
log.Warn("git module has been initialized already, duplicate init should be fixed")
}
return nil
}
// HomeDir is the home dir for git to store the global config file used by Gitea internally
func HomeDir() string {
if setting.RepoRootPath == "" {
// TODO: now, some unit test code call the git module directly without initialization, which is incorrect.
// at the moment, we just use a temp HomeDir to prevent from conflicting with user's git config
// in the future, the git module should be initialized first before use.
tmpHomeDir := filepath.Join(os.TempDir(), "gitea-temp-home")
log.Error("Git's HomeDir is empty (RepoRootPath is empty), the git module is not initialized correctly, using a temp HomeDir (%s) temporarily", tmpHomeDir)
return tmpHomeDir
// 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")
return ""
}
return setting.RepoRootPath
}
// InitSimple initializes git module with a very simple step, no config changes, no global command arguments.
// This method doesn't change anything to filesystem. At the moment, it is only used by "git serv" sub-command, no data-race
// However, in integration test, the sub-command function may be called in the current process, so the InitSimple would be called multiple times, too
func InitSimple(ctx context.Context) error {
if err := checkInit(); err != nil {
return err
}
DefaultContext = ctx
if setting.Git.Timeout.Default > 0 {
defaultCommandExecutionTimeout = time.Duration(setting.Git.Timeout.Default) * time.Second
}
if err := SetExecutablePath(setting.Git.Path); err != nil {
return err
}
// force cleanup args
globalCommandArgs = []string{}
return nil
return SetExecutablePath(setting.Git.Path)
}
var initOnce sync.Once
@ -166,6 +169,10 @@ var initOnce sync.Once
// 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() {
err = InitSimple(ctx)
if err != nil {