1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
|
package githistory
import (
"encoding/hex"
"fmt"
"strings"
"github.com/git-lfs/git-lfs/v3/errors"
"github.com/git-lfs/git-lfs/v3/git"
"github.com/git-lfs/git-lfs/v3/tasklog"
"github.com/git-lfs/git-lfs/v3/tools"
"github.com/git-lfs/git-lfs/v3/tr"
"github.com/git-lfs/gitobj/v2"
)
// 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 *tasklog.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
db *gitobj.ObjectDatabase
}
// 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(fmt.Sprintf("migrate: %s", tr.Tr.Get("Updating refs")))
defer list.Complete()
var maxNameLen int
for _, ref := range r.Refs {
maxNameLen = tools.MaxInt(maxNameLen, len(ref.Name))
}
seen := make(map[string]struct{})
for _, ref := range r.Refs {
if err := r.updateOneRef(list, maxNameLen, seen, ref); err != nil {
return err
}
}
return nil
}
func (r *refUpdater) updateOneTag(tag *gitobj.Tag, toObj []byte) ([]byte, error) {
newTag, err := r.db.WriteTag(&gitobj.Tag{
Object: toObj,
ObjectType: tag.ObjectType,
Name: tag.Name,
Tagger: tag.Tagger,
Message: tag.Message,
})
if err != nil {
return nil, errors.Wrap(err, tr.Tr.Get("could not rewrite tag: %s", tag.Name))
}
return newTag, nil
}
func (r *refUpdater) updateOneRef(list *tasklog.ListTask, maxNameLen int, seen map[string]struct{}, ref *git.Ref) error {
sha1, err := hex.DecodeString(ref.Sha)
if err != nil {
return errors.Wrap(err, tr.Tr.Get("could not decode: %q", ref.Sha))
}
refspec := ref.Refspec()
if _, ok := seen[refspec]; ok {
return nil
}
seen[refspec] = struct{}{}
to, ok := r.CacheFn(sha1)
if ref.Type == git.RefTypeLocalTag {
tag, _ := r.db.Tag(sha1)
if tag != nil && tag.ObjectType == gitobj.TagObjectType {
innerTag, _ := r.db.Tag(tag.Object)
name := fmt.Sprintf("refs/tags/%s", innerTag.Name)
if _, ok := seen[name]; !ok {
old, err := git.ResolveRef(name)
if err != nil {
return err
}
err = r.updateOneRef(list, maxNameLen, seen, old)
if err != nil {
return err
}
}
updated, err := git.ResolveRef(name)
if err != nil {
return err
}
updatedSha, err := hex.DecodeString(updated.Sha)
if err != nil {
return errors.Wrap(err, tr.Tr.Get("could not decode: %q", ref.Sha))
}
newTag, err := r.updateOneTag(tag, updatedSha)
if newTag == nil {
return err
}
to = newTag
ok = true
} else if tag != nil && tag.ObjectType == gitobj.CommitObjectType {
toObj, okObj := r.CacheFn(tag.Object)
if !okObj {
return nil
}
newTag, err := r.updateOneTag(tag, toObj)
if newTag == nil {
return err
}
to = newTag
ok = true
}
}
if !ok {
return nil
}
if err := git.UpdateRefIn(r.Root, ref, to, ""); err != nil {
return err
}
namePadding := tools.MaxInt(maxNameLen-len(ref.Name), 0)
list.Entry(fmt.Sprintf(" %s%s\t%s -> %x", ref.Name, strings.Repeat(" ", namePadding), ref.Sha, to))
return nil
}
|