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
3 changes: 2 additions & 1 deletion client/clienter.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import (

type Clienter interface {
Add(ctx context.Context, dirs []string, name, baseURL string) error
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, args ...string) error
GetDirs(ctx context.Context, dir string) ([]string, error)
GetLogins(ctx context.Context) ([]string, error)
GetRepos(ctx context.Context, name string) ([]*github.Repository, error)
Branches(ctx context.Context, repoDirs []string, args ...string) error
ListTags(ctx context.Context, repoDirs []string, args ...string) error
PullRepos(ctx context.Context, repoDirs []string, args ...string) error
PushRepos(ctx context.Context, repoDirs []string, args ...string) error
Expand Down
45 changes: 45 additions & 0 deletions client/repos_diff.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package client

import (
"bytes"
"context"
"fmt"
"os/exec"
"strings"
)

func (c *Client) DiffRepos(ctx context.Context, dirs []string, args ...string) error {
args = append([]string{"diff"}, args...)

c.scrb.BeginDescribe("Command")
defer c.scrb.EndDescribe()

c.scrb.Print(fmt.Sprintf("git %s", strings.Join(args, " ")))

c.scrb.BeginDescribe("directories")
defer c.scrb.EndDescribe()

for _, dir := range dirs {
out := &bytes.Buffer{}
errout := &bytes.Buffer{}

cmd := exec.CommandContext(ctx, "git", args...)
cmd.Stdout = out
cmd.Stderr = errout
cmd.Dir = dir

err := cmd.Run()

c.scrb.BeginDescribe(dir)
if err != nil {
c.scrb.Error(err)
c.scrb.PrintLines(errout)
} else {
c.scrb.PrintLines(out)
}

c.scrb.EndDescribe()
}

return nil
}
6 changes: 6 additions & 0 deletions client/testclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,9 @@ func (c *TestClient) TagRepos(ctx context.Context, repoDirs []string, args ...st

return nil
}

func (c *TestClient) DiffRepos(ctx context.Context, repoDirs []string, args ...string) error {
c.CommandsCalled = append(c.CommandsCalled, "DiffRepos")

return nil
}
58 changes: 58 additions & 0 deletions cmd/diff.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package cmd

import (
"context"
"fmt"

"github.com/spf13/cobra"
)

var (
short bool
nameOnly bool
)

func init() {
RootCmd.AddCommand(diffCmd)

diffCmd.Flags().StringVar(&dir, "dir", ".", "directory to diff repos from")
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")
}

var diffCmd = &cobra.Command{
Use: "diff [flags] <branch|tag> <branch|tag>",
Short: "Diff all repos in a directory",
Long: `Diff all repos in a directory. Since commit hashes would not be the same between multiple repos,
this command really only makes sense when used with two branch names or two tags.`,
Args: cobra.ExactArgs(2),
PersistentPreRun: setupClient,
RunE: diffFunc,
}

func diffFunc(cmd *cobra.Command, args []string) error {
ctx := context.Background()

repoDirs, err := clt.GetDirs(ctx, dir)
if err != nil {
cmd.SilenceUsage = true
return fmt.Errorf("get dirs: %w", err)
}

switch {
case short:
args = append(args, "--shortstat")
case nameOnly:
args = append(args, "--name-only")
}

err = clt.DiffRepos(ctx, repoDirs, args...)
if err != nil {
cmd.SilenceUsage = true
return fmt.Errorf("diff repos: %w", err)
}

return nil
}
Loading