Skip to content

Commit 1bca6ce

Browse files
jasonkeeneBryan C. Mills
authored and
Bryan C. Mills
committed
cmd/go: ensure git attributes are set
This change disables the export-subst and export-ignore attributes when creating zip files for modules. This is done to prevent the ziphash for a given repo/revision from differing based on variables such as git version or size of repo. The full rational for this change is detailed here: #27153 (comment) Fixes #27153 Change-Id: Ib33f525d91d2581fa0b5d26e70d29620c7e685e9 Reviewed-on: https://go-review.googlesource.com/c/135175 Run-TryBot: Bryan C. Mills <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Bryan C. Mills <[email protected]>
1 parent fa179eb commit 1bca6ce

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

src/cmd/go/internal/modfetch/codehost/git.go

+44
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,10 @@ func (r *gitRepo) ReadZip(rev, subdir string, maxSize int64) (zip io.ReadCloser,
694694
return nil, "", err
695695
}
696696

697+
if err := ensureGitAttributes(r.dir); err != nil {
698+
return nil, "", err
699+
}
700+
697701
// Incredibly, git produces different archives depending on whether
698702
// it is running on a Windows system or not, in an attempt to normalize
699703
// text file line endings. Setting -c core.autocrlf=input means only
@@ -709,3 +713,43 @@ func (r *gitRepo) ReadZip(rev, subdir string, maxSize int64) (zip io.ReadCloser,
709713

710714
return ioutil.NopCloser(bytes.NewReader(archive)), "", nil
711715
}
716+
717+
// ensureGitAttributes makes sure export-subst and export-ignore features are
718+
// disabled for this repo. This is intended to be run prior to running git
719+
// archive so that zip files are generated that produce consistent ziphashes
720+
// for a given revision, independent of variables such as git version and the
721+
// size of the repo.
722+
//
723+
// See: https://github.com/golang/go/issues/27153
724+
func ensureGitAttributes(repoDir string) (err error) {
725+
const attr = "\n* -export-subst -export-ignore\n"
726+
727+
d := repoDir + "/info"
728+
p := d + "/attributes"
729+
730+
if err := os.MkdirAll(d, 0755); err != nil {
731+
return err
732+
}
733+
734+
f, err := os.OpenFile(p, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0666)
735+
if err != nil {
736+
return err
737+
}
738+
defer func() {
739+
closeErr := f.Close()
740+
if closeErr != nil {
741+
err = closeErr
742+
}
743+
}()
744+
745+
b, err := ioutil.ReadAll(f)
746+
if err != nil {
747+
return err
748+
}
749+
if !bytes.HasSuffix(b, []byte(attr)) {
750+
_, err := f.WriteString(attr)
751+
return err
752+
}
753+
754+
return nil
755+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
env GO111MODULE=on
2+
env GOPROXY=
3+
4+
# Testing that git export-subst is disabled
5+
[!net] skip
6+
[!exec:git] skip
7+
go build
8+
9+
-- x.go --
10+
package x
11+
12+
import _ "github.com/jasonkeene/export-subst"
13+
14+
-- go.mod --
15+
module x
16+
17+
require github.com/jasonkeene/export-subst v0.0.0-20180927204031-5845945ec626
18+
19+
-- go.sum --
20+
github.com/jasonkeene/export-subst v0.0.0-20180927204031-5845945ec626 h1:AUkXi/xFnm7lH2pgtvVkGb7buRn1ywFHw+xDpZ29Rz0=
21+
github.com/jasonkeene/export-subst v0.0.0-20180927204031-5845945ec626/go.mod h1:DwJXqVtrgrQkv3Giuf2Jh4YyubVe7y41S1eOIaysTJw=

0 commit comments

Comments
 (0)