aboutsummaryrefslogtreecommitdiff
path: root/internal/db/update.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/db/update.go')
-rw-r--r--internal/db/update.go51
1 files changed, 23 insertions, 28 deletions
diff --git a/internal/db/update.go b/internal/db/update.go
index e8fc41ed..94fc4ee3 100644
--- a/internal/db/update.go
+++ b/internal/db/update.go
@@ -5,19 +5,18 @@
package db
import (
- "container/list"
"fmt"
"os/exec"
"strings"
- git "github.com/gogs/git-module"
+ "github.com/gogs/git-module"
)
// CommitToPushCommit transforms a git.Commit to PushCommit type.
func CommitToPushCommit(commit *git.Commit) *PushCommit {
return &PushCommit{
Sha1: commit.ID.String(),
- Message: commit.Message(),
+ Message: commit.Message,
AuthorEmail: commit.Author.Email,
AuthorName: commit.Author.Name,
CommitterEmail: commit.Committer.Email,
@@ -26,27 +25,22 @@ func CommitToPushCommit(commit *git.Commit) *PushCommit {
}
}
-func ListToPushCommits(l *list.List) *PushCommits {
- if l == nil {
+func CommitsToPushCommits(commits []*git.Commit) *PushCommits {
+ if len(commits) == 0 {
return &PushCommits{}
}
- commits := make([]*PushCommit, 0)
- var actEmail string
- for e := l.Front(); e != nil; e = e.Next() {
- commit := e.Value.(*git.Commit)
- if actEmail == "" {
- actEmail = commit.Committer.Email
- }
- commits = append(commits, CommitToPushCommit(commit))
+ pcs := make([]*PushCommit, len(commits))
+ for i := range commits {
+ pcs[i] = CommitToPushCommit(commits[i])
}
- return &PushCommits{l.Len(), commits, "", nil}
+ return &PushCommits{len(pcs), pcs, "", nil}
}
type PushUpdateOptions struct {
OldCommitID string
NewCommitID string
- RefFullName string
+ FullRefspec string
PusherID int64
PusherName string
RepoUserName string
@@ -56,10 +50,10 @@ type PushUpdateOptions struct {
// PushUpdate must be called for any push actions in order to
// generates necessary push action history feeds.
func PushUpdate(opts PushUpdateOptions) (err error) {
- isNewRef := opts.OldCommitID == git.EMPTY_SHA
- isDelRef := opts.NewCommitID == git.EMPTY_SHA
+ isNewRef := strings.HasPrefix(opts.OldCommitID, git.EmptyID)
+ isDelRef := strings.HasPrefix(opts.NewCommitID, git.EmptyID)
if isNewRef && isDelRef {
- return fmt.Errorf("Old and new revisions are both %s", git.EMPTY_SHA)
+ return fmt.Errorf("both old and new revisions are %q", git.EmptyID)
}
repoPath := RepoPath(opts.RepoUserName, opts.RepoName)
@@ -70,9 +64,9 @@ func PushUpdate(opts PushUpdateOptions) (err error) {
return fmt.Errorf("run 'git update-server-info': %v", err)
}
- gitRepo, err := git.OpenRepository(repoPath)
+ gitRepo, err := git.Open(repoPath)
if err != nil {
- return fmt.Errorf("OpenRepository: %v", err)
+ return fmt.Errorf("open repository: %v", err)
}
owner, err := GetUserByName(opts.RepoUserName)
@@ -90,12 +84,12 @@ func PushUpdate(opts PushUpdateOptions) (err error) {
}
// Push tags
- if strings.HasPrefix(opts.RefFullName, git.TAG_PREFIX) {
+ if strings.HasPrefix(opts.FullRefspec, git.RefsTags) {
if err := CommitRepoAction(CommitRepoActionOptions{
PusherName: opts.PusherName,
RepoOwnerID: owner.ID,
RepoName: repo.Name,
- RefFullName: opts.RefFullName,
+ RefFullName: opts.FullRefspec,
OldCommitID: opts.OldCommitID,
NewCommitID: opts.NewCommitID,
Commits: &PushCommits{},
@@ -105,22 +99,23 @@ func PushUpdate(opts PushUpdateOptions) (err error) {
return nil
}
- var l *list.List
+ var commits []*git.Commit
// Skip read parent commits when delete branch
if !isDelRef {
// Push new branch
- newCommit, err := gitRepo.GetCommit(opts.NewCommitID)
+ newCommit, err := gitRepo.CatFileCommit(opts.NewCommitID)
if err != nil {
return fmt.Errorf("GetCommit [commit_id: %s]: %v", opts.NewCommitID, err)
}
if isNewRef {
- l, err = newCommit.CommitsBeforeLimit(10)
+ commits, err = newCommit.Ancestors(git.LogOptions{MaxCount: 9})
if err != nil {
return fmt.Errorf("CommitsBeforeLimit [commit_id: %s]: %v", newCommit.ID, err)
}
+ commits = append([]*git.Commit{newCommit}, commits...)
} else {
- l, err = newCommit.CommitsBeforeUntil(opts.OldCommitID)
+ commits, err = newCommit.CommitsAfter(opts.OldCommitID)
if err != nil {
return fmt.Errorf("CommitsBeforeUntil [commit_id: %s]: %v", opts.OldCommitID, err)
}
@@ -131,10 +126,10 @@ func PushUpdate(opts PushUpdateOptions) (err error) {
PusherName: opts.PusherName,
RepoOwnerID: owner.ID,
RepoName: repo.Name,
- RefFullName: opts.RefFullName,
+ RefFullName: opts.FullRefspec,
OldCommitID: opts.OldCommitID,
NewCommitID: opts.NewCommitID,
- Commits: ListToPushCommits(l),
+ Commits: CommitsToPushCommits(commits),
}); err != nil {
return fmt.Errorf("CommitRepoAction.(branch): %v", err)
}