Add flag to force indexation

pull/6/head
Mickaël Menu 3 years ago
parent f371f58bd2
commit 1a9604ea34
No known key found for this signature in database
GPG Key ID: 53D73664CD359895

@ -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)
})
}

@ -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)

@ -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

@ -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
})

Loading…
Cancel
Save