Skip to content

Commit 8f39302

Browse files
authored
Merge pull request #82 from getsolus/fix-git-clone
git: Use blobless clone instead of shallow clones
2 parents b1ea794 + c452ef6 commit 8f39302

File tree

1 file changed

+29
-44
lines changed

1 file changed

+29
-44
lines changed

builder/source/git.go

Lines changed: 29 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -72,50 +72,40 @@ func (g *GitSource) submodules() error {
7272

7373
cmd.Dir = g.ClonePath
7474
cmd.Stdout = os.Stdout
75-
cmd.Stderr = os.Stderr
75+
cmd.Stderr = os.Stdout
7676

7777
return cmd.Run()
7878
}
7979

80-
// clone shallow clones an upstream git repository to the local disk.
81-
func clone(uri, path, ref string) error {
82-
var cmd *exec.Cmd
83-
84-
// Check if the reference is a commit
85-
if len(ref) == 40 {
86-
// Init a new repository at the checkout path
87-
initCmd := exec.Command("git", "init", path)
88-
89-
if err := initCmd.Run(); err != nil {
90-
return err
91-
}
80+
// For some reason git blobless clones create pack files with 600 permissions. These break future operations
81+
// as those files cannot be read by non-root users. Fix those permissions so things work as they should.
82+
func (g *GitSource) fixPermissions() error {
83+
cmd := exec.Command("bash", "-c", "chmod +r .git/objects/pack/*.promisor")
9284

93-
// Set the default remote to the upstream URI
94-
addRemoteCmd := exec.Command("git", "remote", "add", "origin", uri)
95-
addRemoteCmd.Dir = path
85+
cmd.Dir = g.ClonePath
9686

97-
if err := addRemoteCmd.Run(); err != nil {
98-
return err
99-
}
87+
return cmd.Run()
88+
}
10089

101-
// Shallow fetch the reference we want
102-
fetchCmd := exec.Command("git", "fetch", "--depth", "1", "origin", ref)
103-
fetchCmd.Dir = path
90+
// clone shallow clones an upstream git repository to the local disk.
91+
func clone(uri, path, ref string) error {
92+
var cmd *exec.Cmd
10493

105-
if err := fetchCmd.Run(); err != nil {
106-
return err
107-
}
94+
// Create a blobless clone without checking out a ref
95+
initCmd := exec.Command("git", "clone", "--filter=blob:none", "--no-checkout", uri, path)
96+
initCmd.Stdout = os.Stdout
97+
initCmd.Stderr = os.Stdout
10898

109-
// Set the next command to run to checkout the head
110-
cmd = exec.Command("git", "checkout", "FETCH_HEAD")
111-
cmd.Dir = path
112-
} else {
113-
// Not a git commit, so shallow clone the repo at the reference
114-
cmd = exec.Command("git", "clone", "--depth", "1", "--branch", ref, uri, path)
99+
if err := initCmd.Run(); err != nil {
100+
return err
115101
}
116102

103+
// Checkout the ref we want
104+
cmd = exec.Command("git", "switch", "--discard-changes", "--recurse-submodules", "--detach", ref)
105+
cmd.Dir = path
106+
117107
cmd.Stdout = os.Stdout
118-
cmd.Stderr = os.Stderr
108+
cmd.Stderr = os.Stdout
119109

120110
return cmd.Run()
121111
}
@@ -125,27 +115,17 @@ func clone(uri, path, ref string) error {
125115
func reset(path, ref string) error {
126116
fetchArgs := []string{
127117
"fetch",
128-
"--depth",
129-
"1",
130118
"origin",
131119
}
132120

133-
// We have to add the tag keyword if the ref is a tag, otherwise
134-
// git won't actually fetch the tag
135-
if len(ref) != 40 {
136-
fetchArgs = append(fetchArgs, "tag")
137-
}
138-
139-
fetchArgs = append(fetchArgs, ref)
140-
141121
fetchCmd := exec.Command("git", fetchArgs...)
142122
fetchCmd.Dir = path
143123

144124
if err := fetchCmd.Run(); err != nil {
145125
return err
146126
}
147127

148-
resetCmd := exec.Command("git", "reset", "--hard", ref)
128+
resetCmd := exec.Command("git", "switch", "--discard-changes", "--recurse-submodules", "--detach", ref)
149129
resetCmd.Dir = path
150130

151131
return resetCmd.Run()
@@ -167,7 +147,12 @@ func (g *GitSource) Fetch() error {
167147
}
168148

169149
// Check out submodules
170-
return g.submodules()
150+
err := g.submodules()
151+
if err != nil {
152+
return err
153+
}
154+
155+
return g.fixPermissions()
171156
}
172157

173158
// IsFetched will check if we have the ref available, if not it will return

0 commit comments

Comments
 (0)