git/githistory: implement *refUpdater
type
This commit is contained in:
parent
42661cdeed
commit
2f6732c4fb
59
git/githistory/ref_updater.go
Normal file
59
git/githistory/ref_updater.go
Normal file
@ -0,0 +1,59 @@
|
||||
package githistory
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
|
||||
"github.com/git-lfs/git-lfs/errors"
|
||||
"github.com/git-lfs/git-lfs/git"
|
||||
"github.com/git-lfs/git-lfs/git/githistory/log"
|
||||
)
|
||||
|
||||
// refUpdater is a type responsible for moving references from one point in the
|
||||
// Git object graph to another.
|
||||
type refUpdater struct {
|
||||
// CacheFn is a function that returns the SHA1 transformation from an
|
||||
// original hash to a new one. It specifies a "bool" return value
|
||||
// signaling whether or not that given "old" SHA1 was migrated.
|
||||
CacheFn func(old []byte) ([]byte, bool)
|
||||
// Logger logs the progress of reference updating.
|
||||
Logger *log.Logger
|
||||
// Refs is a set of *git.Ref's to migrate.
|
||||
Refs []*git.Ref
|
||||
// Root is the given directory on disk in which the repository is
|
||||
// located.
|
||||
Root string
|
||||
}
|
||||
|
||||
// UpdateRefs performs the reference update(s) from existing locations (see:
|
||||
// Refs) to their respective new locations in the graph (see CacheFn).
|
||||
//
|
||||
// It creates reflog entries as well as stderr log entries as it progresses
|
||||
// through the reference updates.
|
||||
//
|
||||
// It returns any error encountered, or nil if the reference update(s) was/were
|
||||
// successful.
|
||||
func (r *refUpdater) UpdateRefs() error {
|
||||
list := r.Logger.List("migrate: Updating refs")
|
||||
defer list.Complete()
|
||||
|
||||
for _, ref := range r.Refs {
|
||||
sha1, err := hex.DecodeString(ref.Sha)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "could not decode: %q", ref.Sha)
|
||||
}
|
||||
|
||||
to, ok := r.CacheFn(sha1)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
if err := git.UpdateRefIn(r.Root, ref, to, ""); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
list.Entry(fmt.Sprintf("%s\t%s -> %x", ref, ref.Sha, to))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
60
git/githistory/ref_updater_test.go
Normal file
60
git/githistory/ref_updater_test.go
Normal file
@ -0,0 +1,60 @@
|
||||
package githistory
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/git-lfs/git-lfs/git"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestRefUpdaterMovesRefs(t *testing.T) {
|
||||
db := DatabaseFromFixture(t, "linear-history-with-tags.git")
|
||||
root, _ := db.Root()
|
||||
|
||||
updater := &refUpdater{
|
||||
CacheFn: func(old []byte) ([]byte, bool) {
|
||||
return HexDecode(t, "d941e4756add6b06f5bee766fcf669f55419f13f"), true
|
||||
},
|
||||
Refs: []*git.Ref{
|
||||
{
|
||||
Name: "middle",
|
||||
Sha: "228afe30855933151f7a88e70d9d88314fd2f191",
|
||||
Type: git.RefTypeLocalTag,
|
||||
},
|
||||
},
|
||||
Root: root,
|
||||
}
|
||||
|
||||
err := updater.UpdateRefs()
|
||||
|
||||
assert.NoError(t, err)
|
||||
|
||||
AssertRef(t, db,
|
||||
"refs/tags/middle", HexDecode(t, "d941e4756add6b06f5bee766fcf669f55419f13f"))
|
||||
}
|
||||
|
||||
func TestRefUpdaterIgnoresUnovedRefs(t *testing.T) {
|
||||
db := DatabaseFromFixture(t, "linear-history-with-tags.git")
|
||||
root, _ := db.Root()
|
||||
|
||||
updater := &refUpdater{
|
||||
CacheFn: func(old []byte) ([]byte, bool) {
|
||||
return nil, false
|
||||
},
|
||||
Refs: []*git.Ref{
|
||||
{
|
||||
Name: "middle",
|
||||
Sha: "228afe30855933151f7a88e70d9d88314fd2f191",
|
||||
Type: git.RefTypeLocalTag,
|
||||
},
|
||||
},
|
||||
Root: root,
|
||||
}
|
||||
|
||||
err := updater.UpdateRefs()
|
||||
|
||||
assert.NoError(t, err)
|
||||
|
||||
AssertRef(t, db,
|
||||
"refs/tags/middle", HexDecode(t, "228afe30855933151f7a88e70d9d88314fd2f191"))
|
||||
}
|
Loading…
Reference in New Issue
Block a user