Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions internal/git/.gitcommitsummaryignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
*-lock.*
*.lock
.yarn/releases/**
**/build/**
**/dist/**
**/target/**
**/out/**
go.sum
13 changes: 4 additions & 9 deletions internal/git/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package git
import (
"bytes"
"context"
_ "embed"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -122,16 +123,10 @@ func (c *Client) diffArgs(color, exclude bool) []string {
if exclude {
args = append(args,
"--diff-filter=ACMRTUXBD",
"--", // separates options from pathspecs
".", // include everything under the repo root
":(exclude)*-lock.*", // package-lock.json, pnpm-lock.yaml, etc.
":(exclude)*.lock", // yarn.lock, poetry.lock, Cargo.lock, etc.
":(exclude)**/build/**",
":(exclude)**/dist/**",
":(exclude)**/target/**",
":(exclude)**/out/**",
":(exclude)go.sum",
"--", // separates options from pathspecs
".", // include everything under the repo root
)
args = append(args, exclusions()...)
}
return args
}
Expand Down
120 changes: 120 additions & 0 deletions internal/git/exclusions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package git

import (
_ "embed"
"os"
"path"
"path/filepath"
"strings"

"github.com/adrg/xdg"
)

//go:embed .gitcommitsummaryignore
var defaultGitCommitSummaryIgnore string

func exclusions() []string {
var excludes []string
for _, content := range []string{
defaultGitCommitSummaryIgnore,
userHomeGitCommitSummaryIgnore(),
projectGitCommitSummaryIgnore(),
xdgConfigGitCommitSummaryIgnore(),
} {
for line := range strings.SplitSeq(content, "\n") {
// remove any comment (hash or double-slash) at the end of the line
if idx := strings.Index(line, "#"); idx != -1 {
line = line[:idx]
}

if idx := strings.Index(line, "//"); idx != -1 {
line = line[:idx]
}

line = strings.TrimSpace(line)
if line == "" {
continue
}

if !validateIgnorePattern(line) {
continue
}

excludes = append(excludes, ":(exclude)"+line)
}
}
return dedupe(excludes)
}

func dedupe(excludes []string) []string {
seen := make(map[string]struct{})
var result []string
for _, exclude := range excludes {
if _, ok := seen[exclude]; !ok {
seen[exclude] = struct{}{}
result = append(result, exclude)
}
}
return result
}

func projectGitCommitSummaryIgnore() string {
cwd, err := os.Getwd()
if err != nil {
return ""
}

for {
ignorePath := filepath.Join(cwd, ".gitcommitsummaryignore")
if data, err := os.ReadFile(ignorePath); err == nil {
return string(data)
}

parent := filepath.Dir(cwd)
if parent == cwd {
break
}
cwd = parent
}
Comment thread
rm-hull marked this conversation as resolved.

return ""
}

func userHomeGitCommitSummaryIgnore() string {
homeDir, err := os.UserHomeDir()
if err != nil {
return ""
}
return readFileString(filepath.Join(homeDir, ".gitcommitsummaryignore"))
}

func xdgConfigGitCommitSummaryIgnore() string {
config, err := xdg.SearchConfigFile("git-commit-summary/.gitcommitsummaryignore")
if err != nil {
return ""
}
return readFileString(config)
}
Comment thread
rm-hull marked this conversation as resolved.

func validateIgnorePattern(pattern string) bool {
if strings.ContainsRune(pattern, 0) {
return false
}
if strings.HasPrefix(pattern, "!") {
return false
}
if strings.Contains(pattern, "\\") {
return false
}

_, err := path.Match(pattern, pattern)
return err == nil
}

func readFileString(path string) string {
data, err := os.ReadFile(path)
if err != nil {
return ""
}
return string(data)
}
53 changes: 53 additions & 0 deletions internal/git/exclusions_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package git

import (
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
)

func TestProjectGitCommitSummaryIgnore(t *testing.T) {
tempDir := t.TempDir()
repoDir := filepath.Join(tempDir, "repo", "subdir")
assert.NoError(t, os.MkdirAll(repoDir, 0o755))

ignoreFile := filepath.Join(tempDir, "repo", ".gitcommitsummaryignore")
assert.NoError(t, os.WriteFile(ignoreFile, []byte("ignored-file.txt\n"), 0o644))

t.Chdir(repoDir)

actual := projectGitCommitSummaryIgnore()
assert.Equal(t, "ignored-file.txt\n", actual)
}

func TestUserHomeGitCommitSummaryIgnore(t *testing.T) {
tempHome := t.TempDir()
t.Setenv("HOME", tempHome)
t.Setenv("USERPROFILE", tempHome)

ignoreFile := filepath.Join(tempHome, ".gitcommitsummaryignore")
assert.NoError(t, os.WriteFile(ignoreFile, []byte("*.secret\n"), 0o644))

actual := userHomeGitCommitSummaryIgnore()
assert.Equal(t, "*.secret\n", actual)
}

func TestValidateIgnorePattern_InvalidGlob(t *testing.T) {
assert.False(t, validateIgnorePattern("[unclosed.glob"))
}

func TestDedupeExclusions(t *testing.T) {
excludes := []string{
":(exclude)foo.txt",
":(exclude)bar.txt",
":(exclude)foo.txt",
}

result := dedupe(excludes)
assert.Equal(t, []string{
":(exclude)foo.txt",
":(exclude)bar.txt",
}, result)
}
Loading