Check if git support --initial-branch before attempting to use it on init

`git init --initial-branch` was only added in git
version 0.28.0. We parse the git version and
check if the argument is supported before using it
Fixes #8
This commit is contained in:
Massaki Archambault 2024-05-07 23:58:14 -04:00
parent 324be5f1f6
commit 0ece3f05a3
2 changed files with 34 additions and 3 deletions

View File

@ -10,6 +10,7 @@ import (
"time" "time"
"github.com/badjware/gitlabfs/fstree" "github.com/badjware/gitlabfs/fstree"
"github.com/badjware/gitlabfs/utils"
"github.com/vmihailenco/taskq/v3" "github.com/vmihailenco/taskq/v3"
"github.com/vmihailenco/taskq/v3/memqueue" "github.com/vmihailenco/taskq/v3/memqueue"
) )
@ -29,6 +30,10 @@ type gitClient struct {
hostnameProg *regexp.Regexp hostnameProg *regexp.Regexp
majorVersion int
minorVersion int
patchVersion string
queue taskq.Queue queue taskq.Queue
cloneTask *taskq.Task cloneTask *taskq.Task
pullTask *taskq.Task pullTask *taskq.Task
@ -50,6 +55,25 @@ func NewClient(p GitClientParam) (*gitClient, error) {
}), }),
} }
// Parse git version
gitVersionOutput, err := utils.ExecProcess("git", "--version")
if err != nil {
return nil, fmt.Errorf("failed to run \"git --version\": %v", err)
}
prog := regexp.MustCompile(`([0-9]+)\.([0-9]+)\.(.+)`)
gitVersionMatches := prog.FindStringSubmatch(gitVersionOutput)
c.majorVersion, err = strconv.Atoi(gitVersionMatches[1])
if err != nil {
return nil, fmt.Errorf("failed to parse git major version \"%v\": %v", gitVersionOutput, err)
}
c.minorVersion, err = strconv.Atoi(gitVersionMatches[2])
if err != nil {
return nil, fmt.Errorf("failed to parse git minor version \"%v\": %v", gitVersionOutput, err)
}
c.patchVersion = gitVersionMatches[3]
fmt.Printf("Detected git version: major = %v minor = %v patch = %v\n", c.majorVersion, c.minorVersion, c.patchVersion)
// Register tasks
c.cloneTask = taskq.RegisterTask(&taskq.TaskOptions{ c.cloneTask = taskq.RegisterTask(&taskq.TaskOptions{
Name: "git-clone", Name: "git-clone",
Handler: c.clone, Handler: c.clone,

View File

@ -16,12 +16,19 @@ func (c *gitClient) clone(url string, defaultBranch string, dst string) error {
// Init the local repo // Init the local repo
fmt.Printf("Initializing %v into %v\n", url, dst) fmt.Printf("Initializing %v into %v\n", url, dst)
_, err := utils.ExecProcess( args := []string{
"git", "init", "init",
"--initial-branch", defaultBranch, }
if c.majorVersion > 2 || c.majorVersion == 2 && c.minorVersion >= 28 {
args = append(args, "--initial-branch", defaultBranch)
} else {
fmt.Printf("Version of git is too old to support --initial-branch. Consider upgrading git to version >= 2.28.0")
}
args = append(args,
"--", "--",
dst, // directory dst, // directory
) )
_, err := utils.ExecProcess("git", args...)
if err != nil { if err != nil {
return fmt.Errorf("failed to init git repo %v to %v: %v", url, dst, err) return fmt.Errorf("failed to init git repo %v to %v: %v", url, dst, err)
} }