Skip to content

Commit e09622c

Browse files
committed
GH-1876, object reference not set to an instance of object
1 parent 1dd9c39 commit e09622c

File tree

12 files changed

+79
-79
lines changed

12 files changed

+79
-79
lines changed

src/GitVersionCore.Tests/GitVersionExecutorTests.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public void CacheKeySameAfterReNormalizing()
7676
}
7777

7878
[Test]
79-
public void GitPreparerShouldFailWhenTargetPathNotInitialized()
79+
public void GitPreparerShouldNotFailWhenTargetPathNotInitialized()
8080
{
8181
RepositoryScope((fixture, vv) =>
8282
{
@@ -88,10 +88,6 @@ public void GitPreparerShouldFailWhenTargetPathNotInitialized()
8888
TargetPath = null
8989
};
9090
var options = Options.Create(arguments);
91-
Should.Throw<NullReferenceException>(() => new GitPreparer(log, environment, options));
92-
93-
arguments.TargetPath = fixture.RepositoryPath;
94-
9591
Should.NotThrow(() => new GitPreparer(log, environment, options));
9692
});
9793
}
@@ -413,7 +409,7 @@ public void GetProjectRootDirectoryWorkingDirectoryWithWorktree()
413409

414410
var gitPreparer = new GitPreparer(log, environment, Options.Create(arguments));
415411

416-
gitPreparer.GetProjectRootDirectory().TrimEnd('/', '\\').ShouldBe(worktreePath);
412+
gitPreparer.GetProjectRootDirectoryInternal().TrimEnd('/', '\\').ShouldBe(worktreePath);
417413
}
418414
finally
419415
{
@@ -437,7 +433,7 @@ public void GetProjectRootDirectoryNoWorktree()
437433

438434
var gitPreparer = new GitPreparer(log, environment, Options.Create(arguments));
439435
var expectedPath = fixture.RepositoryPath.TrimEnd('/', '\\');
440-
gitPreparer.GetProjectRootDirectory().TrimEnd('/', '\\').ShouldBe(expectedPath);
436+
gitPreparer.GetProjectRootDirectoryInternal().TrimEnd('/', '\\').ShouldBe(expectedPath);
441437
});
442438
}
443439

src/GitVersionCore/Cache/GitVersionCacheKeyFactory.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Text;
77
using GitVersion.Configuration;
88
using GitVersion.Logging;
9+
using GitVersion.Extensions;
910

