Skip to content

Commit

Permalink
don't clone repos that already exists
Browse files Browse the repository at this point in the history
  • Loading branch information
dan9186 committed Apr 30, 2024
1 parent cdb9296 commit fd80325
Showing 1 changed file with 39 additions and 8 deletions.
47 changes: 39 additions & 8 deletions client/repos_clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"fmt"
"os"
"os/exec"
"path"

Expand Down Expand Up @@ -42,19 +43,36 @@ 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)

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()
exists, err := dirExists(dest)
if err != nil {
errs = fmt.Errorf("%w; ", fmt.Errorf("clone repo: %w", err))
errs = fmt.Errorf("repo dir: %w; ", err)
continue
}

if exists {
exists, err = dirExists(path.Join(dest, ".git"))
if err != nil {
errs = fmt.Errorf("repo git dir: %w; ", err)
continue
}
}

if !exists {
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])
}

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

return cloned, nil
}

func dirExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}

if os.IsNotExist(err) {
return false, nil
}

return false, fmt.Errorf("exists check: %w", err)
}

0 comments on commit fd80325

Please sign in to comment.