code: func (c *hookCmd) runGofmt() (files []string, err error) {
  	repo, err := repoRoot()
  	if err != nil {
  		return nil, err
  	}
  	if !strings.HasSuffix(repo, string(filepath.Separator)) {
  		repo += string(filepath.Separator)
  	}
  
  	out, err := cmdOutputDirErr(".", "git", "diff-index", "--name-only", "--diff-filter=ACM", "--cached", "HEAD", "--", ":(glob)**/*.go", ":!/vendor/")
  	if err != nil {
  		return nil, err
  	}
  	indexFiles := addRoot(repo, nonBlankLines(out))
  	if len(indexFiles) == 0 {
  		return
  	}
  
  	args := []string{"-l"}
  	// TODO(mpl): it would be nice to TrimPrefix the pwd from each file to get a shorter output.
  	// However, since git sets the pwd to GIT_DIR before running the pre-commit hook, we lost
  	// the actual pwd from when we ran `git commit`, so no dice so far.
  	for _, file := range indexFiles {
  		args = append(args, file)
  	}
  
  	if c.verbose {
  		fmt.Fprintln(cmdmain.Stderr, commandString("gofmt", args))
  	}
  	cmd := exec.Command("gofmt", args...)
  	var stdout, stderr bytes.Buffer
  	cmd.Stdout = &stdout
  	cmd.Stderr = &stderr
  	err = cmd.Run()
  
  	if err != nil {
  		// Error but no stderr: usually can't find gofmt.
  		if stderr.Len() == 0 {
  			return nil, fmt.Errorf("invoking gofmt: %v", err)
  		}
  		return nil, fmt.Errorf("%s: %v", stderr.String(), err)
  	}
  
  	// Build file list.
  	files = lines(stdout.String())
  	sort.Strings(files)
  	return files, nil
  }
docs: // runGofmt runs the external gofmt command over the local version of staged files.
  // It returns the files that need gofmting.