Skip to content

Commit

Permalink
addresses #92. ls-remote before pulling or cloning (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
cooperspencer authored Feb 24, 2022
1 parent d96310d commit bee477b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
23 changes: 16 additions & 7 deletions github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@ package github
import (
"context"
"gickup/types"

"github.com/google/go-github/v41/github"
"github.com/rs/zerolog/log"
"golang.org/x/oauth2"
)

func addWiki(r github.Repository, repo types.GenRepo, token string) types.Repo {
if r.GetHasWiki() && repo.Wiki && r.GetHasPages() {
return types.Repo{Name: *r.Name + ".wiki", Url: types.DotGitRx.ReplaceAllString(r.GetCloneURL(), ".wiki.git"), SshUrl: types.DotGitRx.ReplaceAllString(r.GetSSHURL(), ".wiki.git"), Token: token, Defaultbranch: r.GetDefaultBranch(), Origin: repo, Owner: r.GetOwner().GetLogin(), Hoster: "github.com"}
}
return types.Repo{}
}

func Get(conf *types.Conf) []types.Repo {
repos := []types.Repo{}
for _, repo := range conf.Source.Github {
Expand Down Expand Up @@ -76,8 +82,9 @@ func Get(conf *types.Conf) []types.Repo {
for _, r := range githubrepos {
if include[*r.Name] {
repos = append(repos, types.Repo{Name: r.GetName(), Url: r.GetCloneURL(), SshUrl: r.GetSSHURL(), Token: token, Defaultbranch: r.GetDefaultBranch(), Origin: repo, Owner: r.GetOwner().GetLogin(), Hoster: "github.com"})
if *r.HasWiki && repo.Wiki && *r.HasPages {
repos = append(repos, types.Repo{Name: *r.Name + ".wiki", Url: types.DotGitRx.ReplaceAllString(r.GetCloneURL(), ".wiki.git"), SshUrl: types.DotGitRx.ReplaceAllString(r.GetSSHURL(), ".wiki.git"), Token: token, Defaultbranch: r.GetDefaultBranch(), Origin: repo, Owner: r.GetOwner().GetLogin(), Hoster: "github.com"})
wiki := addWiki(*r, repo, token)
if wiki.Name != "" {
repos = append(repos, wiki)
}
continue
}
Expand All @@ -91,14 +98,16 @@ func Get(conf *types.Conf) []types.Repo {
if len(includeorgs) > 0 {
if includeorgs[r.GetOwner().GetLogin()] {
repos = append(repos, types.Repo{Name: r.GetName(), Url: r.GetCloneURL(), SshUrl: r.GetSSHURL(), Token: token, Defaultbranch: r.GetDefaultBranch(), Origin: repo, Owner: r.GetOwner().GetLogin(), Hoster: "github.com"})
if *r.HasWiki && repo.Wiki && *r.HasPages {
repos = append(repos, types.Repo{Name: *r.Name + ".wiki", Url: types.DotGitRx.ReplaceAllString(r.GetCloneURL(), ".wiki.git"), SshUrl: types.DotGitRx.ReplaceAllString(r.GetSSHURL(), ".wiki.git"), Token: token, Defaultbranch: r.GetDefaultBranch(), Origin: repo, Owner: r.GetOwner().GetLogin(), Hoster: "github.com"})
wiki := addWiki(*r, repo, token)
if wiki.Name != "" {
repos = append(repos, wiki)
}
}
} else {
repos = append(repos, types.Repo{Name: r.GetName(), Url: r.GetCloneURL(), SshUrl: r.GetSSHURL(), Token: token, Defaultbranch: r.GetDefaultBranch(), Origin: repo, Owner: r.GetOwner().GetLogin(), Hoster: "github.com"})
if *r.HasWiki && repo.Wiki && *r.HasPages {
repos = append(repos, types.Repo{Name: *r.Name + ".wiki", Url: types.DotGitRx.ReplaceAllString(r.GetCloneURL(), ".wiki.git"), SshUrl: types.DotGitRx.ReplaceAllString(r.GetSSHURL(), ".wiki.git"), Token: token, Defaultbranch: r.GetDefaultBranch(), Origin: repo, Owner: r.GetOwner().GetLogin(), Hoster: "github.com"})
wiki := addWiki(*r, repo, token)
if wiki.Name != "" {
repos = append(repos, wiki)
}
}
}
Expand Down
20 changes: 19 additions & 1 deletion local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/plumbing/transport"
"github.com/go-git/go-git/v5/plumbing/transport/http"
"github.com/go-git/go-git/v5/plumbing/transport/ssh"
Expand Down Expand Up @@ -68,6 +69,10 @@ func Locally(repo types.Repo, l types.Local, dry bool) {
if x == tries {
log.Fatal().Str("stage", "locally").Str("path", l.Path).Str("repo", repo.Name).Msg(err.Error())
} else {
if strings.Contains(err.Error(), "ERR access denied or repository not exported") {
log.Warn().Str("stage", "locally").Str("path", l.Path).Str("repo", repo.Name).Msgf("%s doesn't exist.", repo.Name)
break
}
if strings.Contains(err.Error(), "remote repository is empty") {
log.Warn().Str("stage", "locally").Str("path", l.Path).Str("repo", repo.Name).Msg(err.Error())
break
Expand Down Expand Up @@ -110,6 +115,13 @@ func updateRepository(repoPath string, auth transport.AuthMethod, dry bool) erro
if err != nil {
return err
}
remote, _ := r.Remote("origin")

rem := git.NewRemote(nil, &config.RemoteConfig{Name: "origin", URLs: remote.Config().URLs})
_, err = rem.List(&git.ListOptions{Auth: auth})
if err != nil {
return err
}

w, err := r.Worktree()
if err != nil {
Expand Down Expand Up @@ -145,7 +157,13 @@ func cloneRepository(repo types.Repo, auth transport.AuthMethod, dry bool) error
}
}

_, err := git.PlainClone(repo.Name, false, &git.CloneOptions{
rem := git.NewRemote(nil, &config.RemoteConfig{Name: "origin", URLs: []string{url}})
_, err := rem.List(&git.ListOptions{Auth: auth})
if err != nil {
return err
}

_, err = git.PlainClone(repo.Name, false, &git.CloneOptions{
URL: url,
Auth: auth,
SingleBranch: false,
Expand Down

0 comments on commit bee477b

Please sign in to comment.