siemonster :: improve checkout to custom branch on git clone
This commit is contained in:
+23
-4
@@ -42,6 +42,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"
|
||||
|
||||
// Random
|
||||
@@ -6368,10 +6369,6 @@ func runInit(ctx context.Context) {
|
||||
Password: password,
|
||||
}
|
||||
}
|
||||
branch := os.Getenv("SHUFFLE_DOWNLOAD_AUTH_BRANCH")
|
||||
if len(branch) > 0 {
|
||||
cloneOptions.ReferenceName = plumbing.ReferenceName(branch)
|
||||
}
|
||||
|
||||
log.Printf("Getting apps from %s", url)
|
||||
|
||||
@@ -6381,6 +6378,28 @@ func runInit(ctx context.Context) {
|
||||
log.Printf("Failed loading repo into memory: %s", err)
|
||||
}
|
||||
|
||||
branch := os.Getenv("SHUFFLE_DOWNLOAD_AUTH_BRANCH")
|
||||
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)
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user