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

0 commit comments

Comments
 (0)