IsIgnored reports whether the given path is ignored by .gitignore rules. Returns an error for fatal git failures (e.g. path outside repository).
(ctx context.Context, path string)
| 791 | // IsIgnored reports whether the given path is ignored by .gitignore rules. |
| 792 | // Returns an error for fatal git failures (e.g. path outside repository). |
| 793 | func (c *Client) IsIgnored(ctx context.Context, path string) (bool, error) { |
| 794 | cmd, err := c.Command(ctx, "check-ignore", "-q", "--", path) |
| 795 | if err != nil { |
| 796 | return false, err |
| 797 | } |
| 798 | _, err = cmd.Output() |
| 799 | if err == nil { |
| 800 | return true, nil |
| 801 | } |
| 802 | // Exit 1 here means we can confirm the path is not ignored. |
| 803 | // Any other error is a real git error. |
| 804 | var exitErr *exec.ExitError |
| 805 | if errors.As(err, &exitErr) && exitErr.ExitCode() == 1 { |
| 806 | return false, nil |
| 807 | } |
| 808 | return false, err |
| 809 | } |
| 810 | |
| 811 | // ShortSHA returns the first 8 characters of a SHA hash for display purposes. |
| 812 | func ShortSHA(sha string) string { |