Skip to content

Commit ee11974

Browse files
gary-kimtechknowlogick
authored andcommitted
Search Commits via Commit Hash (#7400)
* search commits via commit hash Signed-off-by: Gary Kim <[email protected]> * Also include all option for hash search Signed-off-by: Gary Kim <[email protected]> * Remove code duplication in commit search Signed-off-by: Gary Kim <[email protected]> * Add case ignore to commit hash search Signed-off-by: Gary Kim <[email protected]>
1 parent 6097ff6 commit ee11974

File tree

2 files changed

+39
-12
lines changed

2 files changed

+39
-12
lines changed

integrations/repo_commits_search_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,16 @@ func testRepoCommitsSearch(t *testing.T, query, commit string) {
2828
}
2929

3030
func TestRepoCommitsSearch(t *testing.T) {
31+
testRepoCommitsSearch(t, "e8eabd", "")
32+
testRepoCommitsSearch(t, "38a9cb", "")
33+
testRepoCommitsSearch(t, "6e8e", "6e8eabd9a7")
34+
testRepoCommitsSearch(t, "58e97", "58e97d1a24")
3135
testRepoCommitsSearch(t, "author:alice", "6e8eabd9a7")
36+
testRepoCommitsSearch(t, "author:alice 6e8ea", "6e8eabd9a7")
3237
testRepoCommitsSearch(t, "committer:Tom", "58e97d1a24")
3338
testRepoCommitsSearch(t, "author:bob commit-4", "58e97d1a24")
3439
testRepoCommitsSearch(t, "author:bob commit after:2019-03-03", "58e97d1a24")
40+
testRepoCommitsSearch(t, "committer:alice 6e8e before:2019-03-02", "6e8eabd9a7")
3541
testRepoCommitsSearch(t, "committer:alice commit before:2019-03-02", "6e8eabd9a7")
3642
testRepoCommitsSearch(t, "committer:alice author:tom commit before:2019-03-04 after:2019-03-02", "0a8499a22a")
3743
}

modules/git/repo_commit.go

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -208,36 +208,57 @@ func (repo *Repository) commitsByRange(id SHA1, page int) (*list.List, error) {
208208
}
209209

210210
func (repo *Repository) searchCommits(id SHA1, opts SearchCommitsOptions) (*list.List, error) {
211-
cmd := NewCommand("log", id.String(), "-100", "-i", prettyLogFormat)
212-
if len(opts.Keywords) > 0 {
213-
for _, v := range opts.Keywords {
214-
cmd.AddArguments("--grep=" + v)
215-
}
216-
}
211+
cmd := NewCommand("log", id.String(), "-100", prettyLogFormat)
212+
args := []string{"-i"}
217213
if len(opts.Authors) > 0 {
218214
for _, v := range opts.Authors {
219-
cmd.AddArguments("--author=" + v)
215+
args = append(args, "--author="+v)
220216
}
221217
}
222218
if len(opts.Committers) > 0 {
223219
for _, v := range opts.Committers {
224-
cmd.AddArguments("--committer=" + v)
220+
args = append(args, "--committer="+v)
225221
}
226222
}
227223
if len(opts.After) > 0 {
228-
cmd.AddArguments("--after=" + opts.After)
224+
args = append(args, "--after="+opts.After)
229225
}
230226
if len(opts.Before) > 0 {
231-
cmd.AddArguments("--before=" + opts.Before)
227+
args = append(args, "--before="+opts.Before)
232228
}
233229
if opts.All {
234-
cmd.AddArguments("--all")
230+
args = append(args, "--all")
231+
}
232+
if len(opts.Keywords) > 0 {
233+
for _, v := range opts.Keywords {
234+
cmd.AddArguments("--grep=" + v)
235+
}
235236
}
237+
cmd.AddArguments(args...)
236238
stdout, err := cmd.RunInDirBytes(repo.Path)
237239
if err != nil {
238240
return nil, err
239241
}
240-
return repo.parsePrettyFormatLogToList(stdout)
242+
if len(stdout) != 0 {
243+
stdout = append(stdout, '\n')
244+
}
245+
if len(opts.Keywords) > 0 {
246+
for _, v := range opts.Keywords {
247+
if len(v) >= 4 {
248+
hashCmd := NewCommand("log", "-1", prettyLogFormat)
249+
hashCmd.AddArguments(args...)
250+
hashCmd.AddArguments(v)
251+
hashMatching, err := hashCmd.RunInDirBytes(repo.Path)
252+
if err != nil || bytes.Contains(stdout, hashMatching) {
253+
continue
254+
}
255+
stdout = append(stdout, hashMatching...)
256+
stdout = append(stdout, '\n')
257+
}
258+
}
259+
}
260+
261+
return repo.parsePrettyFormatLogToList(bytes.TrimSuffix(stdout, []byte{'\n'}))
241262
}
242263

243264
func (repo *Repository) getFilesChanged(id1, id2 string) ([]string, error) {

0 commit comments

Comments
 (0)