From 183cde56e09af0ee7b9c3ba83f826873ff16091c Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Fri, 25 Jul 2025 10:39:51 +0800 Subject: [PATCH] fix --- modules/git/repo.go | 11 +++++++++-- modules/git/repo_commit.go | 14 +++++++------- services/pull/compare.go | 12 ++---------- 3 files changed, 18 insertions(+), 19 deletions(-) diff --git a/modules/git/repo.go b/modules/git/repo.go index ddc96d036cf..ed1f8e835a4 100644 --- a/modules/git/repo.go +++ b/modules/git/repo.go @@ -31,14 +31,21 @@ type GPGSettings struct { Format string } -const PrettyLogFormat = `--pretty=format:%H` +const prettyLogFormat = `--pretty=format:%H` // GetAllCommitsCount returns count of all commits in repository func (repo *Repository) GetAllCommitsCount() (int64, error) { return AllCommitsCount(repo.Ctx, repo.Path, false) } -func (repo *Repository) ParsePrettyFormatLogToList(logs []byte) ([]*Commit, error) { +func (repo *Repository) ShowPrettyFormatLogToList(ctx context.Context, revisionRange string) ([]*Commit, error) { + // avoid: ambiguous argument 'refs/a...refs/b': unknown revision or path not in the working tree. Use '--': 'git [...] -- [...]' + logs, _, err := NewCommand("log").AddArguments(prettyLogFormat). + AddDynamicArguments(revisionRange).AddArguments("--"). + RunStdBytes(ctx, &RunOpts{Dir: repo.Path}) + if err != nil { + return nil, err + } return repo.parsePrettyFormatLogToList(logs) } diff --git a/modules/git/repo_commit.go b/modules/git/repo_commit.go index 269538beabc..4066a1ca7ba 100644 --- a/modules/git/repo_commit.go +++ b/modules/git/repo_commit.go @@ -59,7 +59,7 @@ func (repo *Repository) getCommitByPathWithID(id ObjectID, relpath string) (*Com relpath = `\` + relpath } - stdout, _, runErr := NewCommand("log", "-1", PrettyLogFormat).AddDynamicArguments(id.String()).AddDashesAndList(relpath).RunStdString(repo.Ctx, &RunOpts{Dir: repo.Path}) + stdout, _, runErr := NewCommand("log", "-1", prettyLogFormat).AddDynamicArguments(id.String()).AddDashesAndList(relpath).RunStdString(repo.Ctx, &RunOpts{Dir: repo.Path}) if runErr != nil { return nil, runErr } @@ -74,7 +74,7 @@ func (repo *Repository) getCommitByPathWithID(id ObjectID, relpath string) (*Com // GetCommitByPath returns the last commit of relative path. func (repo *Repository) GetCommitByPath(relpath string) (*Commit, error) { - stdout, _, runErr := NewCommand("log", "-1", PrettyLogFormat).AddDashesAndList(relpath).RunStdBytes(repo.Ctx, &RunOpts{Dir: repo.Path}) + stdout, _, runErr := NewCommand("log", "-1", prettyLogFormat).AddDashesAndList(relpath).RunStdBytes(repo.Ctx, &RunOpts{Dir: repo.Path}) if runErr != nil { return nil, runErr } @@ -94,7 +94,7 @@ func (repo *Repository) commitsByRangeWithTime(id ObjectID, page, pageSize int, cmd := NewCommand("log"). AddOptionFormat("--skip=%d", (page-1)*pageSize). AddOptionFormat("--max-count=%d", pageSize). - AddArguments(PrettyLogFormat). + AddArguments(prettyLogFormat). AddDynamicArguments(id.String()) if not != "" { @@ -141,7 +141,7 @@ func (repo *Repository) searchCommits(id ObjectID, opts SearchCommitsOptions) ([ } // create new git log command with limit of 100 commits - cmd := NewCommand("log", "-100", PrettyLogFormat).AddDynamicArguments(id.String()) + cmd := NewCommand("log", "-100", prettyLogFormat).AddDynamicArguments(id.String()) // pretend that all refs along with HEAD were listed on command line as // https://git-scm.com/docs/git-log#Documentation/git-log.txt---all @@ -175,7 +175,7 @@ func (repo *Repository) searchCommits(id ObjectID, opts SearchCommitsOptions) ([ // ignore anything not matching a valid sha pattern if id.Type().IsValid(v) { // create new git log command with 1 commit limit - hashCmd := NewCommand("log", "-1", PrettyLogFormat) + hashCmd := NewCommand("log", "-1", prettyLogFormat) // add previous arguments except for --grep and --all addCommonSearchArgs(hashCmd) // add keyword as @@ -410,7 +410,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 ObjectID, limit int) ([]*Commit, error) { - cmd := NewCommand("log", PrettyLogFormat) + cmd := NewCommand("log", prettyLogFormat) if limit > 0 { cmd.AddOptionFormat("-%d", limit) } @@ -536,7 +536,7 @@ func (repo *Repository) AddLastCommitCache(cacheKey, fullName, sha string) error // GetCommitBranchStart returns the commit where the branch diverged func (repo *Repository) GetCommitBranchStart(env []string, branch, endCommitID string) (string, error) { - cmd := NewCommand("log", PrettyLogFormat) + cmd := NewCommand("log", prettyLogFormat) cmd.AddDynamicArguments(endCommitID) stdout, _, runErr := cmd.RunStdBytes(repo.Ctx, &RunOpts{ diff --git a/services/pull/compare.go b/services/pull/compare.go index f8b5ba20a88..4c782f066eb 100644 --- a/services/pull/compare.go +++ b/services/pull/compare.go @@ -67,17 +67,9 @@ func GetCompareInfo(ctx context.Context, baseRepo, headRepo *repo_model.Reposito // We have a common base - therefore we know that ... should work if !fileOnly { - // avoid: ambiguous argument 'refs/a...refs/b': unknown revision or path not in the working tree. Use '--': 'git [...] -- [...]' - var logs []byte - logs, _, err = git.NewCommand("log").AddArguments(git.PrettyLogFormat). - AddDynamicArguments(baseCommitID+separator+headBranch).AddArguments("--"). - RunStdBytes(ctx, &git.RunOpts{Dir: headGitRepo.Path}) + compareInfo.Commits, err = headGitRepo.ShowPrettyFormatLogToList(ctx, baseCommitID+separator+headBranch) if err != nil { - return nil, err - } - compareInfo.Commits, err = headGitRepo.ParsePrettyFormatLogToList(logs) - if err != nil { - return nil, fmt.Errorf("parsePrettyFormatLogToList: %w", err) + return nil, fmt.Errorf("ShowPrettyFormatLogToList: %w", err) } } else { compareInfo.Commits = []*git.Commit{}