-
Notifications
You must be signed in to change notification settings - Fork 652
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,11 +35,9 @@ public void Prepare() | |
var currentBranch = ResolveCurrentBranch(); | ||
|
||
var dotGitDirectory = gitVersionOptions.DotGitDirectory; | ||
var projectRoot = gitVersionOptions.ProjectRootDirectory; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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}'."); | ||
} | ||
|
@@ -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)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
|
@@ -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}'")) | ||
|
@@ -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 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -93,13 +93,14 @@ public void UpdateAssemblyInfo(VersionVariables variables) | |
{ | ||
var gitVersionOptions = options.Value; | ||
|
||
if (gitVersionOptions.AssemblyInfo.ShouldUpdate) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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)) | ||
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
|
@@ -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; | ||
} | ||
} |
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(); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
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 toGitRepositoryWorkingDirectory
to aid understanding whilst refactoring. It's not a critical change.There was a problem hiding this comment.
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. 🤷♂️