Files
byre/internal/git/ignore.go
T

38 lines
796 B
Go

package git
import (
"bytes"
"errors"
"fmt"
"os/exec"
"strings"
)
func Ignored(worktree string, paths []string) (map[string]bool, error) {
ignored := map[string]bool{}
if len(paths) == 0 {
return ignored, nil
}
cmd := exec.Command("git", "check-ignore", "--stdin", "-z")
cmd.Dir = worktree
cmd.Stdin = strings.NewReader(strings.Join(paths, "\x00") + "\x00")
stderr := &strings.Builder{}
cmd.Stderr = stderr
out, err := cmd.Output()
exit := &exec.ExitError{}
if errors.As(err, &exit) && exit.ExitCode() == 1 {
return ignored, nil
}
if err != nil {
return nil, fmt.Errorf("git check-ignore: %w: %s", err, strings.TrimSpace(stderr.String()))
}
for _, p := range bytes.Split(out, []byte{0}) {
if len(p) > 0 {
ignored[string(p)] = true
}
}
return ignored, nil
}