Skip to content

Skip push update when tag reference to blob/tree #23214

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions modules/git/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,22 @@ func (err ErrNotExist) Unwrap() error {
return util.ErrNotExist
}

// ErrWrongType git object with wrong type
type ErrWrongType struct {
ID string
Type string
}

// IsErrWrongType if some error is ErrWrongType
func IsErrWrongType(err error) bool {
_, ok := err.(ErrWrongType)
return ok
}

func (err ErrWrongType) Error() string {
return fmt.Sprintf("git object type is invalid [id: %s, type: %s]", err.ID, err.Type)
}

// ErrBadLink entry.FollowLink error
type ErrBadLink struct {
Name string
Expand Down
61 changes: 47 additions & 14 deletions modules/git/repo_commit_gogit.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ package git
import (
"strings"

"code.gitea.io/gitea/modules/log"

"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
)
Expand Down Expand Up @@ -66,26 +68,57 @@ func (repo *Repository) IsCommitExist(name string) bool {
return err == nil
}

func (repo *Repository) getCommit(id SHA1) (*Commit, error) {
var tagObject *object.Tag

gogitCommit, err := repo.gogitRepo.CommitObject(id)
if err == plumbing.ErrObjectNotFound {
tagObject, err = repo.gogitRepo.TagObject(id)
if err == plumbing.ErrObjectNotFound {
return nil, ErrNotExist{
ID: id.String(),
}
}
if err == nil {
gogitCommit, err = repo.gogitRepo.CommitObject(tagObject.Target)
func (repo *Repository) getGoGitTagDeepestObject(tag *object.Tag) (object.Object, error) {
obj, err := tag.Object()
if err != nil {
return nil, err
}
if subTag, ok := obj.(*object.Tag); ok {
obj, err = repo.getGoGitTagDeepestObject(subTag)
if err != nil {
return nil, err
}
// if we get a plumbing.ErrObjectNotFound here then the repository is broken and it should be 500
}
return obj, nil
}

func (repo *Repository) getCommit(id SHA1) (*Commit, error) {
var gogitCommit *object.Commit

obj, err := repo.gogitRepo.Object(plumbing.AnyObject, id)
if err != nil {
return nil, err
}

switch o := obj.(type) {
case *object.Commit:
gogitCommit = o
case *object.Tag:
var ok bool

obj, err := repo.getGoGitTagDeepestObject(o)
if err != nil {
return nil, err
}
if gogitCommit, ok = obj.(*object.Commit); !ok {
return nil, ErrWrongType{
ID: obj.ID().String(),
Type: obj.Type().String(),
}
}

case *object.Blob, *object.Tree:
return nil, ErrWrongType{
ID: id.String(),
Type: o.Type().String(),
}
default:
log.Debug("Unknown typ: %s", o.Type())
return nil, ErrNotExist{
ID: id.String(),
}
}

commit := convertCommit(gogitCommit)
commit.repo = repo

Expand Down
9 changes: 9 additions & 0 deletions modules/git/repo_commit_nogogit.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,15 @@ func (repo *Repository) getCommitFromBatchReader(rd *bufio.Reader, id SHA1) (*Co
}

return commit, nil
case "blob", "tree":
_, err = rd.Discard(int(size) + 1)
if err != nil {
return nil, err
}
return nil, ErrWrongType{
ID: id.String(),
Type: typ,
}
default:
log.Debug("Unknown typ: %s", typ)
_, err = rd.Discard(int(size) + 1)
Expand Down
4 changes: 4 additions & 0 deletions services/repository/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error {
} else { // is new tag
newCommit, err := gitRepo.GetCommit(opts.NewCommitID)
if err != nil {
if git.IsErrWrongType(err) {
log.Info("ignore special ref push update: %v", err)
continue
}
return fmt.Errorf("gitRepo.GetCommit(%s) in %s/%s[%d]: %w", opts.NewCommitID, repo.OwnerName, repo.Name, repo.ID, err)
}

Expand Down