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
2 changes: 1 addition & 1 deletion client/clienter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
43 changes: 40 additions & 3 deletions client/repos_diff.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
package client

import (
"bufio"
"bytes"
"context"
"fmt"
"os/exec"
"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()
Expand All @@ -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
}

Expand All @@ -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
}
2 changes: 1 addition & 1 deletion client/testclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 14 additions & 4 deletions cmd/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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{
Expand Down Expand Up @@ -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)
Expand Down
Loading