@@ -40,103 +40,105 @@ static KeyValuePair<string, BranchConfig>[] LookupBranchConfiguration(Config con
40
40
41
41
static KeyValuePair < string , BranchConfig > InheritBranchConfiguration ( bool onlyEvaluateTrackedBranches , IRepository repository , Commit currentCommit , Branch currentBranch , KeyValuePair < string , BranchConfig > keyValuePair , BranchConfig branchConfiguration , Config config , IList < Branch > excludedInheritBranches )
42
42
{
43
- Logger . WriteInfo ( "Attempting to inherit branch configuration from parent branch" ) ;
44
- var excludedBranches = new [ ] { currentBranch } ;
45
- // Check if we are a merge commit. If so likely we are a pull request
46
- var parentCount = currentCommit . Parents . Count ( ) ;
47
- if ( parentCount == 2 )
43
+ using ( Logger . IndentLog ( "Attempting to inherit branch configuration from parent branch" ) )
48
44
{
49
- var parents = currentCommit . Parents . ToArray ( ) ;
50
- var branch = repository . Branches . SingleOrDefault ( b => ! b . IsRemote && b . Tip == parents [ 1 ] ) ;
51
- if ( branch != null )
45
+ var excludedBranches = new [ ] { currentBranch } ;
46
+ // Check if we are a merge commit. If so likely we are a pull request
47
+ var parentCount = currentCommit . Parents . Count ( ) ;
48
+ if ( parentCount == 2 )
52
49
{
53
- excludedBranches = new [ ]
50
+ var parents = currentCommit . Parents . ToArray ( ) ;
51
+ var branch = repository . Branches . SingleOrDefault ( b => ! b . IsRemote && b . Tip == parents [ 1 ] ) ;
52
+ if ( branch != null )
54
53
{
54
+ excludedBranches = new [ ]
55
+ {
55
56
currentBranch ,
56
57
branch
57
58
} ;
58
- currentBranch = branch ;
59
+ currentBranch = branch ;
60
+ }
61
+ else
62
+ {
63
+ var possibleTargetBranches = repository . Branches . Where ( b => ! b . IsRemote && b . Tip == parents [ 0 ] ) . ToList ( ) ;
64
+ if ( possibleTargetBranches . Count ( ) > 1 )
65
+ {
66
+ currentBranch = possibleTargetBranches . FirstOrDefault ( b => b . Name == "master" ) ?? possibleTargetBranches . First ( ) ;
67
+ }
68
+ else
69
+ {
70
+ currentBranch = possibleTargetBranches . FirstOrDefault ( ) ?? currentBranch ;
71
+ }
72
+ }
73
+
74
+ Logger . WriteInfo ( "HEAD is merge commit, this is likely a pull request using " + currentBranch . Name + " as base" ) ;
75
+ }
76
+ if ( excludedInheritBranches == null )
77
+ {
78
+ excludedInheritBranches = repository . Branches . Where ( b =>
79
+ {
80
+ var branchConfig = LookupBranchConfiguration ( config , b ) ;
81
+ return branchConfig . Length == 1 && branchConfig [ 0 ] . Value . Increment == IncrementStrategy . Inherit ;
82
+ } ) . ToList ( ) ;
83
+ }
84
+ excludedBranches . ToList ( ) . ForEach ( excludedInheritBranches . Add ) ;
85
+
86
+ var branchPoint = currentBranch . FindCommitBranchWasBranchedFrom ( repository , excludedInheritBranches . ToArray ( ) ) ;
87
+
88
+ List < Branch > possibleParents ;
89
+ if ( branchPoint == null )
90
+ {
91
+ possibleParents = currentCommit . GetBranchesContainingCommit ( repository , true ) . Except ( excludedInheritBranches ) . ToList ( ) ;
59
92
}
60
93
else
61
94
{
62
- var possibleTargetBranches = repository . Branches . Where ( b => ! b . IsRemote && b . Tip == parents [ 0 ] ) . ToList ( ) ;
63
- if ( possibleTargetBranches . Count ( ) > 1 )
95
+ var branches = branchPoint . GetBranchesContainingCommit ( repository , true ) . Except ( excludedInheritBranches ) . ToList ( ) ;
96
+ if ( branches . Count > 1 )
64
97
{
65
- currentBranch = possibleTargetBranches . FirstOrDefault ( b => b . Name == "master" ) ?? possibleTargetBranches . First ( ) ;
98
+ var currentTipBranches = currentCommit . GetBranchesContainingCommit ( repository , true ) . Except ( excludedInheritBranches ) . ToList ( ) ;
99
+ possibleParents = branches . Except ( currentTipBranches ) . ToList ( ) ;
66
100
}
67
101
else
68
102
{
69
- currentBranch = possibleTargetBranches . FirstOrDefault ( ) ?? currentBranch ;
103
+ possibleParents = branches ;
70
104
}
71
105
}
72
106
73
- Logger . WriteInfo ( "HEAD is merge commit, this is likely a pull request using " + currentBranch . Name + " as base" ) ;
74
- }
75
- if ( excludedInheritBranches == null )
76
- {
77
- excludedInheritBranches = repository . Branches . Where ( b =>
78
- {
79
- var branchConfig = LookupBranchConfiguration ( config , b ) ;
80
- return branchConfig . Length == 1 && branchConfig [ 0 ] . Value . Increment == IncrementStrategy . Inherit ;
81
- } ) . ToList ( ) ;
82
- }
83
- excludedBranches . ToList ( ) . ForEach ( excludedInheritBranches . Add ) ;
84
-
85
- var branchPoint = currentBranch . FindCommitBranchWasBranchedFrom ( repository , excludedInheritBranches . ToArray ( ) ) ;
107
+ Logger . WriteInfo ( "Found possible parent branches: " + string . Join ( ", " , possibleParents . Select ( p => p . Name ) ) ) ;
86
108
87
- List < Branch > possibleParents ;
88
- if ( branchPoint == null )
89
- {
90
- possibleParents = currentCommit . GetBranchesContainingCommit ( repository , true ) . Except ( excludedInheritBranches ) . ToList ( ) ;
91
- }
92
- else
93
- {
94
- var branches = branchPoint . GetBranchesContainingCommit ( repository , true ) . Except ( excludedInheritBranches ) . ToList ( ) ;
95
- if ( branches . Count > 1 )
109
+ if ( possibleParents . Count == 1 )
96
110
{
97
- var currentTipBranches = currentCommit . GetBranchesContainingCommit ( repository , true ) . Except ( excludedInheritBranches ) . ToList ( ) ;
98
- possibleParents = branches . Except ( currentTipBranches ) . ToList ( ) ;
111
+ var branchConfig = GetBranchConfiguration ( currentCommit , repository , onlyEvaluateTrackedBranches , config , possibleParents [ 0 ] , excludedInheritBranches ) . Value ;
112
+ return new KeyValuePair < string , BranchConfig > (
113
+ keyValuePair . Key ,
114
+ new BranchConfig ( branchConfiguration )
115
+ {
116
+ Increment = branchConfig . Increment ,
117
+ PreventIncrementOfMergedBranchVersion = branchConfig . PreventIncrementOfMergedBranchVersion
118
+ } ) ;
99
119
}
120
+
121
+ // If we fail to inherit it is probably because the branch has been merged and we can't do much. So we will fall back to develop's config
122
+ // if develop exists and master if not
123
+ string errorMessage ;
124
+ if ( possibleParents . Count == 0 )
125
+ errorMessage = "Failed to inherit Increment branch configuration, no branches found." ;
100
126
else
101
- {
102
- possibleParents = branches ;
103
- }
104
- }
127
+ errorMessage = "Failed to inherit Increment branch configuration, ended up with: " + string . Join ( ", " , possibleParents . Select ( p => p . Name ) ) ;
105
128
106
- Logger . WriteInfo ( "Found possible parent branches: " + string . Join ( ", " , possibleParents . Select ( p => p . Name ) ) ) ;
129
+ var developBranch = repository . Branches . FirstOrDefault ( b => Regex . IsMatch ( b . Name , "^develop" , RegexOptions . IgnoreCase ) ) ;
130
+ var branchName = developBranch != null ? developBranch . Name : "master" ;
107
131
108
- if ( possibleParents . Count == 1 )
109
- {
110
- var branchConfig = GetBranchConfiguration ( currentCommit , repository , onlyEvaluateTrackedBranches , config , possibleParents [ 0 ] , excludedInheritBranches ) . Value ;
132
+ Logger . WriteWarning ( errorMessage + Environment . NewLine + Environment . NewLine + "Falling back to " + branchName + " branch config" ) ;
133
+ var value = GetBranchConfiguration ( currentCommit , repository , onlyEvaluateTrackedBranches , config , repository . Branches [ branchName ] ) . Value ;
111
134
return new KeyValuePair < string , BranchConfig > (
112
135
keyValuePair . Key ,
113
136
new BranchConfig ( branchConfiguration )
114
137
{
115
- Increment = branchConfig . Increment ,
116
- PreventIncrementOfMergedBranchVersion = branchConfig . PreventIncrementOfMergedBranchVersion
138
+ Increment = value . Increment ,
139
+ PreventIncrementOfMergedBranchVersion = value . PreventIncrementOfMergedBranchVersion
117
140
} ) ;
118
141
}
119
-
120
- // If we fail to inherit it is probably because the branch has been merged and we can't do much. So we will fall back to develop's config
121
- // if develop exists and master if not
122
- string errorMessage ;
123
- if ( possibleParents . Count == 0 )
124
- errorMessage = "Failed to inherit Increment branch configuration, no branches found." ;
125
- else
126
- errorMessage = "Failed to inherit Increment branch configuration, ended up with: " + string . Join ( ", " , possibleParents . Select ( p => p . Name ) ) ;
127
-
128
- var developBranch = repository . Branches . FirstOrDefault ( b => Regex . IsMatch ( b . Name , "^develop" , RegexOptions . IgnoreCase ) ) ;
129
- var branchName = developBranch != null ? developBranch . Name : "master" ;
130
-
131
- Logger . WriteWarning ( errorMessage + Environment . NewLine + Environment . NewLine + "Falling back to " + branchName + " branch config" ) ;
132
- var value = GetBranchConfiguration ( currentCommit , repository , onlyEvaluateTrackedBranches , config , repository . Branches [ branchName ] ) . Value ;
133
- return new KeyValuePair < string , BranchConfig > (
134
- keyValuePair . Key ,
135
- new BranchConfig ( branchConfiguration )
136
- {
137
- Increment = value . Increment ,
138
- PreventIncrementOfMergedBranchVersion = value . PreventIncrementOfMergedBranchVersion
139
- } ) ;
140
142
}
141
143
}
142
144
}
0 commit comments