Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions client/repos_diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
type DiffConfig struct {
IgnoreEmpty bool
IgnoreFilePrefix []string
MatchExtension []string
Args []string
}

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

err := cmd.Run()

out = matchExtensions(out, cfg.MatchExtension)

// filter first to have empty check accurate
out = filterLines(out, cfg.IgnoreFilePrefix)

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

return out
}

func matchExtensions(buf *bytes.Buffer, extensions []string) *bytes.Buffer {
if len(extensions) == 0 {
return buf
}

for i := range extensions {
if !strings.HasPrefix(extensions[i], ".") {
extensions[i] = "." + extensions[i]
}
}

scanner := bufio.NewScanner(buf)
out := &bytes.Buffer{}

for scanner.Scan() {
line := scanner.Text()

matched := false
for _, ext := range extensions {
if strings.HasSuffix(line, ext) {
matched = true
break
}
}

if matched {
out.WriteString(line + "\n")
}
}

return out
}
7 changes: 5 additions & 2 deletions cmd/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,18 @@ var (
nameOnly bool
ignoreEmtpy bool
ignoreFilePrefix []string
matchExtension []string
)

func init() {
RootCmd.AddCommand(diffCmd)

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

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

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

Expand Down Expand Up @@ -59,6 +61,7 @@ func diffFunc(cmd *cobra.Command, args []string) error {
cfg := &client.DiffConfig{
IgnoreEmpty: ignoreEmtpy,
IgnoreFilePrefix: ignoreFilePrefix,
MatchExtension: matchExtension,
Args: args,
}

Expand Down
Loading