2020-12-31 02:18:18 +00:00
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2021-03-03 04:43:38 +00:00
|
|
|
"strconv"
|
2020-12-31 02:18:18 +00:00
|
|
|
|
2021-03-03 04:43:38 +00:00
|
|
|
"github.com/badjware/gitlabfs/utils"
|
2020-12-31 02:18:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type gitCloneParam struct {
|
|
|
|
url string
|
|
|
|
defaultBranch string
|
|
|
|
dst string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *gitClient) cloneWorker() {
|
|
|
|
fmt.Println("Started git cloner worker routine")
|
|
|
|
|
|
|
|
for gcp := range c.cloneChan {
|
|
|
|
if _, err := os.Stat(gcp.dst); os.IsNotExist(err) {
|
|
|
|
if err := c.clone(gcp); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *gitClient) clone(gcp *gitCloneParam) error {
|
2021-03-03 04:43:38 +00:00
|
|
|
// branchRef := plumbing.NewBranchReferenceName(gcp.defaultBranch)
|
2020-12-31 02:18:18 +00:00
|
|
|
|
|
|
|
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))
|
2021-03-03 04:43:38 +00:00
|
|
|
// _, 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
|
|
|
|
)
|
2020-12-31 02:18:18 +00:00
|
|
|
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
|
|
|
|
// 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
|
|
|
|
fmt.Printf("Initializing %v into %v\n", gcp.url, gcp.dst)
|
2021-03-03 04:43:38 +00:00
|
|
|
// r, err := git.PlainInit(gcp.dst, false)
|
|
|
|
_, err := utils.ExecProcess(
|
|
|
|
"git", "init",
|
|
|
|
"--initial-branch", gcp.defaultBranch,
|
|
|
|
"--",
|
|
|
|
gcp.dst, // directory
|
|
|
|
)
|
2020-12-31 02:18:18 +00:00
|
|
|
if err != nil {
|
2021-03-03 04:43:38 +00:00
|
|
|
return fmt.Errorf("failed to init git repo %v to %v: %v", gcp.url, gcp.dst, err)
|
2020-12-31 02:18:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Configure the remote
|
2021-03-03 04:43:38 +00:00
|
|
|
// _, err = r.CreateRemote(&config.RemoteConfig{
|
|
|
|
// Name: c.RemoteName,
|
|
|
|
// URLs: []string{gcp.url},
|
|
|
|
// })
|
|
|
|
_, err = utils.ExecProcessInDir(
|
|
|
|
gcp.dst, // workdir
|
|
|
|
"git", "remote", "add",
|
|
|
|
"-t", gcp.defaultBranch,
|
|
|
|
"--",
|
|
|
|
c.RemoteName, // name
|
|
|
|
gcp.url, // url
|
|
|
|
)
|
2020-12-31 02:18:18 +00:00
|
|
|
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
|
2021-03-03 04:43:38 +00:00
|
|
|
// 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)
|
|
|
|
// }
|
2020-12-31 02:18:18 +00:00
|
|
|
|
|
|
|
// Checkout the default branch
|
2021-03-03 04:43:38 +00:00
|
|
|
// 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,
|
|
|
|
// })
|
2020-12-31 02:18:18 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|