Skip to content

Commit e1e06f0

Browse files
arschlesAaron Schlesinger
authored andcommitted
fix(git.go): split repo creation from pre-receive hook generation
and add more debug logs, better error handling
1 parent 13abd91 commit e1e06f0

1 file changed

Lines changed: 31 additions & 15 deletions

File tree

pkg/git/git.go

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,21 @@ func Receive(c cookoo.Context, p *cookoo.Params) (interface{}, cookoo.Interrupt)
7171
}
7272
repo += ".git"
7373

74-
if _, err := createRepo(c, filepath.Join(gitHome, repo), gitHome); err != nil {
75-
log.Infof(c, "Did not create new repo: %s", err)
74+
repoPath := filepath.Join(gitHome, repo)
75+
log.Debugf(c, "creating repo directory %s", repoPath)
76+
if _, err := createRepo(c, repoPath); err != nil {
77+
err = fmt.Errorf("Did not create new repo (%s)", err)
78+
log.Warnf(c, err.Error())
79+
return nil, err
80+
}
81+
82+
log.Debugf(c, "writing pre-receive hook under %s", repoPath)
83+
if err := createPreReceiveHook(c, gitHome, repoPath); err != nil {
84+
err = fmt.Errorf("Did not write pre-receive hook (%s)", err)
85+
log.Warnf(c, err.Error())
86+
return nil, err
7687
}
88+
7789
cmd := exec.Command("git-shell", "-c", fmt.Sprintf("%s '%s'", operation, repo))
7890
log.Infof(c, strings.Join(cmd.Args, " "))
7991

@@ -148,7 +160,7 @@ var createLock sync.Mutex
148160
//
149161
// Returns a bool indicating whether a project was created (true) or already
150162
// existed (false).
151-
func createRepo(c cookoo.Context, repoPath, gitHome string) (bool, error) {
163+
func createRepo(c cookoo.Context, repoPath string) (bool, error) {
152164
createLock.Lock()
153165
defer createLock.Unlock()
154166

@@ -171,21 +183,25 @@ func createRepo(c cookoo.Context, repoPath, gitHome string) (bool, error) {
171183
return false, err
172184
}
173185

174-
// parse & generate the template anew each receive for each new git home
175-
var hookByteBuf bytes.Buffer
176-
if err := preReceiveHookTpl.Execute(&hookByteBuf, map[string]string{"GitHome": gitHome}); err != nil {
177-
return true, err
178-
}
179-
180-
writePath := filepath.Join(repoPath, "hooks", "pre-receive")
181-
log.Debugf(c, "Writing pre-receive hook to %s", writePath)
182-
if err := ioutil.WriteFile(writePath, hookByteBuf.Bytes(), 0755); err != nil {
183-
return false, fmt.Errorf("Cannot write pre-receive hook to %s (%s)", writePath, err)
184-
}
185-
186186
return true, nil
187187
} else if err == nil {
188188
return false, errors.New("Expected directory, found file.")
189189
}
190190
return false, err
191191
}
192+
193+
// createPreReceiveHook renders preReceiveHookTpl to repoPath/hooks/pre-receive
194+
func createPreReceiveHook(c cookoo.Context, gitHome, repoPath string) error {
195+
// parse & generate the template anew each receive for each new git home
196+
var hookByteBuf bytes.Buffer
197+
if err := preReceiveHookTpl.Execute(&hookByteBuf, map[string]string{"GitHome": gitHome}); err != nil {
198+
return err
199+
}
200+
201+
writePath := filepath.Join(repoPath, "hooks", "pre-receive")
202+
log.Debugf(c, "Writing pre-receive hook to %s", writePath)
203+
if err := ioutil.WriteFile(writePath, hookByteBuf.Bytes(), 0755); err != nil {
204+
return fmt.Errorf("Cannot write pre-receive hook to %s (%s)", writePath, err)
205+
}
206+
return nil
207+
}

0 commit comments

Comments
 (0)