Skip to content

Commit

Permalink
search commits via commit hash
Browse files Browse the repository at this point in the history
Signed-off-by: Gary Kim <gary@garykim.dev>
  • Loading branch information
gary-kim committed Jul 10, 2019
1 parent 877df0f commit 27d1bbc
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
6 changes: 6 additions & 0 deletions integrations/repo_commits_search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,16 @@ func testRepoCommitsSearch(t *testing.T, query, commit string) {
}

func TestRepoCommitsSearch(t *testing.T) {
testRepoCommitsSearch(t, "e8eabd", "")
testRepoCommitsSearch(t, "38a9cb", "")
testRepoCommitsSearch(t, "6e8e", "6e8eabd9a7")
testRepoCommitsSearch(t, "58e97", "58e97d1a24")
testRepoCommitsSearch(t, "author:alice", "6e8eabd9a7")
testRepoCommitsSearch(t, "author:alice 6e8ea", "6e8eabd9a7")
testRepoCommitsSearch(t, "committer:Tom", "58e97d1a24")
testRepoCommitsSearch(t, "author:bob commit-4", "58e97d1a24")
testRepoCommitsSearch(t, "author:bob commit after:2019-03-03", "58e97d1a24")
testRepoCommitsSearch(t, "committer:alice 6e8e before:2019-03-02", "6e8eabd9a7")
testRepoCommitsSearch(t, "committer:alice commit before:2019-03-02", "6e8eabd9a7")
testRepoCommitsSearch(t, "committer:alice author:tom commit before:2019-03-04 after:2019-03-02", "0a8499a22a")
}
31 changes: 25 additions & 6 deletions modules/git/repo_commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,35 +203,54 @@ func (repo *Repository) commitsByRange(id SHA1, page int) (*list.List, error) {

func (repo *Repository) searchCommits(id SHA1, opts SearchCommitsOptions) (*list.List, error) {
cmd := NewCommand("log", id.String(), "-100", "-i", prettyLogFormat)
if len(opts.Keywords) > 0 {
for _, v := range opts.Keywords {
cmd.AddArguments("--grep=" + v)
}
}
args := []string{"log", "-i", prettyLogFormat}
if len(opts.Authors) > 0 {
for _, v := range opts.Authors {
cmd.AddArguments("--author=" + v)
args = append(args, "--author="+v)
}
}
if len(opts.Committers) > 0 {
for _, v := range opts.Committers {
cmd.AddArguments("--committer=" + v)
args = append(args, "--committer="+v)
}
}
if len(opts.After) > 0 {
cmd.AddArguments("--after=" + opts.After)
args = append(args, "--after="+opts.After)
}
if len(opts.Before) > 0 {
cmd.AddArguments("--before=" + opts.Before)
args = append(args, "--before="+opts.Before)
}
if opts.All {
cmd.AddArguments("--all")
}
if len(opts.Keywords) > 0 {
for _, v := range opts.Keywords {
cmd.AddArguments("--grep=" + v)
}
}
stdout, err := cmd.RunInDirBytes(repo.Path)
if err != nil {
return nil, err
}
return repo.parsePrettyFormatLogToList(stdout)
if len(opts.Keywords) > 0 {
for _, v := range opts.Keywords {
if len(v) >= 4 {
hashCmd := NewCommand(args...)
hashCmd.AddArguments("-1", v)
hashMatching, err := hashCmd.RunInDirBytes(repo.Path)
if err != nil || bytes.Contains(stdout, hashMatching) {
continue
}
stdout = append(stdout, hashMatching...)
stdout = append(stdout, '\n')
}
}
}
return repo.parsePrettyFormatLogToList(bytes.TrimSuffix(stdout, []byte{'\n'}))
}

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

0 comments on commit 27d1bbc

Please sign in to comment.