gitforgefs/git/clone.go

76 lines
1.7 KiB
Go
Raw Normal View History

package git
import (
"fmt"
"os"
"strconv"
"github.com/badjware/gitlabfs/utils"
)
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 05:24:27 +00:00
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
2021-03-03 05:24:27 +00:00
// Init the local repo
fmt.Printf("Initializing %v into %v\n", gcp.url, gcp.dst)
_, err := utils.ExecProcess(
"git", "init",
"--initial-branch", gcp.defaultBranch,
"--",
gcp.dst, // directory
)
if err != nil {
return fmt.Errorf("failed to init git repo %v to %v: %v", gcp.url, gcp.dst, err)
}
// Configure the remote
_, err = utils.ExecProcessInDir(
gcp.dst, // workdir
"git", "remote", "add",
"-t", gcp.defaultBranch,
"--",
c.RemoteName, // name
gcp.url, // url
)
if err != nil {
return fmt.Errorf("failed to setup remote %v in git repo %v: %v", gcp.url, gcp.dst, err)
}
2021-03-03 05:24:27 +00:00
} 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
}