Debian Codegraph @

repository_extensions.go

raw
1 package git
2
3 import (
4 "errors"
5 "fmt"
6 "strings"
7
8 "github.com/go-git/go-git/v5/config"
9 cfgformat "github.com/go-git/go-git/v5/plumbing/format/config"
10 "github.com/go-git/go-git/v5/storage"
11 )
12
13 var (
14 // ErrUnsupportedExtensionRepositoryFormatVersion represents when an
15 // extension being used is not compatible with the repository's
16 // core.repositoryFormatVersion.
17 ErrUnsupportedExtensionRepositoryFormatVersion = errors.New("core.repositoryformatversion does not support extension")
18
19 // ErrUnsupportedRepositoryFormatVersion represents when an repository
20 // is using a format version that is not supported.
21 ErrUnsupportedRepositoryFormatVersion = errors.New("core.repositoryformatversion not supported")
22
23 // ErrUnknownExtension represents when a repository has an extension
24 // which is unknown or unsupported by go-git.
25 ErrUnknownExtension = errors.New("unknown extension")
26
27 // builtinExtensions defines the Git extensions that are supported by
28 // the core go-git implementation.
29 //
30 // Some extensions are storage-specific, those are defined by the Storers
31 // themselves by implementing the ExtensionChecker interface.
32 builtinExtensions = map[string]struct{}{
33 // noop does not change git’s behavior at all.
34 // It is useful only for testing format-1 compatibility.
35 //
36 // This extension is respected regardless of the
37 // core.repositoryFormatVersion setting.
38 "noop": {},
39
40 // noop-v1 does not change git’s behavior at all.
41 // It is useful only for testing format-1 compatibility.
42 "noop-v1": {},
43 }
44
45 // Some Git extensions were supported upstream before the introduction
46 // of repositoryformatversion. These are the only extensions that can be
47 // enabled while core.repositoryformatversion is unset or set to 0.
48 extensionsValidForV0 = map[string]struct{}{
49 "noop": {},
50 "partialClone": {},
51 "preciousObjects": {},
52 "worktreeConfig": {},
53 }
54 )
55
56 type extension struct {
57 name string
58 value string
59 }
60
61 func extensions(cfg *config.Config) []extension {
62 if cfg == nil || cfg.Raw == nil {
63 return nil
64 }
65
66 if !cfg.Raw.HasSection("extensions") {
67 return nil
68 }
69
70 section := cfg.Raw.Section("extensions")
71 out := make([]extension, 0, len(section.Options))
72 for _, opt := range section.Options {
73 out = append(out, extension{name: strings.ToLower(opt.Key), value: strings.ToLower(opt.Value)})
74 }
75
76 return out
77 }
78
79 func verifyExtensions(st storage.Storer, cfg *config.Config) error {
80 needed := extensions(cfg)
81
82 switch cfg.Core.RepositoryFormatVersion {
83 case "", cfgformat.Version_0, cfgformat.Version_1:
84 default:
85 return fmt.Errorf("%w: %q",
86 ErrUnsupportedRepositoryFormatVersion,
87 cfg.Core.RepositoryFormatVersion)
88 }
89
90 if len(needed) > 0 {
91 if cfg.Core.RepositoryFormatVersion == cfgformat.Version_0 ||
92 cfg.Core.RepositoryFormatVersion == "" {
93 var unsupported []string
94 for _, ext := range needed {
95 if _, ok := extensionsValidForV0[ext.name]; !ok {
96 unsupported = append(unsupported, ext.name)
97 }
98 }
99 if len(unsupported) > 0 {
100 return fmt.Errorf("%w: %s",
101 ErrUnsupportedExtensionRepositoryFormatVersion,
102 strings.Join(unsupported, ", "))
103 }
104 }
105
106 var missing []string
107 for _, ext := range needed {
108 if _, ok := builtinExtensions[ext.name]; ok {
109 continue
110 }
111
112 missing = append(missing, ext.name)
113 }
114
115 if len(missing) > 0 {
116 return fmt.Errorf("%w: %s", ErrUnknownExtension, strings.Join(missing, ", "))
117 }
118 }
119
120 return nil
121 }