Skip to content

Commit

Permalink
Rename note.ignore config property to note.exclude (zk-org#322)
Browse files Browse the repository at this point in the history
  • Loading branch information
mickael-menu authored May 20, 2023
1 parent 6252e51 commit 75205fe
Show file tree
Hide file tree
Showing 15 changed files with 85 additions and 46 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ All notable changes to this project will be documented in this file.
### Changed
* Removed the dependency on `libicu`.
* The `note.ignore` configuration property was renamed to `note.exclude`, to be more consistent with the CLI flags.
### Fixed
Expand Down
4 changes: 2 additions & 2 deletions docs/config-note.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ The `[note]` section from the [configuration file](config.md) is used to set the
* `template` (string)
* Path to the [template](template.md) used to generate the note content.
* Either an absolute path, or relative to `.zk/templates/`.
* `ignore` (list of strings)
* List of [path globs](https://en.wikipedia.org/wiki/Glob_\(programming\)) ignored during note indexing.
* `exclude` (list of strings)
* List of [path globs](https://en.wikipedia.org/wiki/Glob_\(programming\)) excluded during note indexing.
* `id-charset` (string)
* Characters set used to [generate random IDs](note-id.md).
* You can use:
Expand Down
25 changes: 16 additions & 9 deletions internal/core/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func NewDefaultConfig() Config {
Length: 4,
Case: CaseLower,
},
Ignore: []string{},
Exclude: []string{},
},
Groups: map[string]GroupConfig{},
Format: FormatConfig{
Expand Down Expand Up @@ -216,7 +216,7 @@ type NoteConfig struct {
// Settings used when generating a random ID.
IDOptions IDOptions
// Path globs to ignore when indexing notes.
Ignore []string
Exclude []string
}

// GroupConfig holds the user configuration for a given group of notes.
Expand All @@ -226,16 +226,16 @@ type GroupConfig struct {
Extra map[string]string
}

// IgnoreGlobs returns all the Note.Ignore path globs for the group paths,
// ExcludeGlobs returns all the Note.Exclude path globs for the group paths,
// relative to the root of the notebook.
func (c GroupConfig) IgnoreGlobs() []string {
func (c GroupConfig) ExcludeGlobs() []string {
if len(c.Paths) == 0 {
return c.Note.Ignore
return c.Note.Exclude
}

globs := []string{}
for _, p := range c.Paths {
for _, g := range c.Note.Ignore {
for _, g := range c.Note.Exclude {
globs = append(globs, filepath.Join(p, g))
}
}
Expand Down Expand Up @@ -325,8 +325,11 @@ func ParseConfig(content []byte, path string, parentConfig Config, isGlobal bool
if note.DefaultTitle != "" {
config.Note.DefaultTitle = note.DefaultTitle
}
for _, v := range note.Exclude {
config.Note.Exclude = append(config.Note.Exclude, v)
}
for _, v := range note.Ignore {
config.Note.Ignore = append(config.Note.Ignore, v)
config.Note.Exclude = append(config.Note.Exclude, v)
}
if tomlConf.Extra != nil {
for k, v := range tomlConf.Extra {
Expand Down Expand Up @@ -477,8 +480,11 @@ func (c GroupConfig) merge(tomlConf tomlGroupConfig, name string) GroupConfig {
if note.DefaultTitle != "" {
res.Note.DefaultTitle = note.DefaultTitle
}
for _, v := range note.Exclude {
res.Note.Exclude = append(res.Note.Exclude, v)
}
for _, v := range note.Ignore {
res.Note.Ignore = append(res.Note.Ignore, v)
res.Note.Exclude = append(res.Note.Exclude, v)
}
if tomlConf.Extra != nil {
for k, v := range tomlConf.Extra {
Expand Down Expand Up @@ -515,7 +521,8 @@ type tomlNoteConfig struct {
IDCharset string `toml:"id-charset"`
IDLength int `toml:"id-length"`
IDCase string `toml:"id-case"`
Ignore []string `toml:"ignore"`
Exclude []string `toml:"exclude"`
Ignore []string `toml:"ignore"` // Legacy alias to `exclude`
}

type tomlGroupConfig struct {
Expand Down
42 changes: 21 additions & 21 deletions internal/core/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestParseDefaultConfig(t *testing.T) {
},
DefaultTitle: "Untitled",
Lang: "en",
Ignore: []string{},
Exclude: []string{},
},
Groups: make(map[string]GroupConfig),
Format: FormatConfig{
Expand Down Expand Up @@ -81,7 +81,7 @@ func TestParseComplete(t *testing.T) {
id-charset = "alphanum"
id-length = 4
id-case = "lower"
ignore = ["ignored", ".git"]
exclude = ["ignored", ".git"]
[format.markdown]
hashtags = false
Expand Down Expand Up @@ -124,7 +124,7 @@ func TestParseComplete(t *testing.T) {
id-charset = "letters"
id-length = 8
id-case = "mixed"
ignore = ["new-ignored"]
exclude = ["new-ignored"]
[group.log.extra]
log-ext = "value"
Expand Down Expand Up @@ -162,7 +162,7 @@ func TestParseComplete(t *testing.T) {
},
Lang: "fr",
DefaultTitle: "Sans titre",
Ignore: []string{"ignored", ".git"},
Exclude: []string{"ignored", ".git"},
},
Groups: map[string]GroupConfig{
"log": {
Expand All @@ -178,7 +178,7 @@ func TestParseComplete(t *testing.T) {
},
Lang: "de",
DefaultTitle: "Ohne Titel",
Ignore: []string{"ignored", ".git", "new-ignored"},
Exclude: []string{"ignored", ".git", "new-ignored"},
},
Extra: map[string]string{
"hello": "world",
Expand All @@ -199,7 +199,7 @@ func TestParseComplete(t *testing.T) {
},
Lang: "fr",
DefaultTitle: "Sans titre",
Ignore: []string{"ignored", ".git"},
Exclude: []string{"ignored", ".git"},
},
Extra: map[string]string{
"hello": "world",
Expand All @@ -219,7 +219,7 @@ func TestParseComplete(t *testing.T) {
},
Lang: "fr",
DefaultTitle: "Sans titre",
Ignore: []string{"ignored", ".git"},
Exclude: []string{"ignored", ".git"},
},
Extra: map[string]string{
"hello": "world",
Expand Down Expand Up @@ -286,7 +286,7 @@ func TestParseMergesGroupConfig(t *testing.T) {
id-charset = "letters"
id-length = 42
id-case = "upper"
ignore = ["ignored", ".git"]
exclude = ["ignored", ".git"]
[extra]
hello = "world"
Expand Down Expand Up @@ -319,7 +319,7 @@ func TestParseMergesGroupConfig(t *testing.T) {
},
Lang: "fr",
DefaultTitle: "Sans titre",
Ignore: []string{"ignored", ".git"},
Exclude: []string{"ignored", ".git"},
},
Groups: map[string]GroupConfig{
"log": {
Expand All @@ -335,7 +335,7 @@ func TestParseMergesGroupConfig(t *testing.T) {
},
Lang: "fr",
DefaultTitle: "Sans titre",
Ignore: []string{"ignored", ".git"},
Exclude: []string{"ignored", ".git"},
},
Extra: map[string]string{
"hello": "override",
Expand All @@ -356,7 +356,7 @@ func TestParseMergesGroupConfig(t *testing.T) {
},
Lang: "fr",
DefaultTitle: "Sans titre",
Ignore: []string{"ignored", ".git"},
Exclude: []string{"ignored", ".git"},
},
Extra: map[string]string{
"hello": "world",
Expand Down Expand Up @@ -515,31 +515,31 @@ func TestParseLSPDiagnosticsSeverity(t *testing.T) {
assert.Err(t, err, "foobar: unknown LSP diagnostic severity - may be none, hint, info, warning or error")
}

func TestGroupConfigIgnoreGlobs(t *testing.T) {
func TestGroupConfigExcludeGlobs(t *testing.T) {
// empty globs
config := GroupConfig{
Paths: []string{"path"},
Note: NoteConfig{Ignore: []string{}},
Note: NoteConfig{Exclude: []string{}},
}
assert.Equal(t, config.IgnoreGlobs(), []string{})
assert.Equal(t, config.ExcludeGlobs(), []string{})

// empty paths
config = GroupConfig{
Paths: []string{},
Note: NoteConfig{
Ignore: []string{"ignored", ".git"},
Exclude: []string{"ignored", ".git"},
},
}
assert.Equal(t, config.IgnoreGlobs(), []string{"ignored", ".git"})
assert.Equal(t, config.ExcludeGlobs(), []string{"ignored", ".git"})

// several paths
config = GroupConfig{
Paths: []string{"log", "drafts"},
Note: NoteConfig{
Ignore: []string{"ignored", "*.git"},
Exclude: []string{"ignored", "*.git"},
},
}
assert.Equal(t, config.IgnoreGlobs(), []string{"log/ignored", "log/*.git", "drafts/ignored", "drafts/*.git"})
assert.Equal(t, config.ExcludeGlobs(), []string{"log/ignored", "log/*.git", "drafts/ignored", "drafts/*.git"})
}

func TestGroupConfigClone(t *testing.T) {
Expand All @@ -556,7 +556,7 @@ func TestGroupConfigClone(t *testing.T) {
},
Lang: "fr",
DefaultTitle: "Sans titre",
Ignore: []string{"ignored", ".git"},
Exclude: []string{"ignored", ".git"},
},
Extra: map[string]string{
"hello": "world",
Expand All @@ -576,7 +576,7 @@ func TestGroupConfigClone(t *testing.T) {
clone.Note.IDOptions.Case = CaseUpper
clone.Note.Lang = "de"
clone.Note.DefaultTitle = "Ohne Titel"
clone.Note.Ignore = []string{"other-ignored"}
clone.Note.Exclude = []string{"other-ignored"}
clone.Extra["test"] = "modified"

// Check that we didn't modify the original
Expand All @@ -593,7 +593,7 @@ func TestGroupConfigClone(t *testing.T) {
},
Lang: "fr",
DefaultTitle: "Sans titre",
Ignore: []string{"ignored", ".git"},
Exclude: []string{"ignored", ".git"},
},
Extra: map[string]string{
"hello": "world",
Expand Down
6 changes: 3 additions & 3 deletions internal/core/note_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,13 @@ func (t *indexTask) execute(callback func(change paths.DiffChange)) (NoteIndexin
return true, nil
}

for _, ignoreGlob := range group.IgnoreGlobs() {
for _, ignoreGlob := range group.ExcludeGlobs() {
matches, err := doublestar.PathMatch(ignoreGlob, path)
if err != nil {
return true, errors.Wrapf(err, "failed to match ignore glob %s to %s", ignoreGlob, path)
return true, errors.Wrapf(err, "failed to match exclude glob %s to %s", ignoreGlob, path)
}
if matches {
notifyIgnored("matched ignore glob \"" + ignoreGlob + "\"")
notifyIgnored("matched exclude glob \"" + ignoreGlob + "\"")
return true, nil
}
}
Expand Down
15 changes: 15 additions & 0 deletions tests/cmd-index-legacy-ignore.tesh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
$ cd legacy-ignore

# Index initial notes.
$ zk index
>Indexed 3 notes in 0s
> + 3 added
> ~ 0 modified
> - 0 removed

# Ignore path patterns.
$ touch carrot-ignored/ananas.md && zk index
>Indexed 3 notes in 0s
> + 0 added
> ~ 0 modified
> - 0 removed
8 changes: 4 additions & 4 deletions tests/cmd-index.tesh
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ $ touch banana.md && echo "More" >> litchee.md && rm eggplant/apple.md && zk ind
>- removed eggplant/apple.md
>- unchanged eggplant/clementine.md
>- modified litchee.md
>- ignored carrot-ignored/ananas.md: matched ignore glob "carrot-ignored/*"
>- ignored carrot-ignored/tomato.md: matched ignore glob "carrot-ignored/*"
>- ignored carrot-ignored/ananas.md: matched exclude glob "carrot-ignored/*"
>- ignored carrot-ignored/tomato.md: matched exclude glob "carrot-ignored/*"
>- ignored orange.markdown: expected extension "md"
>
>Indexed 3 notes in 0s
Expand All @@ -113,8 +113,8 @@ $ zk index -v
>- unchanged banana.md
>- unchanged eggplant/clementine.md
>- unchanged litchee.md
>- ignored carrot-ignored/ananas.md: matched ignore glob "carrot-ignored/*"
>- ignored carrot-ignored/tomato.md: matched ignore glob "carrot-ignored/*"
>- ignored carrot-ignored/ananas.md: matched exclude glob "carrot-ignored/*"
>- ignored carrot-ignored/tomato.md: matched exclude glob "carrot-ignored/*"
>- ignored orange.markdown: expected extension "md"
>
>Indexed 3 notes in 0s
Expand Down
10 changes: 5 additions & 5 deletions tests/config-note-ignore.tesh → tests/config-note-exclude.tesh
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,26 @@ $ touch dir/orange.md
$ touch dir/subdir/apple.md

# Ignore a file in the root directory.
$ echo "[note]\n ignore = ['orange.md']" > .zk/config.toml
$ echo "[note]\n exclude = ['orange.md']" > .zk/config.toml
$ zk index -v
>- added banana.md
>- added dir/orange.md
>- added dir/subdir/apple.md
>- ignored orange.md: matched ignore glob "orange.md"
>- ignored orange.md: matched exclude glob "orange.md"
>
>Indexed 3 notes in 0s
> + 3 added
> ~ 0 modified
> - 0 removed

# Ignore with wildcards.
$ echo "[note]\n ignore = ['*rang*', 'dir/*']" > .zk/config.toml
$ echo "[note]\n exclude = ['*rang*', 'dir/*']" > .zk/config.toml
$ zk index -v
>- unchanged banana.md
>- removed dir/orange.md
>- unchanged dir/subdir/apple.md
>- ignored dir/orange.md: matched ignore glob "dir/*"
>- ignored orange.md: matched ignore glob "*rang*"
>- ignored dir/orange.md: matched exclude glob "dir/*"
>- ignored orange.md: matched exclude glob "*rang*"
>
>Indexed 2 notes in 0s
> + 0 added
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/index/.zk/config.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[note]
ignore = [
exclude = [
"carrot-ignored/*",
]
4 changes: 4 additions & 0 deletions tests/fixtures/legacy-ignore/.zk/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[note]
ignore = [
"carrot-ignored/*",
]
3 changes: 3 additions & 0 deletions tests/fixtures/legacy-ignore/banana.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Banana

Content of banana
3 changes: 3 additions & 0 deletions tests/fixtures/legacy-ignore/carrot-ignored/tomato.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Tomato

Content of tomato
3 changes: 3 additions & 0 deletions tests/fixtures/legacy-ignore/eggplant/clementine.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Clementine

Content of clementine
3 changes: 3 additions & 0 deletions tests/fixtures/legacy-ignore/litchee.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Litchee

Content of litchee
2 changes: 1 addition & 1 deletion tests/issue-173.tesh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

$ cd blank

$ echo "[note]\n ignore = ['drafts/**']" > .zk/config.toml
$ echo "[note]\n exclude = ['drafts/**']" > .zk/config.toml

$ mkdir -p drafts/subdir/subdir
$ echo "# This is not ignored" > not-ignored.md
Expand Down

0 comments on commit 75205fe

Please sign in to comment.