Skip to content

Commit

Permalink
removed all chdirs
Browse files Browse the repository at this point in the history
  • Loading branch information
cooperspencer committed Apr 2, 2024
1 parent 3d736fe commit c5e5cee
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 74 deletions.
53 changes: 14 additions & 39 deletions gitcmd/gitcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,82 +22,62 @@ func New() (GitCmd, error) {
return GitCmd{CMD: "git"}, nil
}

func (g GitCmd) Clone(url, path string, bare bool) error {
cmd := exec.Command(g.CMD, "clone", url, path)
func (g GitCmd) Clone(url, reponame string, bare bool) error {
cmd := exec.Command(g.CMD, "clone", url, reponame)
if bare {
cmd.Args = append(cmd.Args, "--bare")
}
return cmd.Run()
}

func (g GitCmd) Pull(bare bool) error {
func (g GitCmd) Pull(bare bool, repopath string) error {
var args = []string{}
if bare {
args = []string{"fetch", "--all"}
args = []string{"-C", repopath, "fetch", "--all"}
} else {
args = []string{"pull", "--all"}
args = []string{"-C", repopath, "pull", "--all"}
}
cmd := exec.Command(g.CMD, args...)
return cmd.Run()
}

func (g GitCmd) Fetch(path string) error {
currentpath, err := os.Getwd()
_, err := os.Stat(path)
if err != nil {
return err
}
defer os.Chdir(currentpath)
err = os.Chdir(path)
if err != nil {
return err
}
args := []string{"fetch", "--all", "--tags"}
args := []string{"-C", path, "fetch", "--all", "--tags"}
cmd := exec.Command(g.CMD, args...)
return cmd.Run()
}

func (g GitCmd) MirrorPull(path string) error {
currentpath, err := os.Getwd()
if err != nil {
return err
}
defer os.Chdir(currentpath)
err = os.Chdir(path)
_, err := os.Stat(path)
if err != nil {
return err
}
args := []string{"pull", "--all", "--tags"}
args := []string{"-C", path, "pull", "--all", "--tags"}
cmd := exec.Command(g.CMD, args...)
return cmd.Run()
}

func (g GitCmd) NewRemote(name, url, path string) error {
currentpath, err := os.Getwd()
_, err := os.Stat(path)
if err != nil {
return err
}
defer os.Chdir(currentpath)
err = os.Chdir(path)
if err != nil {
return err
}
args := []string{"remote", "add", name, url}
args := []string{"-C", path, "remote", "add", name, url}
cmd := exec.Command(g.CMD, args...)

return cmd.Run()
}

func (g GitCmd) Push(path, remote string) error {
currentpath, err := os.Getwd()
if err != nil {
return err
}
defer os.Chdir(currentpath)
err = os.Chdir(path)
_, err := os.Stat(path)
if err != nil {
return err
}
args := []string{"push", "--all", remote}
args := []string{"-C", path, "push", "--all", remote}
cmd := exec.Command(g.CMD, args...)

output, _ := cmd.CombinedOutput()
Expand All @@ -112,12 +92,7 @@ func (g GitCmd) Push(path, remote string) error {
}

func (g GitCmd) Checkout(path, branch string) error {
currentpath, err := os.Getwd()
if err != nil {
return err
}
defer os.Chdir(currentpath)
err = os.Chdir(path)
_, err := os.Stat(path)
if err != nil {
return err
}
Expand Down
83 changes: 48 additions & 35 deletions local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ var (
// Locally TODO.
func Locally(repo types.Repo, l types.Local, dry bool) bool {
sub = logger.CreateSubLogger("stage", "locally", "path", l.Path)
originPath, _ := os.Getwd()
if l.LFS {
g, err := gitcmd.New()
if err != nil {
Expand All @@ -59,23 +58,21 @@ func Locally(repo types.Repo, l types.Local, dry bool) bool {
repo.Name = path.Join(repo.Name, fmt.Sprint(date.Unix()))
}

stat, err := os.Stat(l.Path)
_, err := os.Stat(l.Path)
if os.IsNotExist(err) && !dry {
if err := os.MkdirAll(l.Path, 0o777); err != nil {
if err = os.MkdirAll(l.Path, 0o777); err != nil {
sub.Error().
Msg(err.Error())
return false
}

stat, _ = os.Stat(l.Path)
_, err = os.Stat(l.Path)
}

if stat != nil && stat.IsDir() {
if err := os.Chdir(l.Path); err != nil {
sub.Error().
Msg(err.Error())
return false
}
if err != nil {
sub.Error().
Msg(err.Error())
return false
}

tries := 5
Expand Down Expand Up @@ -108,7 +105,7 @@ func Locally(repo types.Repo, l types.Local, dry bool) bool {
}

for x := 1; x <= tries; x++ {
stat, err := os.Stat(repo.Name)
stat, err := os.Stat(filepath.Join(l.Path, repo.Name))
if os.IsNotExist(err) {
sub.Info().
Msgf("cloning %s", types.Green(repo.Name))
Expand Down Expand Up @@ -145,9 +142,9 @@ func Locally(repo types.Repo, l types.Local, dry bool) bool {
break
}

err = os.RemoveAll(repo.Name)
err = os.RemoveAll(filepath.Join(l.Path, repo.Name))
if err != nil {
dir, _ := filepath.Abs(repo.Name)
dir, _ := filepath.Abs(filepath.Join(l.Path, repo.Name))
sub.Warn().
Str("repo", repo.Name).Err(err).
Msgf("couldn't remove %s", types.Red(dir))
Expand Down Expand Up @@ -179,9 +176,9 @@ func Locally(repo types.Repo, l types.Local, dry bool) bool {
sub.Warn().
Str("repo", repo.Name).
Msg(err.Error())
err = os.RemoveAll(repo.Name)
err = os.RemoveAll(filepath.Join(l.Path, repo.Name))
if err != nil {
dir, _ := filepath.Abs(repo.Name)
dir, _ := filepath.Abs(filepath.Join(l.Path, repo.Name))
sub.Warn().
Str("repo", repo.Name).Err(err).
Msgf("couldn't remove %s", types.Red(dir))
Expand All @@ -192,9 +189,9 @@ func Locally(repo types.Repo, l types.Local, dry bool) bool {
Str("repo", repo.Name).Err(err).
Msgf("retry %s from %s", types.Red(x), types.Red(tries))

err = os.RemoveAll(repo.Name)
err = os.RemoveAll(filepath.Join(l.Path, repo.Name))
if err != nil {
dir, _ := filepath.Abs(repo.Name)
dir, _ := filepath.Abs(filepath.Join(l.Path, repo.Name))
sub.Warn().
Str("repo", repo.Name).Err(err).
Msgf("couldn't remove %s", types.Red(dir))
Expand All @@ -210,14 +207,14 @@ func Locally(repo types.Repo, l types.Local, dry bool) bool {
}

if len(repo.Issues) > 0 {
_, err := os.Stat(fmt.Sprintf("%s.issues", repo.Name))
_, err := os.Stat(filepath.Join(l.Path, fmt.Sprintf("%s.issues", repo.Name)))
if os.IsNotExist(err) && !dry {
if err := os.MkdirAll(fmt.Sprintf("%s.issues", repo.Name), 0o777); err != nil {
sub.Error().
Msg(err.Error())
}
}
issuesDir, err := filepath.Abs(fmt.Sprintf("%s.issues", repo.Name))
issuesDir, err := filepath.Abs(filepath.Join(l.Path, fmt.Sprintf("%s.issues", repo.Name)))
if err != nil {
sub.Error().
Msg(err.Error())
Expand All @@ -242,14 +239,14 @@ func Locally(repo types.Repo, l types.Local, dry bool) bool {
}

if l.Zip {
tozip := []string{repo.Name}
tozip := []string{filepath.Join(l.Path, repo.Name)}

if len(repo.Issues) > 0 {
tozip = append(tozip, fmt.Sprintf("%s.issues", repo.Name))
}
sub.Info().
Msgf("zipping %s", types.Green(repo.Name))
err := archiver.Archive(tozip, fmt.Sprintf("%s.zip", repo.Name))
err := archiver.Archive(tozip, filepath.Join(l.Path, fmt.Sprintf("%s.zip", repo.Name)))
if err != nil {
sub.Warn().
Str("repo", repo.Name).Msg(err.Error())
Expand All @@ -264,7 +261,7 @@ func Locally(repo types.Repo, l types.Local, dry bool) bool {
}

if l.Keep > 0 {
parentdir := path.Dir(repo.Name)
parentdir := path.Dir(filepath.Join(l.Path, repo.Name))
files, err := os.ReadDir(parentdir)
if err != nil {
sub.Warn().
Expand Down Expand Up @@ -308,31 +305,27 @@ func Locally(repo types.Repo, l types.Local, dry bool) bool {

x = 5
}
if err := os.Chdir(originPath); err != nil {
sub.Error().
Msg(err.Error())
return false
}

return true
}

func updateRepository(repoPath string, auth transport.AuthMethod, dry bool, l types.Local) error {
r, err := git.PlainOpen(repoPath)
func updateRepository(reponame string, auth transport.AuthMethod, dry bool, l types.Local) error {
r, err := git.PlainOpen(filepath.Join(l.Path, reponame))
if err != nil {
return err
}

if !dry {
if l.LFS {
err = os.Chdir(repoPath)
_, err = os.Stat(filepath.Join(l.Path, reponame))
if err != nil {
return err
}

sub.Info().
Msgf("pulling %s", types.Green(repoPath))
Msgf("pulling %s", types.Green(reponame))

err = gitc.Pull(l.Bare)
err = gitc.Pull(l.Bare, filepath.Join(l.Path, reponame))
if err != nil {
return err
}
Expand All @@ -347,7 +340,7 @@ func updateRepository(repoPath string, auth transport.AuthMethod, dry bool, l ty
}

sub.Info().
Msgf("pulling %s", types.Green(repoPath))
Msgf("pulling %s", types.Green(reponame))

err = w.Pull(&git.PullOptions{Auth: auth, RemoteName: "origin", SingleBranch: false})
if err == git.NoErrAlreadyUpToDate {
Expand Down Expand Up @@ -400,10 +393,30 @@ func cloneRepository(repo types.Repo, auth transport.AuthMethod, dry bool, l typ
}

if l.LFS {
err = gitc.Clone(url, repo.Name, l.Bare)
if repo.Token != "" {
if strings.HasPrefix(url, "http://") {
url = strings.Replace(url, "http://", fmt.Sprintf("http://xyz:%s@", repo.Token), -1)
}

if strings.HasPrefix(url, "https://") {
url = strings.Replace(url, "https://", fmt.Sprintf("https://xyz:%s@", repo.Token), -1)
}
} else {
if repo.Origin.Username != "" && repo.Origin.Password != "" {
if strings.HasPrefix(url, "http://") {
url = strings.Replace(url, "http://", fmt.Sprintf("http://%s:%s@", repo.Origin.Username, repo.Origin.Password), -1)
}

if strings.HasPrefix(url, "https://") {
url = strings.Replace(url, "https://", fmt.Sprintf("https://%s:%s@", repo.Origin.Username, repo.Origin.Password), -1)
}
}
}

err = gitc.Clone(url, filepath.Join(l.Path, repo.Name), l.Bare)
} else {
r := &git.Repository{}
r, err = git.PlainClone(repo.Name, l.Bare, &git.CloneOptions{
r, err = git.PlainClone(filepath.Join(l.Path, repo.Name), l.Bare, &git.CloneOptions{
URL: url,
Auth: auth,
SingleBranch: false,
Expand Down

0 comments on commit c5e5cee

Please sign in to comment.