mirror of
				https://gitee.com/gitea/gitea
				synced 2025-11-04 08:30:25 +08:00 
			
		
		
		
	Unexport git.GlobalCommandArgs (#18376)
Unexport the git.GlobalCommandArgs variable.
This commit is contained in:
		@@ -41,7 +41,7 @@ func TestRepoCloneWiki(t *testing.T) {
 | 
				
			|||||||
		u, _ = url.Parse(r)
 | 
							u, _ = url.Parse(r)
 | 
				
			||||||
		u.User = url.UserPassword("user2", userPassword)
 | 
							u.User = url.UserPassword("user2", userPassword)
 | 
				
			||||||
		t.Run("Clone", func(t *testing.T) {
 | 
							t.Run("Clone", func(t *testing.T) {
 | 
				
			||||||
			assert.NoError(t, git.CloneWithArgs(context.Background(), u.String(), dstPath, allowLFSFilters(), git.CloneRepoOptions{}))
 | 
								assert.NoError(t, git.CloneWithArgs(context.Background(), u.String(), dstPath, git.AllowLFSFiltersArgs(), git.CloneRepoOptions{}))
 | 
				
			||||||
			assertFileEqual(t, filepath.Join(dstPath, "Home.md"), []byte("# Home page\n\nThis is the home page!\n"))
 | 
								assertFileEqual(t, filepath.Join(dstPath, "Home.md"), []byte("# Home page\n\nThis is the home page!\n"))
 | 
				
			||||||
			assertFileExist(t, filepath.Join(dstPath, "Page-With-Image.md"))
 | 
								assertFileExist(t, filepath.Join(dstPath, "Page-With-Image.md"))
 | 
				
			||||||
			assertFileExist(t, filepath.Join(dstPath, "Page-With-Spaced-Name.md"))
 | 
								assertFileExist(t, filepath.Join(dstPath, "Page-With-Spaced-Name.md"))
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -14,7 +14,6 @@ import (
 | 
				
			|||||||
	"path"
 | 
						"path"
 | 
				
			||||||
	"path/filepath"
 | 
						"path/filepath"
 | 
				
			||||||
	"strconv"
 | 
						"strconv"
 | 
				
			||||||
	"strings"
 | 
					 | 
				
			||||||
	"testing"
 | 
						"testing"
 | 
				
			||||||
	"time"
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -60,21 +59,6 @@ func createSSHUrl(gitPath string, u *url.URL) *url.URL {
 | 
				
			|||||||
	return &u2
 | 
						return &u2
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func allowLFSFilters() []string {
 | 
					 | 
				
			||||||
	// Now here we should explicitly allow lfs filters to run
 | 
					 | 
				
			||||||
	filteredLFSGlobalArgs := make([]string, len(git.GlobalCommandArgs))
 | 
					 | 
				
			||||||
	j := 0
 | 
					 | 
				
			||||||
	for _, arg := range git.GlobalCommandArgs {
 | 
					 | 
				
			||||||
		if strings.Contains(arg, "lfs") {
 | 
					 | 
				
			||||||
			j--
 | 
					 | 
				
			||||||
		} else {
 | 
					 | 
				
			||||||
			filteredLFSGlobalArgs[j] = arg
 | 
					 | 
				
			||||||
			j++
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	return filteredLFSGlobalArgs[:j]
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func onGiteaRunTB(t testing.TB, callback func(testing.TB, *url.URL), prepare ...bool) {
 | 
					func onGiteaRunTB(t testing.TB, callback func(testing.TB, *url.URL), prepare ...bool) {
 | 
				
			||||||
	if len(prepare) == 0 || prepare[0] {
 | 
						if len(prepare) == 0 || prepare[0] {
 | 
				
			||||||
		defer prepareTestEnv(t, 1)()
 | 
							defer prepareTestEnv(t, 1)()
 | 
				
			||||||
@@ -115,7 +99,7 @@ func onGiteaRun(t *testing.T, callback func(*testing.T, *url.URL), prepare ...bo
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
func doGitClone(dstLocalPath string, u *url.URL) func(*testing.T) {
 | 
					func doGitClone(dstLocalPath string, u *url.URL) func(*testing.T) {
 | 
				
			||||||
	return func(t *testing.T) {
 | 
						return func(t *testing.T) {
 | 
				
			||||||
		assert.NoError(t, git.CloneWithArgs(context.Background(), u.String(), dstLocalPath, allowLFSFilters(), git.CloneRepoOptions{}))
 | 
							assert.NoError(t, git.CloneWithArgs(context.Background(), u.String(), dstLocalPath, git.AllowLFSFiltersArgs(), git.CloneRepoOptions{}))
 | 
				
			||||||
		exist, err := util.IsExist(filepath.Join(dstLocalPath, "README.md"))
 | 
							exist, err := util.IsExist(filepath.Join(dstLocalPath, "README.md"))
 | 
				
			||||||
		assert.NoError(t, err)
 | 
							assert.NoError(t, err)
 | 
				
			||||||
		assert.True(t, exist)
 | 
							assert.True(t, exist)
 | 
				
			||||||
@@ -124,7 +108,7 @@ func doGitClone(dstLocalPath string, u *url.URL) func(*testing.T) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
func doPartialGitClone(dstLocalPath string, u *url.URL) func(*testing.T) {
 | 
					func doPartialGitClone(dstLocalPath string, u *url.URL) func(*testing.T) {
 | 
				
			||||||
	return func(t *testing.T) {
 | 
						return func(t *testing.T) {
 | 
				
			||||||
		assert.NoError(t, git.CloneWithArgs(context.Background(), u.String(), dstLocalPath, allowLFSFilters(), git.CloneRepoOptions{
 | 
							assert.NoError(t, git.CloneWithArgs(context.Background(), u.String(), dstLocalPath, git.AllowLFSFiltersArgs(), git.CloneRepoOptions{
 | 
				
			||||||
			Filter: "blob:none",
 | 
								Filter: "blob:none",
 | 
				
			||||||
		}))
 | 
							}))
 | 
				
			||||||
		exist, err := util.IsExist(filepath.Join(dstLocalPath, "README.md"))
 | 
							exist, err := util.IsExist(filepath.Join(dstLocalPath, "README.md"))
 | 
				
			||||||
@@ -197,7 +181,7 @@ func doGitCreateBranch(dstPath, branch string) func(*testing.T) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
func doGitCheckoutBranch(dstPath string, args ...string) func(*testing.T) {
 | 
					func doGitCheckoutBranch(dstPath string, args ...string) func(*testing.T) {
 | 
				
			||||||
	return func(t *testing.T) {
 | 
						return func(t *testing.T) {
 | 
				
			||||||
		_, err := git.NewCommandNoGlobals(append(append(allowLFSFilters(), "checkout"), args...)...).RunInDir(dstPath)
 | 
							_, err := git.NewCommandNoGlobals(append(append(git.AllowLFSFiltersArgs(), "checkout"), args...)...).RunInDir(dstPath)
 | 
				
			||||||
		assert.NoError(t, err)
 | 
							assert.NoError(t, err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -211,7 +195,7 @@ func doGitMerge(dstPath string, args ...string) func(*testing.T) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
func doGitPull(dstPath string, args ...string) func(*testing.T) {
 | 
					func doGitPull(dstPath string, args ...string) func(*testing.T) {
 | 
				
			||||||
	return func(t *testing.T) {
 | 
						return func(t *testing.T) {
 | 
				
			||||||
		_, err := git.NewCommandNoGlobals(append(append(allowLFSFilters(), "pull"), args...)...).RunInDir(dstPath)
 | 
							_, err := git.NewCommandNoGlobals(append(append(git.AllowLFSFiltersArgs(), "pull"), args...)...).RunInDir(dstPath)
 | 
				
			||||||
		assert.NoError(t, err)
 | 
							assert.NoError(t, err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -167,7 +167,7 @@ func lfsCommitAndPushTest(t *testing.T, dstPath string) (littleLFS, bigLFS strin
 | 
				
			|||||||
		err = git.AddChanges(dstPath, false, ".gitattributes")
 | 
							err = git.AddChanges(dstPath, false, ".gitattributes")
 | 
				
			||||||
		assert.NoError(t, err)
 | 
							assert.NoError(t, err)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		err = git.CommitChangesWithArgs(dstPath, allowLFSFilters(), git.CommitChangesOptions{
 | 
							err = git.CommitChangesWithArgs(dstPath, git.AllowLFSFiltersArgs(), git.CommitChangesOptions{
 | 
				
			||||||
			Committer: &git.Signature{
 | 
								Committer: &git.Signature{
 | 
				
			||||||
				Email: "user2@example.com",
 | 
									Email: "user2@example.com",
 | 
				
			||||||
				Name:  "User Two",
 | 
									Name:  "User Two",
 | 
				
			||||||
@@ -346,7 +346,7 @@ func generateCommitWithNewData(size int, repoPath, email, fullName, prefix strin
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	// Commit
 | 
						// Commit
 | 
				
			||||||
	// Now here we should explicitly allow lfs filters to run
 | 
						// Now here we should explicitly allow lfs filters to run
 | 
				
			||||||
	globalArgs := allowLFSFilters()
 | 
						globalArgs := git.AllowLFSFiltersArgs()
 | 
				
			||||||
	err = git.AddChangesWithArgs(repoPath, globalArgs, false, filepath.Base(tmpFile.Name()))
 | 
						err = git.AddChangesWithArgs(repoPath, globalArgs, false, filepath.Base(tmpFile.Name()))
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return "", err
 | 
							return "", err
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -20,8 +20,8 @@ import (
 | 
				
			|||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var (
 | 
					var (
 | 
				
			||||||
	// GlobalCommandArgs global command args for external package setting
 | 
						// globalCommandArgs global command args for external package setting
 | 
				
			||||||
	GlobalCommandArgs []string
 | 
						globalCommandArgs []string
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// defaultCommandExecutionTimeout default command execution timeout duration
 | 
						// defaultCommandExecutionTimeout default command execution timeout duration
 | 
				
			||||||
	defaultCommandExecutionTimeout = 360 * time.Second
 | 
						defaultCommandExecutionTimeout = 360 * time.Second
 | 
				
			||||||
@@ -52,9 +52,9 @@ func NewCommand(args ...string) *Command {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
// NewCommandContext creates and returns a new Git Command based on given command and arguments.
 | 
					// NewCommandContext creates and returns a new Git Command based on given command and arguments.
 | 
				
			||||||
func NewCommandContext(ctx context.Context, args ...string) *Command {
 | 
					func NewCommandContext(ctx context.Context, args ...string) *Command {
 | 
				
			||||||
	// Make an explicit copy of GlobalCommandArgs, otherwise append might overwrite it
 | 
						// Make an explicit copy of globalCommandArgs, otherwise append might overwrite it
 | 
				
			||||||
	cargs := make([]string, len(GlobalCommandArgs))
 | 
						cargs := make([]string, len(globalCommandArgs))
 | 
				
			||||||
	copy(cargs, GlobalCommandArgs)
 | 
						copy(cargs, globalCommandArgs)
 | 
				
			||||||
	return &Command{
 | 
						return &Command{
 | 
				
			||||||
		name:          GitExecutable,
 | 
							name:          GitExecutable,
 | 
				
			||||||
		args:          append(cargs, args...),
 | 
							args:          append(cargs, args...),
 | 
				
			||||||
@@ -278,3 +278,19 @@ func (c *Command) RunTimeout(timeout time.Duration) (string, error) {
 | 
				
			|||||||
func (c *Command) Run() (string, error) {
 | 
					func (c *Command) Run() (string, error) {
 | 
				
			||||||
	return c.RunTimeout(-1)
 | 
						return c.RunTimeout(-1)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// AllowLFSFiltersArgs return globalCommandArgs with lfs filter, it should only be used for tests
 | 
				
			||||||
 | 
					func AllowLFSFiltersArgs() []string {
 | 
				
			||||||
 | 
						// Now here we should explicitly allow lfs filters to run
 | 
				
			||||||
 | 
						filteredLFSGlobalArgs := make([]string, len(globalCommandArgs))
 | 
				
			||||||
 | 
						j := 0
 | 
				
			||||||
 | 
						for _, arg := range globalCommandArgs {
 | 
				
			||||||
 | 
							if strings.Contains(arg, "lfs") {
 | 
				
			||||||
 | 
								j--
 | 
				
			||||||
 | 
							} else {
 | 
				
			||||||
 | 
								filteredLFSGlobalArgs[j] = arg
 | 
				
			||||||
 | 
								j++
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return filteredLFSGlobalArgs[:j]
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -84,7 +84,7 @@ func (c *Commit) GetCommitByPath(relpath string) (*Commit, error) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
// AddChanges marks local changes to be ready for commit.
 | 
					// AddChanges marks local changes to be ready for commit.
 | 
				
			||||||
func AddChanges(repoPath string, all bool, files ...string) error {
 | 
					func AddChanges(repoPath string, all bool, files ...string) error {
 | 
				
			||||||
	return AddChangesWithArgs(repoPath, GlobalCommandArgs, all, files...)
 | 
						return AddChangesWithArgs(repoPath, globalCommandArgs, all, files...)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// AddChangesWithArgs marks local changes to be ready for commit.
 | 
					// AddChangesWithArgs marks local changes to be ready for commit.
 | 
				
			||||||
@@ -108,8 +108,8 @@ type CommitChangesOptions struct {
 | 
				
			|||||||
// CommitChanges commits local changes with given committer, author and message.
 | 
					// CommitChanges commits local changes with given committer, author and message.
 | 
				
			||||||
// If author is nil, it will be the same as committer.
 | 
					// If author is nil, it will be the same as committer.
 | 
				
			||||||
func CommitChanges(repoPath string, opts CommitChangesOptions) error {
 | 
					func CommitChanges(repoPath string, opts CommitChangesOptions) error {
 | 
				
			||||||
	cargs := make([]string, len(GlobalCommandArgs))
 | 
						cargs := make([]string, len(globalCommandArgs))
 | 
				
			||||||
	copy(cargs, GlobalCommandArgs)
 | 
						copy(cargs, globalCommandArgs)
 | 
				
			||||||
	return CommitChangesWithArgs(repoPath, cargs, opts)
 | 
						return CommitChangesWithArgs(repoPath, cargs, opts)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -134,21 +134,21 @@ func Init(ctx context.Context) error {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// force cleanup args
 | 
						// force cleanup args
 | 
				
			||||||
	GlobalCommandArgs = []string{}
 | 
						globalCommandArgs = []string{}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if CheckGitVersionAtLeast("2.9") == nil {
 | 
						if CheckGitVersionAtLeast("2.9") == nil {
 | 
				
			||||||
		// Explicitly disable credential helper, otherwise Git credentials might leak
 | 
							// Explicitly disable credential helper, otherwise Git credentials might leak
 | 
				
			||||||
		GlobalCommandArgs = append(GlobalCommandArgs, "-c", "credential.helper=")
 | 
							globalCommandArgs = append(globalCommandArgs, "-c", "credential.helper=")
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// 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 {
 | 
				
			||||||
		GlobalCommandArgs = append(GlobalCommandArgs, "-c", "protocol.version=2")
 | 
							globalCommandArgs = append(globalCommandArgs, "-c", "protocol.version=2")
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// By default partial clones are disabled, enable them from git v2.22
 | 
						// By default partial clones are disabled, enable them from git v2.22
 | 
				
			||||||
	if !setting.Git.DisablePartialClone && CheckGitVersionAtLeast("2.22") == nil {
 | 
						if !setting.Git.DisablePartialClone && CheckGitVersionAtLeast("2.22") == nil {
 | 
				
			||||||
		GlobalCommandArgs = append(GlobalCommandArgs, "-c", "uploadpack.allowfilter=true")
 | 
							globalCommandArgs = append(globalCommandArgs, "-c", "uploadpack.allowfilter=true")
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Save current git version on init to gitVersion otherwise it would require an RWMutex
 | 
						// Save current git version on init to gitVersion otherwise it would require an RWMutex
 | 
				
			||||||
@@ -213,7 +213,7 @@ func Init(ctx context.Context) error {
 | 
				
			|||||||
		if err := checkAndSetConfig("core.protectntfs", "false", true); err != nil {
 | 
							if err := checkAndSetConfig("core.protectntfs", "false", true); err != nil {
 | 
				
			||||||
			return err
 | 
								return err
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		GlobalCommandArgs = append(GlobalCommandArgs, "-c", "core.protectntfs=false")
 | 
							globalCommandArgs = append(globalCommandArgs, "-c", "core.protectntfs=false")
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return nil
 | 
						return nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -29,7 +29,7 @@ func CheckLFSVersion() {
 | 
				
			|||||||
			logger.Error("LFS server support needs at least Git v2.1.2")
 | 
								logger.Error("LFS server support needs at least Git v2.1.2")
 | 
				
			||||||
		} else {
 | 
							} else {
 | 
				
			||||||
			once.Do(func() {
 | 
								once.Do(func() {
 | 
				
			||||||
				GlobalCommandArgs = append(GlobalCommandArgs, "-c", "filter.lfs.required=",
 | 
									globalCommandArgs = append(globalCommandArgs, "-c", "filter.lfs.required=",
 | 
				
			||||||
					"-c", "filter.lfs.smudge=", "-c", "filter.lfs.clean=")
 | 
										"-c", "filter.lfs.smudge=", "-c", "filter.lfs.clean=")
 | 
				
			||||||
			})
 | 
								})
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -106,8 +106,8 @@ type CloneRepoOptions struct {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
// Clone clones original repository to target path.
 | 
					// Clone clones original repository to target path.
 | 
				
			||||||
func Clone(ctx context.Context, from, to string, opts CloneRepoOptions) error {
 | 
					func Clone(ctx context.Context, from, to string, opts CloneRepoOptions) error {
 | 
				
			||||||
	cargs := make([]string, len(GlobalCommandArgs))
 | 
						cargs := make([]string, len(globalCommandArgs))
 | 
				
			||||||
	copy(cargs, GlobalCommandArgs)
 | 
						copy(cargs, globalCommandArgs)
 | 
				
			||||||
	return CloneWithArgs(ctx, from, to, cargs, opts)
 | 
						return CloneWithArgs(ctx, from, to, cargs, opts)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user