git/githistory: teach UpdateRefs option

This commit is contained in:
Taylor Blau 2017-06-19 15:54:00 -04:00
parent 47afc58817
commit 68b76005c9
2 changed files with 57 additions and 0 deletions

@ -8,6 +8,7 @@ import (
"path/filepath"
"sync"
"github.com/git-lfs/git-lfs/errors"
"github.com/git-lfs/git-lfs/filepathfilter"
"github.com/git-lfs/git-lfs/git"
"github.com/git-lfs/git-lfs/git/githistory/log"
@ -46,6 +47,11 @@ type RewriteOptions struct {
// will be excluded.
Exclude []string
// UpdateRefs specifies whether the Rewriter should move refs from the
// original graph onto the migrated one. If true, the refs will be
// moved, and a reflog entry will be created.
UpdateRefs bool
// BlobFn specifies a function to rewrite blobs.
//
// It is called once per unique, unchanged path. That is to say, if
@ -233,6 +239,26 @@ func (r *Rewriter) Rewrite(opt *RewriteOptions) ([]byte, error) {
tip = rewrittenCommit
}
if opt.UpdateRefs {
refs, err := r.refsToMigrate()
if err != nil {
return nil, errors.Wrap(err, "could not find refs to update")
}
root, _ := r.db.Root()
updater := &refUpdater{
CacheFn: r.uncacheCommit,
Logger: r.l,
Refs: refs,
Root: root,
}
if err := updater.UpdateRefs(); err != nil {
return nil, errors.Wrap(err, "could not update refs")
}
}
r.l.Close()
return tip, err

@ -299,6 +299,37 @@ func TestHistoryRewriterUseOriginalParentsForPartialMigration(t *testing.T) {
AssertCommitParent(t, db, hex.EncodeToString(tip), expectedParent)
}
func TestHistoryRewriterUpdatesRefs(t *testing.T) {
db := DatabaseFromFixture(t, "linear-history.git")
r := NewRewriter(db)
tip, err := r.Rewrite(&RewriteOptions{
Include: []string{"refs/heads/master"},
UpdateRefs: true,
BlobFn: func(path string, b *odb.Blob) (*odb.Blob, error) {
suffix := strings.NewReader("_suffix")
return &odb.Blob{
Contents: io.MultiReader(b.Contents, suffix),
Size: b.Size + int64(suffix.Len()),
}, nil
},
})
assert.Nil(t, err)
c1 := hex.EncodeToString(tip)
c2 := "66561fe3ae68651658e18e48053dcfe66a2e9da1"
c3 := "8268d8486c48024a871fa42fc487dbeabd6e3d86"
AssertRef(t, db, "refs/heads/master", tip)
AssertCommitParent(t, db, c1, c2)
AssertCommitParent(t, db, c2, c3)
}
func root(path string) string {
if !strings.HasPrefix(path, string(os.PathSeparator)) {
path = string(os.PathSeparator) + path