public void Worktree_Support(bool detachedHead) { var workingCopyVersion = new VersionOptions { Version = SemanticVersion.Parse("2.3"), }; this.WriteVersionFile(workingCopyVersion); this.InitializeSourceControl(); var oracleOriginal = new VersionOracle(this.Context); this.AddCommits(); string workTreePath = this.CreateDirectoryForNewRepo(); Directory.Delete(workTreePath); if (detachedHead) { this.LibGit2Repository.Worktrees.Add("HEAD~1", "myworktree", workTreePath, isLocked: false); } else { this.LibGit2Repository.Branches.Add("wtbranch", "HEAD~1"); this.LibGit2Repository.Worktrees.Add("wtbranch", "myworktree", workTreePath, isLocked: false); } var context = this.CreateGitContext(workTreePath); var oracleWorkTree = new VersionOracle(context); Assert.Equal(oracleOriginal.Version, oracleWorkTree.Version); Assert.True(context.TrySelectCommit("HEAD")); Assert.True(context.TrySelectCommit(this.LibGit2Repository.Head.Tip.Sha)); }
public void NotRepo() { // Seems safe to assume a temporary path is not a Git directory. var oracle = VersionOracle.Create(Path.GetTempPath()); Assert.Equal(0, oracle.VersionHeight); }
public void VersionHeightResetsWithVersionSpecChanges(string initial, string next) { var options = new VersionOptions { Version = SemanticVersion.Parse(initial), }; this.WriteVersionFile(options); this.InitializeSourceControl(); this.AddCommits(10); var oracle = VersionOracle.Create(this.RepoPath); Assert.Equal(11, oracle.VersionHeight); Assert.Equal(11, this.Repo.Head.GetVersionHeight()); options.Version = SemanticVersion.Parse(next); this.WriteVersionFile(options); oracle = VersionOracle.Create(this.RepoPath); Assert.Equal(1, oracle.VersionHeight); Assert.Equal(1, this.Repo.Head.GetVersionHeight()); foreach (var commit in this.Repo.Head.Commits) { var versionFromId = commit.GetIdAsVersion(); Assert.Contains(commit, this.Repo.GetCommitsFromVersion(versionFromId)); } }
public void CanUseGitProjectRelativePathWithGitRepoRoot() { VersionOptions rootVersion = new VersionOptions { Version = SemanticVersion.Parse("1.1"), }; VersionOptions projectVersion = new VersionOptions { Version = SemanticVersion.Parse("2.2"), }; string childProjectRelativeDir = "ChildProject1"; string childProjectAbsoluteDir = Path.Combine(this.RepoPath, childProjectRelativeDir); this.WriteVersionFile(rootVersion); this.WriteVersionFile(projectVersion, childProjectRelativeDir); this.InitializeSourceControl(); // Check Root Version. Root version will be used var oracle = new VersionOracle(this.Context); Assert.Equal("1.1", oracle.MajorMinorVersion.ToString()); // Check ChildProject with projectRelativeDir, with version file. Child project version will be used. this.Context.RepoRelativeProjectDirectory = childProjectRelativeDir; oracle = new VersionOracle(this.Context); Assert.Equal("2.2", oracle.MajorMinorVersion.ToString()); // Check ChildProject withOUT Version file. Root version will be used. this.Context.RepoRelativeProjectDirectory = "otherChildProject"; oracle = new VersionOracle(this.Context); Assert.Equal("1.1", oracle.MajorMinorVersion.ToString()); }
public void GetVersionManaged() { using var context = GitContext.Create(GetPath(this.ProjectDirectory), writable: false); var oracle = new VersionOracle(context, cloudBuild: null); this.Version = oracle.Version; }
public void NotRepo() { // Seems safe to assume the system directory is not a repository. var oracle = VersionOracle.Create(Environment.SystemDirectory); Assert.Equal(0, oracle.VersionHeight); }
public void VersionHeightResetsWithVersionSpecChanges(string initial, string next) { var options = new VersionOptions { Version = SemanticVersion.Parse(initial), }; this.WriteVersionFile(options); this.InitializeSourceControl(); this.AddCommits(10); var oracle = new VersionOracle(this.Context); Assert.Equal(11, oracle.VersionHeight); options.Version = SemanticVersion.Parse(next); this.WriteVersionFile(options); this.SetContextToHead(); oracle = new VersionOracle(this.Context); Assert.Equal(1, oracle.VersionHeight); if (this.Context is Nerdbank.GitVersioning.LibGit2.LibGit2Context libgit2Context) { foreach (var commit in libgit2Context.Repository.Head.Commits) { var versionFromId = this.GetVersion(committish: commit.Sha); Assert.Contains(commit, Nerdbank.GitVersioning.LibGit2.LibGit2GitExtensions.GetCommitsFromVersion(libgit2Context, versionFromId)); } } }
private static ExitCodes OnGetVersionCommand(string projectPath, string format, string versionOrRef) { if (string.IsNullOrEmpty(format)) { format = DefaultVersionInfoFormat; } if (string.IsNullOrEmpty(versionOrRef)) { versionOrRef = DefaultRef; } string searchPath = GetSpecifiedOrCurrentDirectoryPath(projectPath); var repository = GitExtensions.OpenGitRepo(searchPath); if (repository == null) { Console.Error.WriteLine("No git repo found at or above: \"{0}\"", searchPath); return(ExitCodes.NoGitRepo); } LibGit2Sharp.GitObject refObject = null; try { repository.RevParse(versionOrRef, out var reference, out refObject); } catch (LibGit2Sharp.NotFoundException) { } var commit = refObject as LibGit2Sharp.Commit; if (commit == null) { Console.Error.WriteLine("rev-parse produced no commit for {0}", versionOrRef); return(ExitCodes.BadGitRef); } var oracle = new VersionOracle(searchPath, repository, commit, CloudBuild.Active); switch (format.ToLowerInvariant()) { case "text": Console.WriteLine("Version: {0}", oracle.Version); Console.WriteLine("AssemblyVersion: {0}", oracle.AssemblyVersion); Console.WriteLine("AssemblyInformationalVersion: {0}", oracle.AssemblyInformationalVersion); Console.WriteLine("NuGet package Version: {0}", oracle.NuGetPackageVersion); Console.WriteLine("NPM package Version: {0}", oracle.NpmPackageVersion); break; case "json": Console.WriteLine(JsonConvert.SerializeObject(oracle, Formatting.Indented)); break; default: Console.Error.WriteLine("Unsupported format: {0}", format); return(ExitCodes.UnsupportedFormat); } return(ExitCodes.OK); }
public static VersionOracle GitVersioningGetVersion(this ICakeContext context, string projectDirectory = ".") { var fullProjectDirectory = (new DirectoryInfo(projectDirectory)).FullName; GitExtensions.HelpFindLibGit2NativeBinaries(Path.GetDirectoryName(Assembly.GetAssembly(typeof(GitVersioningAliases)).Location)); return(VersionOracle.Create(fullProjectDirectory, null, CloudBuild.Active)); }
public void NotRepo() { // Seems safe to assume a temporary path is not a Git directory. var context = this.CreateGitContext(Path.GetTempPath()); var oracle = new VersionOracle(context); Assert.Equal(0, oracle.VersionHeight); }
/// <summary> /// Encodes HEAD (or a modified working copy) from history in a <see cref="Version"/> /// so that the original commit can be found later. /// </summary> /// <param name="repoRelativeProjectDirectory">The repo-relative project directory for which to calculate the version.</param> /// <returns> /// A version whose <see cref="Version.Build"/> and /// <see cref="Version.Revision"/> components are calculated based on the commit. /// </returns> /// <remarks> /// In the returned version, the <see cref="Version.Build"/> component is /// the height of the git commit while the <see cref="Version.Revision"/> /// component is the first four bytes of the git commit id (forced to be a positive integer). /// </remarks> protected System.Version GetIdAsVersion(string repoRelativeProjectDirectory = null) { VersionOracle oracle = new VersionOracle( repoRelativeProjectDirectory == null ? this.Repo.Info.WorkingDirectory : Path.Combine(this.Repo.Info.WorkingDirectory, repoRelativeProjectDirectory), this.Repo, null); return(oracle.Version); }
public void VersionJsonWithoutVersion() { File.WriteAllText(Path.Combine(this.RepoPath, VersionFile.JsonFileName), "{}"); this.InitializeSourceControl(); var oracle = VersionOracle.Create(this.RepoPath); Assert.Equal(0, oracle.Version.Major); Assert.Equal(0, oracle.Version.Minor); }
protected static System.Version GetIdAsVersion(Repository repository, Commit commit, string repoRelativeProjectDirectory = null) { VersionOracle oracle = new VersionOracle( repoRelativeProjectDirectory == null ? repository.Info.WorkingDirectory : Path.Combine(repository.Info.WorkingDirectory, repoRelativeProjectDirectory), repository, commit, null); return(oracle.Version); }
public void GetVersion_PackedHead() { using var expandedRepo = TestUtilities.ExtractRepoArchive("PackedHeadRef"); this.Context = this.CreateGitContext(Path.Combine(expandedRepo.RepoPath)); var oracle = new VersionOracle(this.Context); Assert.Equal("1.0.1", oracle.SimpleVersion.ToString()); this.Context.TrySelectCommit("HEAD"); Assert.Equal("1.0.1", oracle.SimpleVersion.ToString()); }
public void VersionJsonWithSingleIntegerForVersion() { File.WriteAllText(Path.Combine(this.RepoPath, VersionFile.JsonFileName), @"{""version"":""3""}"); this.InitializeSourceControl(); var ex = Assert.Throws <FormatException>(() => VersionOracle.Create(this.RepoPath)); Assert.Contains(this.Repo.Head.Commits.First().Sha, ex.Message); Assert.Contains("\"3\"", ex.InnerException.Message); this.Logger.WriteLine(ex.ToString()); }
public override bool Execute() { try { GitExtensions.TryHelpFindLibGit2NativeBinaries(this.TargetsPath); var cloudBuild = CloudBuild.Active; var oracle = VersionOracle.Create(Directory.GetCurrentDirectory(), this.GitRepoRoot, cloudBuild); if (!string.IsNullOrEmpty(this.DefaultPublicRelease)) { oracle.PublicRelease = string.Equals(this.DefaultPublicRelease, "true", StringComparison.OrdinalIgnoreCase); } if (this.BuildMetadata != null) { oracle.BuildMetadata.AddRange(this.BuildMetadata); } this.PublicRelease = oracle.PublicRelease; this.Version = oracle.Version.ToString(); this.AssemblyVersion = oracle.AssemblyVersion.ToString(); this.AssemblyFileVersion = oracle.AssemblyFileVersion.ToString(); this.AssemblyInformationalVersion = oracle.AssemblyInformationalVersion; this.SimpleVersion = oracle.SimpleVersion.ToString(); this.MajorMinorVersion = oracle.MajorMinorVersion.ToString(); this.BuildNumber = oracle.BuildNumber; this.PrereleaseVersion = oracle.PrereleaseVersion; this.GitCommitId = oracle.GitCommitId; this.GitCommitIdShort = oracle.GitCommitIdShort; this.GitVersionHeight = oracle.VersionHeight; this.BuildMetadataFragment = oracle.BuildMetadataFragment; this.CloudBuildNumber = oracle.CloudBuildNumberEnabled ? oracle.CloudBuildNumber : null; this.NuGetPackageVersion = oracle.NuGetPackageVersion; this.NpmPackageVersion = oracle.NpmPackageVersion; if (oracle.CloudBuildVersionVarsEnabled) { this.CloudBuildVersionVars = oracle.CloudBuildVersionVars .Select(item => new TaskItem(item.Key, new Dictionary <string, string> { { "Value", item.Value } })) .ToArray(); } } catch (ArgumentOutOfRangeException ex) { Log.LogErrorFromException(ex); return(false); } return(true); }
public void Informational_version_has_three_components_when_two_component_version_is_used() { var versionOptions = new VersionOptions { Version = SemanticVersion.Parse("1.2") }; this.WriteVersionFile(versionOptions); this.InitializeSourceControl(); this.AddCommits(20); var oracle = new VersionOracle(this.Context); Assert.StartsWith("1.2.21+", oracle.AssemblyInformationalVersion); }
[InlineData("WebDriver", "src/version.json", "0.149.20")] // internal repository public void GetNbgvVersionTest(string repositoryName, string versionPath, string expectedVersion) { string path = Path.Combine( Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), @"Source\Repos", repositoryName); var oracleA = VersionOracle.Create(Path.Combine(path, Path.GetDirectoryName(versionPath))); var version = oracleA.CloudBuildNumber; Assert.Equal(expectedVersion, version); }
public void GetCommitsFromVersion_MatchesOnEitherEndian() { this.InitializeSourceControl(); Commit commit = this.WriteVersionFile(new VersionOptions { Version = SemanticVersion.Parse("1.2"), GitCommitIdShortAutoMinimum = 4 }); Version originalVersion = new VersionOracle(this.Context).Version; Version swappedEndian = new Version(originalVersion.Major, originalVersion.Minor, originalVersion.Build, BinaryPrimitives.ReverseEndianness((ushort)originalVersion.Revision)); ushort twoBytesFromCommitId = checked ((ushort)originalVersion.Revision); Assert.Contains(commit, LibGit2GitExtensions.GetCommitsFromVersion(this.Context, originalVersion)); Assert.Contains(commit, LibGit2GitExtensions.GetCommitsFromVersion(this.Context, swappedEndian)); }
public void Submodule_RecognizedWithCorrectVersion() { using (var expandedRepo = TestUtilities.ExtractRepoArchive("submodules")) { this.Repo = new Repository(expandedRepo.RepoPath); var oracleA = VersionOracle.Create(Path.Combine(expandedRepo.RepoPath, "a")); Assert.Equal("1.3.1", oracleA.SimpleVersion.ToString()); Assert.Equal("e238b03e75", oracleA.GitCommitIdShort); var oracleB = VersionOracle.Create(Path.Combine(expandedRepo.RepoPath, "b", "projB")); Assert.Equal("2.5.2", oracleB.SimpleVersion.ToString()); Assert.Equal("3ea7f010c3", oracleB.GitCommitIdShort); } }
public void NpmPackageVersionIsSemVer2() { VersionOptions workingCopyVersion = new VersionOptions { Version = SemanticVersion.Parse("7.8.9-foo.25"), SemVer1NumericIdentifierPadding = 2 }; this.WriteVersionFile(workingCopyVersion); this.InitializeSourceControl(); var oracle = VersionOracle.Create(this.RepoPath); oracle.PublicRelease = true; Assert.Equal("7.8.9-foo.25", oracle.NpmPackageVersion); }
public void DefaultNuGetPackageVersionIsSemVer1NonPublicRelease() { VersionOptions workingCopyVersion = new VersionOptions { Version = SemanticVersion.Parse("7.8.9-foo.25"), SemVer1NumericIdentifierPadding = 2, }; this.WriteVersionFile(workingCopyVersion); this.InitializeSourceControl(); var oracle = VersionOracle.Create(this.RepoPath); oracle.PublicRelease = false; Assert.Equal($"7.8.9-foo-25-g{this.CommitIdShort}", oracle.NuGetPackageVersion); }
public void DefaultNuGetPackageVersionIsSemVer1PublicRelease() { VersionOptions workingCopyVersion = new VersionOptions { Version = SemanticVersion.Parse("7.8.9-foo.25"), SemVer1NumericIdentifierPadding = 2, }; this.WriteVersionFile(workingCopyVersion); this.InitializeSourceControl(); var oracle = new VersionOracle(this.Context); oracle.PublicRelease = true; Assert.Equal($"7.8.9-foo-25", oracle.NuGetPackageVersion); }
public void SemVer1PrereleaseConversion(string semVer2, string semVer1) { VersionOptions workingCopyVersion = new VersionOptions { Version = SemanticVersion.Parse(semVer2), BuildNumberOffset = 2, }; this.WriteVersionFile(workingCopyVersion); this.InitializeSourceControl(); var oracle = VersionOracle.Create(this.RepoPath); oracle.PublicRelease = true; Assert.Equal(semVer1, oracle.SemVer1); }
public void SemVer1PrereleaseConversionPadding() { VersionOptions workingCopyVersion = new VersionOptions { Version = SemanticVersion.Parse("7.8.9-foo.25"), BuildNumberOffset = 2, SemVer1NumericIdentifierPadding = 3, }; this.WriteVersionFile(workingCopyVersion); this.InitializeSourceControl(); var oracle = VersionOracle.Create(this.RepoPath); oracle.PublicRelease = true; Assert.Equal("7.8.9-foo-025", oracle.SemVer1); }
public void Submodule_RecognizedWithCorrectVersion() { using (var expandedRepo = TestUtilities.ExtractRepoArchive("submodules")) { this.Context = this.CreateGitContext(expandedRepo.RepoPath); this.Context.RepoRelativeProjectDirectory = "a"; var oracleA = new VersionOracle(this.Context); Assert.Equal("1.3.1", oracleA.SimpleVersion.ToString()); Assert.Equal("e238b03e75", oracleA.GitCommitIdShort); this.Context.RepoRelativeProjectDirectory = Path.Combine("b", "projB"); var oracleB = new VersionOracle(this.Context); Assert.Equal("2.5.2", oracleB.SimpleVersion.ToString()); Assert.Equal("3ea7f010c3", oracleB.GitCommitIdShort); } }
public void SemVerStableNonPublicVersion() { var workingCopyVersion = new VersionOptions { Version = SemanticVersion.Parse("2.3"), }; this.WriteVersionFile(workingCopyVersion); this.InitializeSourceControl(); var oracle = VersionOracle.Create(this.RepoPath); oracle.PublicRelease = false; Assert.Matches(@"^2.3.1-[^g]{10}$", oracle.SemVer1); Assert.Matches(@"^2.3.1-g[a-f0-9]{10}$", oracle.SemVer2); Assert.Matches(@"^2.3.1-g[a-f0-9]{10}$", oracle.NuGetPackageVersion); Assert.Matches(@"^2.3.1-g[a-f0-9]{10}$", oracle.ChocolateyPackageVersion); }
public void SemVerStableNonPublicVersionShortened() { var workingCopyVersion = new VersionOptions { Version = SemanticVersion.Parse("2.3"), GitCommitIdShortFixedLength = 7 }; this.WriteVersionFile(workingCopyVersion); this.InitializeSourceControl(); var oracle = new VersionOracle(this.Context); oracle.PublicRelease = false; Assert.Matches(@"^2.3.1-[^g]{7}$", oracle.SemVer1); Assert.Matches(@"^2.3.1-g[a-f0-9]{7}$", oracle.SemVer2); Assert.Matches(@"^2.3.1-g[a-f0-9]{7}$", oracle.NuGetPackageVersion); Assert.Matches(@"^2.3.1-g[a-f0-9]{7}$", oracle.ChocolateyPackageVersion); }
public void CanSetSemVer2ForNuGetPackageVersionNonPublicRelease() { VersionOptions workingCopyVersion = new VersionOptions { Version = SemanticVersion.Parse("7.8.9-foo.25"), NuGetPackageVersion = new VersionOptions.NuGetPackageVersionOptions { SemVer = 2, } }; this.WriteVersionFile(workingCopyVersion); this.InitializeSourceControl(); var oracle = VersionOracle.Create(this.RepoPath); oracle.PublicRelease = false; Assert.Equal($"7.8.9-foo.25.g{this.CommitIdShort}", oracle.NuGetPackageVersion); }
public void CanSetSemVer2ForNuGetPackageVersionPublicRelease() { VersionOptions workingCopyVersion = new VersionOptions { Version = SemanticVersion.Parse("7.8.9-foo.25"), NuGetPackageVersion = new VersionOptions.NuGetPackageVersionOptions { SemVer = 2, } }; this.WriteVersionFile(workingCopyVersion); this.InitializeSourceControl(); var oracle = new VersionOracle(this.Context); oracle.PublicRelease = true; Assert.Equal($"7.8.9-foo.25", oracle.NuGetPackageVersion); }