Skip to content

Commit c0e139a

Browse files
committed
Add null checks to BranchCollection
- Add various null checks to `BranchCollection`. - Also refactor the code a bit for readability and improved stack traces.
1 parent 7a69997 commit c0e139a

File tree

1 file changed

+30
-6
lines changed

1 file changed

+30
-6
lines changed
Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,47 @@
1+
using GitVersion.Extensions;
2+
using LibGit2Sharp;
3+
14
namespace GitVersion;
25

36
internal sealed class BranchCollection : IBranchCollection
47
{
58
private readonly LibGit2Sharp.BranchCollection innerCollection;
6-
internal BranchCollection(LibGit2Sharp.BranchCollection collection) => this.innerCollection = collection;
79

8-
public IEnumerator<IBranch> GetEnumerator() => this.innerCollection.Select(branch => new Branch(branch)).GetEnumerator();
10+
internal BranchCollection(LibGit2Sharp.BranchCollection collection)
11+
=> this.innerCollection = collection.NotNull();
12+
13+
public IEnumerator<IBranch> GetEnumerator()
14+
=> this.innerCollection.Select(branch => new Branch(branch)).GetEnumerator();
15+
916
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
17+
1018
public IBranch? this[string name]
1119
{
1220
get
1321
{
22+
name = name.NotNull();
1423
var branch = this.innerCollection[name];
1524
return branch is null ? null : new Branch(branch);
1625
}
1726
}
1827

19-
public IEnumerable<IBranch> ExcludeBranches(IEnumerable<IBranch> branchesToExclude) =>
20-
this.Where(b => branchesToExclude.All(bte => !b.Equals(bte)));
21-
public void UpdateTrackedBranch(IBranch branch, string remoteTrackingReferenceName) =>
22-
this.innerCollection.Update((Branch)branch, b => b.TrackedBranch = remoteTrackingReferenceName);
28+
public IEnumerable<IBranch> ExcludeBranches(IEnumerable<IBranch> branchesToExclude)
29+
{
30+
branchesToExclude = branchesToExclude.NotNull();
31+
32+
bool BranchIsNotExcluded(IBranch branch)
33+
=> branchesToExclude.All(branchToExclude => !branch.Equals(branchToExclude));
34+
35+
return this.Where(BranchIsNotExcluded);
36+
}
37+
38+
public void UpdateTrackedBranch(IBranch branch, string remoteTrackingReferenceName)
39+
{
40+
var branchToUpdate = (Branch)branch.NotNull();
41+
42+
void Updater(BranchUpdater branchUpdater) =>
43+
branchUpdater.TrackedBranch = remoteTrackingReferenceName;
44+
45+
this.innerCollection.Update(branchToUpdate, Updater);
46+
}
2347
}

0 commit comments

Comments
 (0)