From 23a61705fafa1cc9bd3ff332badcc4e7476e6d01 Mon Sep 17 00:00:00 2001 From: Taylor Blau Date: Mon, 5 Jun 2017 14:41:00 -0600 Subject: [PATCH] git/githistory: make path include absolute root --- git/githistory/rewriter.go | 14 ++++++++------ git/githistory/rewriter_test.go | 18 +++++++++++++----- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/git/githistory/rewriter.go b/git/githistory/rewriter.go index 5192b8f6..f6e7560e 100644 --- a/git/githistory/rewriter.go +++ b/git/githistory/rewriter.go @@ -3,6 +3,7 @@ package githistory import ( "encoding/hex" "fmt" + "os" "path/filepath" "sync" @@ -42,9 +43,10 @@ type RewriteOptions struct { // BlobFn specifies a function to rewrite blobs. // // It is called once per unique, unchanged path. That is to say, if - // a/foo and a/bar contain identical contents, the BlobFn will be called - // twice: once for a/foo and once for a/bar, but no more on each blob - // for subsequent revisions, so long as each entry remains unchanged. + // /a/foo and /a/bar contain identical contents, the BlobFn will be + // called twice: once for /a/foo and once for /a/bar, but no more on + // each blob for subsequent revisions, so long as each entry remains + // unchanged. BlobFn BlobRewriteFn } @@ -57,8 +59,8 @@ type RewriteOptions struct { // // The path argument is given to be an absolute path to the tree entry being // rewritten, where the repository root is the root of the path given. For -// instance, a file "b.txt" in directory "dir" would be given as "dir/b.txt", -// where as a file "a.txt" in the root would be given as "a.txt". +// instance, a file "b.txt" in directory "dir" would be given as "/dir/b.txt", +// where as a file "a.txt" in the root would be given as "/a.txt". // // As above, the path separators are OS specific, and equivalent to the result // of filepath.Join(...) or os.PathSeparator. @@ -115,7 +117,7 @@ func (r *Rewriter) Rewrite(opt *RewriteOptions) ([]byte, error) { } // Rewrite the tree given at that commit. - rewrittenTree, err := r.rewriteTree(original.TreeID, "", opt.BlobFn) + rewrittenTree, err := r.rewriteTree(original.TreeID, string(os.PathSeparator), opt.BlobFn) if err != nil { return nil, err } diff --git a/git/githistory/rewriter_test.go b/git/githistory/rewriter_test.go index 660c5b22..1edd00e3 100644 --- a/git/githistory/rewriter_test.go +++ b/git/githistory/rewriter_test.go @@ -4,6 +4,7 @@ import ( "encoding/hex" "io" "io/ioutil" + "os" "path/filepath" "strconv" "strings" @@ -136,8 +137,8 @@ func TestRewriterDoesntVisitUnchangedSubtrees(t *testing.T) { assert.Nil(t, err) - assert.Equal(t, 2, seen["a.txt"]) - assert.Equal(t, 1, seen[filepath.Join("subdir", "b.txt")]) + assert.Equal(t, 2, seen[root("a.txt")]) + assert.Equal(t, 1, seen[root(filepath.Join("subdir", "b.txt"))]) } func TestRewriterVisitsUniqueEntriesWithIdenticalContents(t *testing.T) { @@ -146,7 +147,7 @@ func TestRewriterVisitsUniqueEntriesWithIdenticalContents(t *testing.T) { tip, err := r.Rewrite(&RewriteOptions{Left: "master", BlobFn: func(path string, b *odb.Blob) (*odb.Blob, error) { - if path == "b.txt" { + if path == root("b.txt") { return b, nil } @@ -193,6 +194,13 @@ func TestRewriterIgnoresPathsThatDontMatchFilter(t *testing.T) { }) assert.Nil(t, err) - assert.Equal(t, 1, seen["a.txt"]) - assert.Equal(t, 0, seen["subdir/b.txt"]) + assert.Equal(t, 1, seen[root("a.txt")]) + assert.Equal(t, 0, seen[root(filepath.Join("subdir", "b.txt"))]) +} + +func root(path string) string { + if !strings.HasPrefix(path, string(os.PathSeparator)) { + path = string(os.PathSeparator) + path + } + return path }