From 1a9604ea3492ba08c8e079abc323010571817616 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mickae=CC=88l=20Menu?= Date: Sat, 16 Jan 2021 20:53:12 +0100 Subject: [PATCH] Add flag to force indexation --- cmd/index.go | 3 +- core/note/index.go | 4 +-- util/paths/diff.go | 8 ++--- util/paths/diff_test.go | 76 +++++++++++++++++++++++++++++++++++------ 4 files changed, 74 insertions(+), 17 deletions(-) diff --git a/cmd/index.go b/cmd/index.go index 16cf9a7..cf7acf9 100644 --- a/cmd/index.go +++ b/cmd/index.go @@ -9,6 +9,7 @@ import ( // Index indexes the content of all the notes in the slip box. type Index struct { Directory string `arg optional type:"path" default:"." help:"Directory containing the notes to index"` + Force bool `help:"Force indexing all the notes" short:"f"` } func (cmd *Index) Run(container *Container) error { @@ -29,6 +30,6 @@ func (cmd *Index) Run(container *Container) error { return db.WithTransaction(func(tx sqlite.Transaction) error { notes := sqlite.NewNoteDAO(tx, container.Logger) - return note.Index(*dir, container.Parser(), notes, container.Logger) + return note.Index(*dir, cmd.Force, container.Parser(), notes, container.Logger) }) } diff --git a/core/note/index.go b/core/note/index.go index 240bf8f..81176ee 100644 --- a/core/note/index.go +++ b/core/note/index.go @@ -41,7 +41,7 @@ type Indexer interface { } // Index indexes the content of the notes in the given directory. -func Index(dir zk.Dir, parser Parser, indexer Indexer, logger util.Logger) error { +func Index(dir zk.Dir, force bool, parser Parser, indexer Indexer, logger util.Logger) error { wrap := errors.Wrapper("indexation failed") source := paths.Walk(dir.Path, dir.Config.Extension, logger) @@ -50,7 +50,7 @@ func Index(dir zk.Dir, parser Parser, indexer Indexer, logger util.Logger) error return wrap(err) } - err = paths.Diff(source, target, func(change paths.DiffChange) error { + err = paths.Diff(source, target, force, func(change paths.DiffChange) error { switch change.Kind { case paths.DiffAdded: metadata, err := metadata(change.Path, dir.Path, parser) diff --git a/util/paths/diff.go b/util/paths/diff.go index 6c8266d..86d1d83 100644 --- a/util/paths/diff.go +++ b/util/paths/diff.go @@ -20,7 +20,7 @@ const ( // // Warning: The Metadata have to be sorted by their Path for the diffing to // work properly. -func Diff(source, target <-chan Metadata, callback func(DiffChange) error) error { +func Diff(source, target <-chan Metadata, forceModified bool, callback func(DiffChange) error) error { var err error var sourceFile, targetFile Metadata var sourceOpened, targetOpened bool = true, true @@ -39,7 +39,7 @@ func Diff(source, target <-chan Metadata, callback func(DiffChange) error) error pair.target = &targetFile } } - change := pair.diff() + change := pair.diff(forceModified) if change != nil { err = callback(*change) } @@ -59,7 +59,7 @@ type diffPair struct { // If the source and target file are at the same path, we check for any change. // If the files are different, that means that either the source file was // added, or the target file was removed. -func (p *diffPair) diff() *DiffChange { +func (p *diffPair) diff(forceModified bool) *DiffChange { var change *DiffChange switch { @@ -75,7 +75,7 @@ func (p *diffPair) diff() *DiffChange { p.source = nil case p.source.Path == p.target.Path: // Same files, compare their modification date. - if p.source.Modified != p.target.Modified { + if forceModified || p.source.Modified != p.target.Modified { change = &DiffChange{p.source.Path, DiffModified} } p.source = nil diff --git a/util/paths/diff_test.go b/util/paths/diff_test.go index 86109bd..143c9a1 100644 --- a/util/paths/diff_test.go +++ b/util/paths/diff_test.go @@ -16,7 +16,7 @@ var date4 = time.Date(2016, 13, 11, 4, 34, 58, 651387237, time.UTC) func TestDiffEmpty(t *testing.T) { source := []Metadata{} target := []Metadata{} - test(t, source, target, []DiffChange{}) + test(t, source, target, false, []DiffChange{}) } func TestNoDiff(t *testing.T) { @@ -35,7 +35,7 @@ func TestNoDiff(t *testing.T) { }, } - test(t, files, files, []DiffChange{}) + test(t, files, files, false, []DiffChange{}) } func TestDiff(t *testing.T) { @@ -73,7 +73,7 @@ func TestDiff(t *testing.T) { }, } - test(t, source, target, []DiffChange{ + test(t, source, target, false, []DiffChange{ { Path: "a/1", Kind: DiffModified, @@ -89,6 +89,62 @@ func TestDiff(t *testing.T) { }) } +func TestDiffForceModified(t *testing.T) { + source := []Metadata{ + { + Path: "a/1", + Modified: date1, + }, + { + Path: "a/2", + Modified: date2, + }, + { + Path: "b/1", + Modified: date3, + }, + } + + target := []Metadata{ + { + // Date changed + Path: "a/1", + Modified: date1.Add(time.Hour), + }, + // 2 is added + { + // 3 is removed + Path: "a/3", + Modified: date3, + }, + { + // No change + Path: "b/1", + Modified: date3, + }, + } + + test(t, source, target, true, []DiffChange{ + { + Path: "a/1", + Kind: DiffModified, + }, + { + Path: "a/2", + Kind: DiffAdded, + }, + { + Path: "a/3", + Kind: DiffRemoved, + }, + { + // Forced modified + Path: "b/1", + Kind: DiffModified, + }, + }) +} + func TestDiffWithMoreInSource(t *testing.T) { source := []Metadata{ { @@ -108,7 +164,7 @@ func TestDiffWithMoreInSource(t *testing.T) { }, } - test(t, source, target, []DiffChange{ + test(t, source, target, false, []DiffChange{ { Path: "a/2", Kind: DiffAdded, @@ -135,7 +191,7 @@ func TestDiffWithMoreInTarget(t *testing.T) { }, } - test(t, source, target, []DiffChange{ + test(t, source, target, false, []DiffChange{ { Path: "a/2", Kind: DiffRemoved, @@ -157,7 +213,7 @@ func TestDiffEmptySource(t *testing.T) { }, } - test(t, source, target, []DiffChange{ + test(t, source, target, false, []DiffChange{ { Path: "a/1", Kind: DiffRemoved, @@ -183,7 +239,7 @@ func TestDiffEmptyTarget(t *testing.T) { target := []Metadata{} - test(t, source, target, []DiffChange{ + test(t, source, target, false, []DiffChange{ { Path: "a/1", Kind: DiffAdded, @@ -210,7 +266,7 @@ func TestDiffCancellation(t *testing.T) { target := []Metadata{} received := make([]DiffChange, 0) - err := Diff(toChannel(source), toChannel(target), func(change DiffChange) error { + err := Diff(toChannel(source), toChannel(target), false, func(change DiffChange) error { received = append(received, change) if len(received) == 1 { @@ -229,9 +285,9 @@ func TestDiffCancellation(t *testing.T) { assert.Err(t, err, "cancelled") } -func test(t *testing.T, source, target []Metadata, expected []DiffChange) { +func test(t *testing.T, source, target []Metadata, forceModified bool, expected []DiffChange) { received := make([]DiffChange, 0) - err := Diff(toChannel(source), toChannel(target), func(change DiffChange) error { + err := Diff(toChannel(source), toChannel(target), forceModified, func(change DiffChange) error { received = append(received, change) return nil })