cleanup on_clone configuration option
This commit is contained in:
parent
cec8ea9806
commit
3c7ad94844
|
@ -30,14 +30,14 @@ git:
|
||||||
|
|
||||||
# Must be set to either "http" or "ssh".
|
# Must be set to either "http" or "ssh".
|
||||||
# The protocol to configure the git remote on.
|
# The protocol to configure the git remote on.
|
||||||
|
# "http" may not work on private repos unless a credential manager is configured
|
||||||
|
# If possible, prefer "ssh" over "http"
|
||||||
pull_method: http
|
pull_method: http
|
||||||
|
|
||||||
# Must be set to either "init", init-pull", "no-checkout" or "checkout".
|
# Must be set to either "init", or "clone".
|
||||||
# If set to "init", the local clone will be initialized with `git init` and set to track the default branch. (fastest)
|
# If set to "init", the local clone will be initialized with `git init` and set to track the default branch. (fastest)
|
||||||
# If set to "init-pull", the local clone will be initialized with `git init` and will than be followed up by an asynchronous `git pull`. (faster)
|
# If set to "clone", the local clone will be initialized with `git clone`. (slowest)
|
||||||
# If set to "no-checkout", the local clone will be initialized with `git clone --no-checkout`. (slower)
|
# NOTE: If set to "init", the local clone will appear empty. Running `git pull` will download the files from the git server.
|
||||||
# If set to "checkout", the local clone will be initialized with `git clone`. (slowest)
|
|
||||||
# NOTE: If set to "init" or "no-checkout", the local clone will appear empty. Running `git pull` will download the files from the git server.
|
|
||||||
# It's highly recommended to leave this setting on "init".
|
# It's highly recommended to leave this setting on "init".
|
||||||
on_clone: init
|
on_clone: init
|
||||||
|
|
||||||
|
@ -59,4 +59,4 @@ git:
|
||||||
pull_queue_size: 500
|
pull_queue_size: 500
|
||||||
|
|
||||||
# The number of parallel `git pull` operation that is allowed to run at once
|
# The number of parallel `git pull` operation that is allowed to run at once
|
||||||
pull_worker_count: 5
|
pull_worker_count: 5
|
||||||
|
|
|
@ -8,19 +8,22 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
CloneInit = iota
|
||||||
|
CloneClone = iota
|
||||||
|
)
|
||||||
|
|
||||||
type GitClonerPuller interface {
|
type GitClonerPuller interface {
|
||||||
CloneOrPull(url string, pid int, defaultBranch string) (localRepoLoc string, err error)
|
CloneOrPull(url string, pid int, defaultBranch string) (localRepoLoc string, err error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type GitClientParam struct {
|
type GitClientParam struct {
|
||||||
CloneLocation string
|
CloneLocation string
|
||||||
RemoteName string
|
RemoteName string
|
||||||
RemoteURL *url.URL
|
RemoteURL *url.URL
|
||||||
PullAfterClone bool
|
CloneMethod int
|
||||||
Clone bool
|
PullDepth int
|
||||||
Checkout bool
|
AutoPull bool
|
||||||
PullDepth int
|
|
||||||
AutoPull bool
|
|
||||||
|
|
||||||
CloneBuffSize int
|
CloneBuffSize int
|
||||||
CloneWorkerCount int
|
CloneWorkerCount int
|
||||||
|
|
77
git/clone.go
77
git/clone.go
|
@ -1,7 +1,6 @@
|
||||||
package git
|
package git
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
@ -28,39 +27,14 @@ func (c *gitClient) cloneWorker() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *gitClient) clone(gcp *gitCloneParam) error {
|
func (c *gitClient) clone(gcp *gitCloneParam) error {
|
||||||
// branchRef := plumbing.NewBranchReferenceName(gcp.defaultBranch)
|
if c.CloneMethod == CloneInit {
|
||||||
|
|
||||||
if c.Clone {
|
|
||||||
// Clone the repo
|
|
||||||
// TODO: figure out why this operation is so memory intensive...
|
|
||||||
fmt.Printf("Cloning %v into %v\n", gcp.url, gcp.dst)
|
|
||||||
// fs := osfs.New(gcp.dst)
|
|
||||||
// storer := filesystem.NewStorage(fs, cache.NewObjectLRU(0))
|
|
||||||
// _, err := git.PlainClone(gcp.dst, false, &git.CloneOptions{
|
|
||||||
// URL: gcp.url,
|
|
||||||
// RemoteName: c.RemoteName,
|
|
||||||
// ReferenceName: branchRef,
|
|
||||||
// NoCheckout: !c.Checkout,
|
|
||||||
// Depth: c.PullDepth,
|
|
||||||
// })
|
|
||||||
_, err := utils.ExecProcess(
|
|
||||||
"git", "clone",
|
|
||||||
"--origin", c.RemoteName,
|
|
||||||
"--depth", strconv.Itoa(c.PullDepth),
|
|
||||||
"--",
|
|
||||||
gcp.url, // repository
|
|
||||||
gcp.dst, // directory
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to clone git repo %v to %v: %v", gcp.url, gcp.dst, err)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// "Fake" cloning the repo by never actually talking to the git server
|
// "Fake" cloning the repo by never actually talking to the git server
|
||||||
// This skip a fetch operation that we would do if we where to do a proper clone
|
// This skip a fetch operation that we would do if we where to do a proper clone
|
||||||
// We can save a lot of time and network i/o doing it this way, at the cost of
|
// We can save a lot of time and network i/o doing it this way, at the cost of
|
||||||
// resulting in a very barebone local copy
|
// resulting in a very barebone local copy
|
||||||
|
|
||||||
|
// Init the local repo
|
||||||
fmt.Printf("Initializing %v into %v\n", gcp.url, gcp.dst)
|
fmt.Printf("Initializing %v into %v\n", gcp.url, gcp.dst)
|
||||||
// r, err := git.PlainInit(gcp.dst, false)
|
|
||||||
_, err := utils.ExecProcess(
|
_, err := utils.ExecProcess(
|
||||||
"git", "init",
|
"git", "init",
|
||||||
"--initial-branch", gcp.defaultBranch,
|
"--initial-branch", gcp.defaultBranch,
|
||||||
|
@ -72,10 +46,6 @@ func (c *gitClient) clone(gcp *gitCloneParam) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Configure the remote
|
// Configure the remote
|
||||||
// _, err = r.CreateRemote(&config.RemoteConfig{
|
|
||||||
// Name: c.RemoteName,
|
|
||||||
// URLs: []string{gcp.url},
|
|
||||||
// })
|
|
||||||
_, err = utils.ExecProcessInDir(
|
_, err = utils.ExecProcessInDir(
|
||||||
gcp.dst, // workdir
|
gcp.dst, // workdir
|
||||||
"git", "remote", "add",
|
"git", "remote", "add",
|
||||||
|
@ -87,35 +57,18 @@ func (c *gitClient) clone(gcp *gitCloneParam) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to setup remote %v in git repo %v: %v", gcp.url, gcp.dst, err)
|
return fmt.Errorf("failed to setup remote %v in git repo %v: %v", gcp.url, gcp.dst, err)
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
// Configure a local branch to track the remote branch
|
// Clone the repo
|
||||||
// err = r.CreateBranch(&config.Branch{
|
_, err := utils.ExecProcess(
|
||||||
// Name: gcp.defaultBranch,
|
"git", "clone",
|
||||||
// Remote: c.RemoteName,
|
"--origin", c.RemoteName,
|
||||||
// Merge: plumbing.ReferenceName(fmt.Sprintf("refs/heads/%s", gcp.defaultBranch)),
|
"--depth", strconv.Itoa(c.PullDepth),
|
||||||
// })
|
"--",
|
||||||
// if err != nil {
|
gcp.url, // repository
|
||||||
// return fmt.Errorf("failed to create branch %v of git repo %v: %v", gcp.defaultBranch, gcp.dst, err)
|
gcp.dst, // directory
|
||||||
// }
|
)
|
||||||
|
if err != nil {
|
||||||
// Checkout the default branch
|
return fmt.Errorf("failed to clone git repo %v to %v: %v", gcp.url, gcp.dst, err)
|
||||||
// w, err := r.Worktree()
|
|
||||||
// if err != nil {
|
|
||||||
// return fmt.Errorf("failed to retrieve worktree of git repo %v: %v", gcp.dst, err)
|
|
||||||
// }
|
|
||||||
// w.Checkout(&git.CheckoutOptions{
|
|
||||||
// Branch: branchRef,
|
|
||||||
// })
|
|
||||||
}
|
|
||||||
if c.PullAfterClone {
|
|
||||||
// Dispatch to pull worker
|
|
||||||
select {
|
|
||||||
case c.pullChan <- &gitPullParam{
|
|
||||||
repoPath: gcp.dst,
|
|
||||||
defaultBranch: gcp.defaultBranch,
|
|
||||||
}:
|
|
||||||
default:
|
|
||||||
return errors.New("failed to pull local repo after clone")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|
11
git/pull.go
11
git/pull.go
|
@ -23,17 +23,7 @@ func (c *gitClient) pullWorker() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *gitClient) pull(gpp *gitPullParam) error {
|
func (c *gitClient) pull(gpp *gitPullParam) error {
|
||||||
// r, err := git.PlainOpen(gpp.repoPath)
|
|
||||||
// if err != nil {
|
|
||||||
// return fmt.Errorf("failed to open git repo %v: %v", gpp.repoPath, err)
|
|
||||||
// }
|
|
||||||
// w, err := r.Worktree()
|
|
||||||
// if err != nil {
|
|
||||||
// return fmt.Errorf("failed to retrieve worktree of git repo %v: %v", gpp.repoPath, err)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// Check if the local repo is on default branch
|
// Check if the local repo is on default branch
|
||||||
// headRef, err := r.Head()
|
|
||||||
branchName, err := utils.ExecProcessInDir(
|
branchName, err := utils.ExecProcessInDir(
|
||||||
gpp.repoPath, // workdir
|
gpp.repoPath, // workdir
|
||||||
"git", "branch",
|
"git", "branch",
|
||||||
|
@ -44,6 +34,7 @@ func (c *gitClient) pull(gpp *gitPullParam) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
if branchName == gpp.defaultBranch {
|
if branchName == gpp.defaultBranch {
|
||||||
|
// Pull the repo
|
||||||
_, err = utils.ExecProcessInDir(
|
_, err = utils.ExecProcessInDir(
|
||||||
gpp.repoPath, // workdir
|
gpp.repoPath, // workdir
|
||||||
"git", "pull",
|
"git", "pull",
|
||||||
|
|
37
main.go
37
main.go
|
@ -13,13 +13,6 @@ import (
|
||||||
"gopkg.in/yaml.v2"
|
"gopkg.in/yaml.v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
|
||||||
OnCloneInit = "init"
|
|
||||||
OnCloneInitPull = "init-pull"
|
|
||||||
OnCloneNoCheckout = "no-checkout"
|
|
||||||
OnCloneCheckout = "checkout"
|
|
||||||
)
|
|
||||||
|
|
||||||
type (
|
type (
|
||||||
Config struct {
|
Config struct {
|
||||||
FS FSConfig `yaml:"fs,omitempty"`
|
FS FSConfig `yaml:"fs,omitempty"`
|
||||||
|
@ -118,36 +111,20 @@ func makeGitConfig(config *Config) (*git.GitClientParam, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// parse on_clone
|
// parse on_clone
|
||||||
pullAfterClone := false
|
cloneMethod := 0
|
||||||
clone := false
|
if config.Git.OnClone == "init" {
|
||||||
checkout := false
|
cloneMethod = git.CloneInit
|
||||||
if config.Git.OnClone == OnCloneInit {
|
} else if config.Git.OnClone == "clone" {
|
||||||
pullAfterClone = false
|
cloneMethod = git.CloneClone
|
||||||
clone = false
|
|
||||||
checkout = false
|
|
||||||
} else if config.Git.OnClone == OnCloneInitPull {
|
|
||||||
pullAfterClone = true
|
|
||||||
clone = false
|
|
||||||
checkout = false
|
|
||||||
} else if config.Git.OnClone == OnCloneNoCheckout {
|
|
||||||
pullAfterClone = false
|
|
||||||
clone = true
|
|
||||||
checkout = false
|
|
||||||
} else if config.Git.OnClone == OnCloneCheckout {
|
|
||||||
pullAfterClone = false
|
|
||||||
clone = true
|
|
||||||
checkout = true
|
|
||||||
} else {
|
} else {
|
||||||
return nil, fmt.Errorf("on_clone must be either \"%v\", \"%v\" or \"%V\"", OnCloneInit, OnCloneNoCheckout, OnCloneCheckout)
|
return nil, fmt.Errorf("on_clone must be either \"init\" or \"clone\"")
|
||||||
}
|
}
|
||||||
|
|
||||||
return &git.GitClientParam{
|
return &git.GitClientParam{
|
||||||
CloneLocation: config.Git.CloneLocation,
|
CloneLocation: config.Git.CloneLocation,
|
||||||
RemoteName: config.Git.Remote,
|
RemoteName: config.Git.Remote,
|
||||||
RemoteURL: parsedGitlabURL,
|
RemoteURL: parsedGitlabURL,
|
||||||
PullAfterClone: pullAfterClone,
|
CloneMethod: cloneMethod,
|
||||||
Clone: clone,
|
|
||||||
Checkout: checkout,
|
|
||||||
AutoPull: config.Git.AutoPull,
|
AutoPull: config.Git.AutoPull,
|
||||||
PullDepth: config.Git.Depth,
|
PullDepth: config.Git.Depth,
|
||||||
CloneBuffSize: config.Git.CloneQueueSize,
|
CloneBuffSize: config.Git.CloneQueueSize,
|
||||||
|
|
Loading…
Reference in New Issue