From 418b730b793c50de4df5b9a4590df643a9150ee1 Mon Sep 17 00:00:00 2001 From: dan9186 Date: Fri, 21 Nov 2025 11:38:28 -0800 Subject: [PATCH] add ability to match file extensions --- client/repos_diff.go | 36 ++++++++++++++++++++++++++++++++++++ cmd/diff.go | 7 +++++-- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/client/repos_diff.go b/client/repos_diff.go index 935d5a1..26d5616 100644 --- a/client/repos_diff.go +++ b/client/repos_diff.go @@ -12,6 +12,7 @@ import ( type DiffConfig struct { IgnoreEmpty bool IgnoreFilePrefix []string + MatchExtension []string Args []string } @@ -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) @@ -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 +} diff --git a/cmd/diff.go b/cmd/diff.go index 85384b5..0dd231c 100644 --- a/cmd/diff.go +++ b/cmd/diff.go @@ -13,6 +13,7 @@ var ( nameOnly bool ignoreEmtpy bool ignoreFilePrefix []string + matchExtension []string ) func init() { @@ -20,9 +21,10 @@ func init() { 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") @@ -59,6 +61,7 @@ func diffFunc(cmd *cobra.Command, args []string) error { cfg := &client.DiffConfig{ IgnoreEmpty: ignoreEmtpy, IgnoreFilePrefix: ignoreFilePrefix, + MatchExtension: matchExtension, Args: args, }