public static VersionVariables ExecuteGitVersion(IFileSystem fileSystem, string targetUrl, string dynamicRepositoryLocation, Authentication authentication, string targetBranch, bool noFetch, string workingDirectory, string commitId) { // Normalise if we are running on build server var gitPreparer = new GitPreparer(targetUrl, dynamicRepositoryLocation, authentication, targetBranch, noFetch, workingDirectory); gitPreparer.Initialise(BuildServerList.GetApplicableBuildServers().Any()); var dotGitDirectory = gitPreparer.GetDotGitDirectory(); var projectRoot = gitPreparer.GetProjectRootDirectory(); Logger.WriteInfo(string.Format("Project root is: " + projectRoot)); if (string.IsNullOrEmpty(dotGitDirectory) || string.IsNullOrEmpty(projectRoot)) { // TODO Link to wiki article throw new Exception(string.Format("Failed to prepare or find the .git directory in path '{0}'.", workingDirectory)); } VersionVariables variables; var versionFinder = new GitVersionFinder(); var configuration = ConfigurationProvider.Provide(projectRoot, fileSystem); using (var repo = RepositoryLoader.GetRepo(dotGitDirectory)) { var gitVersionContext = new GitVersionContext(repo, configuration, commitId: commitId); var semanticVersion = versionFinder.FindVersion(gitVersionContext); var config = gitVersionContext.Configuration; variables = VariableProvider.GetVariablesFor(semanticVersion, config.AssemblyVersioningScheme, config.VersioningMode, config.ContinuousDeploymentFallbackTag, gitVersionContext.IsCurrentCommitTagged); } return(variables); }
public static SemanticVersion GetVersion(string gitDirectory) { using (var repo = RepositoryLoader.GetRepo(gitDirectory)) { var branch = repo.Head; if (branch.Tip == null) { throw new WarningException("No Tip found. Has repo been initialized?"); } var ticks = DirectoryDateFinder.GetLastDirectoryWrite(gitDirectory); var key = string.Format("{0}:{1}:{2}", repo.Head.CanonicalName, repo.Head.Tip.Sha, ticks); CachedVersion cachedVersion; SemanticVersion versionAndBranch; if (versionCacheVersions.TryGetValue(key, out cachedVersion)) { Logger.WriteInfo("Version read from cache."); if (cachedVersion.Timestamp == ticks) { versionAndBranch = cachedVersion.SemanticVersion; } else { Logger.WriteInfo("Change detected. flushing cache."); versionAndBranch = cachedVersion.SemanticVersion = GetSemanticVersion(repo); } } else { Logger.WriteInfo("Version not in cache. Calculating version."); versionAndBranch = GetSemanticVersion(repo); versionCacheVersions[key] = new CachedVersion { SemanticVersion = versionAndBranch, Timestamp = ticks }; } return(versionAndBranch); } }
public static void Run(Arguments arguments, IFileSystem fileSystem) { var gitPreparer = new GitPreparer(arguments); gitPreparer.InitialiseDynamicRepositoryIfNeeded(); var dotGitDirectory = gitPreparer.GetDotGitDirectory(); if (string.IsNullOrEmpty(dotGitDirectory)) { throw new Exception(string.Format("Failed to prepare or find the .git directory in path '{0}'", arguments.TargetPath)); } var applicableBuildServers = GetApplicableBuildServers(arguments.Authentication).ToList(); foreach (var buildServer in applicableBuildServers) { buildServer.PerformPreProcessingSteps(dotGitDirectory, arguments.NoFetch); } VersionVariables variables; var versionFinder = new GitVersionFinder(); var configuration = ConfigurationProvider.Provide(dotGitDirectory, fileSystem); using (var repo = RepositoryLoader.GetRepo(dotGitDirectory)) { var gitVersionContext = new GitVersionContext(repo, configuration, commitId: arguments.CommitId); var semanticVersion = versionFinder.FindVersion(gitVersionContext); var config = gitVersionContext.Configuration; variables = VariableProvider.GetVariablesFor(semanticVersion, config.AssemblyVersioningScheme, config.VersioningMode, config.ContinuousDeploymentFallbackTag, gitVersionContext.IsCurrentCommitTagged); } if (arguments.Output == OutputType.BuildServer) { foreach (var buildServer in applicableBuildServers) { buildServer.WriteIntegration(Console.WriteLine, variables); } } if (arguments.Output == OutputType.Json) { switch (arguments.ShowVariable) { case null: Console.WriteLine(JsonOutputFormatter.ToJson(variables)); break; default: string part; if (!variables.TryGetValue(arguments.ShowVariable, out part)) { throw new WarningException(string.Format("'{0}' variable does not exist", arguments.ShowVariable)); } Console.WriteLine(part); break; } } using (var assemblyInfoUpdate = new AssemblyInfoFileUpdate(arguments, arguments.TargetPath, variables, fileSystem)) { var execRun = RunExecCommandIfNeeded(arguments, arguments.TargetPath, variables); var msbuildRun = RunMsBuildIfNeeded(arguments, arguments.TargetPath, variables); if (!execRun && !msbuildRun) { assemblyInfoUpdate.DoNotRestoreAssemblyInfo(); //TODO Put warning back //if (!context.CurrentBuildServer.IsRunningInBuildAgent()) //{ // Console.WriteLine("WARNING: Not running in build server and /ProjectFile or /Exec arguments not passed"); // Console.WriteLine(); // Console.WriteLine("Run GitVersion.exe /? for help"); //} } } }
static int Run() { try { Arguments arguments; var argumentsWithoutExeName = GetArgumentsWithoutExeName(); try { arguments = ArgumentParser.ParseArguments(argumentsWithoutExeName); } catch (Exception) { Console.WriteLine("Failed to parse arguments: {0}", string.Join(" ", argumentsWithoutExeName)); HelpWriter.Write(); return(1); } if (arguments.IsHelp) { HelpWriter.Write(); return(0); } if (!string.IsNullOrEmpty(arguments.Proj) || !string.IsNullOrEmpty(arguments.Exec)) { arguments.Output = OutputType.BuildServer; } ConfigureLogging(arguments); var gitPreparer = new GitPreparer(arguments); var gitDirectory = gitPreparer.Prepare(); if (string.IsNullOrEmpty(gitDirectory)) { Console.Error.WriteLine("Failed to prepare or find the .git directory in path '{0}'", arguments.TargetPath); return(1); } var fileSystem = new FileSystem(); if (arguments.Init) { ConfigurationProvider.WriteSample(gitDirectory, fileSystem); return(0); } var workingDirectory = Directory.GetParent(gitDirectory).FullName; Logger.WriteInfo("Working directory: " + workingDirectory); var applicableBuildServers = GetApplicableBuildServers(arguments.Authentication).ToList(); foreach (var buildServer in applicableBuildServers) { buildServer.PerformPreProcessingSteps(gitDirectory); } SemanticVersion semanticVersion; var versionFinder = new GitVersionFinder(); var configuration = ConfigurationProvider.Provide(gitDirectory, fileSystem); using (var repo = RepositoryLoader.GetRepo(gitDirectory)) { var gitVersionContext = new GitVersionContext(repo, configuration); semanticVersion = versionFinder.FindVersion(gitVersionContext); } if (arguments.Output == OutputType.BuildServer) { foreach (var buildServer in applicableBuildServers) { buildServer.WriteIntegration(semanticVersion, Console.WriteLine); } } var variables = VariableProvider.GetVariablesFor(semanticVersion, configuration); if (arguments.Output == OutputType.Json) { switch (arguments.VersionPart) { case null: Console.WriteLine(JsonOutputFormatter.ToJson(variables)); break; default: string part; if (!variables.TryGetValue(arguments.VersionPart, out part)) { throw new WarningException(string.Format("Could not extract '{0}' from the available parts.", arguments.VersionPart)); } Console.WriteLine(part); break; } } if (!string.IsNullOrWhiteSpace(arguments.AssemblyVersionFormat) && !variables.ContainsKey(arguments.AssemblyVersionFormat)) { Console.WriteLine("Unrecognised AssemblyVersionFormat argument. Valid values for this argument are: {0}", string.Join(" ", variables.Keys.OrderBy(a => a))); HelpWriter.Write(); return(1); } using (var assemblyInfoUpdate = new AssemblyInfoFileUpdate(arguments, workingDirectory, variables, fileSystem)) { var execRun = RunExecCommandIfNeeded(arguments, workingDirectory, variables); var msbuildRun = RunMsBuildIfNeeded(arguments, workingDirectory, variables); if (!execRun && !msbuildRun) { assemblyInfoUpdate.DoNotRestoreAssemblyInfo(); //TODO Put warning back //if (!context.CurrentBuildServer.IsRunningInBuildAgent()) //{ // Console.WriteLine("WARNING: Not running in build server and /ProjectFile or /Exec arguments not passed"); // Console.WriteLine(); // Console.WriteLine("Run GitVersion.exe /? for help"); //} } } if (gitPreparer.IsDynamicGitRepository) { DeleteHelper.DeleteGitRepository(gitPreparer.DynamicGitRepositoryPath); } } catch (WarningException exception) { var error = string.Format("An error occurred:\r\n{0}", exception.Message); Logger.WriteWarning(error); return(1); } catch (Exception exception) { var error = string.Format("An unexpected error occurred:\r\n{0}", exception); Logger.WriteError(error); return(1); } return(0); }
static void Main() { int?exitCode = null; try { Arguments arguments; var argumentsWithoutExeName = GetArgumentsWithoutExeName(); try { arguments = ArgumentParser.ParseArguments(argumentsWithoutExeName); } catch (Exception) { Console.WriteLine("Failed to parse arguments: {0}", string.Join(" ", argumentsWithoutExeName)); HelpWriter.Write(); return; } if (arguments.IsHelp) { HelpWriter.Write(); return; } if (!string.IsNullOrEmpty(arguments.Proj) || !string.IsNullOrEmpty(arguments.Exec)) { arguments.Output = OutputType.BuildServer; } ConfigureLogging(arguments); var gitPreparer = new GitPreparer(arguments); var gitDirectory = gitPreparer.Prepare(); if (string.IsNullOrEmpty(gitDirectory)) { Console.Error.WriteLine("Failed to prepare or find the .git directory in path '{0}'", arguments.TargetPath); Environment.Exit(1); } var workingDirectory = Directory.GetParent(gitDirectory).FullName; Logger.WriteInfo("Working directory: " + workingDirectory); var applicableBuildServers = GetApplicableBuildServers(arguments.Authentication).ToList(); foreach (var buildServer in applicableBuildServers) { buildServer.PerformPreProcessingSteps(gitDirectory); } SemanticVersion semanticVersion; using (var repo = RepositoryLoader.GetRepo(gitDirectory)) { semanticVersion = GitVersionFinder.GetSemanticVersion(repo); } if (arguments.Output == OutputType.BuildServer) { foreach (var buildServer in applicableBuildServers) { buildServer.WriteIntegration(semanticVersion, Console.WriteLine); } } var variables = VariableProvider.GetVariablesFor(semanticVersion); if (arguments.Output == OutputType.Json) { switch (arguments.VersionPart) { case null: Console.WriteLine(JsonOutputFormatter.ToJson(variables)); break; default: string part; if (!variables.TryGetValue(arguments.VersionPart, out part)) { throw new WarningException(string.Format("Could not extract '{0}' from the available parts.", arguments.VersionPart)); } Console.WriteLine(part); break; } } using (var assemblyInfoUpdate = new AssemblyInfoFileUpdate(arguments, workingDirectory, variables)) { var execRun = RunExecCommandIfNeeded(arguments, workingDirectory, variables); var msbuildRun = RunMsBuildIfNeeded(arguments, workingDirectory, variables); if (!execRun && !msbuildRun) { assemblyInfoUpdate.DoNotRestoreAssemblyInfo(); //TODO Put warning back //if (!context.CurrentBuildServer.IsRunningInBuildAgent()) //{ // Console.WriteLine("WARNING: Not running in build server and /ProjectFile or /Exec arguments not passed"); // Console.WriteLine(); // Console.WriteLine("Run GitVersion.exe /? for help"); //} } } if (gitPreparer.IsDynamicGitRepository) { DeleteHelper.DeleteGitRepository(gitPreparer.DynamicGitRepositoryPath); } } catch (WarningException exception) { var error = string.Format("An error occurred:\r\n{0}", exception.Message); Logger.WriteWarning(error); exitCode = 1; } catch (Exception exception) { var error = string.Format("An unexpected error occurred:\r\n{0}", exception); Logger.WriteError(error); exitCode = 1; } if (Debugger.IsAttached) { Console.ReadKey(); } if (!exitCode.HasValue) { exitCode = 0; } else { // Dump log to console if we fail to complete successfully Console.Write(log.ToString()); } Environment.Exit(exitCode.Value); }