1011
namespace GitVersion.Cache
1112
{
@@ -124,7 +125,8 @@ private static List<string> CalculateDirectoryContents(ILog log, string root)
124125

125126
private static string GetRepositorySnapshotHash(IGitPreparer gitPreparer)
126127
{
127-
var repositorySnapshot = gitPreparer.WithRepository(repo => {
128+
var repositorySnapshot = gitPreparer.GetDotGitDirectory().WithRepository(repo =>
129+
{
128130
var head = repo.Head;
129131
if (head.Tip == null)
130132
{

src/GitVersionCore/Configuration/ConfigFileLocator.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ protected ConfigFileLocator(IFileSystem fileSystem, ILog log)
2323

2424
public string SelectConfigFilePath(IGitPreparer gitPreparer)
2525
{
26-
var workingDirectory = gitPreparer.WorkingDirectory;
26+
var workingDirectory = gitPreparer.GetWorkingDirectory();
2727
var projectRootDirectory = gitPreparer.GetProjectRootDirectory();
2828

2929
if (HasConfigFileAt(workingDirectory))
@@ -50,14 +50,14 @@ public Config ReadConfig(string workingDirectory)
5050

5151
public void Verify(IGitPreparer gitPreparer)
5252
{
53-
if (!string.IsNullOrWhiteSpace(gitPreparer.TargetUrl))
53+
if (!string.IsNullOrWhiteSpace(gitPreparer.GetTargetUrl()))
5454
{
5555
// Assuming this is a dynamic repository. At this stage it's unsure whether we have
5656
// any .git info so we need to skip verification
5757
return;
5858
}
5959

60-
var workingDirectory = gitPreparer.WorkingDirectory;
60+
var workingDirectory = gitPreparer.GetWorkingDirectory();
6161
var projectRootDirectory = gitPreparer.GetProjectRootDirectory();
6262

6363
Verify(workingDirectory, projectRootDirectory);

src/GitVersionCore/Configuration/ConfigFileLocatorFactory.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public ConfigFileLocatorFactory(IFileSystem fileSystem, ILog log, IOptions<Argum
1414
{
1515
this.fileSystem = fileSystem ?? throw new ArgumentNullException(nameof(fileSystem));
1616
this.log = log ?? throw new ArgumentNullException(nameof(log));
17-
this.options = options ?? throw new ArgumentNullException(nameof(fileSystem));
17+
this.options = options ?? throw new ArgumentNullException(nameof(options));
1818
}
1919

2020
public IConfigFileLocator Create()
@@ -24,4 +24,4 @@ public IConfigFileLocator Create()
2424
: new NamedConfigFileLocator(fileSystem, log, options);
2525
}
2626
}
27-
}
27+
}

src/GitVersionCore/Configuration/ConfigProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public ConfigProvider(IFileSystem fileSystem, ILog log, IConfigFileLocator confi
2424

2525
public Config Provide(bool applyDefaults = true, Config overrideConfig = null)
2626
{
27-
var workingDirectory = gitPreparer.WorkingDirectory;
27+
var workingDirectory = gitPreparer.GetWorkingDirectory();
2828
var projectRootDirectory = gitPreparer.GetProjectRootDirectory();
2929

3030
var rootDirectory = configFileLocator.HasConfigFileAt(workingDirectory) ? workingDirectory : projectRootDirectory;

src/GitVersionCore/Configuration/NamedConfigFileLocator.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ namespace GitVersion.Configuration
88
{
99
public class NamedConfigFileLocator : ConfigFileLocator
1010
{
11+
private readonly IOptions<Arguments> options;
12+
1113
public NamedConfigFileLocator(IFileSystem fileSystem, ILog log, IOptions<Arguments> options) : base(fileSystem, log)
1214
{
13-
var filePath = options.Value.ConfigFile;
14-
if (string.IsNullOrEmpty(filePath)) throw new ArgumentNullException(nameof(filePath), "Empty file path provided!");
15-
FilePath = filePath;
15+
this.options = options ?? throw new ArgumentNullException(nameof(options));
1616
}
1717

18-
public string FilePath { get; }
18+
public string FilePath => options.Value.ConfigFile;
1919

2020
public override bool HasConfigFileAt(string workingDirectory) =>
2121
FileSystem.Exists(Path.Combine(workingDirectory, FilePath));

src/GitVersionCore/Extensions/LibGitExtensions.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ namespace GitVersion.Extensions
1111
{
1212
public static class LibGitExtensions
1313
{
14+
public static TResult WithRepository<TResult>(this string dotGitDirectory, Func<IRepository, TResult> action)
15+
{
16+
using var repo = new Repository(dotGitDirectory);
17+
return action(repo);
18+
}
19+
1420
public static DateTimeOffset When(this Commit commit)
1521
{
1622
return commit.Committer.When;

src/GitVersionCore/GitPreparer.cs

Lines changed: 38 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -12,36 +12,43 @@ public class GitPreparer : IGitPreparer
1212
{
1313
private readonly ILog log;
1414
private readonly IEnvironment environment;
15-
private readonly string dynamicRepositoryLocation;
16-
private readonly Arguments arguments;
15+
private readonly IOptions<Arguments> options;
1716

1817
private const string DefaultRemoteName = "origin";
19-
private string dynamicGitRepositoryPath;
18+
private string dotGitDirectory;
19+
private string projectRootDirectory;
20+
21+
public string GetTargetUrl() => options.Value.TargetUrl;
22+
23+
public string GetWorkingDirectory() => options.Value.TargetPath.TrimEnd('/', '\\');
24+
25+
public string GetDotGitDirectory() => dotGitDirectory ??= GetDotGitDirectoryInternal();
26+
27+
public string GetProjectRootDirectory() => projectRootDirectory ??= GetProjectRootDirectoryInternal();
28+
29+
private bool IsDynamicGitRepository => !string.IsNullOrWhiteSpace(DynamicGitRepositoryPath);
30+
private string DynamicGitRepositoryPath;
2031

2132
public GitPreparer(ILog log, IEnvironment environment, IOptions<Arguments> options)
2233
{
2334
this.log = log ?? throw new ArgumentNullException(nameof(log));
24-
this.environment = environment;
25-
arguments = options.Value;
26-
27-
TargetUrl = arguments.TargetUrl;
28-
WorkingDirectory = arguments.TargetPath.TrimEnd('/', '\\');
29-
30-
dynamicRepositoryLocation = arguments.DynamicRepositoryLocation;
35+
this.environment = environment ?? throw new ArgumentNullException(nameof(environment));
36+
this.options = options ?? throw new ArgumentNullException(nameof(options));
3137
}
3238

3339
public void Prepare(bool normalizeGitDirectory, string currentBranch, bool shouldCleanUpRemotes = false)
3440
{
41+
var arguments = options.Value;
3542
var authentication = new AuthenticationInfo
3643
{
3744
Username = arguments.Authentication?.Username,
3845
Password = arguments.Authentication?.Password
3946
};
40-
if (!string.IsNullOrWhiteSpace(TargetUrl))
47+
if (!string.IsNullOrWhiteSpace(GetTargetUrl()))
4148
{
42-
var tempRepositoryPath = CalculateTemporaryRepositoryPath(TargetUrl, dynamicRepositoryLocation);
49+
var tempRepositoryPath = CalculateTemporaryRepositoryPath(GetTargetUrl(), arguments.DynamicRepositoryLocation);
4350

44-
dynamicGitRepositoryPath = CreateDynamicRepository(tempRepositoryPath, authentication, TargetUrl, currentBranch);
51+
DynamicGitRepositoryPath = CreateDynamicRepository(tempRepositoryPath, authentication, GetTargetUrl(), currentBranch);
4552
}
4653
else
4754
{
@@ -52,62 +59,49 @@ public void Prepare(bool normalizeGitDirectory, string currentBranch, bool shoul
5259
CleanupDuplicateOrigin();
5360
}
5461

55-
NormalizeGitDirectory(authentication, currentBranch, GetDotGitDirectory(), IsDynamicGitRepository());
62+
NormalizeGitDirectory(authentication, currentBranch, GetDotGitDirectoryInternal(), IsDynamicGitRepository);
5663
}
5764
}
5865
}
5966

60-
public TResult WithRepository<TResult>(Func<IRepository, TResult> action)
67+
private string GetDotGitDirectoryInternal()
6168
{
62-
using IRepository repo = new Repository(GetDotGitDirectory());
63-
return action(repo);
64-
}
65-
66-
public string GetDotGitDirectory()
67-
{
68-
var dotGitDirectory = IsDynamicGitRepository() ? dynamicGitRepositoryPath : Repository.Discover(WorkingDirectory);
69-
70-
dotGitDirectory = dotGitDirectory?.TrimEnd('/', '\\');
71-
if (string.IsNullOrEmpty(dotGitDirectory))
72-
throw new DirectoryNotFoundException("Can't find the .git directory in " + WorkingDirectory);
69+
var gitDirectory = IsDynamicGitRepository ? DynamicGitRepositoryPath : Repository.Discover(GetWorkingDirectory());
7370

74-
if (dotGitDirectory.Contains(Path.Combine(".git", "worktrees")))
75-
return Directory.GetParent(Directory.GetParent(dotGitDirectory).FullName).FullName;
71+
gitDirectory = gitDirectory?.TrimEnd('/', '\\');
72+
if (string.IsNullOrEmpty(gitDirectory))
73+
throw new DirectoryNotFoundException("Can't find the .git directory in " + gitDirectory);
7674

77-
return dotGitDirectory;
75+
return gitDirectory.Contains(Path.Combine(".git", "worktrees"))
76+
? Directory.GetParent(Directory.GetParent(gitDirectory).FullName).FullName
77+
: gitDirectory;
7878
}
7979

80-
public string GetProjectRootDirectory()
80+
public string GetProjectRootDirectoryInternal()
8181
{
82-
log.Info($"IsDynamicGitRepository: {IsDynamicGitRepository()}");
83-
if (IsDynamicGitRepository())
82+
log.Info($"IsDynamicGitRepository: {IsDynamicGitRepository}");
83+
if (IsDynamicGitRepository)
8484
{
85-
log.Info($"Returning Project Root as {WorkingDirectory}");
86-
return WorkingDirectory;
85+
log.Info($"Returning Project Root as {GetWorkingDirectory()}");
86+
return GetWorkingDirectory();
8787
}
8888

89-
var dotGitDirectory = Repository.Discover(WorkingDirectory);
89+
var dotGitDirectory = Repository.Discover(GetWorkingDirectory());
9090

9191
if (string.IsNullOrEmpty(dotGitDirectory))
92-
throw new DirectoryNotFoundException($"Can't find the .git directory in {WorkingDirectory}");
92+
throw new DirectoryNotFoundException($"Can't find the .git directory in {dotGitDirectory}");
9393

9494
using var repo = new Repository(dotGitDirectory);
9595
var result = repo.Info.WorkingDirectory;
9696
log.Info($"Returning Project Root from DotGitDirectory: {dotGitDirectory} - {result}");
9797
return result;
9898
}
9999

100-
public string TargetUrl { get; }
101-
102-
public string WorkingDirectory { get; }
103-
104-
private bool IsDynamicGitRepository() => !string.IsNullOrWhiteSpace(dynamicGitRepositoryPath);
105-
106100
private void CleanupDuplicateOrigin()
107101
{
108102
var remoteToKeep = DefaultRemoteName;
109103

110-
using var repo = new Repository(GetDotGitDirectory());
104+
using var repo = new Repository(GetDotGitDirectoryInternal());
111105

112106
// check that we have a remote that matches defaultRemoteName if not take the first remote
113107
if (!repo.Network.Remotes.Any(remote => remote.Name.Equals(DefaultRemoteName, StringComparison.InvariantCultureIgnoreCase)))
@@ -193,7 +187,7 @@ private void NormalizeGitDirectory(AuthenticationInfo auth, string targetBranch,
193187
using (log.IndentLog($"Normalizing git directory for branch '{targetBranch}'"))
194188
{
195189
// Normalize (download branches) before using the branch
196-
GitRepositoryHelper.NormalizeGitDirectory(log, environment, gitDirectory, auth, arguments.NoFetch, targetBranch, isDynamicRepository);
190+
GitRepositoryHelper.NormalizeGitDirectory(log, environment, gitDirectory, auth, options.Value.NoFetch, targetBranch, isDynamicRepository);
197191
}
198192
}
199193

src/GitVersionCore/GitVersionCalculator.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using GitVersion.Cache;
55
using GitVersion.Logging;
66
using Microsoft.Extensions.Options;
7+
using GitVersion.Extensions;
78

89
namespace GitVersion
910
{
@@ -18,7 +19,7 @@ public class GitVersionCalculator : IGitVersionCalculator
1819
private readonly IGitVersionFinder gitVersionFinder;
1920
private readonly IGitPreparer gitPreparer;
2021
private readonly IVariableProvider variableProvider;
21-
private readonly Arguments arguments;
22+
private readonly IOptions<Arguments> options;
2223

2324
public GitVersionCalculator(IFileSystem fileSystem, ILog log, IConfigFileLocator configFileLocator,
2425
IConfigProvider configProvider,
@@ -34,11 +35,12 @@ public GitVersionCalculator(IFileSystem fileSystem, ILog log, IConfigFileLocator
3435
this.gitVersionFinder = gitVersionFinder ?? throw new ArgumentNullException(nameof(gitVersionFinder));
3536
this.gitPreparer = gitPreparer ?? throw new ArgumentNullException(nameof(gitPreparer));
3637
this.variableProvider = variableProvider ?? throw new ArgumentNullException(nameof(variableProvider));
37-
this.arguments = options.Value;
38+
this.options = options ?? throw new ArgumentNullException(nameof(options));
3839
}
3940

4041
public VersionVariables CalculateVersionVariables()
4142
{
43+
var arguments = options.Value;
4244
var buildServer = buildServerResolver.Resolve();
4345

4446
// Normalize if we are running on build server
@@ -119,7 +121,7 @@ private VersionVariables ExecuteInternal(string targetBranch, string commitId, C
119121
{
120122
var configuration = configProvider.Provide(overrideConfig: overrideConfig);
121123

122-
return gitPreparer.WithRepository(repo =>
124+
return gitPreparer.GetDotGitDirectory().WithRepository(repo =>
123125
{
124126
var gitVersionContext = new GitVersionContext(repo, log, targetBranch, configuration, commitId: commitId);
125127
var semanticVersion = gitVersionFinder.FindVersion(gitVersionContext);

src/GitVersionCore/IGitPreparer.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
1-
using System;
2-
using LibGit2Sharp;
3-
41
namespace GitVersion
52
{
63
public interface IGitPreparer
74
{
85
void Prepare(bool normalizeGitDirectory, string currentBranch, bool shouldCleanUpRemotes = false);
9-
TResult WithRepository<TResult>(Func<IRepository, TResult> action);
10-
string GetDotGitDirectory();
116
string GetProjectRootDirectory();
12-
string TargetUrl { get; }
13-
string WorkingDirectory { get; }
7+
string GetDotGitDirectory();
8+
string GetTargetUrl();
9+
string GetWorkingDirectory();
1410
}
1511
}

src/GitVersionExe/ExecCommand.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@ public class ExecCommand : IExecCommand
2121
private readonly IBuildServerResolver buildServerResolver;
2222
private readonly ILog log;
2323
private readonly IGitVersionCalculator gitVersionCalculator;
24-
private readonly Arguments arguments;
24+
private readonly IOptions<Arguments> options;
2525
public static readonly string BuildTool = GetMsBuildToolPath();
2626

27-
public ExecCommand(IFileSystem fileSystem, IBuildServerResolver buildServerResolver, ILog log, IGitVersionCalculator gitVersionCalculator, IOptions<Arguments> arguments)
27+
public ExecCommand(IFileSystem fileSystem, IBuildServerResolver buildServerResolver, ILog log, IGitVersionCalculator gitVersionCalculator, IOptions<Arguments> options)
2828
{
2929
this.fileSystem = fileSystem ?? throw new ArgumentNullException(nameof(fileSystem));
3030
this.buildServerResolver = buildServerResolver ?? throw new ArgumentNullException(nameof(buildServerResolver));
3131
this.log = log ?? throw new ArgumentNullException(nameof(log));
3232
this.gitVersionCalculator = gitVersionCalculator ?? throw new ArgumentNullException(nameof(gitVersionCalculator));
33-
this.arguments = arguments.Value;
33+
this.options = options ?? throw new ArgumentNullException(nameof(options));
3434
}
3535

3636
public void Execute()
@@ -39,6 +39,8 @@ public void Execute()
3939

4040
var variables = gitVersionCalculator.CalculateVersionVariables();
4141

42+
var arguments = options.Value;
43+
4244
switch (arguments.Output)
4345
{
4446
case OutputType.BuildServer:

src/GitVersionExe/GitVersionApp.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,22 @@ internal class GitVersionApp : IHostedService
1111
{
1212
private readonly IHostApplicationLifetime applicationLifetime;
1313
private readonly IGitVersionExecutor gitVersionExecutor;
14-
private readonly Arguments arguments;
14+
private readonly ILog log;
15+
private readonly IOptions<Arguments> options;
1516

1617
public GitVersionApp(IHostApplicationLifetime applicationLifetime, IGitVersionExecutor gitVersionExecutor, ILog log, IOptions<Arguments> options)
1718
{
18-
this.arguments = options.Value;
19+
this.options = options ?? throw new ArgumentNullException(nameof(options));
1920
this.applicationLifetime = applicationLifetime ?? throw new ArgumentNullException(nameof(applicationLifetime));
2021
this.gitVersionExecutor = gitVersionExecutor ?? throw new ArgumentNullException(nameof(gitVersionExecutor));
21-
22-
log.Verbosity = arguments.Verbosity;
22+
this.log = log ?? throw new ArgumentNullException(nameof(log));
2323
}
2424
public Task StartAsync(CancellationToken cancellationToken)
2525
{
2626
try
2727
{
28+
var arguments = options.Value;
29+
log.Verbosity = arguments.Verbosity;
2830
gitVersionExecutor.Execute(arguments);
2931
}
3032
catch (Exception exception)

0 commit comments

Comments
 (0)