mirror of
				https://github.com/go-gitea/gitea.git
				synced 2025-10-31 02:46:04 +01:00 
			
		
		
		
	Delete old git.NewCommand() and use it as git.NewCommandContext() (#18552)
This commit is contained in:
		| @@ -58,7 +58,7 @@ func (repo *Repository) getCommitByPathWithID(id SHA1, relpath string) (*Commit, | ||||
| 		relpath = `\` + relpath | ||||
| 	} | ||||
|  | ||||
| 	stdout, err := NewCommandContext(repo.Ctx, "log", "-1", prettyLogFormat, id.String(), "--", relpath).RunInDir(repo.Path) | ||||
| 	stdout, err := NewCommand(repo.Ctx, "log", "-1", prettyLogFormat, id.String(), "--", relpath).RunInDir(repo.Path) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| @@ -73,7 +73,7 @@ func (repo *Repository) getCommitByPathWithID(id SHA1, relpath string) (*Commit, | ||||
|  | ||||
| // GetCommitByPath returns the last commit of relative path. | ||||
| func (repo *Repository) GetCommitByPath(relpath string) (*Commit, error) { | ||||
| 	stdout, err := NewCommandContext(repo.Ctx, "log", "-1", prettyLogFormat, "--", relpath).RunInDirBytes(repo.Path) | ||||
| 	stdout, err := NewCommand(repo.Ctx, "log", "-1", prettyLogFormat, "--", relpath).RunInDirBytes(repo.Path) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| @@ -86,7 +86,7 @@ func (repo *Repository) GetCommitByPath(relpath string) (*Commit, error) { | ||||
| } | ||||
|  | ||||
| func (repo *Repository) commitsByRange(id SHA1, page, pageSize int) ([]*Commit, error) { | ||||
| 	stdout, err := NewCommandContext(repo.Ctx, "log", id.String(), "--skip="+strconv.Itoa((page-1)*pageSize), | ||||
| 	stdout, err := NewCommand(repo.Ctx, "log", id.String(), "--skip="+strconv.Itoa((page-1)*pageSize), | ||||
| 		"--max-count="+strconv.Itoa(pageSize), prettyLogFormat).RunInDirBytes(repo.Path) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| @@ -96,7 +96,7 @@ func (repo *Repository) commitsByRange(id SHA1, page, pageSize int) ([]*Commit, | ||||
|  | ||||
| func (repo *Repository) searchCommits(id SHA1, opts SearchCommitsOptions) ([]*Commit, error) { | ||||
| 	// create new git log command with limit of 100 commis | ||||
| 	cmd := NewCommandContext(repo.Ctx, "log", id.String(), "-100", prettyLogFormat) | ||||
| 	cmd := NewCommand(repo.Ctx, "log", id.String(), "-100", prettyLogFormat) | ||||
| 	// ignore case | ||||
| 	args := []string{"-i"} | ||||
|  | ||||
| @@ -154,7 +154,7 @@ func (repo *Repository) searchCommits(id SHA1, opts SearchCommitsOptions) ([]*Co | ||||
| 			// ignore anything below 4 characters as too unspecific | ||||
| 			if len(v) >= 4 { | ||||
| 				// create new git log command with 1 commit limit | ||||
| 				hashCmd := NewCommandContext(repo.Ctx, "log", "-1", prettyLogFormat) | ||||
| 				hashCmd := NewCommand(repo.Ctx, "log", "-1", prettyLogFormat) | ||||
| 				// add previous arguments except for --grep and --all | ||||
| 				hashCmd.AddArguments(args...) | ||||
| 				// add keyword as <commit> | ||||
| @@ -175,7 +175,7 @@ func (repo *Repository) searchCommits(id SHA1, opts SearchCommitsOptions) ([]*Co | ||||
| } | ||||
|  | ||||
| func (repo *Repository) getFilesChanged(id1, id2 string) ([]string, error) { | ||||
| 	stdout, err := NewCommandContext(repo.Ctx, "diff", "--name-only", id1, id2).RunInDirBytes(repo.Path) | ||||
| 	stdout, err := NewCommand(repo.Ctx, "diff", "--name-only", id1, id2).RunInDirBytes(repo.Path) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| @@ -185,7 +185,7 @@ func (repo *Repository) getFilesChanged(id1, id2 string) ([]string, error) { | ||||
| // FileChangedBetweenCommits Returns true if the file changed between commit IDs id1 and id2 | ||||
| // You must ensure that id1 and id2 are valid commit ids. | ||||
| func (repo *Repository) FileChangedBetweenCommits(filename, id1, id2 string) (bool, error) { | ||||
| 	stdout, err := NewCommandContext(repo.Ctx, "diff", "--name-only", "-z", id1, id2, "--", filename).RunInDirBytes(repo.Path) | ||||
| 	stdout, err := NewCommand(repo.Ctx, "diff", "--name-only", "-z", id1, id2, "--", filename).RunInDirBytes(repo.Path) | ||||
| 	if err != nil { | ||||
| 		return false, err | ||||
| 	} | ||||
| @@ -208,7 +208,7 @@ func (repo *Repository) CommitsByFileAndRange(revision, file string, page int) ( | ||||
| 	}() | ||||
| 	go func() { | ||||
| 		stderr := strings.Builder{} | ||||
| 		err := NewCommandContext(repo.Ctx, "log", revision, "--follow", | ||||
| 		err := NewCommand(repo.Ctx, "log", revision, "--follow", | ||||
| 			"--max-count="+strconv.Itoa(setting.Git.CommitsRangeSize*page), | ||||
| 			prettyLogFormat, "--", file). | ||||
| 			RunInDirPipeline(repo.Path, stdoutWriter, &stderr) | ||||
| @@ -239,7 +239,7 @@ func (repo *Repository) CommitsByFileAndRange(revision, file string, page int) ( | ||||
|  | ||||
| // CommitsByFileAndRangeNoFollow return the commits according revision file and the page | ||||
| func (repo *Repository) CommitsByFileAndRangeNoFollow(revision, file string, page int) ([]*Commit, error) { | ||||
| 	stdout, err := NewCommandContext(repo.Ctx, "log", revision, "--skip="+strconv.Itoa((page-1)*50), | ||||
| 	stdout, err := NewCommand(repo.Ctx, "log", revision, "--skip="+strconv.Itoa((page-1)*50), | ||||
| 		"--max-count="+strconv.Itoa(setting.Git.CommitsRangeSize), prettyLogFormat, "--", file).RunInDirBytes(repo.Path) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| @@ -249,11 +249,11 @@ func (repo *Repository) CommitsByFileAndRangeNoFollow(revision, file string, pag | ||||
|  | ||||
| // FilesCountBetween return the number of files changed between two commits | ||||
| func (repo *Repository) FilesCountBetween(startCommitID, endCommitID string) (int, error) { | ||||
| 	stdout, err := NewCommandContext(repo.Ctx, "diff", "--name-only", startCommitID+"..."+endCommitID).RunInDir(repo.Path) | ||||
| 	stdout, err := NewCommand(repo.Ctx, "diff", "--name-only", startCommitID+"..."+endCommitID).RunInDir(repo.Path) | ||||
| 	if err != nil && strings.Contains(err.Error(), "no merge base") { | ||||
| 		// git >= 2.28 now returns an error if startCommitID and endCommitID have become unrelated. | ||||
| 		// previously it would return the results of git diff --name-only startCommitID endCommitID so let's try that... | ||||
| 		stdout, err = NewCommandContext(repo.Ctx, "diff", "--name-only", startCommitID, endCommitID).RunInDir(repo.Path) | ||||
| 		stdout, err = NewCommand(repo.Ctx, "diff", "--name-only", startCommitID, endCommitID).RunInDir(repo.Path) | ||||
| 	} | ||||
| 	if err != nil { | ||||
| 		return 0, err | ||||
| @@ -267,13 +267,13 @@ func (repo *Repository) CommitsBetween(last, before *Commit) ([]*Commit, error) | ||||
| 	var stdout []byte | ||||
| 	var err error | ||||
| 	if before == nil { | ||||
| 		stdout, err = NewCommandContext(repo.Ctx, "rev-list", last.ID.String()).RunInDirBytes(repo.Path) | ||||
| 		stdout, err = NewCommand(repo.Ctx, "rev-list", last.ID.String()).RunInDirBytes(repo.Path) | ||||
| 	} else { | ||||
| 		stdout, err = NewCommandContext(repo.Ctx, "rev-list", before.ID.String()+".."+last.ID.String()).RunInDirBytes(repo.Path) | ||||
| 		stdout, err = NewCommand(repo.Ctx, "rev-list", before.ID.String()+".."+last.ID.String()).RunInDirBytes(repo.Path) | ||||
| 		if err != nil && strings.Contains(err.Error(), "no merge base") { | ||||
| 			// future versions of git >= 2.28 are likely to return an error if before and last have become unrelated. | ||||
| 			// previously it would return the results of git rev-list before last so let's try that... | ||||
| 			stdout, err = NewCommandContext(repo.Ctx, "rev-list", before.ID.String(), last.ID.String()).RunInDirBytes(repo.Path) | ||||
| 			stdout, err = NewCommand(repo.Ctx, "rev-list", before.ID.String(), last.ID.String()).RunInDirBytes(repo.Path) | ||||
| 		} | ||||
| 	} | ||||
| 	if err != nil { | ||||
| @@ -287,13 +287,13 @@ func (repo *Repository) CommitsBetweenLimit(last, before *Commit, limit, skip in | ||||
| 	var stdout []byte | ||||
| 	var err error | ||||
| 	if before == nil { | ||||
| 		stdout, err = NewCommandContext(repo.Ctx, "rev-list", "--max-count", strconv.Itoa(limit), "--skip", strconv.Itoa(skip), last.ID.String()).RunInDirBytes(repo.Path) | ||||
| 		stdout, err = NewCommand(repo.Ctx, "rev-list", "--max-count", strconv.Itoa(limit), "--skip", strconv.Itoa(skip), last.ID.String()).RunInDirBytes(repo.Path) | ||||
| 	} else { | ||||
| 		stdout, err = NewCommandContext(repo.Ctx, "rev-list", "--max-count", strconv.Itoa(limit), "--skip", strconv.Itoa(skip), before.ID.String()+".."+last.ID.String()).RunInDirBytes(repo.Path) | ||||
| 		stdout, err = NewCommand(repo.Ctx, "rev-list", "--max-count", strconv.Itoa(limit), "--skip", strconv.Itoa(skip), before.ID.String()+".."+last.ID.String()).RunInDirBytes(repo.Path) | ||||
| 		if err != nil && strings.Contains(err.Error(), "no merge base") { | ||||
| 			// future versions of git >= 2.28 are likely to return an error if before and last have become unrelated. | ||||
| 			// previously it would return the results of git rev-list --max-count n before last so let's try that... | ||||
| 			stdout, err = NewCommandContext(repo.Ctx, "rev-list", "--max-count", strconv.Itoa(limit), "--skip", strconv.Itoa(skip), before.ID.String(), last.ID.String()).RunInDirBytes(repo.Path) | ||||
| 			stdout, err = NewCommand(repo.Ctx, "rev-list", "--max-count", strconv.Itoa(limit), "--skip", strconv.Itoa(skip), before.ID.String(), last.ID.String()).RunInDirBytes(repo.Path) | ||||
| 		} | ||||
| 	} | ||||
| 	if err != nil { | ||||
| @@ -332,7 +332,7 @@ func (repo *Repository) CommitsCountBetween(start, end string) (int64, error) { | ||||
|  | ||||
| // commitsBefore the limit is depth, not total number of returned commits. | ||||
| func (repo *Repository) commitsBefore(id SHA1, limit int) ([]*Commit, error) { | ||||
| 	cmd := NewCommandContext(repo.Ctx, "log") | ||||
| 	cmd := NewCommand(repo.Ctx, "log") | ||||
| 	if limit > 0 { | ||||
| 		cmd.AddArguments("-"+strconv.Itoa(limit), prettyLogFormat, id.String()) | ||||
| 	} else { | ||||
| @@ -376,7 +376,7 @@ func (repo *Repository) getCommitsBeforeLimit(id SHA1, num int) ([]*Commit, erro | ||||
|  | ||||
| func (repo *Repository) getBranches(commit *Commit, limit int) ([]string, error) { | ||||
| 	if CheckGitVersionAtLeast("2.7.0") == nil { | ||||
| 		stdout, err := NewCommandContext(repo.Ctx, "for-each-ref", "--count="+strconv.Itoa(limit), "--format=%(refname:strip=2)", "--contains", commit.ID.String(), BranchPrefix).RunInDir(repo.Path) | ||||
| 		stdout, err := NewCommand(repo.Ctx, "for-each-ref", "--count="+strconv.Itoa(limit), "--format=%(refname:strip=2)", "--contains", commit.ID.String(), BranchPrefix).RunInDir(repo.Path) | ||||
| 		if err != nil { | ||||
| 			return nil, err | ||||
| 		} | ||||
| @@ -385,7 +385,7 @@ func (repo *Repository) getBranches(commit *Commit, limit int) ([]string, error) | ||||
| 		return branches, nil | ||||
| 	} | ||||
|  | ||||
| 	stdout, err := NewCommandContext(repo.Ctx, "branch", "--contains", commit.ID.String()).RunInDir(repo.Path) | ||||
| 	stdout, err := NewCommand(repo.Ctx, "branch", "--contains", commit.ID.String()).RunInDir(repo.Path) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| @@ -424,7 +424,7 @@ func (repo *Repository) GetCommitsFromIDs(commitIDs []string) []*Commit { | ||||
|  | ||||
| // IsCommitInBranch check if the commit is on the branch | ||||
| func (repo *Repository) IsCommitInBranch(commitID, branch string) (r bool, err error) { | ||||
| 	stdout, err := NewCommandContext(repo.Ctx, "branch", "--contains", commitID, branch).RunInDir(repo.Path) | ||||
| 	stdout, err := NewCommand(repo.Ctx, "branch", "--contains", commitID, branch).RunInDir(repo.Path) | ||||
| 	if err != nil { | ||||
| 		return false, err | ||||
| 	} | ||||
|   | ||||
		Reference in New Issue
	
	Block a user