Skip to content

Commit 162ae19

Browse files
author
Aaron Schlesinger
committed
fix(git.go): remove plumbCommand
and simplify the logic to execute the git hook to remove the concurrency in the stdin copy logic cc/ @technosophos - I believe this is a cleaner way to run the git pre-receive hook, and I’ve verified that it works for commits with large packfiles
1 parent b784b7c commit 162ae19

1 file changed

Lines changed: 11 additions & 18 deletions

File tree

pkg/git/git.go

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,25 @@ func Receive(c cookoo.Context, p *cookoo.Params) (interface{}, cookoo.Interrupt)
104104
log.Debugf(c, "Working Dir: %s", cmd.Dir)
105105
log.Debugf(c, "Environment: %s", strings.Join(cmd.Env, ","))
106106

107-
if err := plumbCommand(cmd, channel, &errbuff); err != nil {
107+
inpipe, err := cmd.StdinPipe()
108+
if err != nil {
108109
return nil, err
109110
}
111+
cmd.Stdout = channel
112+
cmd.Stderr = io.MultiWriter(channel.Stderr(), &errbuff)
110113

111114
if err := cmd.Start(); err != nil {
112115
err = fmt.Errorf("Failed to start git pre-receive hook: %s (%s)", err, errbuff.Bytes())
113116
log.Warnf(c, err.Error())
114117
return nil, err
115118
}
119+
120+
if _, err := io.Copy(inpipe, channel); err != nil {
121+
err = fmt.Errorf("Failed to write git objects into the git pre-receive hook (%s)", err)
122+
log.Warnf(c, err.Error())
123+
return nil, err
124+
}
125+
116126
fmt.Println("Waiting for git-receive to run.")
117127
fmt.Println("Waiting for deploy.")
118128
if err := cmd.Wait(); err != nil {
@@ -140,23 +150,6 @@ func cleanRepoName(name string) (string, error) {
140150
return strings.TrimPrefix(strings.TrimSuffix(name, ".git"), "/"), nil
141151
}
142152

143-
// plumbCommand connects the exec in/output and the channel in/output.
144-
//
145-
// The sidechannel is for sending errors to logs.
146-
func plumbCommand(cmd *exec.Cmd, channel ssh.Channel, sidechannel io.Writer) error {
147-
inpipe, err := cmd.StdinPipe()
148-
if err != nil {
149-
return err
150-
}
151-
cmd.Stdout = channel
152-
cmd.Stderr = io.MultiWriter(channel.Stderr(), sidechannel)
153-
go func() {
154-
io.Copy(inpipe, channel)
155-
inpipe.Close()
156-
}()
157-
158-
}
159-
160153
var createLock sync.Mutex
161154

162155
// createRepo creates a new Git repo if it is not present already.

0 commit comments

Comments
 (0)