Fix commit search in all branches (#11849)

* Fix commit search in all branches

* comments

Co-authored-by: techknowlogick <techknowlogick@gitea.io>
This commit is contained in:
Cirno the Strongest 2020-06-11 23:44:39 +02:00 committed by GitHub
parent ca8ecf7ffc
commit 5a26192350
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 27 additions and 1 deletions

View File

@ -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 <commis>
// 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 <commit>
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