Skip to content

Commit 2fe7669

Browse files
authored
Merge pull request #4129 from arturcic/fix/4125
Fix help and version arguments on non git folder
2 parents 5687fea + 88afda3 commit 2fe7669

16 files changed

+139
-38
lines changed

src/.run/cli (help).run.xml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<component name="ProjectRunConfigurationManager">
2+
<configuration default="false" name="cli (help)" type="DotNetProject" factoryName=".NET Project">
3+
<option name="EXE_PATH" value="$PROJECT_DIR$/GitVersion.App/bin/Debug/net8.0/gitversion" />
4+
<option name="PROGRAM_PARAMETERS" value="-help" />
5+
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/../.." />
6+
<option name="PASS_PARENT_ENVS" value="1" />
7+
<option name="USE_EXTERNAL_CONSOLE" value="0" />
8+
<option name="USE_MONO" value="0" />
9+
<option name="RUNTIME_ARGUMENTS" value="" />
10+
<option name="PROJECT_PATH" value="$PROJECT_DIR$/GitVersion.App/GitVersion.App.csproj" />
11+
<option name="PROJECT_EXE_PATH_TRACKING" value="1" />
12+
<option name="PROJECT_ARGUMENTS_TRACKING" value="1" />
13+
<option name="PROJECT_WORKING_DIRECTORY_TRACKING" value="0" />
14+
<option name="PROJECT_KIND" value="DotNetCore" />
15+
<option name="PROJECT_TFM" value="net8.0" />
16+
<method v="2">
17+
<option name="Build" />
18+
</method>
19+
</configuration>
20+
</component>

src/.run/cli (showconfig).run.xml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<component name="ProjectRunConfigurationManager">
2+
<configuration default="false" name="cli (showconfig)" type="DotNetProject" factoryName=".NET Project">
3+
<option name="EXE_PATH" value="$PROJECT_DIR$/GitVersion.App/bin/Debug/net8.0/gitversion" />
4+
<option name="PROGRAM_PARAMETERS" value="-showconfig" />
5+
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/.." />
6+
<option name="PASS_PARENT_ENVS" value="1" />
7+
<option name="USE_EXTERNAL_CONSOLE" value="0" />
8+
<option name="USE_MONO" value="0" />
9+
<option name="RUNTIME_ARGUMENTS" value="" />
10+
<option name="PROJECT_PATH" value="$PROJECT_DIR$/GitVersion.App/GitVersion.App.csproj" />
11+
<option name="PROJECT_EXE_PATH_TRACKING" value="1" />
12+
<option name="PROJECT_ARGUMENTS_TRACKING" value="1" />
13+
<option name="PROJECT_WORKING_DIRECTORY_TRACKING" value="0" />
14+
<option name="PROJECT_KIND" value="DotNetCore" />
15+
<option name="PROJECT_TFM" value="net8.0" />
16+
<method v="2">
17+
<option name="Build" />
18+
</method>
19+
</configuration>
20+
</component>

src/.run/cli (version).run.xml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<component name="ProjectRunConfigurationManager">
2+
<configuration default="false" name="cli (version)" type="DotNetProject" factoryName=".NET Project">
3+
<option name="EXE_PATH" value="$PROJECT_DIR$/GitVersion.App/bin/Debug/net8.0/gitversion" />
4+
<option name="PROGRAM_PARAMETERS" value="-version" />
5+
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/../.." />
6+
<option name="PASS_PARENT_ENVS" value="1" />
7+
<option name="USE_EXTERNAL_CONSOLE" value="0" />
8+
<option name="USE_MONO" value="0" />
9+
<option name="RUNTIME_ARGUMENTS" value="" />
10+
<option name="PROJECT_PATH" value="$PROJECT_DIR$/GitVersion.App/GitVersion.App.csproj" />
11+
<option name="PROJECT_EXE_PATH_TRACKING" value="1" />
12+
<option name="PROJECT_ARGUMENTS_TRACKING" value="1" />
13+
<option name="PROJECT_WORKING_DIRECTORY_TRACKING" value="0" />
14+
<option name="PROJECT_KIND" value="DotNetCore" />
15+
<option name="PROJECT_TFM" value="net8.0" />
16+
<method v="2">
17+
<option name="Build" />
18+
</method>
19+
</configuration>
20+
</component>

