@@ -1157,12 +1157,11 @@ func getCommitIDsFromRepo(repo *Repository, oldCommitID, newCommitID, baseBranch
1157
1157
return nil , false , err
1158
1158
}
1159
1159
1160
- oldCommitBranch , err := oldCommit .GetBranchName ()
1161
- if err != nil {
1160
+ if err = oldCommit .LoadBranchName (); err != nil {
1162
1161
return nil , false , err
1163
1162
}
1164
1163
1165
- if oldCommitBranch == "" {
1164
+ if len ( oldCommit . Branch ) == 0 {
1166
1165
commitIDs = make ([]string , 2 )
1167
1166
commitIDs [0 ] = oldCommitID
1168
1167
commitIDs [1 ] = newCommitID
@@ -1175,25 +1174,102 @@ func getCommitIDsFromRepo(repo *Repository, oldCommitID, newCommitID, baseBranch
1175
1174
return nil , false , err
1176
1175
}
1177
1176
1178
- var commits * list.List
1177
+ var (
1178
+ commits * list.List
1179
+ commitChecks map [string ]commitBranchCheckItem
1180
+ )
1179
1181
commits , err = newCommit .CommitsBeforeUntil (oldCommitID )
1180
1182
if err != nil {
1181
1183
return nil , false , err
1182
1184
}
1183
1185
1184
1186
commitIDs = make ([]string , 0 , commits .Len ())
1187
+ commitChecks = make (map [string ]commitBranchCheckItem )
1185
1188
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 ,
1191
1193
}
1194
+ }
1192
1195
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 )
1195
1204
}
1196
1205
}
1197
1206
1198
1207
return
1199
1208
}
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
+ }
0 commit comments