Refactor get tag to remove unnecessary steps (#14058)

* Fix tag cache bug
release/v1.15
Lunny Xiao 2020-12-20 06:30:39 +08:00 committed by GitHub
parent 4aabbacdfe
commit 1d3a580a3e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 19 additions and 33 deletions

View File

@ -30,21 +30,16 @@ func (repo *Repository) CreateAnnotatedTag(name, message, revision string) error
return err return err
} }
func (repo *Repository) getTag(id SHA1) (*Tag, error) { func (repo *Repository) getTag(tagID SHA1, name string) (*Tag, error) {
t, ok := repo.tagCache.Get(id.String()) t, ok := repo.tagCache.Get(tagID.String())
if ok { if ok {
log("Hit cache: %s", id) log("Hit cache: %s", tagID)
tagClone := *t.(*Tag) tagClone := *t.(*Tag)
tagClone.Name = name // This is necessary because lightweight tags may have same id
return &tagClone, nil return &tagClone, nil
} }
// Get tag name tp, err := repo.GetTagType(tagID)
name, err := repo.GetTagNameBySHA(id.String())
if err != nil {
return nil, err
}
tp, err := repo.GetTagType(id)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -60,24 +55,9 @@ func (repo *Repository) getTag(id SHA1) (*Tag, error) {
return nil, err return nil, err
} }
// tagID defaults to the commit ID as the tag ID and then tries to get a tag ID (only annotated tags)
tagID := commitID
if tagIDStr, err := repo.GetTagID(name); err != nil {
// if the err is NotExist then we can ignore and just keep tagID as ID (is lightweight tag)
// all other errors we return
if !IsErrNotExist(err) {
return nil, err
}
} else {
tagID, err = NewIDFromString(tagIDStr)
if err != nil {
return nil, err
}
}
// If type is "commit, the tag is a lightweight tag // If type is "commit, the tag is a lightweight tag
if ObjectType(tp) == ObjectCommit { if ObjectType(tp) == ObjectCommit {
commit, err := repo.GetCommit(id.String()) commit, err := repo.GetCommit(commitIDStr)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -85,18 +65,18 @@ func (repo *Repository) getTag(id SHA1) (*Tag, error) {
Name: name, Name: name,
ID: tagID, ID: tagID,
Object: commitID, Object: commitID,
Type: string(ObjectCommit), Type: tp,
Tagger: commit.Committer, Tagger: commit.Committer,
Message: commit.Message(), Message: commit.Message(),
repo: repo, repo: repo,
} }
repo.tagCache.Set(id.String(), tag) repo.tagCache.Set(tagID.String(), tag)
return tag, nil return tag, nil
} }
// The tag is an annotated tag with a message. // The tag is an annotated tag with a message.
data, err := NewCommand("cat-file", "-p", id.String()).RunInDirBytes(repo.Path) data, err := NewCommand("cat-file", "-p", tagID.String()).RunInDirBytes(repo.Path)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -107,11 +87,11 @@ func (repo *Repository) getTag(id SHA1) (*Tag, error) {
} }
tag.Name = name tag.Name = name
tag.ID = id tag.ID = tagID
tag.repo = repo tag.repo = repo
tag.Type = tp tag.Type = tp
repo.tagCache.Set(id.String(), tag) repo.tagCache.Set(tagID.String(), tag)
return tag, nil return tag, nil
} }
@ -170,7 +150,7 @@ func (repo *Repository) GetTag(name string) (*Tag, error) {
return nil, err return nil, err
} }
tag, err := repo.getTag(id) tag, err := repo.getTag(id, name)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -244,7 +224,13 @@ func (repo *Repository) GetAnnotatedTag(sha string) (*Tag, error) {
return nil, ErrNotExist{ID: id.String()} return nil, ErrNotExist{ID: id.String()}
} }
tag, err := repo.getTag(id) // Get tag name
name, err := repo.GetTagNameBySHA(id.String())
if err != nil {
return nil, err
}
tag, err := repo.getTag(id, name)
if err != nil { if err != nil {
return nil, err return nil, err
} }