diff options
author | Cirno the Strongest <1447794+CirnoT@users.noreply.github.com> | 2020-06-11 23:44:39 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-11 17:44:39 -0400 |
commit | 5a261923500bb332ae9016f54292f77f77928e2a (patch) | |
tree | 2a2574a77b193e5c1bb2af0aa3ae68cfc50f76eb /modules | |
parent | ca8ecf7ffcb66381f7a6fae0402a3742b99a6dc5 (diff) | |
download | gitea-5a261923500bb332ae9016f54292f77f77928e2a.tar.gz gitea-5a261923500bb332ae9016f54292f77f77928e2a.zip |
Fix commit search in all branches (#11849)
* Fix commit search in all branches
* comments
Co-authored-by: techknowlogick <techknowlogick@gitea.io>
Diffstat (limited to 'modules')
-rw-r--r-- | modules/git/repo_commit.go | 28 |
1 files changed, 27 insertions, 1 deletions
diff --git a/modules/git/repo_commit.go b/modules/git/repo_commit.go index 397c390e84..e282c9c674 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 <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 |