Skip to content

Commit 678780e

Browse files
authored
Merge pull request #36 from gomicro/show-only-files
add ability to match file extensions
2 parents 9a0d2ee + 418b730 commit 678780e

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

client/repos_diff.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
type DiffConfig struct {
1313
IgnoreEmpty bool
1414
IgnoreFilePrefix []string
15+
MatchExtension []string
1516
Args []string
1617
}
1718

@@ -37,6 +38,8 @@ func (c *Client) DiffRepos(ctx context.Context, dirs []string, cfg *DiffConfig)
3738

3839
err := cmd.Run()
3940

41+
out = matchExtensions(out, cfg.MatchExtension)
42+
4043
// filter first to have empty check accurate
4144
out = filterLines(out, cfg.IgnoreFilePrefix)
4245

@@ -84,3 +87,36 @@ func filterLines(buf *bytes.Buffer, prefixes []string) *bytes.Buffer {
8487

8588
return out
8689
}
90+
91+
func matchExtensions(buf *bytes.Buffer, extensions []string) *bytes.Buffer {
92+
if len(extensions) == 0 {
93+
return buf
94+
}
95+
96+
for i := range extensions {
97+
if !strings.HasPrefix(extensions[i], ".") {
98+
extensions[i] = "." + extensions[i]
99+
}
100+
}
101+
102+
scanner := bufio.NewScanner(buf)
103+
out := &bytes.Buffer{}
104+
105+
for scanner.Scan() {
106+
line := scanner.Text()
107+
108+
matched := false
109+
for _, ext := range extensions {
110+
if strings.HasSuffix(line, ext) {
111+
matched = true
112+
break
113+
}
114+
}
115+
116+
if matched {
117+
out.WriteString(line + "\n")
118+
}
119+
}
120+
121+
return out
122+
}

cmd/diff.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,18 @@ var (
1313
nameOnly bool
1414
ignoreEmtpy bool
1515
ignoreFilePrefix []string
16+
matchExtension []string
1617
)
1718

1819
func init() {
1920
RootCmd.AddCommand(diffCmd)
2021

2122
diffCmd.Flags().StringVar(&dir, "dir", ".", "directory to diff repos from")
2223

23-
diffCmd.Flags().BoolVar(&ignoreEmtpy, "ignore-empty", false, "ignore empty diffs")
24-
diffCmd.Flags().StringArrayVar(&ignoreFilePrefix, "ignore-file-prefix", []string{}, "ignore files in diffs with the given prefix")
24+
diffCmd.Flags().StringArrayVar(&ignoreFilePrefix, "ignore-file-prefix", []string{}, "ignore files in diffs with the given prefix(es)")
25+
diffCmd.Flags().StringArrayVar(&matchExtension, "match-extension", []string{}, "only include files in diffs with the given extension(s)")
2526

27+
diffCmd.Flags().BoolVar(&ignoreEmtpy, "ignore-empty", false, "ignore empty diffs")
2628
diffCmd.Flags().BoolVar(&short, "shortstat", false, "show only the number of changed files, insertions, and deletions")
2729
diffCmd.Flags().BoolVar(&nameOnly, "name-only", false, "show only names of changed files")
2830

@@ -59,6 +61,7 @@ func diffFunc(cmd *cobra.Command, args []string) error {
5961
cfg := &client.DiffConfig{
6062
IgnoreEmpty: ignoreEmtpy,
6163
IgnoreFilePrefix: ignoreFilePrefix,
64+
MatchExtension: matchExtension,
6265
Args: args,
6366
}
6467

0 commit comments

Comments
 (0)