From 5a261923500bb332ae9016f54292f77f77928e2a Mon Sep 17 00:00:00 2001 From: Cirno the Strongest <1447794+CirnoT@users.noreply.github.com> Date: Thu, 11 Jun 2020 23:44:39 +0200 Subject: [PATCH] Fix commit search in all branches (#11849) * Fix commit search in all branches * comments Co-authored-by: techknowlogick --- modules/git/repo_commit.go | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/modules/git/repo_commit.go b/modules/git/repo_commit.go index 397c390e8..e282c9c67 100644 --- a/modules/git/repo_commit.go +++ b/modules/git/repo_commit.go @@ -220,32 +220,49 @@ func (repo *Repository) commitsByRange(id SHA1, page, pageSize int) (*list.List, } func (repo *Repository) searchCommits(id SHA1, opts SearchCommitsOptions) (*list.List, error) { + // create new git log command with limit of 100 commis cmd := NewCommand("log", id.String(), "-100", prettyLogFormat) + // ignore case args := []string{"-i"} + + // add authors if present in search query if len(opts.Authors) > 0 { for _, v := range opts.Authors { args = append(args, "--author="+v) } } + + // add commiters if present in search query if len(opts.Committers) > 0 { for _, v := range opts.Committers { args = append(args, "--committer="+v) } } + + // add time constraints if present in search query if len(opts.After) > 0 { args = append(args, "--after="+opts.After) } if len(opts.Before) > 0 { args = append(args, "--before="+opts.Before) } + + // 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 + // note this is done only for command created above if opts.All { - args = append(args, "--all") + cmd.AddArguments("--all") } + + // add remaining keywords from search string + // note this is done only for command created above if len(opts.Keywords) > 0 { for _, v := range opts.Keywords { cmd.AddArguments("--grep=" + v) } } + + // search for commits matching given constraints and keywords in commit msg cmd.AddArguments(args...) stdout, err := cmd.RunInDirBytes(repo.Path) if err != nil { @@ -254,12 +271,21 @@ func (repo *Repository) searchCommits(id SHA1, opts SearchCommitsOptions) (*list if len(stdout) != 0 { stdout = append(stdout, '\n') } + + // if there are any keywords (ie not commiter:, author:, time:) + // then let's iterate over them if len(opts.Keywords) > 0 { for _, v := range opts.Keywords { + // ignore anything below 4 characters as too unspecific if len(v) >= 4 { + // create new git log command with 1 commit limit hashCmd := NewCommand("log", "-1", prettyLogFormat) + // add previous arguments except for --grep and --all hashCmd.AddArguments(args...) + // add keyword as hashCmd.AddArguments(v) + + // search with given constraints for commit matching sha hash of v hashMatching, err := hashCmd.RunInDirBytes(repo.Path) if err != nil || bytes.Contains(stdout, hashMatching) { continue