Skip to content

Commit 7642a54

Browse files
committed
Tidy up code
1 parent 4b386ea commit 7642a54

File tree

3 files changed

+161
-116
lines changed

3 files changed

+161
-116
lines changed

git_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,9 @@ const repoPath = "testdata/testrepo.git"
2222
var testrepo *Repository
2323

2424
func TestMain(m *testing.M) {
25-
verbose := flag.Bool("verbose", false, "")
2625
flag.Parse()
2726

28-
if *verbose {
27+
if testing.Verbose() {
2928
SetOutput(os.Stdout)
3029
}
3130

repo_grep.go

+46-40
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Copyright 2022 The Gogs Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
15
package git
26

37
import (
@@ -7,17 +11,20 @@ import (
711
"time"
812
)
913

14+
// GrepOptions contains optional arguments for grep search over repository files.
15+
//
16+
// Docs: https://git-scm.com/docs/git-grep
1017
type GrepOptions struct {
11-
// TreeID to search in
12-
TreeID string
13-
// Limits the search to files in the specified pathspec
18+
// The tree to run the search. Defaults to "HEAD".
19+
Tree string
20+
// Limits the search to files in the specified pathspec.
1421
Pathspec string
15-
// Case insensitive search.
22+
// Whether to do case insensitive search.
1623
IgnoreCase bool
17-
// Match the pattern only at word boundaries.
18-
WordMatch bool
19-
// Whether or not to use extended regular expressions.
20-
ExtendedRegex bool
24+
// Whether to match the pattern only at word boundaries.
25+
WordRegexp bool
26+
// Whether use extended regular expressions.
27+
ExtendedRegexp bool
2128
// The timeout duration before giving up for each shell command execution. The
2229
// default timeout duration will be used when not supplied.
2330
Timeout time.Duration
@@ -27,8 +34,8 @@ type GrepOptions struct {
2734

2835
// GrepResult represents a single result from a grep search.
2936
type GrepResult struct {
30-
// The TreeID of the file that matched. Could be `HEAD` or a tree ID.
31-
TreeID string
37+
// The tree of the file that matched, e.g. "HEAD".
38+
Tree string
3239
// The path of the file that matched.
3340
Path string
3441
// The line number of the match.
@@ -42,15 +49,14 @@ type GrepResult struct {
4249
func parseGrepLine(line string) (*GrepResult, error) {
4350
r := &GrepResult{}
4451
sp := strings.SplitN(line, ":", 5)
45-
4652
var n int
4753
switch len(sp) {
4854
case 4:
49-
// HEAD tree ID
50-
r.TreeID = "HEAD"
55+
// HEAD
56+
r.Tree = "HEAD"
5157
case 5:
52-
// Tree ID included
53-
r.TreeID = sp[0]
58+
// Tree included
59+
r.Tree = sp[0]
5460
n++
5561
default:
5662
return nil, fmt.Errorf("invalid grep line: %s", line)
@@ -62,53 +68,53 @@ func parseGrepLine(line string) (*GrepResult, error) {
6268
r.Column, _ = strconv.Atoi(sp[n])
6369
n++
6470
r.Text = sp[n]
65-
6671
return r, nil
6772
}
6873

6974
// Grep returns the results of a grep search in the repository.
70-
func (r *Repository) Grep(pattern string, opts ...GrepOptions) ([]*GrepResult, error) {
75+
func (r *Repository) Grep(pattern string, opts ...GrepOptions) []*GrepResult {
7176
var opt GrepOptions
7277
if len(opts) > 0 {
7378
opt = opts[0]
7479
}
75-
if opt.TreeID == "" {
76-
opt.TreeID = "HEAD"
80+
if opt.Tree == "" {
81+
opt.Tree = "HEAD"
7782
}
7883

7984
cmd := NewCommand("grep").
8085
AddOptions(opt.CommandOptions).
81-
// Result full-name, line number & column number
86+
// Display full-name, line number and column number
8287
AddArgs("--full-name", "--line-number", "--column")
8388
if opt.IgnoreCase {
84-
cmd.AddArgs("-i")
89+
cmd.AddArgs("--ignore-case")
8590
}
86-
if opt.WordMatch {
87-
cmd.AddArgs("-w")
91+
if opt.WordRegexp {
92+
cmd.AddArgs("--word-regexp")
8893
}
89-
if opt.ExtendedRegex {
90-
cmd.AddArgs("-E")
94+
if opt.ExtendedRegexp {
95+
cmd.AddArgs("--extended-regexp")
9196
}
92-
cmd.AddArgs(pattern, opt.TreeID)
97+
cmd.AddArgs(pattern, opt.Tree)
9398
if opt.Pathspec != "" {
9499
cmd.AddArgs("--", opt.Pathspec)
95100
}
96101

97-
results := make([]*GrepResult, 0)
98102
stdout, err := cmd.RunInDirWithTimeout(opt.Timeout, r.path)
99-
if err == nil && len(stdout) > 0 {
100-
// normalize line endings
101-
lines := strings.Split(strings.ReplaceAll(string(stdout), "\r", ""), "\n")
102-
for _, line := range lines {
103-
if len(line) == 0 {
104-
continue
105-
}
106-
r, err := parseGrepLine(line)
107-
if err == nil {
108-
results = append(results, r)
109-
}
110-
}
103+
if err != nil {
104+
return nil
111105
}
112106

113-
return results, nil
107+
var results []*GrepResult
108+
// Normalize line endings
109+
lines := strings.Split(strings.ReplaceAll(string(stdout), "\r", ""), "\n")
110+
for _, line := range lines {
111+
if len(line) == 0 {
112+
continue
113+
}
114+
r, err := parseGrepLine(line)
115+
if err == nil {
116+
results = append(results, r)
117+
}
118+
}
119+
return results
114120
}

repo_grep_test.go

+114-74
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Copyright 2022 The Gogs Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
15
package git
26

37
import (
@@ -6,94 +10,130 @@ import (
610
"github.com/stretchr/testify/assert"
711
)
812

9-
func TestRepoGrepSimple(t *testing.T) {
10-
pattern := "programmingPoints"
11-
expect := []GrepResult{
12-
{
13-
TreeID: "HEAD", Path: "src/Main.groovy", Line: 7, Column: 5, Text: "int programmingPoints = 10",
14-
},
15-
{
16-
TreeID: "HEAD", Path: "src/Main.groovy", Line: 10, Column: 33, Text: `println "${name} has at least ${programmingPoints} programming points."`,
17-
},
18-
{
19-
TreeID: "HEAD", Path: "src/Main.groovy", Line: 11, Column: 12, Text: `println "${programmingPoints} squared is ${square(programmingPoints)}"`,
20-
},
21-
{
22-
TreeID: "HEAD", Path: "src/Main.groovy", Line: 12, Column: 12, Text: `println "${programmingPoints} divided by 2 bonus points is ${divide(programmingPoints, 2)}"`,
23-
},
24-
{
25-
TreeID: "HEAD", Path: "src/Main.groovy", Line: 13, Column: 12, Text: `println "${programmingPoints} minus 7 bonus points is ${subtract(programmingPoints, 7)}"`,
26-
},
27-
{
28-
TreeID: "HEAD", Path: "src/Main.groovy", Line: 14, Column: 12, Text: `println "${programmingPoints} plus 3 bonus points is ${sum(programmingPoints, 3)}"`,
13+
func TestRepository_Grep_Simple(t *testing.T) {
14+
want := []*GrepResult{
15+
{
16+
Tree: "HEAD",
17+
Path: "src/Main.groovy",
18+
Line: 7,
19+
Column: 5,
20+
Text: "int programmingPoints = 10",
21+
}, {
22+
Tree: "HEAD",
23+
Path: "src/Main.groovy",
24+
Line: 10,
25+
Column: 33,
26+
Text: `println "${name} has at least ${programmingPoints} programming points."`,
27+
}, {
28+
Tree: "HEAD",
29+
Path: "src/Main.groovy",
30+
Line: 11,
31+
Column: 12,
32+
Text: `println "${programmingPoints} squared is ${square(programmingPoints)}"`,
33+
}, {
34+
Tree: "HEAD",
35+
Path: "src/Main.groovy",
36+
Line: 12,
37+
Column: 12,
38+
Text: `println "${programmingPoints} divided by 2 bonus points is ${divide(programmingPoints, 2)}"`,
39+
}, {
40+
Tree: "HEAD",
41+
Path: "src/Main.groovy",
42+
Line: 13,
43+
Column: 12,
44+
Text: `println "${programmingPoints} minus 7 bonus points is ${subtract(programmingPoints, 7)}"`,
45+
}, {
46+
Tree: "HEAD",
47+
Path: "src/Main.groovy",
48+
Line: 14,
49+
Column: 12,
50+
Text: `println "${programmingPoints} plus 3 bonus points is ${sum(programmingPoints, 3)}"`,
2951
},
3052
}
31-
results, err := testrepo.Grep(pattern)
32-
assert.NoError(t, err)
33-
for i, result := range results {
34-
assert.Equal(t, expect[i], *result)
35-
}
53+
got := testrepo.Grep("programmingPoints")
54+
assert.Equal(t, want, got)
3655
}
3756

38-
func TestRepoGrepIgnoreCase(t *testing.T) {
39-
pattern := "Hello"
40-
expect := []GrepResult{
41-
{
42-
TreeID: "HEAD", Path: "README.txt", Line: 9, Column: 36, Text: "* [email protected]:matthewmccullough/hellogitworld.git",
43-
},
44-
{
45-
TreeID: "HEAD", Path: "README.txt", Line: 10, Column: 38, Text: "* git://github.com/matthewmccullough/hellogitworld.git",
46-
},
47-
{
48-
TreeID: "HEAD", Path: "README.txt", Line: 11, Column: 58, Text: "* https://[email protected]/matthewmccullough/hellogitworld.git",
49-
},
50-
{
51-
TreeID: "HEAD", Path: "src/Main.groovy", Line: 9, Column: 10, Text: `println "Hello ${name}"`,
52-
},
53-
{
54-
TreeID: "HEAD", Path: "src/main/java/com/github/App.java", Line: 4, Column: 4, Text: " * Hello again",
55-
},
56-
{
57-
TreeID: "HEAD", Path: "src/main/java/com/github/App.java", Line: 5, Column: 4, Text: " * Hello world!",
58-
},
59-
{
60-
TreeID: "HEAD", Path: "src/main/java/com/github/App.java", Line: 6, Column: 4, Text: " * Hello",
61-
},
62-
{
63-
TreeID: "HEAD", Path: "src/main/java/com/github/App.java", Line: 13, Column: 30, Text: ` System.out.println( "Hello World!" );`,
57+
func TestRepository_Grep_IgnoreCase(t *testing.T) {
58+
want := []*GrepResult{
59+
{
60+
Tree: "HEAD",
61+
Path: "README.txt",
62+
Line: 9,
63+
Column: 36,
64+
Text: "* [email protected]:matthewmccullough/hellogitworld.git",
65+
}, {
66+
Tree: "HEAD",
67+
Path: "README.txt",
68+
Line: 10,
69+
Column: 38,
70+
Text: "* git://github.com/matthewmccullough/hellogitworld.git",
71+
}, {
72+
Tree: "HEAD",
73+
Path: "README.txt",
74+
Line: 11,
75+
Column: 58,
76+
Text: "* https://[email protected]/matthewmccullough/hellogitworld.git",
77+
}, {
78+
Tree: "HEAD",
79+
Path: "src/Main.groovy",
80+
Line: 9,
81+
Column: 10,
82+
Text: `println "Hello ${name}"`,
83+
}, {
84+
Tree: "HEAD",
85+
Path: "src/main/java/com/github/App.java",
86+
Line: 4,
87+
Column: 4,
88+
Text: " * Hello again",
89+
}, {
90+
Tree: "HEAD",
91+
Path: "src/main/java/com/github/App.java",
92+
Line: 5,
93+
Column: 4,
94+
Text: " * Hello world!",
95+
}, {
96+
Tree: "HEAD",
97+
Path: "src/main/java/com/github/App.java",
98+
Line: 6,
99+
Column: 4,
100+
Text: " * Hello",
101+
}, {
102+
Tree: "HEAD",
103+
Path: "src/main/java/com/github/App.java",
104+
Line: 13,
105+
Column: 30,
106+
Text: ` System.out.println( "Hello World!" );`,
64107
},
65108
}
66-
results, err := testrepo.Grep(pattern, GrepOptions{IgnoreCase: true})
67-
assert.NoError(t, err)
68-
for i, result := range results {
69-
assert.Equal(t, expect[i], *result)
70-
}
109+
got := testrepo.Grep("Hello", GrepOptions{IgnoreCase: true})
110+
assert.Equal(t, want, got)
71111
}
72112

73-
func TestRepoGrepRegex(t *testing.T) {
74-
pattern := "Hello\\sW\\w+"
75-
expect := []GrepResult{
113+
func TestRepository_Grep_ExtendedRegexp(t *testing.T) {
114+
want := []*GrepResult{
76115
{
77-
TreeID: "HEAD", Path: "src/main/java/com/github/App.java", Line: 13, Column: 30, Text: ` System.out.println( "Hello World!" );`,
116+
Tree: "HEAD",
117+
Path: "src/main/java/com/github/App.java",
118+
Line: 5,
119+
Column: 4,
120+
Text: ` * Hello world!`,
78121
},
79122
}
80-
results, err := testrepo.Grep(pattern, GrepOptions{ExtendedRegex: true})
81-
assert.NoError(t, err)
82-
for i, result := range results {
83-
assert.Equal(t, expect[i], *result)
84-
}
123+
got := testrepo.Grep(`Hello \w+`, GrepOptions{ExtendedRegexp: true})
124+
assert.Equal(t, want, got)
85125
}
86126

87-
func TestRepoGrepWord(t *testing.T) {
88-
pattern := "Hello\\sW\\w+"
89-
expect := []GrepResult{
127+
func TestRepository_Grep_WordRegexp(t *testing.T) {
128+
want := []*GrepResult{
90129
{
91-
TreeID: "HEAD", Path: "src/main/java/com/github/App.java", Line: 13, Column: 36, Text: ` System.out.println( "Hello World!" );`,
130+
Tree: "HEAD",
131+
Path: "src/main/java/com/github/App.java",
132+
Line: 5,
133+
Column: 10,
134+
Text: ` * Hello world!`,
92135
},
93136
}
94-
results, err := testrepo.Grep(pattern, GrepOptions{WordMatch: true})
95-
assert.NoError(t, err)
96-
for i, result := range results {
97-
assert.Equal(t, expect[i], *result)
98-
}
137+
got := testrepo.Grep("world", GrepOptions{WordRegexp: true})
138+
assert.Equal(t, want, got)
99139
}

0 commit comments

Comments
 (0)