@@ -65,24 +65,63 @@ public static Commit FindCommitBranchWasBranchedFrom([NotNull] this Branch branc
65
65
return null ;
66
66
}
67
67
68
- var otherBranches = repository . Branches . Except ( excludedBranches ) . Where ( b => IsSameBranch ( branch , b ) ) . ToList ( ) ;
69
- var mergeBases = otherBranches . Select ( b =>
68
+ var otherBranches = repository . Branches
69
+ . Except ( excludedBranches )
70
+ . Where ( b => IsSameBranch ( branch , b ) )
71
+ . ToList ( ) ;
72
+ var mergeBases = otherBranches . Select ( otherBranch =>
70
73
{
71
- if ( b . Tip == null )
74
+ if ( otherBranch . Tip == null )
72
75
{
73
- Logger . WriteWarning ( string . Format ( missingTipFormat , b . Name ) ) ;
76
+ Logger . WriteWarning ( string . Format ( missingTipFormat , otherBranch . Name ) ) ;
74
77
return null ;
75
78
}
76
79
77
- var otherCommit = b . Tip ;
78
- if ( b . Tip . Parents . Contains ( branch . Tip ) )
80
+ // Otherbranch tip is a forward merge
81
+ var commitToFindCommonBase = otherBranch . Tip ;
82
+ if ( otherBranch . Tip . Parents . Contains ( branch . Tip ) )
79
83
{
80
- otherCommit = b . Tip . Parents . First ( ) ;
84
+ commitToFindCommonBase = otherBranch . Tip . Parents . First ( ) ;
81
85
}
82
- var mergeBase = repository . Commits . FindMergeBase ( otherCommit , branch . Tip ) ;
83
- return mergeBase ;
84
- } ) . Where ( b => b != null ) . ToList ( ) ;
85
- return mergeBases . OrderByDescending ( b => b . Committer . When ) . FirstOrDefault ( ) ;
86
+
87
+ var findMergeBase = repository . Commits . FindMergeBase ( branch . Tip , commitToFindCommonBase ) ;
88
+ if ( findMergeBase != null )
89
+ {
90
+ using ( Logger . IndentLog ( string . Format ( "Found merge base of {0} against {1}" , findMergeBase . Sha , otherBranch . Name ) ) )
91
+ {
92
+ // We do not want to include merge base commits which got forward merged into the other branch
93
+ bool mergeBaseWasFowardMerge ;
94
+ do
95
+ {
96
+ // Now make sure that the merge base is not a forward merge
97
+ mergeBaseWasFowardMerge = otherBranch . Commits
98
+ . SkipWhile ( c => c != commitToFindCommonBase )
99
+ . TakeWhile ( c => c != findMergeBase )
100
+ . Any ( c => c . Parents . Contains ( findMergeBase ) ) ;
101
+ if ( mergeBaseWasFowardMerge )
102
+ {
103
+ Logger . WriteInfo ( "Merge base was due to a forward merge, moving to next merge base" ) ;
104
+ var second = commitToFindCommonBase . Parents . First ( ) ;
105
+ var mergeBase = repository . Commits . FindMergeBase ( branch . Tip , second ) ;
106
+ if ( mergeBase == findMergeBase ) break ;
107
+ findMergeBase = mergeBase ;
108
+ }
109
+ } while ( mergeBaseWasFowardMerge ) ;
110
+ }
111
+ }
112
+ return new
113
+ {
114
+ mergeBaseCommit = findMergeBase ,
115
+ branch = otherBranch
116
+ } ;
117
+ } ) . Where ( b => b != null ) . OrderByDescending ( b => b . mergeBaseCommit . Committer . When ) . ToList ( ) ;
118
+
119
+ var firstOrDefault = mergeBases . FirstOrDefault ( ) ;
120
+ if ( firstOrDefault != null )
121
+ {
122
+ return firstOrDefault . mergeBaseCommit ;
123
+ }
124
+ return null ;
86
125
}
87
126
}
88
127
0 commit comments