Skip to content

Commit ae20de7

Browse files
a1012112796lafriks
andauthored
fix bug about can't skip commits base on base branch (#11555)
* fix bug about can't skip commits base on base branch Signed-off-by: a1012112796 <[email protected]> * Update modules/git/commit.go Co-authored-by: Lauris BH <[email protected]> * Update models/issue_comment.go Co-authored-by: Lauris BH <[email protected]> * fix lint Co-authored-by: Lauris BH <[email protected]>
1 parent 8aef7ae commit ae20de7

File tree

2 files changed

+97
-11
lines changed

2 files changed

+97
-11
lines changed

models/issue_comment.go

Lines changed: 87 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1157,12 +1157,11 @@ func getCommitIDsFromRepo(repo *Repository, oldCommitID, newCommitID, baseBranch
11571157
return nil, false, err
11581158
}
11591159

1160-
oldCommitBranch, err := oldCommit.GetBranchName()
1161-
if err != nil {
1160+
if err = oldCommit.LoadBranchName(); err != nil {
11621161
return nil, false, err
11631162
}
11641163

1165-
if oldCommitBranch == "" {
1164+
if len(oldCommit.Branch) == 0 {
11661165
commitIDs = make([]string, 2)
11671166
commitIDs[0] = oldCommitID
11681167
commitIDs[1] = newCommitID
@@ -1175,25 +1174,102 @@ func getCommitIDsFromRepo(repo *Repository, oldCommitID, newCommitID, baseBranch
11751174
return nil, false, err
11761175
}
11771176

1178-
var commits *list.List
1177+
var (
1178+
commits *list.List
1179+
commitChecks map[string]commitBranchCheckItem
1180+
)
11791181
commits, err = newCommit.CommitsBeforeUntil(oldCommitID)
11801182
if err != nil {
11811183
return nil, false, err
11821184
}
11831185

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

1186-
for e := commits.Back(); e != nil; e = e.Prev() {
1187-
commit := e.Value.(*git.Commit)
1188-
commitBranch, err := commit.GetBranchName()
1189-
if err != nil {
1190-
return nil, false, err
1189+
for e := commits.Front(); e != nil; e = e.Next() {
1190+
commitChecks[e.Value.(*git.Commit).ID.String()] = commitBranchCheckItem{
1191+
Commit: e.Value.(*git.Commit),
1192+
Checked: false,
11911193
}
1194+
}
11921195

1193-
if commitBranch != baseBranch {
1194-
commitIDs = append(commitIDs, commit.ID.String())
1196+
if err = commitBranchCheck(gitRepo, newCommit, oldCommitID, baseBranch, commitChecks); err != nil {
1197+
return
1198+
}
1199+
1200+
for e := commits.Back(); e != nil; e = e.Prev() {
1201+
commitID := e.Value.(*git.Commit).ID.String()
1202+
if item, ok := commitChecks[commitID]; ok && item.Checked {
1203+
commitIDs = append(commitIDs, commitID)
11951204
}
11961205
}
11971206

11981207
return
11991208
}
1209+
1210+
type commitBranchCheckItem struct {
1211+
Commit *git.Commit
1212+
Checked bool
1213+
}
1214+
1215+
func commitBranchCheck(gitRepo *git.Repository, startCommit *git.Commit, endCommitID, baseBranch string, commitList map[string]commitBranchCheckItem) (err error) {
1216+
var (
1217+
item commitBranchCheckItem
1218+
ok bool
1219+
listItem *list.Element
1220+
tmp string
1221+
)
1222+
1223+
if startCommit.ID.String() == endCommitID {
1224+
return
1225+
}
1226+
1227+
checkStack := list.New()
1228+
checkStack.PushBack(startCommit.ID.String())
1229+
listItem = checkStack.Back()
1230+
1231+
for listItem != nil {
1232+
tmp = listItem.Value.(string)
1233+
checkStack.Remove(listItem)
1234+
1235+
if item, ok = commitList[tmp]; !ok {
1236+
listItem = checkStack.Back()
1237+
continue
1238+
}
1239+
1240+
if item.Commit.ID.String() == endCommitID {
1241+
listItem = checkStack.Back()
1242+
continue
1243+
}
1244+
1245+
if err = item.Commit.LoadBranchName(); err != nil {
1246+
return
1247+
}
1248+
1249+
if item.Commit.Branch == baseBranch {
1250+
listItem = checkStack.Back()
1251+
continue
1252+
}
1253+
1254+
if item.Checked {
1255+
listItem = checkStack.Back()
1256+
continue
1257+
}
1258+
1259+
item.Checked = true
1260+
commitList[tmp] = item
1261+
1262+
parentNum := item.Commit.ParentCount()
1263+
for i := 0; i < parentNum; i++ {
1264+
var parentCommit *git.Commit
1265+
parentCommit, err = item.Commit.Parent(i)
1266+
if err != nil {
1267+
return
1268+
}
1269+
checkStack.PushBack(parentCommit.ID.String())
1270+
}
1271+
1272+
listItem = checkStack.Back()
1273+
}
1274+
return nil
1275+
}

modules/git/commit.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,16 @@ func (c *Commit) GetBranchName() (string, error) {
482482
return strings.SplitN(strings.TrimSpace(data), "~", 2)[0], nil
483483
}
484484

485+
// LoadBranchName load branch name for commit
486+
func (c *Commit) LoadBranchName() (err error) {
487+
if len(c.Branch) != 0 {
488+
return
489+
}
490+
491+
c.Branch, err = c.GetBranchName()
492+
return
493+
}
494+
485495
// GetTagName gets the current tag name for given commit
486496
func (c *Commit) GetTagName() (string, error) {
487497
data, err := NewCommand("describe", "--exact-match", "--tags", "--always", c.ID.String()).RunInDir(c.repo.Path)

0 commit comments

Comments
 (0)