mirror of
				https://gitee.com/gitea/gitea
				synced 2025-11-04 08:30:25 +08:00 
			
		
		
		
	Delete old git.NewCommand() and use it as git.NewCommandContext() (#18552)
This commit is contained in:
		@@ -76,7 +76,7 @@ func TestPatch(pr *models.PullRequest) error {
 | 
			
		||||
	defer gitRepo.Close()
 | 
			
		||||
 | 
			
		||||
	// 1. update merge base
 | 
			
		||||
	pr.MergeBase, err = git.NewCommandContext(ctx, "merge-base", "--", "base", "tracking").RunInDir(tmpBasePath)
 | 
			
		||||
	pr.MergeBase, err = git.NewCommand(ctx, "merge-base", "--", "base", "tracking").RunInDir(tmpBasePath)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		var err2 error
 | 
			
		||||
		pr.MergeBase, err2 = gitRepo.GetRefCommitID(git.BranchPrefix + "base")
 | 
			
		||||
@@ -166,7 +166,7 @@ func attemptMerge(ctx context.Context, file *unmergedFile, tmpBasePath string, g
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// Need to get the objects from the object db to attempt to merge
 | 
			
		||||
		root, err := git.NewCommandContext(ctx, "unpack-file", file.stage1.sha).RunInDir(tmpBasePath)
 | 
			
		||||
		root, err := git.NewCommand(ctx, "unpack-file", file.stage1.sha).RunInDir(tmpBasePath)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return fmt.Errorf("unable to get root object: %s at path: %s for merging. Error: %w", file.stage1.sha, file.stage1.path, err)
 | 
			
		||||
		}
 | 
			
		||||
@@ -175,7 +175,7 @@ func attemptMerge(ctx context.Context, file *unmergedFile, tmpBasePath string, g
 | 
			
		||||
			_ = util.Remove(filepath.Join(tmpBasePath, root))
 | 
			
		||||
		}()
 | 
			
		||||
 | 
			
		||||
		base, err := git.NewCommandContext(ctx, "unpack-file", file.stage2.sha).RunInDir(tmpBasePath)
 | 
			
		||||
		base, err := git.NewCommand(ctx, "unpack-file", file.stage2.sha).RunInDir(tmpBasePath)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return fmt.Errorf("unable to get base object: %s at path: %s for merging. Error: %w", file.stage2.sha, file.stage2.path, err)
 | 
			
		||||
		}
 | 
			
		||||
@@ -183,7 +183,7 @@ func attemptMerge(ctx context.Context, file *unmergedFile, tmpBasePath string, g
 | 
			
		||||
		defer func() {
 | 
			
		||||
			_ = util.Remove(base)
 | 
			
		||||
		}()
 | 
			
		||||
		head, err := git.NewCommandContext(ctx, "unpack-file", file.stage3.sha).RunInDir(tmpBasePath)
 | 
			
		||||
		head, err := git.NewCommand(ctx, "unpack-file", file.stage3.sha).RunInDir(tmpBasePath)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return fmt.Errorf("unable to get head object:%s at path: %s for merging. Error: %w", file.stage3.sha, file.stage3.path, err)
 | 
			
		||||
		}
 | 
			
		||||
@@ -193,13 +193,13 @@ func attemptMerge(ctx context.Context, file *unmergedFile, tmpBasePath string, g
 | 
			
		||||
		}()
 | 
			
		||||
 | 
			
		||||
		// now git merge-file annoyingly takes a different order to the merge-tree ...
 | 
			
		||||
		_, conflictErr := git.NewCommandContext(ctx, "merge-file", base, root, head).RunInDir(tmpBasePath)
 | 
			
		||||
		_, conflictErr := git.NewCommand(ctx, "merge-file", base, root, head).RunInDir(tmpBasePath)
 | 
			
		||||
		if conflictErr != nil {
 | 
			
		||||
			return &errMergeConflict{file.stage2.path}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// base now contains the merged data
 | 
			
		||||
		hash, err := git.NewCommandContext(ctx, "hash-object", "-w", "--path", file.stage2.path, base).RunInDir(tmpBasePath)
 | 
			
		||||
		hash, err := git.NewCommand(ctx, "hash-object", "-w", "--path", file.stage2.path, base).RunInDir(tmpBasePath)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
@@ -222,7 +222,7 @@ func checkConflicts(pr *models.PullRequest, gitRepo *git.Repository, tmpBasePath
 | 
			
		||||
	defer finished()
 | 
			
		||||
 | 
			
		||||
	// First we use read-tree to do a simple three-way merge
 | 
			
		||||
	if _, err := git.NewCommandContext(ctx, "read-tree", "-m", pr.MergeBase, "base", "tracking").RunInDir(tmpBasePath); err != nil {
 | 
			
		||||
	if _, err := git.NewCommand(ctx, "read-tree", "-m", pr.MergeBase, "base", "tracking").RunInDir(tmpBasePath); err != nil {
 | 
			
		||||
		log.Error("Unable to run read-tree -m! Error: %v", err)
 | 
			
		||||
		return false, fmt.Errorf("unable to run read-tree -m! Error: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
@@ -267,7 +267,7 @@ func checkConflicts(pr *models.PullRequest, gitRepo *git.Repository, tmpBasePath
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if !conflict {
 | 
			
		||||
		treeHash, err := git.NewCommandContext(ctx, "write-tree").RunInDir(tmpBasePath)
 | 
			
		||||
		treeHash, err := git.NewCommand(ctx, "write-tree").RunInDir(tmpBasePath)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return false, err
 | 
			
		||||
		}
 | 
			
		||||
@@ -326,7 +326,7 @@ func checkConflicts(pr *models.PullRequest, gitRepo *git.Repository, tmpBasePath
 | 
			
		||||
	pr.Status = models.PullRequestStatusChecking
 | 
			
		||||
 | 
			
		||||
	// 3. Read the base branch in to the index of the temporary repository
 | 
			
		||||
	_, err = git.NewCommandContext(gitRepo.Ctx, "read-tree", "base").RunInDir(tmpBasePath)
 | 
			
		||||
	_, err = git.NewCommand(gitRepo.Ctx, "read-tree", "base").RunInDir(tmpBasePath)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return false, fmt.Errorf("git read-tree %s: %v", pr.BaseBranch, err)
 | 
			
		||||
	}
 | 
			
		||||
@@ -371,7 +371,7 @@ func checkConflicts(pr *models.PullRequest, gitRepo *git.Repository, tmpBasePath
 | 
			
		||||
 | 
			
		||||
	// 7. Run the check command
 | 
			
		||||
	conflict = false
 | 
			
		||||
	err = git.NewCommandContext(gitRepo.Ctx, args...).
 | 
			
		||||
	err = git.NewCommand(gitRepo.Ctx, args...).
 | 
			
		||||
		RunInDirTimeoutEnvFullPipelineFunc(
 | 
			
		||||
			nil, -1, tmpBasePath,
 | 
			
		||||
			nil, stderrWriter, nil,
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user