From c10249b8a6033a6cd59f1c47048a24b9492d3d65 Mon Sep 17 00:00:00 2001 From: Eric Ju Date: Tue, 5 Dec 2023 11:41:09 -0400 Subject: [PATCH] git: Add GitattributesSupportReadingFromHead flag Git 2.43.0 starts supporting reading gitattributes from HEAD reference. This is a required feature for us to get rid of gitattributes-related RPCs. This `GitattributesSupportReadingFromHead` flag function check if current git version is above or equal 2.43.0. --- internal/git/version.go | 8 ++++++++ internal/git/version_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/internal/git/version.go b/internal/git/version.go index 4a552b62cf..e0d81043a3 100644 --- a/internal/git/version.go +++ b/internal/git/version.go @@ -109,6 +109,14 @@ func (v Version) LessThan(other Version) bool { } } +// GitattributesSupportReadingFromHead detects whether the Git version supports reading +// gitattributes from HEAD reference in bare repositories automatically. +func (v Version) GitattributesSupportReadingFromHead() bool { + return !v.LessThan(Version{ + major: 2, minor: 43, + }) +} + func parseVersion(versionStr string) (Version, error) { versionSplit := strings.SplitN(versionStr, ".", 4) if len(versionSplit) < 3 { diff --git a/internal/git/version_test.go b/internal/git/version_test.go index c9a0db8fb5..e7374da77f 100644 --- a/internal/git/version_test.go +++ b/internal/git/version_test.go @@ -122,3 +122,29 @@ func TestVersion_IsSupported(t *testing.T) { }) } } + +func TestVersion_GitattributesSupportReadingFromHead(t *testing.T) { + t.Parallel() + + for _, tc := range []struct { + version string + expect bool + }{ + {"1.0.0", false}, + {"2.40.2", false}, + {"2.41.0", false}, + {"2.42.0", false}, + {"2.42.2", false}, + {"2.43.0", true}, + {"2.43.1.gl2", true}, + {"3.0.0", true}, + } { + tc := tc + t.Run(tc.version, func(t *testing.T) { + version, err := parseVersion(tc.version) + require.NoError(t, err) + require.Equal(t, tc.expect, + version.GitattributesSupportReadingFromHead()) + }) + } +} -- GitLab