diff --git a/client/clienter.go b/client/clienter.go index 4393071..ddbc491 100644 --- a/client/clienter.go +++ b/client/clienter.go @@ -11,7 +11,7 @@ type Clienter interface { Branches(ctx context.Context, repoDirs []string, args ...string) error CheckoutRepos(ctx context.Context, repoDirs []string, args ...string) error CloneRepos(ctx context.Context, dir string) ([]*Repository, error) - DiffRepos(ctx context.Context, repoDirs []string, ignoreEmtpy bool, args ...string) error + DiffRepos(ctx context.Context, repoDirs []string, cfg *DiffConfig) error GetDirs(ctx context.Context, dir string) ([]string, error) GetLogins(ctx context.Context) ([]string, error) GetRepos(ctx context.Context, name string) ([]*github.Repository, error) diff --git a/client/repos_diff.go b/client/repos_diff.go index e1da72a..935d5a1 100644 --- a/client/repos_diff.go +++ b/client/repos_diff.go @@ -1,6 +1,7 @@ package client import ( + "bufio" "bytes" "context" "fmt" @@ -8,8 +9,14 @@ import ( "strings" ) -func (c *Client) DiffRepos(ctx context.Context, dirs []string, ignoreEmtpy bool, args ...string) error { - args = append([]string{"diff"}, args...) +type DiffConfig struct { + IgnoreEmpty bool + IgnoreFilePrefix []string + Args []string +} + +func (c *Client) DiffRepos(ctx context.Context, dirs []string, cfg *DiffConfig) error { + args := append([]string{"diff"}, cfg.Args...) c.scrb.BeginDescribe("Command") defer c.scrb.EndDescribe() @@ -30,7 +37,10 @@ func (c *Client) DiffRepos(ctx context.Context, dirs []string, ignoreEmtpy bool, err := cmd.Run() - if ignoreEmtpy && out.Len() == 0 && err == nil { + // filter first to have empty check accurate + out = filterLines(out, cfg.IgnoreFilePrefix) + + if cfg.IgnoreEmpty && out.Len() == 0 && err == nil { continue } @@ -47,3 +57,30 @@ func (c *Client) DiffRepos(ctx context.Context, dirs []string, ignoreEmtpy bool, return nil } + +func filterLines(buf *bytes.Buffer, prefixes []string) *bytes.Buffer { + if len(prefixes) == 0 { + return buf + } + + scanner := bufio.NewScanner(buf) + out := &bytes.Buffer{} + + for scanner.Scan() { + line := scanner.Text() + + ignore := false + for _, prefix := range prefixes { + if strings.HasPrefix(line, prefix) { + ignore = true + break + } + } + + if !ignore { + out.WriteString(line + "\n") + } + } + + return out +} diff --git a/client/testclient/client.go b/client/testclient/client.go index dc8d9a8..6757a47 100644 --- a/client/testclient/client.go +++ b/client/testclient/client.go @@ -99,7 +99,7 @@ func (c *TestClient) TagRepos(ctx context.Context, repoDirs []string, args ...st return nil } -func (c *TestClient) DiffRepos(ctx context.Context, repoDirs []string, ignoreEmtpy bool, args ...string) error { +func (c *TestClient) DiffRepos(ctx context.Context, repoDirs []string, cfg *client.DiffConfig) error { c.CommandsCalled = append(c.CommandsCalled, "DiffRepos") return nil diff --git a/cmd/diff.go b/cmd/diff.go index 453b44d..85384b5 100644 --- a/cmd/diff.go +++ b/cmd/diff.go @@ -4,13 +4,15 @@ import ( "context" "fmt" + "github.com/gomicro/align/client" "github.com/spf13/cobra" ) var ( - short bool - nameOnly bool - ignoreEmtpy bool + short bool + nameOnly bool + ignoreEmtpy bool + ignoreFilePrefix []string ) func init() { @@ -19,11 +21,13 @@ 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().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") diffCmd.MarkFlagsMutuallyExclusive("shortstat", "name-only") + diffCmd.MarkFlagsMutuallyExclusive("shortstat", "ignore-file-prefix") } var diffCmd = &cobra.Command{ @@ -52,7 +56,13 @@ func diffFunc(cmd *cobra.Command, args []string) error { args = append(args, "--name-only") } - err = clt.DiffRepos(ctx, repoDirs, ignoreEmtpy, args...) + cfg := &client.DiffConfig{ + IgnoreEmpty: ignoreEmtpy, + IgnoreFilePrefix: ignoreFilePrefix, + Args: args, + } + + err = clt.DiffRepos(ctx, repoDirs, cfg) if err != nil { cmd.SilenceUsage = true return fmt.Errorf("diff repos: %w", err)