Skip to content
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

fix bug about can't skip commits base on base branch #11555

Merged
merged 9 commits into from
Jun 24, 2020
98 changes: 87 additions & 11 deletions models/issue_comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -1124,12 +1124,11 @@ func getCommitIDsFromRepo(repo *Repository, oldCommitID, newCommitID, baseBranch
return nil, false, err
}

oldCommitBranch, err := oldCommit.GetBranchName()
if err != nil {
if err = oldCommit.LoadBranchName(); err != nil {
return nil, false, err
}

if oldCommitBranch == "" {
if oldCommit.Branch == "" {
a1012112796 marked this conversation as resolved.
Show resolved Hide resolved
commitIDs = make([]string, 2)
commitIDs[0] = oldCommitID
commitIDs[1] = newCommitID
Expand All @@ -1142,25 +1141,102 @@ func getCommitIDsFromRepo(repo *Repository, oldCommitID, newCommitID, baseBranch
return nil, false, err
}

var commits *list.List
var (
commits *list.List
commitChecks map[string]commitBranchCheckItem
)
commits, err = newCommit.CommitsBeforeUntil(oldCommitID)
if err != nil {
return nil, false, err
}

commitIDs = make([]string, 0, commits.Len())
commitChecks = make(map[string]commitBranchCheckItem)

for e := commits.Back(); e != nil; e = e.Prev() {
commit := e.Value.(*git.Commit)
commitBranch, err := commit.GetBranchName()
if err != nil {
return nil, false, err
for e := commits.Front(); e != nil; e = e.Next() {
commitChecks[e.Value.(*git.Commit).ID.String()] = commitBranchCheckItem{
Commit: e.Value.(*git.Commit),
Checked: false,
}
}

if commitBranch != baseBranch {
commitIDs = append(commitIDs, commit.ID.String())
if err = commitBranchCheck(gitRepo, newCommit, oldCommitID, baseBranch, commitChecks); err != nil {
return
}

for e := commits.Back(); e != nil; e = e.Prev() {
commitID := e.Value.(*git.Commit).ID.String()
if item, ok := commitChecks[commitID]; ok && item.Checked {
commitIDs = append(commitIDs, commitID)
}
}

return
}

type commitBranchCheckItem struct {
Commit *git.Commit
Checked bool
}

func commitBranchCheck(gitRepo *git.Repository, startCommit *git.Commit, endCommitID, baseBranch string, commitList map[string]commitBranchCheckItem) (err error) {
var (
item commitBranchCheckItem
ok bool
listItem *list.Element
tmp string
)

if startCommit.ID.String() == endCommitID {
return
}

checkStack := list.New()
checkStack.PushBack(startCommit.ID.String())
listItem = checkStack.Back()

for listItem != nil {
tmp = listItem.Value.(string)
checkStack.Remove(listItem)

if item, ok = commitList[tmp]; !ok {
listItem = checkStack.Back()
continue
}

if item.Commit.ID.String() == endCommitID {
listItem = checkStack.Back()
continue
}

if err = item.Commit.LoadBranchName(); err != nil {
return
}

if item.Commit.Branch == baseBranch {
listItem = checkStack.Back()
continue
}

if item.Checked {
listItem = checkStack.Back()
continue
}

item.Checked = true
commitList[tmp] = item

parentNum := item.Commit.ParentCount()
for i := 0; i < parentNum; i++ {
var parentCommit *git.Commit
parentCommit, err = item.Commit.Parent(i)
if err != nil {
return
}
checkStack.PushBack(parentCommit.ID.String())
}

listItem = checkStack.Back()
}
return nil
}
10 changes: 10 additions & 0 deletions modules/git/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,16 @@ func (c *Commit) GetBranchName() (string, error) {
return strings.SplitN(strings.TrimSpace(data), "~", 2)[0], nil
}

// LoadBranchName load branch name for commit
func (c *Commit) LoadBranchName() (err error) {
if c.Branch != "" {
a1012112796 marked this conversation as resolved.
Show resolved Hide resolved
return
}

c.Branch, err = c.GetBranchName()
return
}

// CommitFileStatus represents status of files in a commit.
type CommitFileStatus struct {
Added []string
Expand Down