cleanup on_clone configuration option

This commit is contained in:
Massaki Archambault 2021-03-03 00:24:27 -05:00
parent cec8ea9806
commit 3c7ad94844
5 changed files with 40 additions and 116 deletions

View File

@ -30,14 +30,14 @@ git:
# Must be set to either "http" or "ssh".
# 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
# 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-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 "no-checkout", the local clone will be initialized with `git clone --no-checkout`. (slower)
# 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.
# If set to "clone", the local clone will be initialized with `git clone`. (slowest)
# NOTE: If set to "init", 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".
on_clone: init
@ -59,4 +59,4 @@ git:
pull_queue_size: 500
# The number of parallel `git pull` operation that is allowed to run at once
pull_worker_count: 5
pull_worker_count: 5

View File

@ -8,19 +8,22 @@ import (
"strconv"
)
const (
CloneInit = iota
CloneClone = iota
)
type GitClonerPuller interface {
CloneOrPull(url string, pid int, defaultBranch string) (localRepoLoc string, err error)
}
type GitClientParam struct {
CloneLocation string
RemoteName string
RemoteURL *url.URL
PullAfterClone bool
Clone bool
Checkout bool
PullDepth int
AutoPull bool
CloneLocation string
RemoteName string
RemoteURL *url.URL
CloneMethod int
PullDepth int
AutoPull bool
CloneBuffSize int
CloneWorkerCount int

View File

@ -1,7 +1,6 @@
package git
import (
"errors"
"fmt"
"os"
"strconv"
@ -28,39 +27,14 @@ func (c *gitClient) cloneWorker() {
}
func (c *gitClient) clone(gcp *gitCloneParam) error {
// branchRef := plumbing.NewBranchReferenceName(gcp.defaultBranch)
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 {
if c.CloneMethod == CloneInit {
// "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
// 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
// Init the local repo
fmt.Printf("Initializing %v into %v\n", gcp.url, gcp.dst)
// r, err := git.PlainInit(gcp.dst, false)
_, err := utils.ExecProcess(
"git", "init",
"--initial-branch", gcp.defaultBranch,
@ -72,10 +46,6 @@ func (c *gitClient) clone(gcp *gitCloneParam) error {
}
// Configure the remote
// _, err = r.CreateRemote(&config.RemoteConfig{
// Name: c.RemoteName,
// URLs: []string{gcp.url},
// })
_, err = utils.ExecProcessInDir(
gcp.dst, // workdir
"git", "remote", "add",
@ -87,35 +57,18 @@ func (c *gitClient) clone(gcp *gitCloneParam) error {
if err != nil {
return fmt.Errorf("failed to setup remote %v in git repo %v: %v", gcp.url, gcp.dst, err)
}
// Configure a local branch to track the remote branch
// err = r.CreateBranch(&config.Branch{
// Name: gcp.defaultBranch,
// Remote: c.RemoteName,
// Merge: plumbing.ReferenceName(fmt.Sprintf("refs/heads/%s", gcp.defaultBranch)),
// })
// if err != nil {
// return fmt.Errorf("failed to create branch %v of git repo %v: %v", gcp.defaultBranch, gcp.dst, err)
// }
// Checkout the default branch
// 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")
} else {
// Clone the repo
_, 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)
}
}
return nil

View File

@ -23,17 +23,7 @@ func (c *gitClient) pullWorker() {
}
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
// headRef, err := r.Head()
branchName, err := utils.ExecProcessInDir(
gpp.repoPath, // workdir
"git", "branch",
@ -44,6 +34,7 @@ func (c *gitClient) pull(gpp *gitPullParam) error {
}
if branchName == gpp.defaultBranch {
// Pull the repo
_, err = utils.ExecProcessInDir(
gpp.repoPath, // workdir
"git", "pull",

37
main.go
View File

@ -13,13 +13,6 @@ import (
"gopkg.in/yaml.v2"
)
const (
OnCloneInit = "init"
OnCloneInitPull = "init-pull"
OnCloneNoCheckout = "no-checkout"
OnCloneCheckout = "checkout"
)
type (
Config struct {
FS FSConfig `yaml:"fs,omitempty"`
@ -118,36 +111,20 @@ func makeGitConfig(config *Config) (*git.GitClientParam, error) {
}
// parse on_clone
pullAfterClone := false
clone := false
checkout := false
if config.Git.OnClone == OnCloneInit {
pullAfterClone = false
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
cloneMethod := 0
if config.Git.OnClone == "init" {
cloneMethod = git.CloneInit
} else if config.Git.OnClone == "clone" {
cloneMethod = git.CloneClone
} 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{
CloneLocation: config.Git.CloneLocation,
RemoteName: config.Git.Remote,
RemoteURL: parsedGitlabURL,
PullAfterClone: pullAfterClone,
Clone: clone,
Checkout: checkout,
CloneMethod: cloneMethod,
AutoPull: config.Git.AutoPull,
PullDepth: config.Git.Depth,
CloneBuffSize: config.Git.CloneQueueSize,