admin: New option to pass branch to use
This commit is contained in:
parent
f623699f56
commit
f5529ff72d
3 changed files with 25 additions and 5 deletions
|
|
@ -11,6 +11,7 @@ import (
|
|||
|
||||
"github.com/go-git/go-git/v5"
|
||||
"github.com/go-git/go-git/v5/config"
|
||||
"github.com/go-git/go-git/v5/plumbing"
|
||||
"github.com/go-git/go-git/v5/plumbing/transport/ssh"
|
||||
)
|
||||
|
||||
|
|
@ -19,10 +20,11 @@ import (
|
|||
type GitImporter struct {
|
||||
li LocalImporter
|
||||
Remote string
|
||||
Branch string
|
||||
Auth ssh.AuthMethod
|
||||
}
|
||||
|
||||
func NewGitImporter(li LocalImporter, remote string) GitImporter {
|
||||
func NewGitImporter(li LocalImporter, remote string, branch string) GitImporter {
|
||||
var auth ssh.AuthMethod
|
||||
|
||||
// If there is no ssh-agent setup, try to use a default ssh key
|
||||
|
|
@ -47,6 +49,7 @@ func NewGitImporter(li LocalImporter, remote string) GitImporter {
|
|||
return GitImporter{
|
||||
li: li,
|
||||
Remote: remote,
|
||||
Branch: branch,
|
||||
Auth: auth,
|
||||
}
|
||||
}
|
||||
|
|
@ -76,6 +79,7 @@ func (i GitImporter) Init() error {
|
|||
} else if n == 0 {
|
||||
_, err = git.PlainClone(i.li.Base, false, &git.CloneOptions{
|
||||
URL: i.Remote,
|
||||
ReferenceName: plumbing.ReferenceName(i.Branch),
|
||||
RecurseSubmodules: git.DefaultSubmoduleRecursionDepth,
|
||||
Auth: i.Auth,
|
||||
})
|
||||
|
|
@ -116,6 +120,7 @@ func (i GitImporter) Sync() error {
|
|||
// Perform a git pull --rebase origin/master
|
||||
err = w.Pull(&git.PullOptions{
|
||||
RemoteName: "origin",
|
||||
ReferenceName: plumbing.ReferenceName(i.Branch),
|
||||
Depth: 1,
|
||||
RecurseSubmodules: git.DefaultSubmoduleRecursionDepth,
|
||||
Force: true,
|
||||
|
|
|
|||
|
|
@ -22,12 +22,18 @@ import (
|
|||
type GitImporter struct {
|
||||
li LocalImporter
|
||||
Remote string
|
||||
Branch string
|
||||
}
|
||||
|
||||
func NewGitImporter(li LocalImporter, remote string) GitImporter {
|
||||
func NewGitImporter(li LocalImporter, remote string, branch string) GitImporter {
|
||||
if len(branch) == 0 {
|
||||
branch = "master"
|
||||
}
|
||||
|
||||
return GitImporter{
|
||||
li: li,
|
||||
Remote: remote,
|
||||
Branch: branch,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -62,8 +68,14 @@ func (i GitImporter) Init() error {
|
|||
if n, err := countFileInDir(i.li.Base); err != nil {
|
||||
return err
|
||||
} else if n == 0 {
|
||||
args := []string{"clone", "--recursive", "--depth", "1"}
|
||||
if i.Branch != "" {
|
||||
args = append(args, "-b", i.Branch)
|
||||
}
|
||||
args = append(args, "--shallow-submodules", i.Remote, i.li.Base)
|
||||
|
||||
log.Println("Please wait while creating the local git repository...")
|
||||
cmdclone := exec.Command("git", "clone", "--recursive", "--depth", "1", "--shallow-submodules", i.Remote, i.li.Base)
|
||||
cmdclone := exec.Command("git", args...)
|
||||
stdout, err := cmdclone.CombinedOutput()
|
||||
if err != nil {
|
||||
return fmt.Errorf("%w:\n%s", err, stdout)
|
||||
|
|
@ -109,7 +121,7 @@ func (i GitImporter) Sync() error {
|
|||
}
|
||||
}
|
||||
|
||||
cmdreset := exec.Command("git", "-C", i.li.Base, "reset", "--hard", "--recurse-submodule", "origin/master")
|
||||
cmdreset := exec.Command("git", "-C", i.li.Base, "reset", "--hard", "--recurse-submodule", "origin/"+i.Branch)
|
||||
stdout, err = cmdreset.CombinedOutput()
|
||||
if err != nil {
|
||||
log.Printf("Local git repository reset failed: %s\n%s", err, stdout)
|
||||
|
|
|
|||
Reference in a new issue