gitforgefs/git/pull.go

46 lines
1.0 KiB
Go
Raw Normal View History

package git
import (
"fmt"
"strconv"
2024-08-14 02:55:18 +00:00
"github.com/badjware/gitforgefs/utils"
)
func (c *gitClient) pull(repoPath string, defaultBranch string) error {
// Check if the local repo is on default branch
branchName, err := utils.ExecProcessInDir(
2024-06-06 05:51:34 +00:00
c.logger,
repoPath, // workdir
"git", "branch",
"--show-current",
)
if err != nil {
return fmt.Errorf("failed to retrieve HEAD of git repo %v: %v", repoPath, err)
}
if branchName == defaultBranch {
2021-03-03 05:24:27 +00:00
// Pull the repo
args := []string{
"pull",
}
if c.GitClientConfig.Depth != 0 {
args = append(args, "--depth", strconv.Itoa(c.GitClientConfig.Depth))
}
args = append(args,
"--",
c.GitClientConfig.Remote, // repository
defaultBranch, // refspec
)
2024-06-06 05:51:34 +00:00
_, err = utils.ExecProcessInDir(c.logger, repoPath, "git", args...)
if err != nil {
return fmt.Errorf("failed to pull git repo %v: %v", repoPath, err)
}
} 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)
}
return nil
}