2020-12-31 02:18:18 +00:00
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
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 gitPullParam struct {
|
|
|
|
repoPath string
|
|
|
|
defaultBranch string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *gitClient) pullWorker() {
|
|
|
|
fmt.Println("Started git puller worker routine")
|
|
|
|
|
|
|
|
for gpp := range c.pullChan {
|
|
|
|
if err := c.pull(gpp); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *gitClient) pull(gpp *gitPullParam) error {
|
|
|
|
// Check if the local repo is on default branch
|
2021-03-03 04:43:38 +00:00
|
|
|
branchName, err := utils.ExecProcessInDir(
|
|
|
|
gpp.repoPath, // workdir
|
|
|
|
"git", "branch",
|
|
|
|
"--show-current",
|
|
|
|
)
|
2020-12-31 02:18:18 +00:00
|
|
|
if err != nil {
|
2021-03-03 04:43:38 +00:00
|
|
|
return fmt.Errorf("failed to retrieve HEAD of git repo %v: %v", gpp.repoPath, err)
|
2020-12-31 02:18:18 +00:00
|
|
|
}
|
|
|
|
|
2021-03-03 04:43:38 +00:00
|
|
|
if branchName == gpp.defaultBranch {
|
2021-03-03 05:24:27 +00:00
|
|
|
// Pull the repo
|
2021-03-03 04:43:38 +00:00
|
|
|
_, err = utils.ExecProcessInDir(
|
|
|
|
gpp.repoPath, // workdir
|
|
|
|
"git", "pull",
|
|
|
|
"--depth", strconv.Itoa(c.PullDepth),
|
|
|
|
"--",
|
|
|
|
c.RemoteName, // repository
|
|
|
|
gpp.defaultBranch, // refspec
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to pull git repo %v: %v", gpp.repoPath, err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fmt.Printf("%v != %v, skipping pull", branchName, gpp.defaultBranch)
|
2020-12-31 02:18:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|