2020-12-31 02:18:18 +00:00
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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 05:24:27 +00:00
|
|
|
if c.CloneMethod == CloneInit {
|
2020-12-31 02:18:18 +00:00
|
|
|
// "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
|
2020-12-31 02:18:18 +00:00
|
|
|
fmt.Printf("Initializing %v into %v\n", gcp.url, gcp.dst)
|
2021-03-03 04:43:38 +00:00
|
|
|
_, 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 = 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)
|
|
|
|
}
|
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)
|
2020-12-31 02:18:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|