siemonster :: improve checkout to custom branch on git clone

This commit is contained in:
Harduino
2020-09-17 18:56:41 +03:00
parent 074093adb4
commit f537437eef
2 changed files with 46 additions and 10 deletions
+23 -6
View File
@@ -29,6 +29,7 @@ import (
"github.com/go-git/go-billy/v5/memfs"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/storage/memory"
http2 "gopkg.in/src-d/go-git.v4/plumbing/transport/http"
//"github.com/gorilla/websocket"
@@ -4175,16 +4176,11 @@ func loadGithubWorkflows(url, username, password, userId, branch string) error {
// FIXME: Better auth.
if len(username) > 0 && len(password) > 0 {
cloneOptions.Auth = &http2.BasicAuth{
Username: username,
Password: password,
}
}
if len(branch) > 0 {
cloneOptions.ReferenceName = plumbing.ReferenceName(branch)
}
storer := memory.NewStorage()
r, err := git.Clone(storer, fs, cloneOptions)
if err != nil {
@@ -4192,9 +4188,30 @@ func loadGithubWorkflows(url, username, password, userId, branch string) error {
return err
}
if len(branch) > 0 {
log.Printf("Checkout to branch: %s", branch)
w, _ := r.Worktree()
err := r.Fetch(&git.FetchOptions{
RefSpecs: []config.RefSpec{"refs/*:refs/*", "HEAD:refs/heads/HEAD"},
})
if err != nil {
log.Printf("Failed fetch for git repo: %s", err)
}
err = w.Checkout(&git.CheckoutOptions{
Branch: plumbing.ReferenceName(fmt.Sprintf("refs/heads/%s", branch)),
Force: true,
})
if err != nil {
log.Printf("Failed checkout for git repo: %s", err)
}
}
dir, err := fs.ReadDir("/")
if err != nil {
log.Printf("FAiled reading folder: %s", err)
log.Printf("Failed reading folder: %s", err)
}
_ = r