diff options
author | Lauris BH <lauris@nix.lv> | 2017-09-20 08:26:49 +0300 |
---|---|---|
committer | Lunny Xiao <xiaolunwen@gmail.com> | 2017-09-20 13:26:49 +0800 |
commit | 7a0297819d3aececb8b633de5a2995c40b7bfcae (patch) | |
tree | 7aed967ad21f95df2b16359ec1551662d653a1fd /models/update.go | |
parent | 8b6236d67b21f2a3291207ef77d103a3ce7476ba (diff) | |
download | gitea-7a0297819d3aececb8b633de5a2995c40b7bfcae.tar.gz gitea-7a0297819d3aececb8b633de5a2995c40b7bfcae.zip |
Sync releases table with tags on push and for mirrors (#2459)
* Sync releases table with tags on push and for mirrors
* Code style fixes
* Fix api to return only releases
* Optimize release creation and update
Minimize posibility of race conditions
* Fix release lower tag name updating
* handle tag reference update by addionally comparing commit id
Diffstat (limited to 'models/update.go')
-rw-r--r-- | models/update.go | 105 |
1 files changed, 100 insertions, 5 deletions
diff --git a/models/update.go b/models/update.go index e6cbba64a5..56a227b24a 100644 --- a/models/update.go +++ b/models/update.go @@ -81,6 +81,93 @@ func PushUpdate(branch string, opt PushUpdateOptions) error { return nil } +func pushUpdateDeleteTag(repo *Repository, gitRepo *git.Repository, tagName string) error { + rel, err := GetRelease(repo.ID, tagName) + if err != nil { + if IsErrReleaseNotExist(err) { + return nil + } + return fmt.Errorf("GetRelease: %v", err) + } + if rel.IsTag { + if _, err = x.Id(rel.ID).Delete(new(Release)); err != nil { + return fmt.Errorf("Delete: %v", err) + } + } else { + rel.IsDraft = true + rel.NumCommits = 0 + rel.Sha1 = "" + if _, err = x.Id(rel.ID).AllCols().Update(rel); err != nil { + return fmt.Errorf("Update: %v", err) + } + } + + return nil +} + +func pushUpdateAddTag(repo *Repository, gitRepo *git.Repository, tagName string) error { + rel, err := GetRelease(repo.ID, tagName) + if err != nil && !IsErrReleaseNotExist(err) { + return fmt.Errorf("GetRelease: %v", err) + } + + tag, err := gitRepo.GetTag(tagName) + if err != nil { + return fmt.Errorf("GetTag: %v", err) + } + commit, err := tag.Commit() + if err != nil { + return fmt.Errorf("Commit: %v", err) + } + tagCreatedUnix := commit.Author.When.Unix() + + author, err := GetUserByEmail(commit.Author.Email) + if err != nil && !IsErrUserNotExist(err) { + return fmt.Errorf("GetUserByEmail: %v", err) + } + + commitsCount, err := commit.CommitsCount() + if err != nil { + return fmt.Errorf("CommitsCount: %v", err) + } + + if rel == nil { + rel = &Release{ + RepoID: repo.ID, + Title: "", + TagName: tagName, + LowerTagName: strings.ToLower(tagName), + Target: "", + Sha1: commit.ID.String(), + NumCommits: commitsCount, + Note: "", + IsDraft: false, + IsPrerelease: false, + IsTag: true, + CreatedUnix: tagCreatedUnix, + } + if author != nil { + rel.PublisherID = author.ID + } + + if _, err = x.InsertOne(rel); err != nil { + return fmt.Errorf("InsertOne: %v", err) + } + } else { + rel.Sha1 = commit.ID.String() + rel.CreatedUnix = tagCreatedUnix + rel.NumCommits = commitsCount + rel.IsDraft = false + if rel.IsTag && author != nil { + rel.PublisherID = author.ID + } + if _, err = x.Id(rel.ID).AllCols().Update(rel); err != nil { + return fmt.Errorf("Update: %v", err) + } + } + return nil +} + func pushUpdate(opts PushUpdateOptions) (repo *Repository, err error) { isNewRef := opts.OldCommitID == git.EmptySHA isDelRef := opts.NewCommitID == git.EmptySHA @@ -106,23 +193,31 @@ func pushUpdate(opts PushUpdateOptions) (repo *Repository, err error) { return nil, fmt.Errorf("GetRepositoryByName: %v", err) } + gitRepo, err := git.OpenRepository(repoPath) + if err != nil { + return nil, fmt.Errorf("OpenRepository: %v", err) + } + if isDelRef { + // Tag has been deleted + if strings.HasPrefix(opts.RefFullName, git.TagPrefix) { + err = pushUpdateDeleteTag(repo, gitRepo, opts.RefFullName[len(git.TagPrefix):]) + if err != nil { + return nil, fmt.Errorf("pushUpdateDeleteTag: %v", err) + } + } log.GitLogger.Info("Reference '%s' has been deleted from '%s/%s' by %s", opts.RefFullName, opts.RepoUserName, opts.RepoName, opts.PusherName) return repo, nil } - gitRepo, err := git.OpenRepository(repoPath) - if err != nil { - return nil, fmt.Errorf("OpenRepository: %v", err) - } - if err = repo.UpdateSize(); err != nil { log.Error(4, "Failed to update size for repository: %v", err) } // Push tags. if strings.HasPrefix(opts.RefFullName, git.TagPrefix) { + pushUpdateAddTag(repo, gitRepo, opts.RefFullName[len(git.TagPrefix):]) if err := CommitRepoAction(CommitRepoActionOptions{ PusherName: opts.PusherName, RepoOwnerID: owner.ID, |