@@ -80,7 +80,7 @@ public virtual bool IsRemote
80
80
}
81
81
82
82
/// <summary>
83
- /// Gets the remote branch which is connected to this local one.
83
+ /// Gets the remote branch which is connected to this local one, or null if there is none .
84
84
/// </summary>
85
85
public virtual Branch TrackedBranch
86
86
{
@@ -95,20 +95,43 @@ public virtual bool IsTracking
95
95
get { return TrackedBranch != null ; }
96
96
}
97
97
98
+ private bool ExistsPathToTrackedBranch ( )
99
+ {
100
+ if ( ! IsTracking )
101
+ {
102
+ return false ;
103
+ }
104
+
105
+ if ( repo . Commits . FindCommonAncestor ( Tip , TrackedBranch . Tip ) == null )
106
+ {
107
+ return false ;
108
+ }
109
+
110
+ return true ;
111
+ }
112
+
98
113
/// <summary>
99
114
/// Gets the number of commits, starting from the <see cref="Tip"/>, that have been performed on this local branch and aren't known from the remote one.
115
+ /// <para>
116
+ /// This property will return null if there is no remote branch linked to this local branch, or if the remote branch and the local branch do
117
+ /// not share a common ancestor.
118
+ /// </para>
100
119
/// </summary>
101
- public virtual int AheadBy
120
+ public virtual int ? AheadBy
102
121
{
103
- get { return IsTracking ? repo . Commits . QueryBy ( new Filter { Since = Tip , Until = TrackedBranch } ) . Count ( ) : 0 ; }
122
+ get { return ExistsPathToTrackedBranch ( ) ? repo . Commits . QueryBy ( new Filter { Since = Tip , Until = TrackedBranch } ) . Count ( ) : ( int ? ) null ; }
104
123
}
105
124
106
125
/// <summary>
107
126
/// Gets the number of commits that exist in the remote branch, on top of <see cref="Tip"/>, and aren't known from the local one.
127
+ /// <para>
128
+ /// This property will return null if there is no remote branch linked to this local branch, or if the remote branch and the local branch do
129
+ /// not share a common ancestor.
130
+ /// </para>
108
131
/// </summary>
109
- public virtual int BehindBy
132
+ public virtual int ? BehindBy
110
133
{
111
- get { return IsTracking ? repo . Commits . QueryBy ( new Filter { Since = TrackedBranch , Until = Tip } ) . Count ( ) : 0 ; }
134
+ get { return ExistsPathToTrackedBranch ( ) ? repo . Commits . QueryBy ( new Filter { Since = TrackedBranch , Until = Tip } ) . Count ( ) : ( int ? ) null ; }
112
135
}
113
136
114
137
/// <summary>
@@ -140,16 +163,23 @@ public virtual ICommitLog Commits
140
163
141
164
private Branch ResolveTrackedBranch ( )
142
165
{
143
- using ( ReferenceSafeHandle branchPtr = repo . Refs . RetrieveReferencePtr ( CanonicalName ) )
144
- using ( ReferenceSafeHandle referencePtr = Proxy . git_branch_tracking ( branchPtr ) )
166
+ using ( ReferenceSafeHandle branchPtr = repo . Refs . RetrieveReferencePtr ( CanonicalName , false ) )
145
167
{
146
- if ( referencePtr == null )
168
+ if ( branchPtr == null )
147
169
{
148
170
return null ;
149
171
}
150
172
151
- var reference = Reference . BuildFromPtr < Reference > ( referencePtr , repo ) ;
152
- return repo . Branches [ reference . CanonicalName ] ;
173
+ using ( ReferenceSafeHandle referencePtr = Proxy . git_branch_tracking ( branchPtr ) )
174
+ {
175
+ if ( referencePtr == null )
176
+ {
177
+ return null ;
178
+ }
179
+
180
+ var reference = Reference . BuildFromPtr < Reference > ( referencePtr , repo ) ;
181
+ return repo . Branches [ reference . CanonicalName ] ;
182
+ }
153
183
}
154
184
}
155
185
0 commit comments