From e9670d59a1ddfd46dc8ab55d2efc4d430dba7212 Mon Sep 17 00:00:00 2001 From: Massaki Archambault Date: Tue, 7 May 2024 23:58:14 -0400 Subject: [PATCH] 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 --- git/client.go | 24 ++++++++++++++++++++++++ git/clone.go | 13 ++++++++++--- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/git/client.go b/git/client.go index fa73f7d..4ab3b8d 100644 --- a/git/client.go +++ b/git/client.go @@ -10,6 +10,7 @@ import ( "time" "github.com/badjware/gitlabfs/fstree" + "github.com/badjware/gitlabfs/utils" "github.com/vmihailenco/taskq/v3" "github.com/vmihailenco/taskq/v3/memqueue" ) @@ -29,6 +30,10 @@ type gitClient struct { hostnameProg *regexp.Regexp + majorVersion int + minorVersion int + patchVersion string + queue taskq.Queue cloneTask *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{ Name: "git-clone", Handler: c.clone, diff --git a/git/clone.go b/git/clone.go index ac7ea3a..4efac57 100644 --- a/git/clone.go +++ b/git/clone.go @@ -16,12 +16,19 @@ func (c *gitClient) clone(url string, defaultBranch string, dst string) error { // Init the local repo fmt.Printf("Initializing %v into %v\n", url, dst) - _, err := utils.ExecProcess( - "git", "init", - "--initial-branch", defaultBranch, + args := []string{ + "init", + } + 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 ) + _, err := utils.ExecProcess("git", args...) if err != nil { return fmt.Errorf("failed to init git repo %v to %v: %v", url, dst, err) }