string GetGitInfoFromUrl() { var gitDirectory = Path.Combine(arguments.TargetPath, "_dynamicrepository", ".git"); if (Directory.Exists(gitDirectory)) { Logger.WriteInfo(string.Format("Deleting existing .git folder from '{0}' to force new checkout from url", gitDirectory)); DeleteHelper.DeleteGitRepository(gitDirectory); } Credentials credentials = null; var authentication = arguments.Authentication; if (!string.IsNullOrWhiteSpace(authentication.Username) && !string.IsNullOrWhiteSpace(authentication.Password)) { Logger.WriteInfo(string.Format("Setting up credentials using name '{0}'", authentication.Username)); credentials = new UsernamePasswordCredentials { Username = authentication.Username, Password = authentication.Password }; } Logger.WriteInfo(string.Format("Retrieving git info from url '{0}'", arguments.TargetUrl)); Repository.Clone(arguments.TargetUrl, gitDirectory, new CloneOptions { IsBare = true, Checkout = false, Credentials = credentials }); if (!string.IsNullOrWhiteSpace(arguments.TargetBranch)) { // Normalize (download branches) before using the branch GitHelper.NormalizeGitDirectory(gitDirectory, arguments.Authentication); using (var repository = new Repository(gitDirectory)) { var targetBranchName = string.Format("refs/heads/{0}", arguments.TargetBranch); if (!string.Equals(repository.Head.CanonicalName, targetBranchName)) { Logger.WriteInfo(string.Format("Switching to branch '{0}'", arguments.TargetBranch)); repository.Refs.UpdateTarget("HEAD", targetBranchName); } repository.CheckoutFilesIfExist("NextVersion.txt"); } } DynamicGitRepositoryPath = gitDirectory; return(gitDirectory); }
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).ToList(); foreach (var buildServer in applicableBuildServers) { buildServer.PerformPreProcessingSteps(gitDirectory); } var semanticVersion = VersionCache.GetVersion(gitDirectory); 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; } Environment.Exit(exitCode.Value); }
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); }
string GetGitInfoFromUrl() { var gitRootDirectory = Path.Combine(arguments.TargetPath, "_dynamicrepository"); var gitDirectory = Path.Combine(gitRootDirectory, ".git"); if (Directory.Exists(gitRootDirectory)) { Logger.WriteInfo(string.Format("Deleting existing .git folder from '{0}' to force new checkout from url", gitRootDirectory)); DeleteHelper.DeleteGitRepository(gitRootDirectory); } Credentials credentials = null; var authentication = arguments.Authentication; if (!string.IsNullOrWhiteSpace(authentication.Username) && !string.IsNullOrWhiteSpace(authentication.Password)) { Logger.WriteInfo(string.Format("Setting up credentials using name '{0}'", authentication.Username)); credentials = new UsernamePasswordCredentials { Username = authentication.Username, Password = authentication.Password }; } Logger.WriteInfo(string.Format("Retrieving git info from url '{0}'", arguments.TargetUrl)); Repository.Clone(arguments.TargetUrl, gitDirectory, new CloneOptions { IsBare = true, Checkout = false, CredentialsProvider = (url, usernameFromUrl, types) => credentials }); if (!string.IsNullOrWhiteSpace(arguments.TargetBranch)) { // Normalize (download branches) before using the branch GitHelper.NormalizeGitDirectory(gitDirectory, arguments.Authentication); using (var repository = new Repository(gitDirectory)) { Reference newHead = null; var localReference = GetLocalReference(repository, arguments.TargetBranch); if (localReference != null) { newHead = localReference; } if (newHead == null) { var remoteReference = GetRemoteReference(repository, arguments.TargetBranch, arguments.TargetUrl); if (remoteReference != null) { repository.Network.Fetch(arguments.TargetUrl, new[] { string.Format("{0}:{1}", remoteReference.CanonicalName, arguments.TargetBranch) }); newHead = repository.Refs[string.Format("refs/heads/{0}", arguments.TargetBranch)]; } } if (newHead != null) { Logger.WriteInfo(string.Format("Switching to branch '{0}'", arguments.TargetBranch)); repository.Refs.UpdateTarget(repository.Refs.Head, newHead); } repository.CheckoutFilesIfExist("NextVersion.txt"); } } DynamicGitRepositoryPath = gitDirectory; return(gitDirectory); }