MCPcopy Index your code
hub / github.com/perkeep/perkeep / runGofmt

Method runGofmt

dev/devcam/hook.go:182–227  ·  view source on GitHub ↗

runGofmt runs the external gofmt command over the local version of staged files. It returns the files that need gofmting.

()

Source from the content-addressed store, hash-verified

180// runGofmt runs the external gofmt command over the local version of staged files.
181// It returns the files that need gofmting.
182func (c *hookCmd) runGofmt() (files []string, err error) {
183 repo, err := repoRoot()
184 if err != nil {
185 return nil, err
186 }
187 if !strings.HasSuffix(repo, string(filepath.Separator)) {
188 repo += string(filepath.Separator)
189 }
190
191 out, err := cmdOutputDirErr(".", "git", "diff-index", "--name-only", "--diff-filter=ACM", "--cached", "HEAD", "--", ":(glob)**/*.go", ":!/vendor/")
192 if err != nil {
193 return nil, err
194 }
195 indexFiles := addRoot(repo, nonBlankLines(out))
196 if len(indexFiles) == 0 {
197 return
198 }
199
200 args := []string{"-l"}
201 // TODO(mpl): it would be nice to TrimPrefix the pwd from each file to get a shorter output.
202 // However, since git sets the pwd to GIT_DIR before running the pre-commit hook, we lost
203 // the actual pwd from when we ran `git commit`, so no dice so far.
204 args = append(args, indexFiles...)
205
206 if c.verbose {
207 fmt.Fprintln(cmdmain.Stderr, commandString("gofmt", args))
208 }
209 cmd := exec.Command("gofmt", args...)
210 var stdout, stderr bytes.Buffer
211 cmd.Stdout = &stdout
212 cmd.Stderr = &stderr
213 err = cmd.Run()
214
215 if err != nil {
216 // Error but no stderr: usually can't find gofmt.
217 if stderr.Len() == 0 {
218 return nil, fmt.Errorf("invoking gofmt: %v", err)
219 }
220 return nil, fmt.Errorf("%s: %v", stderr.String(), err)
221 }
222
223 // Build file list.
224 files = lines(stdout.String())
225 sort.Strings(files)
226 return files, nil
227}
228
229func printf(format string, args ...interface{}) {
230 cmdmain.Errorf(format, args...)

Callers 1

hookGofmtMethod · 0.95

Calls 10

repoRootFunction · 0.85
cmdOutputDirErrFunction · 0.85
addRootFunction · 0.85
nonBlankLinesFunction · 0.85
commandStringFunction · 0.85
linesFunction · 0.85
CommandMethod · 0.65
RunMethod · 0.65
LenMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected