Skip to content

Spike showing new CLI usage - got one command working #2263

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 0 additions & 18 deletions src/GitVersion.sln
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ VisualStudioVersion = 16.0.28714.193
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GitVersionExe", "GitVersionExe\GitVersionExe.csproj", "{C3578A7B-09A6-4444-9383-0DEAFA4958BD}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GitVersionTask.Tests", "GitVersionTask.Tests\GitVersionTask.Tests.csproj", "{5A86453B-96FB-4B6E-A283-225BB9F753D3}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GitVersionCore.Tests", "GitVersionCore.Tests\GitVersionCore.Tests.csproj", "{BF905F84-382C-440D-92F5-C61108626D8D}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{3EFFC5D6-88D0-49D9-BB53-E1B7EB49DD45}"
Expand All @@ -29,10 +27,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GitVersionCore", "GitVersio
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GitVersionExe.Tests", "GitVersionExe.Tests\GitVersionExe.Tests.csproj", "{75C2BE85-1DAF-4E34-8305-B17AFAA982A6}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GitVersionTask", "GitVersionTask\GitVersionTask.csproj", "{F7AC0E71-3E9A-4F6D-B986-E004825A48E1}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GitVersionTask.MsBuild", "GitVersionTask.MsBuild\GitVersionTask.MsBuild.csproj", "{0F1AEC4E-E81D-4F84-B2E8-3415A1A4DBF4}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -43,10 +37,6 @@ Global
{C3578A7B-09A6-4444-9383-0DEAFA4958BD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C3578A7B-09A6-4444-9383-0DEAFA4958BD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C3578A7B-09A6-4444-9383-0DEAFA4958BD}.Release|Any CPU.Build.0 = Release|Any CPU
{5A86453B-96FB-4B6E-A283-225BB9F753D3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5A86453B-96FB-4B6E-A283-225BB9F753D3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5A86453B-96FB-4B6E-A283-225BB9F753D3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5A86453B-96FB-4B6E-A283-225BB9F753D3}.Release|Any CPU.Build.0 = Release|Any CPU
{BF905F84-382C-440D-92F5-C61108626D8D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BF905F84-382C-440D-92F5-C61108626D8D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BF905F84-382C-440D-92F5-C61108626D8D}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand All @@ -59,14 +49,6 @@ Global
{75C2BE85-1DAF-4E34-8305-B17AFAA982A6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{75C2BE85-1DAF-4E34-8305-B17AFAA982A6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{75C2BE85-1DAF-4E34-8305-B17AFAA982A6}.Release|Any CPU.Build.0 = Release|Any CPU
{F7AC0E71-3E9A-4F6D-B986-E004825A48E1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F7AC0E71-3E9A-4F6D-B986-E004825A48E1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F7AC0E71-3E9A-4F6D-B986-E004825A48E1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F7AC0E71-3E9A-4F6D-B986-E004825A48E1}.Release|Any CPU.Build.0 = Release|Any CPU
{0F1AEC4E-E81D-4F84-B2E8-3415A1A4DBF4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0F1AEC4E-E81D-4F84-B2E8-3415A1A4DBF4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0F1AEC4E-E81D-4F84-B2E8-3415A1A4DBF4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0F1AEC4E-E81D-4F84-B2E8-3415A1A4DBF4}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
4 changes: 2 additions & 2 deletions src/GitVersionCore.Tests/Core/GitVersionExecutorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ public void GetProjectRootDirectoryWorkingDirectoryWithWorktree()

sp = GetServiceProvider(gitVersionOptions);

gitVersionOptions.ProjectRootDirectory.TrimEnd('/', '\\').ShouldBe(worktreePath);
Copy link
Member Author

@dazinator dazinator May 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: ProjectRootDirectory was actually being set to the "working directory" of the git repository. This is different from the command line's working directory, or the directory of the .git folder. I renamed it to GitRepositoryWorkingDirectory to aid understanding whilst refactoring. It's not a critical change.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has been a constant source of friction, because I think it's used to support dynamic repositories in a weird way I don't quite understand. I think it's been changed back and forth several times in different PR's, each breaking one other use case that isn't well enough tested. 🤷‍♂️

gitVersionOptions.GitRepositoryWorkingDirectory.TrimEnd('/', '\\').ShouldBe(worktreePath);
}
finally
{
Expand All @@ -441,7 +441,7 @@ public void GetProjectRootDirectoryNoWorktree()
sp = GetServiceProvider(gitVersionOptions);

var expectedPath = fixture.RepositoryPath.TrimEnd('/', '\\');
gitVersionOptions.ProjectRootDirectory.TrimEnd('/', '\\').ShouldBe(expectedPath);
gitVersionOptions.GitRepositoryWorkingDirectory.TrimEnd('/', '\\').ShouldBe(expectedPath);
}

[Test]
Expand Down
4 changes: 2 additions & 2 deletions src/GitVersionCore/Configuration/ConfigFileLocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ protected ConfigFileLocator(IFileSystem fileSystem)
public string SelectConfigFilePath(GitVersionOptions gitVersionOptions)
{
var workingDirectory = gitVersionOptions.WorkingDirectory;
var projectRootDirectory = gitVersionOptions.ProjectRootDirectory;
var projectRootDirectory = gitVersionOptions.GitRepositoryWorkingDirectory;

return GetConfigFilePath(HasConfigFileAt(workingDirectory) ? workingDirectory : projectRootDirectory);
}
Expand Down Expand Up @@ -50,7 +50,7 @@ public void Verify(GitVersionOptions gitVersionOptions)
}

var workingDirectory = gitVersionOptions.WorkingDirectory;
var projectRootDirectory = gitVersionOptions.ProjectRootDirectory;
var projectRootDirectory = gitVersionOptions.GitRepositoryWorkingDirectory;

Verify(workingDirectory, projectRootDirectory);
}
Expand Down
2 changes: 1 addition & 1 deletion src/GitVersionCore/Configuration/ConfigProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public Config Provide(bool applyDefaults = true, Config overrideConfig = null)
{
var gitVersionOptions = options.Value;
var workingDirectory = gitVersionOptions.WorkingDirectory;
var projectRootDirectory = gitVersionOptions.ProjectRootDirectory;
var projectRootDirectory = gitVersionOptions.GitRepositoryWorkingDirectory;

var rootDirectory = configFileLocator.HasConfigFileAt(workingDirectory) ? workingDirectory : projectRootDirectory;
return Provide(rootDirectory, applyDefaults, overrideConfig);
Expand Down
101 changes: 8 additions & 93 deletions src/GitVersionCore/Core/GitPreparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,9 @@ public void Prepare()
var currentBranch = ResolveCurrentBranch();

var dotGitDirectory = gitVersionOptions.DotGitDirectory;
var projectRoot = gitVersionOptions.ProjectRootDirectory;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This argument wasn't used, and I removed it whilst refactoring because it helped me.


log.Info($"Project root is: {projectRoot}");
log.Info($"DotGit directory is: {dotGitDirectory}");
if (string.IsNullOrEmpty(dotGitDirectory) || string.IsNullOrEmpty(projectRoot))
if (string.IsNullOrEmpty(dotGitDirectory))
{
throw new Exception($"Failed to prepare or find the .git directory in path '{gitVersionOptions.WorkingDirectory}'.");
}
Expand All @@ -50,22 +48,17 @@ public void Prepare()
private void PrepareInternal(bool normalizeGitDirectory, string currentBranch, bool shouldCleanUpRemotes = false)
{
var gitVersionOptions = options.Value;
if (!string.IsNullOrWhiteSpace(gitVersionOptions.RepositoryInfo.TargetUrl))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed the dynamic repository stuff as per the comments on this PR. User can clone down their own repo and run gitversion normalise against it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, good riddance! 😅

{
CreateDynamicRepository(currentBranch);
}
else

if (normalizeGitDirectory)
{
if (normalizeGitDirectory)
if (shouldCleanUpRemotes)
{
if (shouldCleanUpRemotes)
{
CleanupDuplicateOrigin();
}

NormalizeGitDirectory(currentBranch, gitVersionOptions.DotGitDirectory, false);
CleanupDuplicateOrigin();
}

NormalizeGitDirectory(currentBranch, gitVersionOptions.DotGitDirectory, false);
}

}

private string ResolveCurrentBranch()
Expand Down Expand Up @@ -108,32 +101,6 @@ private void CleanupDuplicateOrigin()
}
}

private void CreateDynamicRepository(string targetBranch)
{
var gitVersionOptions = options.Value;
if (string.IsNullOrWhiteSpace(targetBranch))
{
throw new Exception("Dynamic Git repositories must have a target branch (/b)");
}

var repositoryInfo = gitVersionOptions.RepositoryInfo;
var gitDirectory = gitVersionOptions.DynamicGitRepositoryPath;

using (log.IndentLog($"Creating dynamic repository at '{gitDirectory}'"))
{
var authentication = gitVersionOptions.Authentication;
if (!Directory.Exists(gitDirectory))
{
CloneRepository(repositoryInfo.TargetUrl, gitDirectory, authentication);
}
else
{
log.Info("Git repository already exists");
}
NormalizeGitDirectory(targetBranch, gitDirectory, true);
}
}

private void NormalizeGitDirectory(string targetBranch, string gitDirectory, bool isDynamicRepository)
{
using (log.IndentLog($"Normalizing git directory for branch '{targetBranch}'"))
Expand All @@ -143,58 +110,6 @@ private void NormalizeGitDirectory(string targetBranch, string gitDirectory, boo
}
}

private void CloneRepository(string repositoryUrl, string gitDirectory, AuthenticationInfo auth)
{
Credentials credentials = null;

if (auth != null)
{
if (!string.IsNullOrWhiteSpace(auth.Username))
{
log.Info($"Setting up credentials using name '{auth.Username}'");

credentials = new UsernamePasswordCredentials
{
Username = auth.Username,
Password = auth.Password ?? string.Empty
};
}
}

try
{
using (log.IndentLog($"Cloning repository from url '{repositoryUrl}'"))
{
var cloneOptions = new CloneOptions
{
Checkout = false,
CredentialsProvider = (url, usernameFromUrl, types) => credentials
};

var returnedPath = Repository.Clone(repositoryUrl, gitDirectory, cloneOptions);
log.Info($"Returned path after repository clone: {returnedPath}");
}
}
catch (LibGit2SharpException ex)
{
var message = ex.Message;
if (message.Contains("401"))
{
throw new Exception("Unauthorized: Incorrect username/password");
}
if (message.Contains("403"))
{
throw new Exception("Forbidden: Possibly Incorrect username/password");
}
if (message.Contains("404"))
{
throw new Exception("Not found: The repository was not found");
}

throw new Exception("There was an unknown problem with the Git repository you provided", ex);
}
}

/// <summary>
/// Normalization of a git directory turns all remote branches into local branches, turns pull request refs into a real branch and a few other things. This is designed to be run *only on the build server* which checks out repositories in different ways.
/// It is not recommended to run normalization against a local repository
Expand Down
15 changes: 8 additions & 7 deletions src/GitVersionCore/Core/GitVersionTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,14 @@ public void UpdateAssemblyInfo(VersionVariables variables)
{
var gitVersionOptions = options.Value;

if (gitVersionOptions.AssemblyInfo.ShouldUpdate)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can eventually become a seperate tool as discussed.

{
using (assemblyInfoFileUpdater)
{
assemblyInfoFileUpdater.Execute(variables, new AssemblyInfoContext(gitVersionOptions.WorkingDirectory, gitVersionOptions.AssemblyInfo.EnsureAssemblyInfo, gitVersionOptions.AssemblyInfo.Files.ToArray()));
}
}
throw new NotImplementedException();
//if (gitVersionOptions.AssemblyInfo.ShouldUpdate)
//{
// using (assemblyInfoFileUpdater)
// {
// assemblyInfoFileUpdater.Execute(variables, new AssemblyInfoContext(gitVersionOptions.WorkingDirectory, gitVersionOptions.AssemblyInfo.EnsureAssemblyInfo, gitVersionOptions.AssemblyInfo.Files.ToArray()));
// }
//}
}

public void UpdateWixVersionFile(VersionVariables variables)
Expand Down
21 changes: 5 additions & 16 deletions src/GitVersionCore/Extensions/GitVersionOptionsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ public static class GitVersionOptionsExtensions
{
public static string GetDotGitDirectory(this GitVersionOptions gitVersionOptions)
{
var dotGitDirectory = !string.IsNullOrWhiteSpace(gitVersionOptions.DynamicGitRepositoryPath)
? gitVersionOptions.DynamicGitRepositoryPath
: Repository.Discover(gitVersionOptions.WorkingDirectory);
var dotGitDirectory = Repository.Discover(gitVersionOptions.WorkingDirectory);

dotGitDirectory = dotGitDirectory?.TrimEnd('/', '\\');
if (string.IsNullOrEmpty(dotGitDirectory))
Expand All @@ -22,20 +20,11 @@ public static string GetDotGitDirectory(this GitVersionOptions gitVersionOptions
: dotGitDirectory;
}

public static string GetProjectRootDirectory(this GitVersionOptions gitVersionOptions)
public static string GetRepositoryWorkingDirectory(this GitVersionOptions gitVersionOptions)
{
if (!string.IsNullOrWhiteSpace(gitVersionOptions.DynamicGitRepositoryPath))
{
return gitVersionOptions.WorkingDirectory;
}

var dotGitDirectory = Repository.Discover(gitVersionOptions.WorkingDirectory);

if (string.IsNullOrEmpty(dotGitDirectory))
throw new DirectoryNotFoundException($"Can't find the .git directory in {dotGitDirectory}");

using var repository = new Repository(dotGitDirectory);
return repository.Info.WorkingDirectory;
//return gitVersionOptions.WorkingDirectory;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thought it made more sense to always ask git what the working directory is, rather than assume its the cli 's working directory (current directory) - given we always resolve the dotGitDirectory.

using var repository = new Repository(gitVersionOptions.DotGitDirectory);
return repository.Info.WorkingDirectory;
}

public static string GetDynamicGitRepositoryPath(this GitVersionOptions gitVersionOptions)
Expand Down
25 changes: 8 additions & 17 deletions src/GitVersionCore/Model/GitVersionOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@ namespace GitVersion
public class GitVersionOptions
{
private Lazy<string> dotGitDirectory;
private Lazy<string> projectRootDirectory;
private Lazy<string> dynamicGitRepositoryPath;
private Lazy<string> gitRepositoryWorkingDirectory;

public GitVersionOptions()
{
WorkingDirectory = System.Environment.CurrentDirectory;
dotGitDirectory = new Lazy<string>(this.GetDotGitDirectory);
projectRootDirectory = new Lazy<string>(this.GetProjectRootDirectory);
dynamicGitRepositoryPath = new Lazy<string>(this.GetDynamicGitRepositoryPath);
gitRepositoryWorkingDirectory = new Lazy<string>(this.GetRepositoryWorkingDirectory);
}

public string[] Args { get; set; }
public string WorkingDirectory { get; set; }

public string DotGitDirectory => dotGitDirectory.Value;
public string ProjectRootDirectory => projectRootDirectory.Value;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ProjectRootDirectory --> renamed to GitRepositoryWorkingDirectory for selfish reasons.

public string DynamicGitRepositoryPath => dynamicGitRepositoryPath.Value;
public string GitRepositoryWorkingDirectory => gitRepositoryWorkingDirectory.Value;
public bool LogToConsole { get; set; } = false;
public string LogFilePath;

public AssemblyInfoData AssemblyInfo { get; } = new AssemblyInfoData();
//public AssemblyInfoData AssemblyInfo { get; } = new AssemblyInfoData();
public AuthenticationInfo Authentication { get; } = new AuthenticationInfo();
public ConfigInfo ConfigInfo { get; } = new ConfigInfo();
public RepositoryInfo RepositoryInfo { get; } = new RepositoryInfo();
Expand All @@ -37,19 +37,10 @@ public GitVersionOptions()
public bool IsVersion;
public bool IsHelp;

public string LogFilePath;
public string ShowVariable;
public string OutputFile;
public ISet<OutputType> Output = new HashSet<OutputType>();
public Verbosity Verbosity = Verbosity.Normal;

[Obsolete]
public string Proj;
[Obsolete]
public string ProjArgs;
[Obsolete]
public string Exec;
[Obsolete]
public string ExecArgs;
}
}
50 changes: 50 additions & 0 deletions src/GitVersionExe/Commands/CalculateCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using GitVersion.Extensions;
using GitVersion.Logging;
using System;
using System.CommandLine;
using System.CommandLine.Invocation;
using System.Threading.Tasks;

namespace GitVersion
{

public class CalculateCommand : Command
{
private readonly Logging.IConsole console;
private readonly IGitVersionTool gitversionTool;
private readonly GitVersionCommandExecutor executor;

public CalculateCommand(Logging.IConsole console, IGitVersionTool gitversionTool, GitVersionCommandExecutor executor) : base("calculate", "Calculates version information from your git repository")
{
this.console = console;
this.gitversionTool = gitversionTool;
this.executor = executor;
this.AddOption(new Option<bool>(
"--normalize",
"Attempt to mutate your git repository so gitversion has enough information (local branches, commit history etc) to calculate."));
this.Handler = CommandHandler.Create<GlobalCommandOptions, bool?>(ExecuteAsync);
}

private async Task<int> ExecuteAsync(GlobalCommandOptions globalOptions, bool? normalize)
{
// The executor wraps execution of the command logic inside somethng that
// will do error handling according to the old behaviour.
return await executor.Execute(globalOptions, async () =>
{
if (normalize ?? false)
{
await Normalize();
}

var variables = this.gitversionTool.CalculateVersionVariables();
console.WriteLine(variables.ToString());
return 0;
});
}

private Task Normalize()
{
throw new NotImplementedException();
}
}
}
Loading