src/.run/cli.run.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<component name="ProjectRunConfigurationManager">
22
<configuration default="false" name="cli" type="DotNetProject" factoryName=".NET Project">
3-
<option name="EXE_PATH" value="$PROJECT_DIR$/GitVersion.App/bin/Debug/net8.0/gitversion.exe" />
3+
<option name="EXE_PATH" value="$PROJECT_DIR$/GitVersion.App/bin/Debug/net8.0/gitversion" />
44
<option name="PROGRAM_PARAMETERS" value="-nocache -l console" />
55
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/GitVersion.App/bin/Debug/net8.0" />
66
<option name="PASS_PARENT_ENVS" value="1" />

src/.run/schema.run.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<component name="ProjectRunConfigurationManager">
22
<configuration default="false" name="schema" type="DotNetProject" factoryName=".NET Project">
3-
<option name="EXE_PATH" value="$PROJECT_DIR$/GitVersion.Schema/bin/Debug/net8.0/schema.exe" />
3+
<option name="EXE_PATH" value="$PROJECT_DIR$/GitVersion.Schema/bin/Debug/net8.0/schema" />
44
<option name="PROGRAM_PARAMETERS" value="--OutputDirectory schemas --Version 6.0" />
55
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/.." />
66
<option name="PASS_PARENT_ENVS" value="1" />

