diff --git a/internal/git/version.go b/internal/git/version.go index 4a552b62cf52cbaf1aa5c30bad2a10076b26b71b..e0d81043a341467ed955063efec14532c0484cea 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 c9a0db8fb5f4462a8d46862f058d4a19643f40ec..e7374da77f35e0d625f500883bd4a70fb5e0a751 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()) + }) + } +}