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 @@ -13,7 +13,7 @@ type Clienter interface {
GetDirs(ctx context.Context, dir string) ([]string, error)
GetLogins(ctx context.Context) ([]string, error)
GetRepos(ctx context.Context, name string) ([]*github.Repository, error)
ListBranches(ctx context.Context, repoDirs []string, args ...string) 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
68 changes: 68 additions & 0 deletions client/repos_branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package client
import (
"bytes"
"context"
"fmt"
"os/exec"
"strings"

"github.com/gosuri/uiprogress"
)

func (c *Client) ListBranches(ctx context.Context, dirs []string, args ...string) error {
Expand Down Expand Up @@ -41,3 +44,68 @@ func (c *Client) ListBranches(ctx context.Context, dirs []string, args ...string

return nil
}

func (c *Client) Branches(ctx context.Context, dirs []string, args ...string) error {
count := len(dirs)
args = append([]string{"branch"}, args...)

verbose := Verbose(ctx)

var bar *uiprogress.Bar
currRepo := ""

if verbose {
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()
} else {
bar = uiprogress.AddBar(count).
AppendCompleted().
PrependElapsed().
PrependFunc(func(b *uiprogress.Bar) string {
return fmt.Sprintf("Branches (%d/%d)", b.Current(), count)
}).
AppendFunc(func(b *uiprogress.Bar) string {
return currRepo
})
}

for _, dir := range dirs {
currRepo = fmt.Sprintf("\nCurrent Repo: %v", dir)

out := &bytes.Buffer{}
errout := &bytes.Buffer{}

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

err := cmd.Run()
if err != nil && !verbose {
return fmt.Errorf("branch: %w", err) // TODO: collect errors and return them all
}

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

c.scrb.EndDescribe()
} else {
bar.Incr()
}
}

currRepo = ""

return nil
}
4 changes: 2 additions & 2 deletions client/testclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ func (c *TestClient) GetRepos(ctx context.Context, name string) ([]*github.Repos
return nil, nil
}

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

return nil
}
Expand Down
33 changes: 13 additions & 20 deletions cmd/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ func init() {

branchCmd.Flags().BoolVarP(&all, "all", "a", false, "list all branches")
branchCmd.Flags().BoolVarP(&del, "delete", "d", false, "delete the branch from the repos")
branchCmd.Flags().BoolVar(&delForce, "D", false, "force delete the branch from the repos")
branchCmd.Flags().BoolVarP(&delForce, "force-delete", "D", false, "force delete the branch from the repos")
branchCmd.Flags().BoolVarP(&force, "force", "f", false, "force the desired action")

branchCmd.MarkFlagsMutuallyExclusive("all", "delete", "D")
branchCmd.MarkFlagsMutuallyExclusive("all", "delete", "force-delete")
}

var branchCmd = &cobra.Command{
Expand Down Expand Up @@ -65,34 +65,27 @@ func branchFunc(cmd *cobra.Command, args []string) error {

args = append(args, name)

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

return nil
}

if all {
args = append(args, "--all")
}

return listBranches(ctx, cmd, args, repoDirs)
}
// This must be verbose to show anything
ctx = client.WithVerbose(ctx, true)

func listBranches(ctx context.Context, cmd *cobra.Command, args []string, repoDirs []string) error {
err := clt.ListBranches(ctx, repoDirs, args...)
err = clt.Branches(ctx, repoDirs, args...)
if err != nil {
cmd.SilenceUsage = true
return fmt.Errorf("list branches: %w", err)
return fmt.Errorf("list: %w", err)
}

return nil
}

func deleteBranch(ctx context.Context, cmd *cobra.Command, args []string, repoDirs []string) error {
/*
err := clt.DeleteBranch(ctx, repoDirs, args...)
if err != nil {
cmd.SilenceUsage = true
return fmt.Errorf("delete branch: %w", err)
}
*/

return nil
}
Loading