forked from keybase/kbfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit_config_without_remotes_storer.go
105 lines (93 loc) · 2.97 KB
/
git_config_without_remotes_storer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright 2017 Keybase Inc. All rights reserved.
// Use of this source code is governed by a BSD
// license that can be found in the LICENSE file.
package libgit
import (
"github.com/keybase/kbfs/libfs"
gogitcfg "gopkg.in/src-d/go-git.v4/config"
format "gopkg.in/src-d/go-git.v4/plumbing/format/config"
"gopkg.in/src-d/go-git.v4/plumbing/storer"
"gopkg.in/src-d/go-git.v4/storage"
"gopkg.in/src-d/go-git.v4/storage/filesystem"
)
// GitConfigWithoutRemotesStorer strips remotes from the config before
// writing them to disk, to work around a gcfg bug (used by go-git
// when reading configs from disk) that causes a freakout when it sees
// backslashes in git file URLs.
type GitConfigWithoutRemotesStorer struct {
*filesystem.Storage
cfg *gogitcfg.Config
stored bool
}
// NewGitConfigWithoutRemotesStorer creates a new git config
// implementation that strips remotes from the config before writing
// them to disk.
func NewGitConfigWithoutRemotesStorer(fs *libfs.FS) (
*GitConfigWithoutRemotesStorer, error) {
fsStorer, err := filesystem.NewStorage(fs)
if err != nil {
return nil, err
}
cfg, err := fsStorer.Config()
if err != nil {
return nil, err
}
// To figure out if this config has been written already, check if
// it differs from the zero Core value (probably because the
// IsBare bit is flipped).
return &GitConfigWithoutRemotesStorer{
fsStorer,
cfg,
cfg.Core != gogitcfg.Config{}.Core,
}, nil
}
// Init implements the `storer.Initializer` interface.
func (cwrs *GitConfigWithoutRemotesStorer) Init() error {
return cwrs.Storage.Init()
}
// Config implements the `storer.Storer` interface.
func (cwrs *GitConfigWithoutRemotesStorer) Config() (*gogitcfg.Config, error) {
return cwrs.cfg, nil
}
// SetConfig implements the `storer.Storer` interface.
func (cwrs *GitConfigWithoutRemotesStorer) SetConfig(c *gogitcfg.Config) (
err error) {
if cwrs.stored && c.Core == cwrs.cfg.Core {
// Ignore any change that doesn't change the core we know
// about, to avoid attempting to write config files with
// read-only access.
return nil
}
defer func() {
if err != nil {
cwrs.stored = true
}
}()
cwrs.cfg = c
if len(c.Remotes) != 0 {
// If there are remotes, we need to strip them out before
// writing them out to disk. Do that by making a copy of
// everything but the remotes. (Note that we can't just
// Marshal+Unmarshal for a deep-copy, since Unmarshal is where
// the gcfg bug is.)
cCopy := gogitcfg.NewConfig()
cCopy.Core = c.Core
for k, v := range c.Submodules {
v2 := *v
cCopy.Submodules[k] = &v2
}
// Get the raw config so we don't lose any unsupported fields
// from c, but clear out the remotes.
_, err := c.Marshal()
if err != nil {
return err
}
s := c.Raw.Section("remote")
s.Subsections = make(format.Subsections, 0)
cCopy.Raw = c.Raw
c = cCopy
}
return cwrs.Storage.SetConfig(c)
}
var _ storage.Storer = (*GitConfigWithoutRemotesStorer)(nil)
var _ storer.Initializer = (*GitConfigWithoutRemotesStorer)(nil)