History

Support search operators for commits search (#6479)

* Support searching commits with prefix syntax For now, support auther: committer: When more than one prefix is supplied is presented, the result is the union. When different prefixes are supplied, the result is the intersection. For example, "author:alice author:bob" => the result is all commits authored by Alice OR Bob "hello committer:alice" => the result is all commits committed by Alice AND has the keyword 'hello' in the message. Note that there should NOT have any space after the colon(:) of the prefix. For example, "author:bill" => correct "author: bill" => wrong * Remove unneeded logging * Add missing files of test repository * Add missing repo_unit entries to test fixtures * Update test cases * Add tooltip for commits search button * Update tooltip text I have no idea about how to format it with line breaks. * Make the usage example more real * Add a test case * Add new options struct for SearchCommits * Prefer len(s) > 0 over s != "" * Add NewSearchCommitsOptions

Mura Li committed 7 years ago

Commit 3186ef51 parent 1b7dffc41 files · +675 −9Browse files

Showing the changes to modules/git/commit.go (1 of 41 files). Show all

Unified · Split

modules/git/commit.go+41−2
@@ -214,9 +214,48 @@ func (c *Commit) CommitsBeforeUntil(commitID string) (*list.List, error) { · 213 unmodified lines
214214 return c.repo.CommitsBetween(c, endCommit)
215215 }
216216
217+// SearchCommitsOptions specify the parameters for SearchCommits
218+type SearchCommitsOptions struct {
219+ Keywords []string
220+ Authors, Committers []string
221+ After, Before string
222+ All bool
223+}
224+
225+// NewSearchCommitsOptions contruct a SearchCommitsOption from a space-delimited search string
226+func NewSearchCommitsOptions(searchString string, forAllRefs bool) SearchCommitsOptions {
227+ var keywords, authors, committers []string
228+ var after, before string
229+
230+ fields := strings.Fields(searchString)
231+ for _, k := range fields {
232+ switch {
233+ case strings.HasPrefix(k, "author:"):
234+ authors = append(authors, strings.TrimPrefix(k, "author:"))
235+ case strings.HasPrefix(k, "committer:"):
236+ committers = append(committers, strings.TrimPrefix(k, "committer:"))
237+ case strings.HasPrefix(k, "after:"):
238+ after = strings.TrimPrefix(k, "after:")
239+ case strings.HasPrefix(k, "before:"):
240+ before = strings.TrimPrefix(k, "before:")
241+ default:
242+ keywords = append(keywords, k)
243+ }
244+ }
245+
246+ return SearchCommitsOptions{
247+ Keywords: keywords,
248+ Authors: authors,
249+ Committers: committers,
250+ After: after,
251+ Before: before,
252+ All: forAllRefs,
253+ }
254+}
255+
217256 // SearchCommits returns the commits match the keyword before current revision
218−func (c *Commit) SearchCommits(keyword string, all bool) (*list.List, error) {
219− return c.repo.searchCommits(c.ID, keyword, all)
257+func (c *Commit) SearchCommits(opts SearchCommitsOptions) (*list.List, error) {
258+ return c.repo.searchCommits(c.ID, opts)
220259 }
221260
222261 // GetFilesChangedSinceCommit get all changed file names between pastCommit to current revision

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t