diff --git a/git/githistory/history_rewriter.go b/git/githistory/rewriter.go similarity index 87% rename from git/githistory/history_rewriter.go rename to git/githistory/rewriter.go index f1df8569..7be270dc 100644 --- a/git/githistory/history_rewriter.go +++ b/git/githistory/rewriter.go @@ -10,9 +10,9 @@ import ( "github.com/git-lfs/git-lfs/git/odb" ) -// HistoryRewriter allows rewriting topologically equivalent Git histories +// Rewriter allows rewriting topologically equivalent Git histories // between two revisions. -type HistoryRewriter struct { +type Rewriter struct { // mu guards entries and commits (see below) mu *sync.Mutex // entries is a mapping of old tree entries to new (rewritten) ones. @@ -59,10 +59,9 @@ type RewriteOptions struct { // of filepath.Join(...) or os.PathSeparator. type BlobRewriteFn func(path string, b *odb.Blob) (*odb.Blob, error) -// NewHistoryRewriter constructs a *HistoryRewriter from the given -// *ObjectDatabase instance. -func NewHistoryRewriter(db *odb.ObjectDatabase) *HistoryRewriter { - return &HistoryRewriter{ +// NewRewriter constructs a *Rewriter from the given *ObjectDatabase instance. +func NewRewriter(db *odb.ObjectDatabase) *Rewriter { + return &Rewriter{ mu: new(sync.Mutex), entries: make(map[string]*odb.TreeEntry), commits: make(map[string][]byte), @@ -73,7 +72,7 @@ func NewHistoryRewriter(db *odb.ObjectDatabase) *HistoryRewriter { // Rewrite rewrites the range of commits given by *RewriteOptions.{Left,Right} // using the BlobRewriteFn to rewrite the individual blobs. -func (r *HistoryRewriter) Rewrite(opt *RewriteOptions) ([]byte, error) { +func (r *Rewriter) Rewrite(opt *RewriteOptions) ([]byte, error) { // First, construct a scanner to iterate through the range of commits to // rewrite. scanner, err := git.NewRevListScanner(opt.Left, opt.Right, r.scannerOpts()) @@ -147,7 +146,7 @@ func (r *HistoryRewriter) Rewrite(opt *RewriteOptions) ([]byte, error) { // // It returns the new SHA of the rewritten tree, or an error if the tree was // unable to be rewritten. -func (r *HistoryRewriter) rewriteTree(sha []byte, path string, fn BlobRewriteFn) ([]byte, error) { +func (r *Rewriter) rewriteTree(sha []byte, path string, fn BlobRewriteFn) ([]byte, error) { tree, err := r.db.Tree(sha) if err != nil { return nil, err @@ -190,7 +189,7 @@ func (r *HistoryRewriter) rewriteTree(sha []byte, path string, fn BlobRewriteFn) // database by the SHA1 "from" []byte. It writes and returns the new blob SHA, // or an error if either the BlobRewriteFn returned one, or if the object could // not be loaded/saved. -func (r *HistoryRewriter) rewriteBlob(from []byte, path string, fn BlobRewriteFn) ([]byte, error) { +func (r *Rewriter) rewriteBlob(from []byte, path string, fn BlobRewriteFn) ([]byte, error) { blob, err := r.db.Blob(from) if err != nil { return nil, err @@ -206,9 +205,9 @@ func (r *HistoryRewriter) rewriteBlob(from []byte, path string, fn BlobRewriteFn // scannerOpts returns a *git.ScanRefsOptions instance to be given to the // *git.RevListScanner. // -// If the database this *HistoryRewriter is operating in a given root (not in -// memory) it re-assigns the working directory to be there. -func (r *HistoryRewriter) scannerOpts() *git.ScanRefsOptions { +// If the database this *Rewriter is operating in a given root (not in memory) +// it re-assigns the working directory to be there. +func (r *Rewriter) scannerOpts() *git.ScanRefsOptions { opts := &git.ScanRefsOptions{ Mode: git.ScanRefsMode, Order: git.TopoRevListOrder, @@ -228,7 +227,7 @@ func (r *HistoryRewriter) scannerOpts() *git.ScanRefsOptions { // cacheEntry caches then given "from" entry so that it is always rewritten as // a *TreeEntry equivalent to "to". -func (r *HistoryRewriter) cacheEntry(from, to *odb.TreeEntry) *odb.TreeEntry { +func (r *Rewriter) cacheEntry(from, to *odb.TreeEntry) *odb.TreeEntry { r.mu.Lock() defer r.mu.Unlock() @@ -240,7 +239,7 @@ func (r *HistoryRewriter) cacheEntry(from, to *odb.TreeEntry) *odb.TreeEntry { // uncacheEntry returns a *TreeEntry that is cached from the given *TreeEntry // "from". That is to say, it returns the *TreeEntry that "from" should be // rewritten to, or nil if none could be found. -func (r *HistoryRewriter) uncacheEntry(from *odb.TreeEntry) *odb.TreeEntry { +func (r *Rewriter) uncacheEntry(from *odb.TreeEntry) *odb.TreeEntry { r.mu.Lock() defer r.mu.Unlock() @@ -248,13 +247,13 @@ func (r *HistoryRewriter) uncacheEntry(from *odb.TreeEntry) *odb.TreeEntry { } // entryKey returns a unique key for a given *TreeEntry "e". -func (r *HistoryRewriter) entryKey(e *odb.TreeEntry) string { +func (r *Rewriter) entryKey(e *odb.TreeEntry) string { return fmt.Sprintf("%s:%x", e.Name, e.Oid) } // cacheEntry caches then given "from" commit so that it is always rewritten as // a *git/odb.Commit equivalent to "to". -func (r *HistoryRewriter) cacheCommit(from, to []byte) { +func (r *Rewriter) cacheCommit(from, to []byte) { r.mu.Lock() defer r.mu.Unlock() @@ -264,7 +263,7 @@ func (r *HistoryRewriter) cacheCommit(from, to []byte) { // uncacheCommit returns a *git/odb.Commit that is cached from the given // *git/odb.Commit "from". That is to say, it returns the *git/odb.Commit that // "from" should be rewritten to, or nil if none could be found. -func (r *HistoryRewriter) uncacheCommit(from []byte) []byte { +func (r *Rewriter) uncacheCommit(from []byte) []byte { r.mu.Lock() defer r.mu.Unlock() diff --git a/git/githistory/history_rewriter_test.go b/git/githistory/rewriter_test.go similarity index 92% rename from git/githistory/history_rewriter_test.go rename to git/githistory/rewriter_test.go index d00592a7..6148293d 100644 --- a/git/githistory/history_rewriter_test.go +++ b/git/githistory/rewriter_test.go @@ -13,9 +13,9 @@ import ( "github.com/stretchr/testify/assert" ) -func TestHistoryRewriterRewritesHistory(t *testing.T) { +func TestRewriterRewritesHistory(t *testing.T) { db := DatabaseFromFixture(t, "linear-history.git") - r := NewHistoryRewriter(db) + r := NewRewriter(db) tip, err := r.Rewrite(&RewriteOptions{Left: "master", BlobFn: func(path string, b *odb.Blob) (*odb.Blob, error) { @@ -74,9 +74,9 @@ func TestHistoryRewriterRewritesHistory(t *testing.T) { AssertBlobContents(t, db, tree3, "hello.txt", "2") } -func TestHistoryRewriterRewritesOctopusMerges(t *testing.T) { +func TestRewriterRewritesOctopusMerges(t *testing.T) { db := DatabaseFromFixture(t, "octopus-merge.git") - r := NewHistoryRewriter(db) + r := NewRewriter(db) tip, err := r.Rewrite(&RewriteOptions{Left: "master", BlobFn: func(path string, b *odb.Blob) (*odb.Blob, error) { @@ -119,9 +119,9 @@ func TestHistoryRewriterRewritesOctopusMerges(t *testing.T) { AssertCommitParent(t, db, "ca447959bdcd20253d69b227bcc7c2e1d3126d5c", "9237567f379b3c83ddf53ad9a2ae3755afb62a09") } -func TestHistoryRewriterDoesntVisitUnchangedSubtrees(t *testing.T) { +func TestRewriterDoesntVisitUnchangedSubtrees(t *testing.T) { db := DatabaseFromFixture(t, "repeated-subtrees.git") - r := NewHistoryRewriter(db) + r := NewRewriter(db) seen := make(map[string]int) @@ -139,9 +139,9 @@ func TestHistoryRewriterDoesntVisitUnchangedSubtrees(t *testing.T) { assert.Equal(t, 1, seen[filepath.Join("subdir", "b.txt")]) } -func TestHistoryRewriterVisitsUniqueEntriesWithIdenticalContents(t *testing.T) { +func TestRewriterVisitsUniqueEntriesWithIdenticalContents(t *testing.T) { db := DatabaseFromFixture(t, "identical-blobs.git") - r := NewHistoryRewriter(db) + r := NewRewriter(db) tip, err := r.Rewrite(&RewriteOptions{Left: "master", BlobFn: func(path string, b *odb.Blob) (*odb.Blob, error) {