.gitea
cli
cmd
pkg
app
autocomplete
catalogue
client
compose
config
container
context
dns
formatter
git
branch.go
clone.go
commit.go
common.go
init.go
push.go
read.go
remote.go
integration
jsontable
limit
lint
recipe
runtime
secret
server
service
ssh
test
upstream
web
scripts
tests
.dockerignore
.drone.yml
.e2e.env.sample
.envrc.sample
.gitignore
.goreleaser.yml
AUTHORS.md
Dockerfile
LICENSE
Makefile
README.md
go.mod
go.sum
renovate.json
Also may have rooted out another go-git cloning bug 🙄 Closes coop-cloud/organising#365
51 lines
1.2 KiB
Go
51 lines
1.2 KiB
Go
package git
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/go-git/go-git/v5"
|
|
"github.com/go-git/go-git/v5/plumbing"
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
// Clone runs a git clone which accounts for different default branches.
|
|
func Clone(dir, url string) error {
|
|
if _, err := os.Stat(dir); os.IsNotExist(err) {
|
|
logrus.Debugf("%s does not exist, attempting to git clone from %s", dir, url)
|
|
|
|
_, err := git.PlainClone(dir, false, &git.CloneOptions{
|
|
URL: url,
|
|
Tags: git.AllTags,
|
|
ReferenceName: plumbing.ReferenceName("refs/heads/master"),
|
|
SingleBranch: true,
|
|
})
|
|
if err != nil {
|
|
logrus.Debugf("cloning %s default branch failed, attempting from main branch", url)
|
|
|
|
_, err := git.PlainClone(dir, false, &git.CloneOptions{
|
|
URL: url,
|
|
Tags: git.AllTags,
|
|
ReferenceName: plumbing.ReferenceName("refs/heads/main"),
|
|
SingleBranch: true,
|
|
})
|
|
if err != nil {
|
|
if strings.Contains(err.Error(), "authentication required") {
|
|
name := filepath.Base(dir)
|
|
return fmt.Errorf("unable to clone %s, does %s exist?", name, url)
|
|
}
|
|
|
|
return err
|
|
}
|
|
}
|
|
|
|
logrus.Debugf("%s has been git cloned successfully", dir)
|
|
} else {
|
|
logrus.Debugf("%s already exists", dir)
|
|
}
|
|
|
|
return nil
|
|
}
|