Skip to content

Commit

Permalink
Filter for dirty repositories
Browse files Browse the repository at this point in the history
  • Loading branch information
carhartl committed Oct 22, 2024
1 parent 1e000f1 commit 3274ab8
Show file tree
Hide file tree
Showing 3 changed files with 209 additions and 4 deletions.
48 changes: 47 additions & 1 deletion cmd/git-unsaved/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"bufio"
"io/fs"
"log"
"os"
Expand All @@ -11,6 +12,8 @@ import (
"github.com/charmbracelet/bubbles/spinner"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
git "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/format/gitignore"
"github.com/urfave/cli/v2"
)

Expand Down Expand Up @@ -101,7 +104,29 @@ func getRepos(sub chan repoMsg) tea.Cmd {
if err != nil {
return err
}
sub <- repoMsg{repo: repo{path: filepath.Dir(abspath)}}
repopath := filepath.Dir(abspath)

r, err := git.PlainOpen(repopath)
if err != nil {
return err
}

w, err := r.Worktree()
if err != nil {
return err
}

// Required until https://github.com/go-git/go-git/issues/1210 is fixed
addDefaultGitignoreToWorktree(w)

status, err := w.Status()
if err != nil {
return err
}

if !status.IsClean() {
sub <- repoMsg{repo: repo{path: repopath}}
}
}
return fs.SkipDir
}
Expand All @@ -120,6 +145,27 @@ func waitForRepoStatus(sub chan repoMsg) tea.Cmd {
}
}

func addDefaultGitignoreToWorktree(w *git.Worktree) {
home, err := os.UserHomeDir()
if err != nil {
panic(err)
}

//TODO: first try $XDG_CONFIG_HOME/git/ignore, then fall back to $HOME/.config/git/ignore
f, err := os.Open(filepath.Join(home, ".config", "git", "ignore"))
if err != nil {
panic(err)
}
defer f.Close()

sc := bufio.NewScanner(f)
sc.Split(bufio.ScanLines)
for sc.Scan() {
ignorePattern := sc.Text()
w.Excludes = append(w.Excludes, gitignore.ParsePattern(ignorePattern, nil))
}
}

func main() {
cli.AppHelpTemplate = `{{.Name}} - {{.Usage}}
Expand Down
23 changes: 22 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,39 @@ require (
)

require (
dario.cat/mergo v1.0.0 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/ProtonMail/go-crypto v1.0.0 // indirect
github.com/atotto/clipboard v0.1.4 // indirect
github.com/cloudflare/circl v1.3.7 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.5 // indirect
github.com/cyphar/filepath-securejoin v0.2.4 // indirect
github.com/emirpasic/gods v1.18.1 // indirect
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
github.com/go-git/go-billy/v5 v5.5.0 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
github.com/pjbgf/sha1cd v0.3.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/sahilm/fuzzy v0.1.1 // indirect
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect
github.com/skeema/knownhosts v1.2.2 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 // indirect
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/mod v0.12.0 // indirect
golang.org/x/net v0.22.0 // indirect
golang.org/x/tools v0.13.0 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
)

require (
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/charmbracelet/x/ansi v0.2.3 // indirect
github.com/charmbracelet/x/term v0.2.0 // indirect
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect
github.com/go-git/go-git/v5 v5.12.0
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-localereader v0.0.1 // indirect
Expand All @@ -32,5 +53,5 @@ require (
github.com/urfave/cli/v2 v2.27.5
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.24.0 // indirect
golang.org/x/text v0.3.8 // indirect
golang.org/x/text v0.14.0 // indirect
)
Loading

0 comments on commit 3274ab8

Please sign in to comment.