Skip to content

Commit

Permalink
Merge pull request #6 from gomicro/system-git
Browse files Browse the repository at this point in the history
system git
  • Loading branch information
dan9186 authored Apr 15, 2023
2 parents 31828f9 + c0077a4 commit 84ce4bd
Show file tree
Hide file tree
Showing 227 changed files with 129 additions and 42,154 deletions.
14 changes: 7 additions & 7 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ jobs:

steps:
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: golangci-lint
uses: golangci/golangci-lint-action@v2
with:
version: v1.45
version: v1.52

test:
name: Test
Expand All @@ -26,10 +26,10 @@ jobs:
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: 1.18
go-version: "1.20"

- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v3
with:
fetch-depth: 0

Expand All @@ -50,14 +50,14 @@ jobs:
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: 1.18
go-version: "1.20"

- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Login to Docker Hub
- name: Login to Docker Registry
uses: docker/login-action@v1
with:
registry: ghcr.io
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ align
/dist
.DS_Store
/download_test
/git_test

# Generated Files
coverage.txt
4 changes: 3 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,20 @@ run:

linters:
enable:
- deadcode
- errcheck
- exportloopref
- gofmt
- gosimple
- govet
- ineffassign
- misspell
- rowserrcheck
- sqlclosecheck
- staticcheck
- structcheck
- typecheck
- unused
- unparam
- varcheck
disable:
- wsl
Expand Down
6 changes: 4 additions & 2 deletions client/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ func WithRepos(ctx context.Context, repos []*github.Repository) context.Context
}