src/GitVersion.App.Tests/ExecCmdLineArgumentTest.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,23 @@ public void CheckBuildServerVerbosityConsole(string verbosityArg, string expecte
5252
[Test]
5353
public void WorkingDirectoryWithoutGitFolderFailsWithInformativeMessage()
5454
{
55-
var result = GitVersionHelper.ExecuteIn(SysEnv.SystemDirectory, null, false);
55+
var result = GitVersionHelper.ExecuteIn(Path.GetTempPath(), null, false);
5656

5757
result.ExitCode.ShouldNotBe(0);
5858
result.Output.ShouldNotBeNull();
5959
result.Output.ShouldContain("Cannot find the .git directory");
6060
}
6161

62+
[TestCase(" -help")]
63+
[TestCase(" -version")]
64+
public void WorkingDirectoryWithoutGitFolderDoesNotFailForVersionAndHelp(string argument)
65+
{
66+
var result = GitVersionHelper.ExecuteIn(workingDirectory: null, arguments: argument);
67+
68+
result.ExitCode.ShouldBe(0);
69+
result.Output.ShouldNotBeNull();
70+
}
71+
6272
[Test]
6373
public void WorkingDirectoryWithoutCommitsFailsWithInformativeMessage()
6474
{

src/GitVersion.App.Tests/Helpers/ArgumentBuilder.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ namespace GitVersion.App.Tests;
44

55
public class ArgumentBuilder
66
{
7-
public ArgumentBuilder(string workingDirectory) => this.WorkingDirectory = workingDirectory;
7+
public ArgumentBuilder(string? workingDirectory) => this.WorkingDirectory = workingDirectory;
88

9-
public ArgumentBuilder(string workingDirectory, string? exec, string? execArgs, string? projectFile, string? projectArgs, string? logFile)
9+
public ArgumentBuilder(string? workingDirectory, string? exec, string? execArgs, string? projectFile, string? projectArgs, string? logFile)
1010
{
1111
this.WorkingDirectory = workingDirectory;
1212
this.exec = exec;
@@ -16,22 +16,25 @@ public ArgumentBuilder(string workingDirectory, string? exec, string? execArgs,
1616
this.LogFile = logFile;
1717
}
1818

19-
public ArgumentBuilder(string workingDirectory, string? additionalArguments, string? logFile)
19+
public ArgumentBuilder(string? workingDirectory, string? additionalArguments, string? logFile)
2020
{
2121
this.WorkingDirectory = workingDirectory;
2222
this.additionalArguments = additionalArguments;
2323
this.LogFile = logFile;
2424
}
2525

26-
public string WorkingDirectory { get; }
26+
public string? WorkingDirectory { get; }
2727

2828
public string? LogFile { get; }
2929

3030
public override string ToString()
3131
{
3232
var arguments = new StringBuilder();
3333

34-
arguments.Append(" /targetpath \"").Append(this.WorkingDirectory).Append('\"');
34+
if (!this.WorkingDirectory.IsNullOrWhiteSpace())
35+
{
36+
arguments.Append(" /targetpath \"").Append(this.WorkingDirectory).Append('\"');
37+
}
3538

3639
if (!this.exec.IsNullOrWhiteSpace())
3740
{

src/GitVersion.App.Tests/Helpers/GitVersionHelper.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace GitVersion.App.Tests;
77

88
public static class GitVersionHelper
99
{
10-
public static ExecutionResults ExecuteIn(string workingDirectory,
10+
public static ExecutionResults ExecuteIn(string? workingDirectory,
1111
string? exec = null,
1212
string? execArgs = null,
1313
string? projectFile = null,
@@ -22,12 +22,12 @@ public static ExecutionResults ExecuteIn(string workingDirectory,
2222
}
2323

2424
public static ExecutionResults ExecuteIn(
25-
string workingDirectory,
25+
string? workingDirectory,
2626
string? arguments,
2727
bool logToFile = true,
2828
params KeyValuePair<string, string?>[] environments)
2929
{
30-
var logFile = logToFile ? PathHelper.Combine(workingDirectory, "log.txt") : null;
30+
var logFile = workingDirectory is not null && logToFile ? PathHelper.Combine(workingDirectory, "log.txt") : null;
3131
var args = new ArgumentBuilder(workingDirectory, arguments, logFile);
3232
return ExecuteIn(args, environments);
3333
}
@@ -64,13 +64,15 @@ private static ExecutionResults ExecuteIn(ArgumentBuilder arguments,
6464
Console.WriteLine("Executing: {0} {1}", executable, args);
6565
Console.WriteLine();
6666

67+
var workingDirectory = arguments.WorkingDirectory ?? PathHelper.GetCurrentDirectory();
68+
6769
exitCode = ProcessHelper.Run(
6870
s => output.AppendLine(s),
6971
s => output.AppendLine(s),
7072
null,
7173
executable,
7274
args,
73-
arguments.WorkingDirectory,
75+
workingDirectory,
7476
[.. environmentalVariables]);
7577
}
7678
catch (Exception exception)

src/GitVersion.App/GitVersionExecutor.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,29 @@ namespace GitVersion;
99
internal class GitVersionExecutor(
1010
ILog log,
1111
IConsole console,
12+
IVersionWriter versionWriter,
13+
IHelpWriter helpWriter,
1214
IConfigurationFileLocator configurationFileLocator,
1315
IConfigurationProvider configurationProvider,
1416
IConfigurationSerializer configurationSerializer,
1517
IGitVersionCalculateTool gitVersionCalculateTool,
1618
IGitVersionOutputTool gitVersionOutputTool,
17-
IVersionWriter versionWriter,
18-
IHelpWriter helpWriter,
19+
IGitRepository gitRepository,
1920
IGitRepositoryInfo repositoryInfo)
2021
: IGitVersionExecutor
2122
{
2223
private readonly ILog log = log.NotNull();
2324
private readonly IConsole console = console.NotNull();
25+
private readonly IVersionWriter versionWriter = versionWriter.NotNull();
26+
private readonly IHelpWriter helpWriter = helpWriter.NotNull();
27+
2428
private readonly IConfigurationFileLocator configurationFileLocator = configurationFileLocator.NotNull();
2529
private readonly IConfigurationProvider configurationProvider = configurationProvider.NotNull();
2630
private readonly IConfigurationSerializer configurationSerializer = configurationSerializer.NotNull();
2731

2832
private readonly IGitVersionCalculateTool gitVersionCalculateTool = gitVersionCalculateTool.NotNull();
2933
private readonly IGitVersionOutputTool gitVersionOutputTool = gitVersionOutputTool.NotNull();
30-
private readonly IVersionWriter versionWriter = versionWriter.NotNull();
31-
private readonly IHelpWriter helpWriter = helpWriter.NotNull();
34+
private readonly IGitRepository gitRepository = gitRepository.NotNull();
3235
private readonly IGitRepositoryInfo repositoryInfo = repositoryInfo.NotNull();
3336

3437
public int Execute(GitVersionOptions gitVersionOptions)
@@ -49,6 +52,7 @@ public int Execute(GitVersionOptions gitVersionOptions)
4952

5053
private int RunGitVersionTool(GitVersionOptions gitVersionOptions)
5154
{
55+
this.gitRepository.DiscoverRepository(gitVersionOptions.WorkingDirectory);
5256
var mutexName = this.repositoryInfo.DotGitDirectory?.Replace(Path.DirectorySeparatorChar.ToString(), "") ?? string.Empty;
5357
using var mutex = new Mutex(true, $@"Global\gitversion{mutexName}", out var acquired);
5458

src/GitVersion.Core.Tests/Core/DynamicRepositoryTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ public void FindsVersionInDynamicRepo(string name, string url, string targetBran
7474

7575
var sp = ConfigureServices(services => services.AddSingleton(options));
7676

77+
sp.DiscoverRepository();
78+
7779
var gitPreparer = sp.GetRequiredService<IGitPreparer>();
7880
gitPreparer.Prepare();
7981

src/GitVersion.Core.Tests/Core/GitVersionExecutorTests.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ public void CacheKeySameAfterReNormalizing()
3434

3535
this.sp = GetServiceProvider(gitVersionOptions, environment: environment);
3636

37+
sp.DiscoverRepository();
38+
3739
var preparer = this.sp.GetRequiredService<IGitPreparer>();
3840

3941
preparer.Prepare();
@@ -78,6 +80,8 @@ public void CacheKeyForWorktree()
7880

7981
this.sp = GetServiceProvider(gitVersionOptions);
8082

83+
sp.DiscoverRepository();
84+
8185
var preparer = this.sp.GetRequiredService<IGitPreparer>();
8286
preparer.Prepare();
8387
var cacheKeyFactory = this.sp.GetRequiredService<IGitVersionCacheKeyFactory>();
@@ -516,6 +520,9 @@ public void CalculateVersionVariables_TwoBranchHasSameCommitHeadDetachedAndNotTa
516520
environment.SetEnvironmentVariable(AzurePipelines.EnvironmentVariableName, "true");
517521

518522
this.sp = GetServiceProvider(gitVersionOptions, environment: environment);
523+
524+
sp.DiscoverRepository();
525+
519526
var sut = sp.GetRequiredService<IGitVersionCalculateTool>();
520527

521528
// Execute & Verify
@@ -543,6 +550,9 @@ public void CalculateVersionVariables_TwoBranchHasSameCommitHeadDetachedAndTagge
543550
environment.SetEnvironmentVariable(AzurePipelines.EnvironmentVariableName, "true");
544551

545552
this.sp = GetServiceProvider(gitVersionOptions, environment: environment);
553+
554+
sp.DiscoverRepository();
555+
546556
var sut = sp.GetRequiredService<IGitVersionCalculateTool>();
547557

548558
// Execute
@@ -568,6 +578,9 @@ public void CalculateVersionVariables_ShallowFetch_ThrowException()
568578
environment.SetEnvironmentVariable(AzurePipelines.EnvironmentVariableName, "true");
569579

570580
this.sp = GetServiceProvider(gitVersionOptions, environment: environment);
581+
582+
sp.DiscoverRepository();
583+
571584
var sut = sp.GetRequiredService<IGitVersionCalculateTool>();
572585

573586
// Execute & Verify
@@ -583,6 +596,8 @@ private IGitVersionCalculateTool GetGitVersionCalculator(GitVersionOptions gitVe
583596
this.log = this.sp.GetRequiredService<ILog>();
584597
this.gitVersionCacheProvider = (GitVersionCacheProvider)this.sp.GetRequiredService<IGitVersionCacheProvider>();
585598

599+
sp.DiscoverRepository();
600+
586601
return this.sp.GetRequiredService<IGitVersionCalculateTool>();
587602
}
588603

src/GitVersion.Core.Tests/Extensions/GitToolsTestingExtensions.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ public static IBranch CreateMockBranch(string name, params ICommit[] commits)
4646
return branch;
4747
}
4848

49+
public static void DiscoverRepository(this IServiceProvider sp)
50+
{
51+
var gitRepository = sp.GetRequiredService<IGitRepository>();
52+
var gitRepositoryInfo = sp.GetRequiredService<IGitRepositoryInfo>();
53+
gitRepository.DiscoverRepository(gitRepositoryInfo.GitRootPath);
54+
}
55+
4956
public static IBranch FindBranch(this IGitRepository repository, string branchName)
5057
=> repository.Branches.FirstOrDefault(branch => branch.Name.WithoutOrigin == branchName)
5158
?? throw new GitVersionException($"Branch {branchName} not found");
@@ -84,6 +91,9 @@ public static GitVersionVariables GetVersion(this RepositoryFixtureBase fixture,
8491
services.AddSingleton(options);
8592
services.AddSingleton(configurationProviderMock);
8693
});
94+
95+
sp.DiscoverRepository();
96+
8797
var variableProvider = sp.GetRequiredService<IVariableProvider>();
8898
var nextVersionCalculator = sp.GetRequiredService<INextVersionCalculator>();
8999
var contextOptions = sp.GetRequiredService<Lazy<GitVersionContext>>();

src/GitVersion.Core.Tests/IntegrationTests/RemoteRepositoryScenarios.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ public void GivenARemoteGitRepositoryWithCommitsAndBranchesThenClonedLocalShould
6363
services.AddSingleton<IEnvironment>(environment);
6464
});
6565

66+
sp.DiscoverRepository();
67+
6668
var gitPreparer = sp.GetRequiredService<IGitPreparer>();
6769

6870
gitPreparer.Prepare();

src/GitVersion.LibGit2Sharp/Git/GitRepository.extended.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ namespace GitVersion.Git;
88

99
internal partial class GitRepository(ILog log) : IMutatingGitRepository
1010
{
11+
private readonly ILog log = log.NotNull();
12+
1113
public void Clone(string? sourceUrl, string? workdirPath, AuthenticationInfo auth)
1214
{
1315
try
1416
{
1517
var path = Repository.Clone(sourceUrl, workdirPath, GetCloneOptions(auth));
16-
log.Info($"Returned path after repository clone: {path}");
18+
this.log.Info($"Returned path after repository clone: {path}");
1719
}
1820
catch (LibGit2Sharp.LockedFileException ex)
1921
{
@@ -48,7 +50,7 @@ public void Fetch(string remote, IEnumerable<string> refSpecs, AuthenticationInf
4850
Commands.Fetch((Repository)RepositoryInstance, remote, refSpecs, GetFetchOptions(auth), logMessage));
4951
public void CreateBranchForPullRequestBranch(AuthenticationInfo auth) => RepositoryExtensions.RunSafe(() =>
5052
{
51-
log.Info("Fetching remote refs to see if there is a pull request ref");
53+
this.log.Info("Fetching remote refs to see if there is a pull request ref");
5254

5355
// FIX ME: What to do when Tip is null?
5456
if (Head.Tip == null)
@@ -59,21 +61,21 @@ public void CreateBranchForPullRequestBranch(AuthenticationInfo auth) => Reposit
5961
var reference = GetPullRequestReference(auth, remote, headTipSha);
6062
var canonicalName = reference.CanonicalName;
6163
var referenceName = ReferenceName.Parse(reference.CanonicalName);
62-
log.Info($"Found remote tip '{canonicalName}' pointing at the commit '{headTipSha}'.");
64+
this.log.Info($"Found remote tip '{canonicalName}' pointing at the commit '{headTipSha}'.");
6365

6466
if (referenceName.IsTag)
6567
{
66-
log.Info($"Checking out tag '{canonicalName}'");
68+
this.log.Info($"Checking out tag '{canonicalName}'");
6769
Checkout(reference.Target.Sha);
6870
}
6971
else if (referenceName.IsPullRequest)
7072
{
7173
var fakeBranchName = canonicalName.Replace("refs/pull/", "refs/heads/pull/").Replace("refs/pull-requests/", "refs/heads/pull-requests/");
7274

73-
log.Info($"Creating fake local branch '{fakeBranchName}'.");
75+
this.log.Info($"Creating fake local branch '{fakeBranchName}'.");
7476
Refs.Add(fakeBranchName, headTipSha);
7577

76-
log.Info($"Checking local branch '{fakeBranchName}' out.");
78+
this.log.Info($"Checking local branch '{fakeBranchName}' out.");
7779
Checkout(fakeBranchName);
7880
}
7981
else
@@ -91,7 +93,7 @@ private DirectReference GetPullRequestReference(AuthenticationInfo auth, LibGit2
9193
: network.ListReferences(remote))
9294
.Select(r => r.ResolveToDirectReference()).ToList();
9395

94-
log.Info($"Remote Refs:{PathHelper.NewLine}" + string.Join(PathHelper.NewLine, remoteTips.Select(r => r.CanonicalName)));
96+
this.log.Info($"Remote Refs:{PathHelper.NewLine}" + string.Join(PathHelper.NewLine, remoteTips.Select(r => r.CanonicalName)));
9597
var refs = remoteTips.Where(r => r.TargetIdentifier == headTipSha).ToList();
9698

9799
switch (refs.Count)

0 commit comments

Comments
 (0)