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
|
|
|
)
|
|
|
|
|
2022-03-03 22:47:36 +00:00
|
|
|
func (c *gitClient) pull(repoPath string, defaultBranch string) error {
|
2020-12-31 02:18:18 +00:00
|
|
|
// Check if the local repo is on default branch
|
2021-03-03 04:43:38 +00:00
|
|
|
branchName, err := utils.ExecProcessInDir(
|
2024-06-06 05:51:34 +00:00
|
|
|
c.logger,
|
2022-03-03 22:47:36 +00:00
|
|
|
repoPath, // workdir
|
2021-03-03 04:43:38 +00:00
|
|
|
"git", "branch",
|
|
|
|
"--show-current",
|
|
|
|
)
|
2020-12-31 02:18:18 +00:00
|
|
|
if err != nil {
|
2022-03-03 22:47:36 +00:00
|
|
|
return fmt.Errorf("failed to retrieve HEAD of git repo %v: %v", repoPath, err)
|
2020-12-31 02:18:18 +00:00
|
|
|
}
|
|
|
|
|
2022-03-03 22:47:36 +00:00
|
|
|
if branchName == defaultBranch {
|
2021-03-03 05:24:27 +00:00
|
|
|
// Pull the repo
|
2024-05-08 03:31:52 +00:00
|
|
|
args := []string{
|
|
|
|
"pull",
|
|
|
|
}
|
2024-08-04 19:44:50 +00:00
|
|
|
if c.GitClientConfig.Depth != 0 {
|
|
|
|
args = append(args, "--depth", strconv.Itoa(c.GitClientConfig.Depth))
|
2024-05-08 03:31:52 +00:00
|
|
|
}
|
|
|
|
args = append(args,
|
2021-03-03 04:43:38 +00:00
|
|
|
"--",
|
2024-08-04 19:44:50 +00:00
|
|
|
c.GitClientConfig.Remote, // repository
|
|
|
|
defaultBranch, // refspec
|
2021-03-03 04:43:38 +00:00
|
|
|
)
|
2024-05-08 03:31:52 +00:00
|
|
|
|
2024-06-06 05:51:34 +00:00
|
|
|
_, err = utils.ExecProcessInDir(c.logger, repoPath, "git", args...)
|
2021-03-03 04:43:38 +00:00
|
|
|
if err != nil {
|
2022-03-03 22:47:36 +00:00
|
|
|
return fmt.Errorf("failed to pull git repo %v: %v", repoPath, err)
|
2021-03-03 04:43:38 +00:00
|
|
|
}
|
|
|
|
} else {
|
2024-06-06 05:51:34 +00:00
|
|
|
c.logger.Info("Skipping pull because local is not on default branch", "currentBranch", branchName, "defaultBranch", defaultBranch)
|
2020-12-31 02:18:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|