Skip to content

Commit b601568

Browse files
authored
Merge pull request #3064 from asbjornu/feature/gh-2825
Add `null` protection
2 parents 6706400 + c6fefe1 commit b601568

17 files changed

+398
-374
lines changed

src/GitVersion.Core/Core/RepositoryStore.cs

Lines changed: 166 additions & 194 deletions
Large diffs are not rendered by default.
Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using GitVersion.Extensions;
12
using GitVersion.Helpers;
23

34
namespace GitVersion;
@@ -11,7 +12,7 @@ internal sealed class Branch : IBranch
1112

1213
internal Branch(LibGit2Sharp.Branch branch)
1314
{
14-
this.innerBranch = branch;
15+
this.innerBranch = branch.NotNull();
1516
Name = new ReferenceName(branch.CanonicalName);
1617

1718
var commit = this.innerBranch.Tip;
@@ -20,19 +21,17 @@ internal Branch(LibGit2Sharp.Branch branch)
2021
var commits = this.innerBranch.Commits;
2122
Commits = commits is null ? null : new CommitCollection(commits);
2223
}
24+
2325
public ReferenceName Name { get; }
2426
public ICommit? Tip { get; }
2527
public ICommitCollection? Commits { get; }
26-
2728
public int CompareTo(IBranch other) => comparerHelper.Compare(this, other);
2829
public bool Equals(IBranch? other) => equalityHelper.Equals(this, other);
30+
public bool IsDetachedHead => Name.Canonical.Equals("(no branch)", StringComparison.OrdinalIgnoreCase);
31+
public bool IsRemote => this.innerBranch.IsRemote;
32+
public bool IsTracking => this.innerBranch.IsTracking;
2933
public override bool Equals(object obj) => Equals((obj as IBranch));
3034
public override int GetHashCode() => equalityHelper.GetHashCode(this);
3135
public override string ToString() => Name.ToString();
3236
public static implicit operator LibGit2Sharp.Branch(Branch d) => d.innerBranch;
33-
34-
public bool IsDetachedHead => Name.Canonical.Equals("(no branch)", StringComparison.OrdinalIgnoreCase);
35-
36-
public bool IsRemote => this.innerBranch.IsRemote;
37-
public bool IsTracking => this.innerBranch.IsTracking;
3837
}
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
}
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using GitVersion.Extensions;
12
using GitVersion.Helpers;
23

34
namespace GitVersion;
@@ -11,20 +12,18 @@ internal sealed class Commit : GitObject, ICommit
1112

1213
internal Commit(LibGit2Sharp.Commit innerCommit) : base(innerCommit)
1314
{
14-
this.innerCommit = innerCommit;
15+
this.innerCommit = innerCommit.NotNull();
1516
Parents = innerCommit.Parents.Select(parent => new Commit(parent));
1617
When = innerCommit.Committer.When;
1718
}
1819

1920
public int CompareTo(ICommit other) => comparerHelper.Compare(this, other);
2021
public bool Equals(ICommit? other) => equalityHelper.Equals(this, other);
22+
public IEnumerable<ICommit> Parents { get; }
23+
public DateTimeOffset When { get; }
24+
public string Message => this.innerCommit.Message;
2125
public override bool Equals(object obj) => Equals((obj as ICommit));
2226
public override int GetHashCode() => equalityHelper.GetHashCode(this);
2327
public override string ToString() => $"{Id.ToString(7)} {this.innerCommit.MessageShort}";
2428
public static implicit operator LibGit2Sharp.Commit(Commit d) => d.innerCommit;
25-
26-
public IEnumerable<ICommit> Parents { get; }
27-
public DateTimeOffset When { get; }
28-
29-
public string Message => this.innerCommit.Message;
3029
}

src/GitVersion.LibGit2Sharp/Git/CommitCollection.cs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
1+
using GitVersion.Extensions;
12
using LibGit2Sharp;
23

34
namespace GitVersion;
45

56
internal sealed class CommitCollection : ICommitCollection
67
{
78
private readonly ICommitLog innerCollection;
8-
internal CommitCollection(ICommitLog collection) => this.innerCollection = collection;
99

10-
public IEnumerator<ICommit> GetEnumerator() => this.innerCollection.Select(commit => new Commit(commit)).GetEnumerator();
10+
internal CommitCollection(ICommitLog collection) => this.innerCollection = collection.NotNull();
11+
12+
public IEnumerator<ICommit> GetEnumerator()
13+
=> this.innerCollection.Select(commit => new Commit(commit)).GetEnumerator();
1114

1215
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
1316

14-
public IEnumerable<ICommit> GetCommitsPriorTo(DateTimeOffset olderThan) => this.SkipWhile(c => c.When > olderThan);
17+
public IEnumerable<ICommit> GetCommitsPriorTo(DateTimeOffset olderThan)
18+
=> this.SkipWhile(c => c.When > olderThan);
19+
1520
public IEnumerable<ICommit> QueryBy(CommitFilter commitFilter)
1621
{
1722
static object? GetReacheableFrom(object? item) =>
@@ -24,13 +29,7 @@ public IEnumerable<ICommit> QueryBy(CommitFilter commitFilter)
2429

2530
var includeReachableFrom = GetReacheableFrom(commitFilter.IncludeReachableFrom);
2631
var excludeReachableFrom = GetReacheableFrom(commitFilter.ExcludeReachableFrom);
27-
var filter = new LibGit2Sharp.CommitFilter
28-
{
29-
IncludeReachableFrom = includeReachableFrom,
30-
ExcludeReachableFrom = excludeReachableFrom,
31-
FirstParentOnly = commitFilter.FirstParentOnly,
32-
SortBy = (LibGit2Sharp.CommitSortStrategies)commitFilter.SortBy
33-
};
32+
var filter = new LibGit2Sharp.CommitFilter { IncludeReachableFrom = includeReachableFrom, ExcludeReachableFrom = excludeReachableFrom, FirstParentOnly = commitFilter.FirstParentOnly, SortBy = (LibGit2Sharp.CommitSortStrategies)commitFilter.SortBy };
3433
var commitLog = ((IQueryableCommitLog)this.innerCollection).QueryBy(filter);
3534
return new CommitCollection(commitLog);
3635
}

src/GitVersion.LibGit2Sharp/Git/GitObject.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using GitVersion.Extensions;
12
using GitVersion.Helpers;
23

34
namespace GitVersion;
@@ -9,6 +10,7 @@ internal class GitObject : IGitObject
910

1011
internal GitObject(LibGit2Sharp.GitObject innerGitObject)
1112
{
13+
innerGitObject = innerGitObject.NotNull();
1214
Id = new ObjectId(innerGitObject.Id);
1315
Sha = innerGitObject.Sha;
1416
}

0 commit comments

Comments
 (0)