From fdc9840e63495d93436bd578ef1f7224d78ee8e7 Mon Sep 17 00:00:00 2001 From: Derek McGowan Date: Mon, 18 Dec 2017 16:57:33 -0800 Subject: [PATCH] Revert "Emit unmodified change events for directories" This reverts commit ba10e497b931449f1e8ecf689a67b4c373e6ab85. Signed-off-by: Derek McGowan --- fs/diff.go | 83 ++++--------------------------------------------- fs/diff_test.go | 64 +------------------------------------- 2 files changed, 7 insertions(+), 140 deletions(-) diff --git a/fs/diff.go b/fs/diff.go index 3a53f4215..9073d0d92 100644 --- a/fs/diff.go +++ b/fs/diff.go @@ -222,10 +222,8 @@ func doubleWalkDiff(ctx context.Context, changeFn ChangeFunc, a, b string) (err c1 = make(chan *currentPath) c2 = make(chan *currentPath) - f1, f2 *currentPath - rmdir string - lastEmittedDir = string(filepath.Separator) - parents []os.FileInfo + f1, f2 *currentPath + rmdir string ) g.Go(func() error { defer close(c1) @@ -260,10 +258,7 @@ func doubleWalkDiff(ctx context.Context, changeFn ChangeFunc, a, b string) (err continue } - var ( - f os.FileInfo - emit = true - ) + var f os.FileInfo k, p := pathChange(f1, f2) switch k { case ChangeKindAdd: @@ -299,35 +294,13 @@ func doubleWalkDiff(ctx context.Context, changeFn ChangeFunc, a, b string) (err f2 = nil if same { if !isLinked(f) { - emit = false + continue } k = ChangeKindUnmodified } } - if emit { - emittedDir, emitParents := commonParents(lastEmittedDir, p, parents) - for _, pf := range emitParents { - p := filepath.Join(emittedDir, pf.Name()) - if err := changeFn(ChangeKindUnmodified, p, pf, nil); err != nil { - return err - } - emittedDir = p - } - - if err := changeFn(k, p, f, nil); err != nil { - return err - } - - if f != nil && f.IsDir() { - lastEmittedDir = p - } else { - lastEmittedDir = emittedDir - } - - parents = parents[:0] - } else if f.IsDir() { - lastEmittedDir, parents = commonParents(lastEmittedDir, p, parents) - parents = append(parents, f) + if err := changeFn(k, p, f, nil); err != nil { + return err } } return nil @@ -335,47 +308,3 @@ func doubleWalkDiff(ctx context.Context, changeFn ChangeFunc, a, b string) (err return g.Wait() } - -func commonParents(base, updated string, dirs []os.FileInfo) (string, []os.FileInfo) { - if basePrefix := makePrefix(base); strings.HasPrefix(updated, basePrefix) { - var ( - parents []os.FileInfo - last = base - ) - for _, d := range dirs { - next := filepath.Join(last, d.Name()) - if strings.HasPrefix(updated, makePrefix(last)) { - parents = append(parents, d) - last = next - } else { - break - } - } - return base, parents - } - - baseS := strings.Split(base, string(filepath.Separator)) - updatedS := strings.Split(updated, string(filepath.Separator)) - commonS := []string{string(filepath.Separator)} - - min := len(baseS) - if len(updatedS) < min { - min = len(updatedS) - } - for i := 0; i < min; i++ { - if baseS[i] == updatedS[i] { - commonS = append(commonS, baseS[i]) - } else { - break - } - } - - return filepath.Join(commonS...), []os.FileInfo{} -} - -func makePrefix(d string) string { - if d == "" || d[len(d)-1] != filepath.Separator { - return d + string(filepath.Separator) - } - return d -} diff --git a/fs/diff_test.go b/fs/diff_test.go index 004caac7e..f3b2d9763 100644 --- a/fs/diff_test.go +++ b/fs/diff_test.go @@ -44,7 +44,6 @@ func TestSimpleDiff(t *testing.T) { fstest.Remove("/etc/unexpected"), ) diff := []TestChange{ - Unchanged("/etc"), Modify("/etc/hosts"), Modify("/etc/profile"), Delete("/etc/unexpected"), @@ -71,7 +70,6 @@ func TestDirectoryReplace(t *testing.T) { fstest.CreateFile("/dir1/f2", []byte("Now file"), 0666), ) diff := []TestChange{ - Unchanged("/dir1"), Add("/dir1/f11"), Modify("/dir1/f2"), } @@ -134,13 +132,10 @@ func TestParentDirectoryPermission(t *testing.T) { fstest.CreateFile("/dir3/f", []byte("irrelevant"), 0644), ) diff := []TestChange{ - Unchanged("/dir1"), Add("/dir1/d"), Add("/dir1/d/f"), Add("/dir1/f"), - Unchanged("/dir2"), Add("/dir2/f"), - Unchanged("/dir3"), Add("/dir3/f"), } @@ -198,56 +193,6 @@ func TestUpdateWithSameTime(t *testing.T) { } } -func TestUnchangedParent(t *testing.T) { - skipDiffTestOnWindows(t) - l1 := fstest.Apply( - fstest.CreateDir("/dir1", 0755), - fstest.CreateDir("/dir3", 0755), - fstest.CreateDir("/dir3/a", 0755), - fstest.CreateDir("/dir3/a/e", 0755), - fstest.CreateDir("/dir3/z", 0755), - ) - l2 := fstest.Apply( - fstest.CreateDir("/dir1/a", 0755), - fstest.CreateFile("/dir1/a/b", []byte("irrelevant"), 0644), - fstest.CreateDir("/dir1/a/e", 0755), - fstest.CreateFile("/dir1/a/e/f", []byte("irrelevant"), 0644), - fstest.CreateFile("/dir1/f", []byte("irrelevant"), 0644), - fstest.CreateDir("/dir2", 0755), - fstest.CreateFile("/dir2/f", []byte("irrelevant"), 0644), - fstest.CreateFile("/dir3/a/b", []byte("irrelevant"), 0644), - fstest.CreateDir("/dir3/a/c", 0755), - fstest.CreateDir("/dir3/a/e/i", 0755), - fstest.CreateFile("/dir3/a/e/i/f", []byte("irrelevant"), 0644), - fstest.CreateFile("/dir3/f", []byte("irrelevant"), 0644), - fstest.CreateFile("/dir3/z/f", []byte("irrelevant"), 0644), - ) - diff := []TestChange{ - Unchanged("/dir1"), - Add("/dir1/a"), - Add("/dir1/a/b"), - Add("/dir1/a/e"), - Add("/dir1/a/e/f"), - Add("/dir1/f"), - Add("/dir2"), - Add("/dir2/f"), - Unchanged("/dir3"), - Unchanged("/dir3/a"), - Add("/dir3/a/b"), - Add("/dir3/a/c"), - Unchanged("/dir3/a/e"), - Add("/dir3/a/e/i"), - Add("/dir3/a/e/i/f"), - Add("/dir3/f"), - Unchanged("/dir3/z"), - Add("/dir3/z/f"), - } - - if err := testDiffWithBase(l1, l2, diff); err != nil { - t.Fatalf("Failed diff with base: %+v", err) - } -} - // buildkit#172 func TestLchtimes(t *testing.T) { skipDiffTestOnWindows(t) @@ -411,7 +356,7 @@ func diffString(c1, c2 []TestChange) string { func changesString(c []TestChange) string { strs := make([]string, len(c)) for i := range c { - strs[i] = fmt.Sprintf("\t%-10s\t%s", c[i].Kind, c[i].Path) + strs[i] = fmt.Sprintf("\t%s\t%s", c[i].Kind, c[i].Path) } return strings.Join(strs, "\n") } @@ -436,10 +381,3 @@ func Modify(p string) TestChange { Path: filepath.FromSlash(p), } } - -func Unchanged(p string) TestChange { - return TestChange{ - Kind: ChangeKindUnmodified, - Path: filepath.FromSlash(p), - } -}