chore: import official kitty sources from upstream
Bring the kovidgoyal/kitty codebase in as the base of FWS-Kitty, vendored at upstream commit 91fb38a without its git history.
This commit is contained in:
commit
7e7d249099
1134 files changed
+433629
No files matched your search
@@ -0,0 +1,32 @@
|
||||
package ignorefiles
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"io/fs"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var _ = fmt.Print
|
||||
|
||||
type IgnoreFile interface {
|
||||
Len() int // number of rules
|
||||
LoadString(string) error
|
||||
LoadBytes([]byte) error
|
||||
LoadLines(...string) error
|
||||
LoadFile(io.Reader) error
|
||||
LoadPath(string) error
|
||||
|
||||
// relpath is the path relative to the directory containing the ignorefile.
|
||||
// When the result is due to a rule matching, linenum_of_matching_rule is
|
||||
// >=0 and pattern is the textual representation of the rule. Otherwise
|
||||
// linenum_of_matching_rule is -1 and pattern is the empty string.
|
||||
IsIgnored(relpath string, ftype fs.FileMode) (is_ignored bool, linenum_of_matching_rule int, pattern string)
|
||||
}
|
||||
|
||||
func NewGitignore() IgnoreFile { return &Gitignore{index_of_last_negated_rule: -1} }
|
||||
|
||||
// The global gitignore from ~/.config/git/ignore
|
||||
var GlobalGitignore = sync.OnceValue(func() IgnoreFile {
|
||||
return get_global_gitignore()
|
||||
})
|
||||
@@ -0,0 +1,313 @@
|
||||
package ignorefiles
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"github.com/kovidgoyal/kitty/tools/utils"
|
||||
)
|
||||
|
||||
var _ = fmt.Print
|
||||
|
||||
type GitPattern struct {
|
||||
line_number int
|
||||
only_dirs bool
|
||||
negated bool
|
||||
pattern string
|
||||
parts []string
|
||||
matcher func(path string) bool
|
||||
}
|
||||
|
||||
type Gitignore struct {
|
||||
patterns []GitPattern
|
||||
index_of_last_negated_rule int
|
||||
line_number_offset int
|
||||
}
|
||||
|
||||
func (g Gitignore) Len() int { return len(g.patterns) }
|
||||
|
||||
func (g Gitignore) IsIgnored(relpath string, ftype os.FileMode) (is_ignored bool, linenum_of_matching_rule int, pattern string) {
|
||||
if os.PathSeparator != '/' {
|
||||
relpath = strings.ReplaceAll(relpath, string(os.PathSeparator), "/")
|
||||
}
|
||||
linenum_of_matching_rule = -1
|
||||
for i, pat := range g.patterns {
|
||||
if is_ignored {
|
||||
if i > g.index_of_last_negated_rule {
|
||||
break
|
||||
}
|
||||
if pat.negated && pat.Match(relpath, ftype) {
|
||||
is_ignored = false
|
||||
linenum_of_matching_rule = pat.line_number
|
||||
pattern = pat.pattern
|
||||
}
|
||||
} else {
|
||||
if !pat.negated && pat.Match(relpath, ftype) {
|
||||
is_ignored = true
|
||||
linenum_of_matching_rule = pat.line_number
|
||||
pattern = pat.pattern
|
||||
}
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (g *Gitignore) load_line(line string, line_number int) {
|
||||
if p, skipped_line := CompileGitIgnoreLine(line); !skipped_line {
|
||||
p.line_number = g.line_number_offset + line_number
|
||||
g.patterns = append(g.patterns, p)
|
||||
if p.negated {
|
||||
g.index_of_last_negated_rule = len(g.patterns) - 1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (g *Gitignore) LoadLines(lines ...string) error {
|
||||
for i, line := range lines {
|
||||
g.load_line(line, i)
|
||||
}
|
||||
g.line_number_offset += len(lines)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *Gitignore) LoadString(text string) error {
|
||||
s := utils.NewLineScanner(text)
|
||||
lnum := 0
|
||||
for s.Scan() {
|
||||
g.load_line(s.Text(), lnum)
|
||||
lnum++
|
||||
}
|
||||
g.line_number_offset += lnum
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *Gitignore) LoadBytes(text []byte) error {
|
||||
return g.LoadString(string(text))
|
||||
}
|
||||
|
||||
func (g *Gitignore) LoadPath(path string) error {
|
||||
if data, err := os.ReadFile(path); err == nil {
|
||||
return g.LoadString(utils.UnsafeBytesToString(data))
|
||||
} else {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
func (g *Gitignore) LoadFile(f io.Reader) error {
|
||||
if data, err := io.ReadAll(f); err == nil {
|
||||
return g.LoadString(utils.UnsafeBytesToString(data))
|
||||
} else {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
func (p GitPattern) Match(path string, ftype fs.FileMode) bool {
|
||||
if p.only_dirs && ftype&fs.ModeDir == 0 {
|
||||
return false
|
||||
}
|
||||
return p.matcher(path)
|
||||
}
|
||||
|
||||
func anchored_single_match(path string, pattern string) bool {
|
||||
name, _, _ := strings.Cut(path, "/")
|
||||
matches, err := filepath.Match(pattern, name)
|
||||
return err == nil && matches
|
||||
}
|
||||
|
||||
func unanchored_single_match(path string, pattern string) bool {
|
||||
for path != "" {
|
||||
var name string
|
||||
name, path, _ = strings.Cut(path, "/")
|
||||
matches, err := filepath.Match(pattern, name)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
if matches {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func anchored_simple_match(path string, parts []string) bool {
|
||||
for ; path != "" && len(parts) > 0; parts = parts[1:] {
|
||||
var name string
|
||||
name, path, _ = strings.Cut(path, "/")
|
||||
if matches, err := filepath.Match(parts[0], name); err != nil || !matches {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return path == "" && len(parts) == 0
|
||||
}
|
||||
|
||||
func anchored_full_match(path string, parts []string) bool {
|
||||
pos, last := 0, len(parts)-1
|
||||
for pos <= last && path != "" {
|
||||
var name string
|
||||
name, path, _ = strings.Cut(path, "/")
|
||||
switch parts[pos] {
|
||||
case "**":
|
||||
for pos+1 < len(parts) && parts[pos+1] == "**" {
|
||||
pos++
|
||||
}
|
||||
if pos == last {
|
||||
return true
|
||||
}
|
||||
pos++
|
||||
for {
|
||||
matches, err := filepath.Match(parts[pos], name)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
if matches {
|
||||
return anchored_full_match(path, parts[pos+1:])
|
||||
}
|
||||
if path == "" {
|
||||
return false
|
||||
}
|
||||
name, path, _ = strings.Cut(path, "/")
|
||||
}
|
||||
default:
|
||||
if matches, err := filepath.Match(parts[pos], name); err != nil || !matches {
|
||||
return false
|
||||
}
|
||||
pos++
|
||||
}
|
||||
}
|
||||
return path == "" && pos > last
|
||||
}
|
||||
|
||||
// Parse a line from a .gitignore file, see man gitignore for the syntax
|
||||
func CompileGitIgnoreLine(line string) (ans GitPattern, skipped_line bool) {
|
||||
// Strip comments
|
||||
if strings.HasPrefix(line, `#`) {
|
||||
skipped_line = true
|
||||
return
|
||||
}
|
||||
|
||||
// Trim OS-specific carriage returns.
|
||||
line = strings.TrimRight(line, "\r")
|
||||
|
||||
// Trim trailing spaces unless backslash escaped
|
||||
for strings.HasSuffix(line, " ") {
|
||||
if strings.HasSuffix(line, `\ `) {
|
||||
line = line[:len(line)-2] + " "
|
||||
break
|
||||
}
|
||||
line = line[:len(line)-1]
|
||||
}
|
||||
|
||||
// Empty lines are ignored
|
||||
if line == "" {
|
||||
skipped_line = true
|
||||
return
|
||||
}
|
||||
ans.pattern = line
|
||||
|
||||
// Handle negated (accept) patterns
|
||||
if line[0] == '!' {
|
||||
line = line[1:]
|
||||
ans.negated = true
|
||||
}
|
||||
|
||||
// Handle leading slash used to escape leading # or !
|
||||
if strings.HasPrefix(line, `\#`) || strings.HasPrefix(line, `\!`) {
|
||||
line = line[1:]
|
||||
}
|
||||
if strings.HasSuffix(line, "/") {
|
||||
ans.only_dirs = true
|
||||
line = strings.TrimRight(line, "/")
|
||||
if line == "" {
|
||||
skipped_line = true
|
||||
return
|
||||
}
|
||||
}
|
||||
starts_with_slash := strings.HasPrefix(line, "/")
|
||||
if starts_with_slash {
|
||||
line = strings.TrimLeft(line, "/")
|
||||
}
|
||||
ans.parts = strings.Split(line, "/")
|
||||
if slices.Contains(ans.parts, "") {
|
||||
ans.parts = slices.DeleteFunc(ans.parts, func(x string) bool { return x == "" })
|
||||
}
|
||||
if len(ans.parts) == 0 {
|
||||
skipped_line = true
|
||||
return
|
||||
}
|
||||
if len(ans.parts) == 1 {
|
||||
pattern := ans.parts[0]
|
||||
if pattern == "**" {
|
||||
ans.matcher = func(string) bool { return true }
|
||||
} else {
|
||||
if starts_with_slash {
|
||||
ans.matcher = func(path string) bool { return anchored_single_match(path, pattern) }
|
||||
} else {
|
||||
ans.matcher = func(path string) bool { return unanchored_single_match(path, pattern) }
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if slices.Contains(ans.parts, "**") {
|
||||
ans.matcher = func(path string) bool { return anchored_full_match(path, ans.parts) }
|
||||
} else {
|
||||
ans.matcher = func(path string) bool { return anchored_simple_match(path, ans.parts) }
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func get_global_gitconfig_excludesfile() (ans string) {
|
||||
return _get_global_gitconfig_excludesfile(utils.Expanduser("~/.gitconfig"))
|
||||
}
|
||||
|
||||
func _get_global_gitconfig_excludesfile(home_gitconfig string) (ans string) {
|
||||
cfhome := os.Getenv("XDG_CONFIG_HOME")
|
||||
if cfhome == "" {
|
||||
cfhome = utils.Expanduser("~/.config")
|
||||
}
|
||||
for _, candidate := range []string{"/etc/gitconfig", filepath.Join(cfhome, "git", "config"), home_gitconfig} {
|
||||
if data, err := os.ReadFile(candidate); err == nil {
|
||||
s := utils.NewLineScanner(utils.UnsafeBytesToString(data))
|
||||
in_core := false
|
||||
for s.Scan() {
|
||||
line := strings.TrimSpace(s.Text())
|
||||
if in_core {
|
||||
if strings.HasPrefix(line, "[") {
|
||||
in_core = line == "[core]"
|
||||
continue
|
||||
}
|
||||
if k, rest, found := strings.Cut(line, "="); found && strings.ToLower(strings.TrimSpace(k)) == `excludesfile` {
|
||||
ans = strings.TrimSpace(rest)
|
||||
ans = utils.Expanduser(ans)
|
||||
if !filepath.IsAbs(ans) {
|
||||
if a, err := filepath.Abs(ans); err != nil {
|
||||
ans = a
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if strings.ToLower(line) == "[core]" {
|
||||
in_core = true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if ans == "" {
|
||||
ans = filepath.Join(cfhome, "git", "ignore")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func get_global_gitignore() (ans IgnoreFile) {
|
||||
excludesfile := get_global_gitconfig_excludesfile()
|
||||
if data, err := os.ReadFile(excludesfile); err == nil {
|
||||
q := NewGitignore()
|
||||
if q.LoadString(utils.UnsafeBytesToString(data)) == nil {
|
||||
ans = q
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,166 @@
|
||||
package ignorefiles
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/kovidgoyal/kitty/tools/utils"
|
||||
)
|
||||
|
||||
var _ = fmt.Print
|
||||
|
||||
func TestGitignore(t *testing.T) {
|
||||
for line, expected := range map[string]struct {
|
||||
skipped, negated, only_dirs bool
|
||||
parts []string
|
||||
}{
|
||||
"": {skipped: true},
|
||||
" ": {skipped: true},
|
||||
" ": {skipped: true},
|
||||
"/": {skipped: true},
|
||||
"//": {skipped: true},
|
||||
"# abc": {skipped: true},
|
||||
"!": {skipped: true},
|
||||
"! ": {skipped: true},
|
||||
`\!moose \ `: {parts: []string{`!moose `}},
|
||||
`\#m\oose `: {parts: []string{`#m\oose`}},
|
||||
} {
|
||||
p, skipped := CompileGitIgnoreLine(line)
|
||||
if skipped != expected.skipped {
|
||||
t.Fatalf("skipped: %v != %v for line: %s", expected.skipped, skipped, line)
|
||||
}
|
||||
if !skipped {
|
||||
if p.negated != expected.negated {
|
||||
t.Fatalf("negated: %v != %v for line: %s", expected.negated, p.negated, line)
|
||||
}
|
||||
if p.only_dirs != expected.only_dirs {
|
||||
t.Fatalf("only_dirs: %v != %v for line: %s", expected.only_dirs, p.only_dirs, line)
|
||||
}
|
||||
if diff := cmp.Diff(expected.parts, p.parts); diff != "" {
|
||||
t.Fatalf("parts not equal for line: %s\n%s", line, diff)
|
||||
}
|
||||
}
|
||||
}
|
||||
type ptest struct {
|
||||
path string
|
||||
expected bool
|
||||
}
|
||||
for _, x := range []struct {
|
||||
line string
|
||||
tests []ptest
|
||||
}{
|
||||
{"foo", []ptest{
|
||||
{"foo", true}, {"x/foo", true}, {"foo/x", true},
|
||||
}},
|
||||
{"/foo", []ptest{
|
||||
{"foo", true}, {"x/foo", false},
|
||||
}},
|
||||
{"doc/frotz/", []ptest{
|
||||
{"doc/frotz/", true}, {"a/doc/frotz/", false}, {"doc/frotz", false},
|
||||
}},
|
||||
{"frotz/", []ptest{
|
||||
{"frotz/", true}, {"a/doc/frotz/", true}, {"doc/frotz", false}, {"frotz/", true},
|
||||
}},
|
||||
{"foo.*", []ptest{
|
||||
{"foo.txt", true}, {"foo", false}, {"a/foo.x", true}, {"foo.", true},
|
||||
}},
|
||||
|
||||
{"**", []ptest{
|
||||
{"foo", true}, {"x/foo", true}, {"foo/x", true},
|
||||
}},
|
||||
|
||||
{"**/foo", []ptest{
|
||||
{"foo", true}, {"x/foo", true}, {"foo/x", false},
|
||||
}},
|
||||
{"**/foo/bar", []ptest{
|
||||
{"foo", false}, {"x/foo", false}, {"foo/bar", true}, {"a/foo/bar", true}, {"foo/bar/a", false},
|
||||
}},
|
||||
{"foo/**", []ptest{
|
||||
{"foo", false}, {"x/foo", false}, {"foo/bar", true}, {"foo/bar/a", true}, {"foo/bar/a/", true},
|
||||
}},
|
||||
{"a/**/b", []ptest{
|
||||
{"a/b", true}, {"a/x/b", true}, {"a/x/y/b", true}, {"x/a/b", false}, {"a/b/x", false},
|
||||
}},
|
||||
{"a/**/b/**/c", []ptest{
|
||||
{"a/b/c", true}, {"a/x/b/c", true}, {"a/x/y/b/m/n/c", true},
|
||||
}},
|
||||
} {
|
||||
p, skipped := CompileGitIgnoreLine(x.line)
|
||||
if skipped {
|
||||
t.Fatalf("Unexpectedly failed to compile: %#v", x.line)
|
||||
}
|
||||
for _, test := range x.tests {
|
||||
path := strings.TrimRight(test.path, "/")
|
||||
ftype := utils.IfElse(len(path) < len(test.path), fs.ModeDir, 0)
|
||||
if actual := p.Match(path, ftype); actual != test.expected {
|
||||
t.Fatalf("matched: %v != %v for pattern: %#v and path: %#v", test.expected, actual, x.line, test.path)
|
||||
}
|
||||
}
|
||||
}
|
||||
for text, tests := range map[string]map[string]bool{
|
||||
``: {"foo": false},
|
||||
`
|
||||
# exclude everything except directory foo/bar
|
||||
/*
|
||||
!/foo
|
||||
/foo/*
|
||||
!/foo/bar`: {
|
||||
"a": true, "foo": false, "foo/x": true, "foo/bar": false, "foo/bar/": false,
|
||||
},
|
||||
`
|
||||
**/foo
|
||||
bar `: {
|
||||
"foo": true, "baz/foo": true, "bar": true, "baz/bar": true, "a": false,
|
||||
},
|
||||
`/*.c`: {"a.c": true, "b/a.c": false},
|
||||
`
|
||||
**/external/**/*.json
|
||||
**/external/**/.*ignore
|
||||
**/external/foobar/*.css`: {
|
||||
"external/foobar/angular.foo.css": true, "external/barfoo/.gitignore": true, "external/barfoo/.bower.json": true,
|
||||
},
|
||||
"abc/def\r\nxyz": {"abc/def": true, "a/xyz": true},
|
||||
`/**/foo`: {"foo": true, "foo/": true, "a/b/foo": true, "fooo": false, "ofoo": false},
|
||||
"/.js": {".js": true, ".js/": true, ".js/a": true, ".jsa": false},
|
||||
"*.js": {".js": true, ".js/": true, ".js/a": true, "a.js/a": true, "a.js/a.js": true, ".jsa": false, "a.jsa": false},
|
||||
"foo/**/": {"foo/": false, "foo": false, "foo/abc/": true, "foo/a/b/c/": true, "foo/a": false},
|
||||
"foo/**/*.bar": {"foo/": false, "abc.bar": false, "foo/abc.bar": true, "foo/a.bar/": true, "foo/x/y/z.bar": true},
|
||||
`\#abc`: {"abc": false, "#abc": true},
|
||||
"abc\n!abc/x": {"abc": true, "abc/x": false, "abc/y": true},
|
||||
`abc/*`: {"abc": false, "abc/": false, "abc/x": true},
|
||||
} {
|
||||
p := NewGitignore()
|
||||
if err := p.LoadString(text); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for tpath, expected := range tests {
|
||||
path := strings.TrimRight(tpath, "/")
|
||||
ftype := utils.IfElse(len(path) < len(tpath), fs.ModeDir, 0)
|
||||
if actual, _, _ := p.IsIgnored(path, ftype); actual != expected {
|
||||
t.Fatalf("ignored: %v != %v for path: %#v and ignorefile:\n%s", expected, actual, tpath, text)
|
||||
}
|
||||
}
|
||||
}
|
||||
tdir := t.TempDir()
|
||||
gc := filepath.Join(tdir, ".gitconfig")
|
||||
os.WriteFile(gc, []byte(`
|
||||
[core]
|
||||
something
|
||||
[else]
|
||||
...
|
||||
[core]
|
||||
...
|
||||
[core]
|
||||
excludesfile = one
|
||||
[core]
|
||||
excludesfile = ~/global-gitignore
|
||||
`), 0600)
|
||||
if ef := _get_global_gitconfig_excludesfile(gc); ef != utils.Expanduser("~/global-gitignore") {
|
||||
t.Fatalf("global gitconfig excludes file incorrect: %s != %s", utils.Expanduser("~/global-gitignore"), ef)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user