func RepoMap(ctx context.Context) (map[string][]*repository, error) {
repoMap, ok := ctx.Value(reposContextKey).(map[string][]*repository)
v := ctx.Value(reposContextKey)
repoMap, ok := v.(map[string][]*repository)
if !ok {
return nil, ErrNotFoundInContext
}
Expand All @@ -38,7 +39,8 @@ func WithExcludes(ctx context.Context, repos []*repository) context.Context {
}

func Excludes(ctx context.Context) ([]*repository, error) {
excludes, ok := ctx.Value(excludesContextKey).([]*repository)
v := ctx.Value(excludesContextKey)
excludes, ok := v.([]*repository)
if !ok {
return nil, nil
}
Expand Down
57 changes: 22 additions & 35 deletions client/repos_checkout.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
package client

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

"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/gosuri/uiprogress"
)

func (c *Client) CheckoutRepos(ctx context.Context, dirs []string, branch string) error {
var (
ErrUnstagedChanges = errors.New("unstanged changes")
)

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

currRepo := ""
bar := uiprogress.AddBar(count).
Expand All @@ -26,21 +31,26 @@ func (c *Client) CheckoutRepos(ctx context.Context, dirs []string, branch string
})

unstagedRepos := []string{}
for i := range dirs {
currRepo = fmt.Sprintf("\nCurrent Repo: %v", dirs[i])
err := c.CheckoutRepo(ctx, dirs[i], branch)
if err != nil {
if errors.Is(err, git.ErrUnstagedChanges) {
unstagedRepos = append(unstagedRepos, dirs[i])
continue
}
for _, dir := range dirs {
currRepo = fmt.Sprintf("\nCurrent Repo: %v", dir)

cmd := exec.CommandContext(ctx, "git", args...)

if errors.Is(err, plumbing.ErrReferenceNotFound) {
buf := bytes.Buffer{}
cmd.Stdout = &buf

cmd.Dir = dir

err := cmd.Run()
if err != nil {
if errors.Is(err, ErrUnstagedChanges) {
unstagedRepos = append(unstagedRepos, dir)
continue
}

return fmt.Errorf("checkout repo: %w", err)
}

bar.Incr()
}

Expand All @@ -52,26 +62,3 @@ func (c *Client) CheckoutRepos(ctx context.Context, dirs []string, branch string

return nil
}

func (c *Client) CheckoutRepo(ctx context.Context, dir, branch string) error {
r, err := git.PlainOpen(dir)
if err != nil {
return fmt.Errorf("open dir: %w: %s", err, dir)
}

w, err := r.Worktree()
if err != nil {
return fmt.Errorf("worktree: %w", err)
}

opts := &git.CheckoutOptions{
Branch: plumbing.NewBranchReferenceName(branch),
}

err = w.Checkout(opts)
if err != nil {
return fmt.Errorf("checkout: %w: %s", err, dir)
}

return nil
}
47 changes: 12 additions & 35 deletions client/repos_clone.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package client

import (
"bytes"
"context"
"errors"
"fmt"
"os"
"os/exec"
"path"
"strings"

"github.com/go-git/go-git/v5"
"github.com/gosuri/uiprogress"
)

Expand All @@ -24,7 +22,7 @@ func (c *Client) CloneRepos(ctx context.Context) ([]*repository, error) {
}

count := 0
for rs := range dirRepos {
for _, rs := range dirRepos {
count += len(rs)
}

Expand All @@ -44,10 +42,18 @@ func (c *Client) CloneRepos(ctx context.Context) ([]*repository, error) {
for dir, rs := range dirRepos {
for i := range rs {
currRepo = fmt.Sprintf("\nCurrent Repo: %v/%v", dir, rs[i].name)
err := c.CloneRepo(ctx, dir, rs[i].name, rs[i].url, false)

dest := path.Join(".", dir, rs[i].name)
cmd := exec.CommandContext(ctx, "git", "clone", rs[i].url, dest)

buf := bytes.Buffer{}
cmd.Stdout = &buf

err := cmd.Run()
if err != nil {
errs = fmt.Errorf("%w; ", fmt.Errorf("clone repo: %w", err))
}

cloned = append(cloned, rs[i])
bar.Incr()
}
Expand All @@ -61,32 +67,3 @@ func (c *Client) CloneRepos(ctx context.Context) ([]*repository, error) {

return cloned, nil
}

// CloneRepo takes a context, base directory to clone individual repos into, the
// name to call the repo, the url to clone the repo from, and a boolean to show
// the output. It attempts to clone the repo into the directory structure of
// "baseDir/name". If the repo already exists it will skip it, and otherwise
// returns any errors it encounters.
func (c *Client) CloneRepo(ctx context.Context, baseDir, name, url string, show bool) error {
dir := path.Join(".", baseDir, name)

opts := &git.CloneOptions{
URL: url,
Auth: c.ghHTTPSAuth,
}

if show {
opts.Progress = os.Stdout
}

if strings.HasPrefix(url, "git@") {
opts.Auth = c.ghSSHAuth
}

_, err := git.PlainCloneContext(ctx, dir, false, opts)
if err != nil && !errors.Is(err, git.ErrRepositoryAlreadyExists) {
return fmt.Errorf("plain clone: %w", err)
}

return nil
}
43 changes: 14 additions & 29 deletions client/repos_pull.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package client

import (
"bytes"
"context"
"errors"
"fmt"
"os/exec"

"github.com/go-git/go-git/v5"
"github.com/gosuri/uiprogress"
)

Expand All @@ -23,40 +23,25 @@ func (c *Client) PullRepos(ctx context.Context, dirs []string) error {
return currRepo
})

for i := range dirs {
currRepo = fmt.Sprintf("\nCurrent Repo: %v", dirs[i])
err := c.PullRepo(ctx, dirs[i])
if err != nil {
return fmt.Errorf("pull repo: %w", err)
}
bar.Incr()
}
for _, dir := range dirs {
currRepo = fmt.Sprintf("\nCurrent Repo: %v", dir)

currRepo = ""
cmd := exec.CommandContext(ctx, "git", "pull")

return nil
}
buf := bytes.Buffer{}
cmd.Stdout = &buf

func (c *Client) PullRepo(ctx context.Context, dir string) error {
r, err := git.PlainOpen(dir)
if err != nil {
return fmt.Errorf("open dir: %w", err)
}
cmd.Dir = dir

w, err := r.Worktree()
if err != nil {
return fmt.Errorf("worktree: %w", err)
}
err := cmd.Run()
if err != nil {
return fmt.Errorf("pull repo: %w", err)
}

opts := &git.PullOptions{
RemoteName: "origin",
Auth: c.ghSSHAuth,
bar.Incr()
}

err = w.PullContext(ctx, opts)
if err != nil && !errors.Is(err, git.NoErrAlreadyUpToDate) {
return fmt.Errorf("pull: %w", err)
}
currRepo = ""

return nil
}
4 changes: 1 addition & 3 deletions cmd/checkout.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ var checkoutCmd = &cobra.Command{
func checkoutFunc(cmd *cobra.Command, args []string) error {
ctx := context.Background()

branch := args[0]

dir := "."
if len(args) > 1 {
dir = args[1]
Expand All @@ -35,7 +33,7 @@ func checkoutFunc(cmd *cobra.Command, args []string) error {
return fmt.Errorf("get dirs: %w", err)
}

err = clt.CheckoutRepos(ctx, repoDirs, branch)
err = clt.CheckoutRepos(ctx, repoDirs, args)
if err != nil {
return fmt.Errorf("checkout repos: %w", err)
}
Expand Down
12 changes: 1 addition & 11 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import (
)

var (
clt *client.Client
dryRun bool
clt *client.Client
)

func init() {
Expand All @@ -30,19 +29,12 @@ func init() {
)

rootCmd.PersistentFlags().BoolP("verbose", "v", false, "show more verbose output")
rootCmd.PersistentFlags().BoolP("dryRun", "d", false, "attempt the specified command without actually making live changes")

err := viper.BindPFlag("verbose", rootCmd.PersistentFlags().Lookup("verbose"))
if err != nil {
fmt.Printf("Error setting up: %v\n", err.Error())
os.Exit(1)
}

err = viper.BindPFlag("dryRun", rootCmd.PersistentFlags().Lookup("dryRun"))
if err != nil {
fmt.Printf("Error setting up: %v\n", err.Error())
os.Exit(1)
}
}

func initEnvs() {
Expand Down Expand Up @@ -74,6 +66,4 @@ func setupClient(cmd *cobra.Command, args []string) {
fmt.Printf("Error: %v", err.Error())
os.Exit(1)
}

dryRun = viper.GetBool("dryRun")
}
Loading

0 comments on commit 84ce4bd

Please sign in to comment.