admin: New option to pass branch to use

This commit is contained in:
nemunaire 2023-05-04 13:19:42 +02:00
parent f623699f56
commit f5529ff72d
3 changed files with 25 additions and 5 deletions

View File

@ -66,6 +66,7 @@ func main() {
cloudPassword := ""
localImporterDirectory := ""
gitImporterRemote := ""
gitImporterBranch := ""
localImporterSymlink := false
baseURL := "/"
checkplugins := sync.CheckPluginList{}
@ -113,6 +114,8 @@ func main() {
"Copy files or just create symlink?")
flag.StringVar(&gitImporterRemote, "git-import-remote", gitImporterRemote,
"Remote URL of the git repository to use as synchronization source")
flag.StringVar(&gitImporterBranch, "git-branch", gitImporterBranch,
"Branch to use in the git repository")
flag.StringVar(&cloudDAVBase, "clouddav", cloudDAVBase,
"Base directory where found challenges files to import, cloud part")
flag.StringVar(&cloudUsername, "clouduser", cloudUsername, "Username used to sync")
@ -133,7 +136,7 @@ func main() {
log.Fatal("Cannot have both --clouddav and --git-import-remote defined.")
return
} else if gitImporterRemote != "" {
sync.GlobalImporter = sync.NewGitImporter(sync.LocalImporter{Base: localImporterDirectory, Symlink: localImporterSymlink}, gitImporterRemote)
sync.GlobalImporter = sync.NewGitImporter(sync.LocalImporter{Base: localImporterDirectory, Symlink: localImporterSymlink}, gitImporterRemote, gitImporterBranch)
} else if localImporterDirectory != "" {
sync.GlobalImporter = sync.LocalImporter{Base: localImporterDirectory, Symlink: localImporterSymlink}
} else if cloudDAVBase != "" {

View File

@ -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,

View File

@ -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)