Skip to content

Commit fe6f482

Browse files
authored
fix(pkg/git/git.go): write the pre-receive hook script directly to the file descriptor
Fixes #321
1 parent 80d8cf9 commit fe6f482

5 files changed

Lines changed: 36 additions & 8 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ vendor/
33
rootfs/usr/bin/
44
./builder
55
coverage.txt
6+
testdata/hooks/pre-receive

pkg/git/git.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"errors"
88
"fmt"
99
"io"
10-
"io/ioutil"
1110
"os"
1211
"os/exec"
1312
"path/filepath"
@@ -158,16 +157,20 @@ func createRepo(repoPath string) (bool, error) {
158157

159158
// createPreReceiveHook renders preReceiveHookTpl to repoPath/hooks/pre-receive
160159
func createPreReceiveHook(gitHome, repoPath string) error {
161-
// parse & generate the template anew each receive for each new git home
162-
var hookByteBuf bytes.Buffer
163-
if err := preReceiveHookTpl.Execute(&hookByteBuf, map[string]string{"GitHome": gitHome}); err != nil {
164-
return err
160+
writePath := filepath.Join(repoPath, "hooks", "pre-receive")
161+
fd, err := os.Create(writePath)
162+
if err != nil {
163+
return fmt.Errorf("Cannot create pre-receive hook file at %s (%s)", writePath, err)
165164
}
165+
defer fd.Close()
166166

167-
writePath := filepath.Join(repoPath, "hooks", "pre-receive")
168-
log.Info("Writing pre-receive hook to %s", writePath)
169-
if err := ioutil.WriteFile(writePath, hookByteBuf.Bytes(), 0755); err != nil {
167+
// parse & generate the template anew each receive for each new git home
168+
if err := preReceiveHookTpl.Execute(fd, map[string]string{"GitHome": gitHome}); err != nil {
170169
return fmt.Errorf("Cannot write pre-receive hook to %s (%s)", writePath, err)
171170
}
171+
if err := os.Chmod(writePath, 0755); err != nil {
172+
return fmt.Errorf("Cannot change pre-receive hook script permissions (%s)", err)
173+
}
174+
172175
return nil
173176
}

pkg/git/git_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package git
2+
3+
import (
4+
"fmt"
5+
"io/ioutil"
6+
"os"
7+
"path/filepath"
8+
"strings"
9+
"testing"
10+
11+
"github.com/arschles/assert"
12+
)
13+
14+
func TestCreatePreReceiveHook(t *testing.T) {
15+
const gitHome = "TestGitHome"
16+
gopath := os.Getenv("GOPATH")
17+
repoPath := filepath.Join(gopath, "src", "github.com", "deis", "builder", "testdata")
18+
assert.NoErr(t, createPreReceiveHook(gitHome, repoPath))
19+
hookBytes, err := ioutil.ReadFile(filepath.Join(repoPath, "hooks", "pre-receive"))
20+
assert.NoErr(t, err)
21+
hookStr := string(hookBytes)
22+
gitHomeIdx := strings.Index(hookStr, fmt.Sprintf("GIT_HOME=%s", gitHome))
23+
assert.False(t, gitHomeIdx == -1, "GIT_HOME was not found")
24+
}

testdata/.gitkeep

Whitespace-only changes.

testdata/hooks/.gitkeep

Whitespace-only changes.

0 commit comments

Comments
 (0)