@@ -36,7 +36,7 @@ public static IEnumerable<SemanticVersion> GetVersionTagsOnBranch(this Branch br
36
36
/// Find the commit where the given branch was branched from another branch.
37
37
/// If there are multiple such commits and branches, returns the newest commit.
38
38
/// </summary>
39
- public static Commit FindCommitBranchWasBranchedFrom ( [ NotNull ] this Branch branch , IRepository repository , params Branch [ ] excludedBranches )
39
+ public static BranchCommit FindCommitBranchWasBranchedFrom ( [ NotNull ] this Branch branch , IRepository repository , params Branch [ ] excludedBranches )
40
40
{
41
41
const string missingTipFormat = "{0} has no tip. Please see http://example.com/docs for information on how to fix this." ;
42
42
@@ -50,35 +50,25 @@ public static Commit FindCommitBranchWasBranchedFrom([NotNull] this Branch branc
50
50
if ( branch . Tip == null )
51
51
{
52
52
Logger . WriteWarning ( string . Format ( missingTipFormat , branch . FriendlyName ) ) ;
53
- return null ;
53
+ return BranchCommit . Empty ;
54
54
}
55
55
56
56
var otherBranches = repository . Branches
57
- . Except ( excludedBranches )
58
- . Where ( b => IsSameBranch ( branch , b ) )
59
- . ToList ( ) ;
57
+ . ExcludingBranches ( excludedBranches )
58
+ . Where ( b => ! IsSameBranch ( branch , b ) ) ;
60
59
var mergeBases = otherBranches . Select ( otherBranch =>
61
60
{
62
61
if ( otherBranch . Tip == null )
63
62
{
64
63
Logger . WriteWarning ( string . Format ( missingTipFormat , otherBranch . FriendlyName ) ) ;
65
- return null ;
64
+ return BranchCommit . Empty ;
66
65
}
67
66
68
67
var findMergeBase = FindMergeBase ( branch , otherBranch , repository ) ;
69
- return new
70
- {
71
- mergeBaseCommit = findMergeBase ,
72
- branch = otherBranch
73
- } ;
74
- } ) . Where ( b => b . mergeBaseCommit != null ) . OrderByDescending ( b => b . mergeBaseCommit . Committer . When ) . ToList ( ) ;
68
+ return new BranchCommit ( findMergeBase , otherBranch ) ;
69
+ } ) . Where ( b => b . Commit != null ) . OrderByDescending ( b => b . Commit . Committer . When ) ;
75
70
76
- var firstOrDefault = mergeBases . FirstOrDefault ( ) ;
77
- if ( firstOrDefault != null )
78
- {
79
- return firstOrDefault . mergeBaseCommit ;
80
- }
81
- return null ;
71
+ return mergeBases . FirstOrDefault ( ) ;
82
72
}
83
73
}
84
74
@@ -127,11 +117,28 @@ public static Commit FindMergeBase(this Branch branch, Branch otherBranch, IRepo
127
117
}
128
118
}
129
119
130
- static bool IsSameBranch ( Branch branch , Branch b )
120
+ /// <summary>
121
+ /// Checks if the two branch objects refer to the same branch (have the same friendly name).
122
+ /// </summary>
123
+ public static bool IsSameBranch ( Branch branch , Branch otherBranch )
124
+ {
125
+ // For each branch, fixup the friendly name if the branch is remote.
126
+ var otherBranchFriendlyName = otherBranch . IsRemote ?
127
+ otherBranch . FriendlyName . Substring ( otherBranch . FriendlyName . IndexOf ( "/" , StringComparison . Ordinal ) + 1 ) :
128
+ otherBranch . FriendlyName ;
129
+ var branchFriendlyName = branch . IsRemote ?
130
+ branch . FriendlyName . Substring ( branch . FriendlyName . IndexOf ( "/" , StringComparison . Ordinal ) + 1 ) :
131
+ branch . FriendlyName ;
132
+
133
+ return otherBranchFriendlyName == branchFriendlyName ;
134
+ }
135
+
136
+ /// <summary>
137
+ /// Exclude the given branches (by value equality according to friendly name).
138
+ /// </summary>
139
+ public static IEnumerable < Branch > ExcludingBranches ( [ NotNull ] this IEnumerable < Branch > branches , [ NotNull ] IEnumerable < Branch > branchesToExclude )
131
140
{
132
- return ( b . IsRemote ?
133
- b . FriendlyName . Substring ( b . FriendlyName . IndexOf ( "/" , StringComparison . Ordinal ) + 1 ) :
134
- b . FriendlyName ) != branch . FriendlyName ;
141
+ return branches . Where ( b => branchesToExclude . All ( bte => ! IsSameBranch ( b , bte ) ) ) ;
135
142
}
136
143
137
144
public static IEnumerable < Branch > GetBranchesContainingCommit ( [ NotNull ] this Commit commit , IRepository repository , IList < Branch > branches , bool onlyTrackedBranches )
0 commit comments