From 27d1bbce2876fd28be30cf8ecb9e149844a386c1 Mon Sep 17 00:00:00 2001 From: Gary Kim Date: Tue, 9 Jul 2019 17:14:31 +0800 Subject: [PATCH 1/4] search commits via commit hash Signed-off-by: Gary Kim --- integrations/repo_commits_search_test.go | 6 +++++ modules/git/repo_commit.go | 31 +++++++++++++++++++----- 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/integrations/repo_commits_search_test.go b/integrations/repo_commits_search_test.go index f477550c0ebea..c8bc1b56e65c0 100644 --- a/integrations/repo_commits_search_test.go +++ b/integrations/repo_commits_search_test.go @@ -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") } diff --git a/modules/git/repo_commit.go b/modules/git/repo_commit.go index 8ea2a331458b4..d2ed5e56aa27d 100644 --- a/modules/git/repo_commit.go +++ b/modules/git/repo_commit.go @@ -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) { From 00eb1f02710b61e1e23751c68ea966288206dcdd Mon Sep 17 00:00:00 2001 From: Gary Kim Date: Thu, 11 Jul 2019 17:38:16 +0800 Subject: [PATCH 2/4] Also include all option for hash search Signed-off-by: Gary Kim --- modules/git/repo_commit.go | 1 + 1 file changed, 1 insertion(+) diff --git a/modules/git/repo_commit.go b/modules/git/repo_commit.go index d2ed5e56aa27d..de53fd7aa3818 100644 --- a/modules/git/repo_commit.go +++ b/modules/git/repo_commit.go @@ -226,6 +226,7 @@ func (repo *Repository) searchCommits(id SHA1, opts SearchCommitsOptions) (*list } if opts.All { cmd.AddArguments("--all") + args = append(args, "--all") } if len(opts.Keywords) > 0 { for _, v := range opts.Keywords { From 813bc5825f4293396ae7f053b6a2109182973df2 Mon Sep 17 00:00:00 2001 From: Gary Kim Date: Mon, 12 Aug 2019 11:18:02 +0800 Subject: [PATCH 3/4] Remove code duplication in commit search Signed-off-by: Gary Kim --- modules/git/repo_commit.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/modules/git/repo_commit.go b/modules/git/repo_commit.go index de53fd7aa3818..2594574371e1f 100644 --- a/modules/git/repo_commit.go +++ b/modules/git/repo_commit.go @@ -203,29 +203,24 @@ 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) - args := []string{"log", "-i", prettyLogFormat} + var args []string 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") args = append(args, "--all") } if len(opts.Keywords) > 0 { @@ -233,15 +228,20 @@ func (repo *Repository) searchCommits(id SHA1, opts SearchCommitsOptions) (*list cmd.AddArguments("--grep=" + v) } } + cmd.AddArguments(args...) stdout, err := cmd.RunInDirBytes(repo.Path) if err != nil { return nil, err } + if len(stdout) != 0 { + stdout = append(stdout, '\n') + } if len(opts.Keywords) > 0 { for _, v := range opts.Keywords { if len(v) >= 4 { - hashCmd := NewCommand(args...) - hashCmd.AddArguments("-1", v) + hashCmd := NewCommand("log", "-1", prettyLogFormat) + hashCmd.AddArguments(args...) + hashCmd.AddArguments(v) hashMatching, err := hashCmd.RunInDirBytes(repo.Path) if err != nil || bytes.Contains(stdout, hashMatching) { continue From dd348de5c1b15939e89acaa28aeefb667e2fe4a9 Mon Sep 17 00:00:00 2001 From: Gary Kim Date: Tue, 13 Aug 2019 20:48:48 +0800 Subject: [PATCH 4/4] Add case ignore to commit hash search Signed-off-by: Gary Kim --- modules/git/repo_commit.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/modules/git/repo_commit.go b/modules/git/repo_commit.go index 2594574371e1f..01622db69165a 100644 --- a/modules/git/repo_commit.go +++ b/modules/git/repo_commit.go @@ -202,8 +202,8 @@ 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) - var args []string + cmd := NewCommand("log", id.String(), "-100", prettyLogFormat) + args := []string{"-i"} if len(opts.Authors) > 0 { for _, v := range opts.Authors { args = append(args, "--author="+v) @@ -251,6 +251,7 @@ func (repo *Repository) searchCommits(id SHA1, opts SearchCommitsOptions) (*list } } } + return repo.parsePrettyFormatLogToList(bytes.TrimSuffix(stdout, []byte{'\n'})) }