Skip to content

Commit 01c8940

Browse files
committed
nullable cleanup for LibGit2Sharp implementation
1 parent 17d1921 commit 01c8940

File tree

16 files changed

+48
-64
lines changed

16 files changed

+48
-64
lines changed

src/GitTools.Testing/GitTestExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public static void MergeNoFF(this IRepository repository, string branch, Signatu
3030
public static Commit[] MakeCommits(this IRepository repository, int numCommitsToMake)
3131
{
3232
return Enumerable.Range(1, numCommitsToMake)
33-
.Select(x => repository.MakeACommit())
33+
.Select(_ => repository.MakeACommit())
3434
.ToArray();
3535
}
3636

src/GitVersion.LibGit2Sharp/Git/Branch.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,10 @@ internal class Branch : IBranch
1111

1212
private readonly LibGit2Sharp.Branch innerBranch;
1313

14-
internal Branch(LibGit2Sharp.Branch branch)
15-
{
16-
innerBranch = branch;
17-
}
14+
internal Branch(LibGit2Sharp.Branch branch) => innerBranch = branch;
1815

1916
public int CompareTo(IBranch other) => comparerHelper.Compare(this, other);
20-
public override bool Equals(object obj) => Equals(obj as IBranch);
17+
public override bool Equals(object obj) => Equals((obj as IBranch)!);
2118
public bool Equals(IBranch other) => equalityHelper.Equals(this, other);
2219
public override int GetHashCode() => equalityHelper.GetHashCode(this);
2320
public static implicit operator LibGit2Sharp.Branch(Branch d) => d.innerBranch;
@@ -35,7 +32,7 @@ internal Branch(LibGit2Sharp.Branch branch)
3532
? FriendlyName.Substring("origin/".Length)
3633
: FriendlyName;
3734

38-
public virtual ICommit Tip
35+
public virtual ICommit? Tip
3936
{
4037
get
4138
{
@@ -44,7 +41,7 @@ public virtual ICommit Tip
4441
}
4542
}
4643

47-
public virtual ICommitCollection Commits
44+
public virtual ICommitCollection? Commits
4845
{
4946
get
5047
{

src/GitVersion.LibGit2Sharp/Git/BranchCollection.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ public virtual IEnumerator<IBranch> GetEnumerator()
1414
return innerCollection.Select(branch => new Branch(branch)).GetEnumerator();
1515
}
1616
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
17-
public virtual IBranch this[string friendlyName]
17+
public virtual IBranch? this[string friendlyName]
1818
{
1919
get
2020
{
21-
var branch = innerCollection?[friendlyName];
21+
var branch = innerCollection[friendlyName];
2222
return branch is null ? null : new Branch(branch);
2323
}
2424
}

src/GitVersion.LibGit2Sharp/Git/Commit.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,16 @@ internal class Commit : ICommit
1111

1212
private readonly LibGit2Sharp.Commit innerObjectId;
1313

14-
internal Commit(LibGit2Sharp.Commit objectId)
15-
{
16-
innerObjectId = objectId;
17-
}
14+
internal Commit(LibGit2Sharp.Commit objectId) => innerObjectId = objectId;
1815

1916
public int CompareTo(ICommit other) => comparerHelper.Compare(this, other);
20-
public override bool Equals(object obj) => Equals(obj as ICommit);
17+
public override bool Equals(object obj) => Equals((obj as ICommit)!);
2118
public bool Equals(ICommit other) => equalityHelper.Equals(this, other);
2219
public override int GetHashCode() => equalityHelper.GetHashCode(this);
2320

2421
public static implicit operator LibGit2Sharp.Commit(Commit d) => d.innerObjectId;
2522

26-
public virtual IEnumerable<ICommit> Parents
23+
public virtual IEnumerable<ICommit?> Parents
2724
{
2825
get
2926
{
@@ -36,7 +33,7 @@ public virtual IEnumerable<ICommit> Parents
3633

3734
public virtual string Sha => innerObjectId.Sha;
3835

39-
public virtual IObjectId Id
36+
public virtual IObjectId? Id
4037
{
4138
get
4239
{

src/GitVersion.LibGit2Sharp/Git/CommitCollection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public virtual IEnumerator<ICommit> GetEnumerator()
2121
public IEnumerable<ICommit> GetCommitsPriorTo(DateTimeOffset olderThan) => this.SkipWhile(c => c.CommitterWhen > olderThan);
2222
public virtual ICommitCollection QueryBy(CommitFilter commitFilter)
2323
{
24-
static object GetReacheableFrom(object item)
24+
static object? GetReacheableFrom(object item)
2525
{
2626
return item switch
2727
{

src/GitVersion.LibGit2Sharp/Git/GitRepository.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ internal GitRepository(ILog log, string gitRootDirectory)
2525

2626
internal GitRepository(IRepository repository)
2727
{
28+
log = new NullLog();
2829
repositoryLazy = new Lazy<IRepository>(() => repository);
2930
}
3031

@@ -133,7 +134,7 @@ public void CreateBranchForPullRequestBranch(AuthenticationInfo auth)
133134

134135
log.Info($"Remote Refs:{System.Environment.NewLine}" + string.Join(System.Environment.NewLine, remoteTips.Select(r => r.CanonicalName)));
135136

136-
var headTipSha = Head.Tip.Sha;
137+
var headTipSha = Head.Tip?.Sha;
137138

138139
var refs = remoteTips.Where(r => r.TargetIdentifier == headTipSha).ToList();
139140

@@ -242,11 +243,11 @@ private static CloneOptions GetCloneOptions(AuthenticationInfo auth)
242243
CredentialsProvider = GetCredentialsProvider(auth)
243244
};
244245
}
245-
private static CredentialsHandler GetCredentialsProvider(AuthenticationInfo auth)
246+
private static CredentialsHandler? GetCredentialsProvider(AuthenticationInfo auth)
246247
{
247248
if (!string.IsNullOrWhiteSpace(auth.Username))
248249
{
249-
return (url, user, types) => new UsernamePasswordCredentials
250+
return (_, _, _) => new UsernamePasswordCredentials
250251
{
251252
Username = auth.Username,
252253
Password = auth.Password ?? string.Empty
@@ -289,7 +290,7 @@ public IEnumerable<ICommit> GetCommitsReacheableFromHead(ICommit headCommit)
289290

290291
return commitCollection.ToList();
291292
}
292-
public ICommit GetForwardMerge(ICommit commitToFindCommonBase, ICommit findMergeBase)
293+
public ICommit? GetForwardMerge(ICommit commitToFindCommonBase, ICommit findMergeBase)
293294
{
294295
var filter = new CommitFilter
295296
{
@@ -302,7 +303,7 @@ public ICommit GetForwardMerge(ICommit commitToFindCommonBase, ICommit findMerge
302303
.FirstOrDefault(c => c.Parents.Contains(findMergeBase));
303304
return forwardMerge;
304305
}
305-
public IEnumerable<ICommit> GetMergeBaseCommits(ICommit mergeCommit, ICommit mergedHead, ICommit findMergeBase)
306+
public IEnumerable<ICommit> GetMergeBaseCommits(ICommit? mergeCommit, ICommit mergedHead, ICommit findMergeBase)
306307
{
307308
var filter = new CommitFilter
308309
{

src/GitVersion.LibGit2Sharp/Git/GitRepositoryInfo.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,27 @@ internal class GitRepositoryInfo : IGitRepositoryInfo
1111
private readonly IOptions<GitVersionOptions> options;
1212
private GitVersionOptions gitVersionOptions => options.Value;
1313

14-
private Lazy<string> dotGitDirectory;
14+
private Lazy<string?> dynamicGitRepositoryPath;
15+
private Lazy<string?> dotGitDirectory;
16+
private Lazy<string?> gitRootPath;
1517
private Lazy<string> projectRootDirectory;
16-
private Lazy<string> dynamicGitRepositoryPath;
17-
private Lazy<string> gitRootPath;
1818

1919
public GitRepositoryInfo(IOptions<GitVersionOptions> options)
2020
{
2121
this.options = options ?? throw new ArgumentNullException(nameof(options));
2222

23-
dynamicGitRepositoryPath = new Lazy<string>(GetDynamicGitRepositoryPath);
24-
dotGitDirectory = new Lazy<string>(GetDotGitDirectory);
23+
dynamicGitRepositoryPath = new Lazy<string?>(GetDynamicGitRepositoryPath);
24+
dotGitDirectory = new Lazy<string?>(GetDotGitDirectory);
25+
gitRootPath = new Lazy<string?>(GetGitRootPath);
2526
projectRootDirectory = new Lazy<string>(GetProjectRootDirectory);
26-
gitRootPath = new Lazy<string>(GetGitRootPath);
2727
}
2828

29-
public string DotGitDirectory => dotGitDirectory.Value;
29+
public string? DynamicGitRepositoryPath => dynamicGitRepositoryPath.Value;
30+
public string? DotGitDirectory => dotGitDirectory.Value;
31+
public string? GitRootPath => gitRootPath.Value;
3032
public string ProjectRootDirectory => projectRootDirectory.Value;
31-
public string DynamicGitRepositoryPath => dynamicGitRepositoryPath.Value;
32-
public string GitRootPath => gitRootPath.Value;
3333

34-
private string GetDynamicGitRepositoryPath()
34+
private string? GetDynamicGitRepositoryPath()
3535
{
3636
var repositoryInfo = gitVersionOptions.RepositoryInfo;
3737
if (string.IsNullOrWhiteSpace(repositoryInfo.TargetUrl)) return null;
@@ -60,7 +60,7 @@ private string GetDynamicGitRepositoryPath()
6060
return repositoryPath;
6161
}
6262

63-
private string GetDotGitDirectory()
63+
private string? GetDotGitDirectory()
6464
{
6565
var _dotGitDirectory = !string.IsNullOrWhiteSpace(DynamicGitRepositoryPath)
6666
? DynamicGitRepositoryPath
@@ -70,7 +70,7 @@ private string GetDotGitDirectory()
7070
if (string.IsNullOrEmpty(_dotGitDirectory))
7171
throw new DirectoryNotFoundException("Cannot find the .git directory");
7272

73-
return _dotGitDirectory.Contains(Path.Combine(".git", "worktrees"))
73+
return _dotGitDirectory is null ? null : _dotGitDirectory.Contains(Path.Combine(".git", "worktrees"))
7474
? Directory.GetParent(Directory.GetParent(_dotGitDirectory).FullName).FullName
7575
: _dotGitDirectory;
7676
}
@@ -90,7 +90,7 @@ private string GetProjectRootDirectory()
9090
return new GitRepository(new NullLog(), _dotGitDirectory).WorkingDirectory;
9191
}
9292

93-
private string GetGitRootPath()
93+
private string? GetGitRootPath()
9494
{
9595
var isDynamicRepo = !string.IsNullOrWhiteSpace(DynamicGitRepositoryPath);
9696
var rootDirectory = isDynamicRepo ? DotGitDirectory : ProjectRootDirectory;

src/GitVersion.LibGit2Sharp/Git/ObjectId.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,14 @@ internal class ObjectId : IObjectId
88
private static readonly LambdaKeyComparer<IObjectId, string> comparerHelper = new(x => x.Sha);
99

1010
private readonly LibGit2Sharp.ObjectId innerObjectId;
11-
internal ObjectId(LibGit2Sharp.ObjectId objectId)
12-
{
13-
innerObjectId = objectId;
14-
}
11+
internal ObjectId(LibGit2Sharp.ObjectId objectId) => innerObjectId = objectId;
1512

1613
public ObjectId(string sha) : this(new LibGit2Sharp.ObjectId(sha))
1714
{
1815
}
1916

2017
public int CompareTo(IObjectId other) => comparerHelper.Compare(this, other);
21-
public override bool Equals(object obj) => Equals(obj as IObjectId);
18+
public override bool Equals(object obj) => Equals((obj as IObjectId)!);
2219
public bool Equals(IObjectId other) => equalityHelper.Equals(this, other);
2320
public override int GetHashCode() => equalityHelper.GetHashCode(this);
2421
public static implicit operator LibGit2Sharp.ObjectId(ObjectId d) => d.innerObjectId;

src/GitVersion.LibGit2Sharp/Git/Reference.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,10 @@ internal class Reference : IReference
1111
internal readonly LibGit2Sharp.Reference innerReference;
1212
private DirectReference directReference => innerReference.ResolveToDirectReference();
1313

14-
internal Reference(LibGit2Sharp.Reference reference)
15-
{
16-
innerReference = reference;
17-
}
14+
internal Reference(LibGit2Sharp.Reference reference) => innerReference = reference;
1815

1916
public int CompareTo(IReference other) => comparerHelper.Compare(this, other);
20-
public override bool Equals(object obj) => Equals(obj as IReference);
17+
public override bool Equals(object obj) => Equals((obj as IReference)!);
2118
public bool Equals(IReference other) => equalityHelper.Equals(this, other);
2219
public override int GetHashCode() => equalityHelper.GetHashCode(this);
2320

src/GitVersion.LibGit2Sharp/Git/Remote.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,10 @@ internal class Remote : IRemote
1010

1111
private readonly LibGit2Sharp.Remote innerRemote;
1212

13-
internal Remote(LibGit2Sharp.Remote remote)
14-
{
15-
innerRemote = remote;
16-
}
13+
internal Remote(LibGit2Sharp.Remote remote) => innerRemote = remote;
1714

1815
public int CompareTo(IRemote other) => comparerHelper.Compare(this, other);
19-
public override bool Equals(object obj) => Equals(obj as IRemote);
16+
public override bool Equals(object obj) => Equals((obj as IRemote)!);
2017
public bool Equals(IRemote other) => equalityHelper.Equals(this, other);
2118
public override int GetHashCode() => equalityHelper.GetHashCode(this);
2219
public virtual string Name => innerRemote.Name;

0 commit comments

Comments
 (0)