Exemplo n.º 1
0
        public void GitPreparerShouldNotFailWhenTargetPathNotInitialized()
        {
            var targetUrl = "https://github.com/GitTools/GitVersion.git";

            var gitVersionOptions = new GitVersionOptions
            {
                RepositoryInfo   = { TargetUrl = targetUrl },
                WorkingDirectory = null
            };

            Should.NotThrow(() =>
            {
                sp = GetServiceProvider(gitVersionOptions);

                sp.GetService <IGitPreparer>();
            });
        }
Exemplo n.º 2
0
        public void GetProjectRootDirectoryNoWorktree()
        {
            using var fixture = new EmptyRepositoryFixture();
            var targetUrl = "https://github.com/GitTools/GitVersion.git";

            var gitVersionOptions = new GitVersionOptions
            {
                RepositoryInfo   = { TargetUrl = targetUrl },
                WorkingDirectory = fixture.RepositoryPath
            };

            sp = GetServiceProvider(gitVersionOptions);

            var expectedPath = fixture.RepositoryPath.TrimEnd('/', '\\');

            gitVersionOptions.ProjectRootDirectory.TrimEnd('/', '\\').ShouldBe(expectedPath);
        }
Exemplo n.º 3
0
        public static string GetProjectRootDirectory(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);
        }
Exemplo n.º 4
0
        public void DynamicRepositoriesShouldNotErrorWithFailedToFindGitDirectory()
        {
            using var fixture = new EmptyRepositoryFixture();
            fixture.Repository.MakeACommit();

            var gitVersionOptions = new GitVersionOptions
            {
                WorkingDirectory = fixture.RepositoryPath,
                RepositoryInfo   =
                {
                    TargetUrl    = "https://github.com/GitTools/GitVersion.git",
                    TargetBranch = "refs/head/master"
                }
            };

            var gitVersionCalculator = GetGitVersionCalculator(gitVersionOptions, repository: fixture.Repository);

            gitPreparer.Prepare();
            gitVersionCalculator.CalculateVersionVariables();
        }
Exemplo n.º 5
0
        /// <summary>
        /// Simulates running on build server
        /// </summary>
        public static void InitializeRepo(this RemoteRepositoryFixture fixture)
        {
            var gitVersionOptions = new GitVersionOptions
            {
                WorkingDirectory = fixture.LocalRepositoryFixture.RepositoryPath
            };
            var options = Options.Create(gitVersionOptions);

            var environment = new TestEnvironment();

            environment.SetEnvironmentVariable(AzurePipelines.EnvironmentVariableName, "true");

            var serviceProvider = ConfigureServices(services =>
            {
                services.AddSingleton(options);
                services.AddSingleton(environment);
            });

            var gitPreparer = serviceProvider.GetService <IGitPreparer>();

            gitPreparer.Prepare();
        }
Exemplo n.º 6
0
        private static IServiceProvider BuildServiceProvider(GitVersionTaskBase task)
        {
            var services = new ServiceCollection();

            var gitVersionOptions = new GitVersionOptions
            {
                WorkingDirectory = task.SolutionDirectory,
            };

            gitVersionOptions.Output.Add(OutputType.BuildServer);

            services.AddSingleton(Options.Create(gitVersionOptions));
            services.AddModule(new GitVersionTaskModule());

            services.AddSingleton <IConsole>(new MsBuildAdapter(task.Log));

            var sp = services.BuildServiceProvider();

            Configure(sp, task);

            return(sp);
        }
Exemplo n.º 7
0
 private static IServiceProvider GetServiceProvider(GitVersionOptions gitVersionOptions, ILog log = null, IRepository repository = null, IFileSystem fileSystem = null, IEnvironment environment = null)
 {
     return(ConfigureServices(services =>
     {
         if (log != null)
         {
             services.AddSingleton(log);
         }
         if (fileSystem != null)
         {
             services.AddSingleton(fileSystem);
         }
         if (repository != null)
         {
             services.AddSingleton(repository);
         }
         if (environment != null)
         {
             services.AddSingleton(environment);
         }
         services.AddSingleton(Options.Create(gitVersionOptions));
     }));
 }
Exemplo n.º 8
0
 private static IServiceProvider GetServiceProvider(GitVersionOptions gitVersionOptions, ILog log = null, IGitRepository repository = null, IFileSystem fileSystem = null, IEnvironment environment = null) =>
 ConfigureServices(services =>
 {
     if (log != null)
     {
         services.AddSingleton(log);
     }
     if (fileSystem != null)
     {
         services.AddSingleton(fileSystem);
     }
     if (repository != null)
     {
         services.AddSingleton(repository);
     }
     if (environment != null)
     {
         services.AddSingleton(environment);
     }
     var options = Options.Create(gitVersionOptions);
     services.AddSingleton(options);
     services.AddSingleton(RepositoryExtensions.ToGitRepositoryInfo(options));
 });
        public void FindsVersionInDynamicRepo(string name, string url, string targetBranch, string commitId, string expectedFullSemVer)
        {
            var root              = Path.Combine(workDirectory, name);
            var dynamicDirectory  = Path.Combine(root, "D"); // dynamic, keeping directory as short as possible
            var workingDirectory  = Path.Combine(root, "W"); // working, keeping directory as short as possible
            var gitVersionOptions = new GitVersionOptions
            {
                RepositoryInfo =
                {
                    TargetUrl                  = url,
                    DynamicRepositoryClonePath = dynamicDirectory,
                    TargetBranch = targetBranch,
                    CommitId     = commitId,
                },
                Settings         = { NoFetch = false },
                WorkingDirectory = workingDirectory,
            };
            var options = Options.Create(gitVersionOptions);

            Directory.CreateDirectory(dynamicDirectory);
            Directory.CreateDirectory(workingDirectory);

            var sp = ConfigureServices(services =>
            {
                services.AddSingleton(options);
            });

            var gitPreparer = sp.GetService <IGitPreparer>();

            gitPreparer?.Prepare();

            var gitVersionCalculator = sp.GetService <IGitVersionCalculateTool>();

            var versionVariables = gitVersionCalculator?.CalculateVersionVariables();

            Assert.AreEqual(expectedFullSemVer, versionVariables?.FullSemVer);
        }
Exemplo n.º 10
0
    public void CalculateVersionVariables_TwoBranchHasSameCommitHeadDetachedAndTagged_ReturnSemver()
    {
        // Setup
        using var fixture = new RemoteRepositoryFixture();
        fixture.LocalRepositoryFixture.Repository.MakeACommit("Init commit");
        fixture.LocalRepositoryFixture.Repository.CreateBranch("feature/1.0");
        fixture.LocalRepositoryFixture.Checkout("feature/1.0");
        var commit = fixture.LocalRepositoryFixture.Repository.MakeACommit("feat: a new commit");

        fixture.LocalRepositoryFixture.Repository.CreateBranch("support/1.0");
        fixture.LocalRepositoryFixture.ApplyTag("1.0.1");
        fixture.LocalRepositoryFixture.Checkout(commit.Sha);

        using var worktreeFixture = new LocalRepositoryFixture(new Repository(fixture.LocalRepositoryFixture.RepositoryPath));
        var gitVersionOptions = new GitVersionOptions {
            WorkingDirectory = worktreeFixture.RepositoryPath
        };

        var environment = new TestEnvironment();

        environment.SetEnvironmentVariable(AzurePipelines.EnvironmentVariableName, "true");

        this.sp = GetServiceProvider(gitVersionOptions, environment: environment);

        var _ = this.sp.GetService <Lazy <GitVersionContext> >()?.Value;

        var sut = sp.GetService <IGitVersionCalculateTool>();

        // Execute
        var version = sut !.CalculateVersionVariables();

        // Verify
        version.SemVer.ShouldBe("1.0.1");
        var commits = worktreeFixture.Repository.Head.Commits;

        version.Sha.ShouldBe(commits.First().Sha);
    }
Exemplo n.º 11
0
    private void PrepareInternal(GitVersionOptions gitVersionOptions)
    {
        var currentBranch = ResolveCurrentBranch();

        if (!gitVersionOptions.RepositoryInfo.TargetUrl.IsNullOrWhiteSpace())
        {
            CreateDynamicRepository(currentBranch);
            NormalizeGitDirectory(currentBranch, true);
        }
        else
        {
            // Normalize if we are running on build server
            if (gitVersionOptions.Settings.NoNormalize || this.buildAgent is LocalBuild)
            {
                return;
            }
            if (this.buildAgent.ShouldCleanUpRemotes())
            {
                CleanupDuplicateOrigin();
            }

            NormalizeGitDirectory(currentBranch, false);
        }
    }
Exemplo n.º 12
0
        public void ConfigChangeInvalidatesCache()
        {
            const string versionCacheFileContent = @"
        Major: 4
        Minor: 10
        Patch: 3
        PreReleaseTag: test.19
        PreReleaseTagWithDash: -test.19
        PreReleaseLabel: test
        PreReleaseLabelWithDash: -test
        PreReleaseNumber: 19
        WeightedPreReleaseNumber: 19
        BuildMetaData:
        BuildMetaDataPadded:
        FullBuildMetaData: Branch.feature/test.Sha.dd2a29aff0c948e1bdf3dabbe13e1576e70d5f9f
        MajorMinorPatch: 4.10.3
        SemVer: 4.10.3-test.19
        LegacySemVer: 4.10.3-test19
        LegacySemVerPadded: 4.10.3-test0019
        AssemblySemVer: 4.10.3.0
        AssemblySemFileVer: 4.10.3.0
        FullSemVer: 4.10.3-test.19
        InformationalVersion: 4.10.3-test.19+Branch.feature/test.Sha.dd2a29aff0c948e1bdf3dabbe13e1576e70d5f9f
        BranchName: feature/test
        EscapedBranchName: feature-test
        Sha: dd2a29aff0c948e1bdf3dabbe13e1576e70d5f9f
        ShortSha: dd2a29af
        NuGetVersionV2: 4.10.3-test0019
        NuGetVersion: 4.10.3-test0019
        NuGetPreReleaseTagV2: test0019
        NuGetPreReleaseTag: test0019
        VersionSourceSha: 4.10.2
        CommitsSinceVersionSource: 19
        CommitsSinceVersionSourcePadded: 0019
        CommitDate: 2015-11-10
        UncommittedChanges: 0 
        ";

            using var fixture = new EmptyRepositoryFixture();

            var gitVersionOptions = new GitVersionOptions {
                WorkingDirectory = fixture.RepositoryPath
            };

            fixture.Repository.MakeACommit();

            var gitVersionCalculator = GetGitVersionCalculator(gitVersionOptions);
            var versionVariables     = gitVersionCalculator.CalculateVersionVariables();

            versionVariables.AssemblySemVer.ShouldBe("0.1.0.0");
            versionVariables.FileName.ShouldNotBeNullOrEmpty();

            fileSystem.WriteAllText(versionVariables.FileName, versionCacheFileContent);

            versionVariables = gitVersionCalculator.CalculateVersionVariables();
            versionVariables.AssemblySemVer.ShouldBe("4.10.3.0");

            var configPath = Path.Combine(fixture.RepositoryPath, DefaultConfigFileLocator.DefaultFileName);

            fileSystem.WriteAllText(configPath, "next-version: 5.0");

            gitVersionCalculator = GetGitVersionCalculator(gitVersionOptions, fs: fileSystem);

            versionVariables = gitVersionCalculator.CalculateVersionVariables();
            versionVariables.AssemblySemVer.ShouldBe("5.0.0.0");
        }
Exemplo n.º 13
0
        public void CacheFileExistsOnDiskWhenOverrideConfigIsSpecifiedVersionShouldBeDynamicallyCalculatedWithoutSavingInCache()
        {
            const string versionCacheFileContent = @"
        Major: 4
        Minor: 10
        Patch: 3
        PreReleaseTag: test.19
        PreReleaseTagWithDash: -test.19
        PreReleaseLabel: test
        PreReleaseLabelWithDash: -test
        PreReleaseNumber: 19
        BuildMetaData:
        BuildMetaDataPadded:
        FullBuildMetaData: Branch.feature/test.Sha.dd2a29aff0c948e1bdf3dabbe13e1576e70d5f9f
        MajorMinorPatch: 4.10.3
        SemVer: 4.10.3-test.19
        LegacySemVer: 4.10.3-test19
        LegacySemVerPadded: 4.10.3-test0019
        AssemblySemVer: 4.10.3.0
        AssemblySemFileVer: 4.10.3.0
        FullSemVer: 4.10.3-test.19
        InformationalVersion: 4.10.3-test.19+Branch.feature/test.Sha.dd2a29aff0c948e1bdf3dabbe13e1576e70d5f9f
        BranchName: feature/test
        EscapedBranchName: feature-test
        Sha: dd2a29aff0c948e1bdf3dabbe13e1576e70d5f9f
        ShortSha: dd2a29af
        NuGetVersionV2: 4.10.3-test0019
        NuGetVersion: 4.10.3-test0019
        NuGetPreReleaseTagV2: test0019
        NuGetPreReleaseTag: test0019
        CommitsSinceVersionSource: 19
        CommitsSinceVersionSourcePadded: 0019
        CommitDate: 2015-11-10
        UncommittedChanges: 0
        ";

            using var fixture = new EmptyRepositoryFixture();
            fixture.Repository.MakeACommit();

            var gitVersionOptions = new GitVersionOptions {
                WorkingDirectory = fixture.RepositoryPath
            };
            var gitVersionCalculator = GetGitVersionCalculator(gitVersionOptions, log);

            var versionVariables = gitVersionCalculator.CalculateVersionVariables();

            versionVariables.AssemblySemVer.ShouldBe("0.1.0.0");

            fileSystem.WriteAllText(versionVariables.FileName, versionCacheFileContent);

            var cacheDirectory = gitVersionCache.GetCacheDirectory();

            var cacheDirectoryTimestamp = fileSystem.GetLastDirectoryWrite(cacheDirectory);

            var config = new ConfigurationBuilder().Add(new Config {
                TagPrefix = "prefix"
            }).Build();

            gitVersionOptions = new GitVersionOptions {
                WorkingDirectory = fixture.RepositoryPath, ConfigInfo = { OverrideConfig = config }
            };

            gitVersionCalculator = GetGitVersionCalculator(gitVersionOptions);
            versionVariables     = gitVersionCalculator.CalculateVersionVariables();

            versionVariables.AssemblySemVer.ShouldBe("0.1.0.0");

            var cachedDirectoryTimestampAfter = fileSystem.GetLastDirectoryWrite(cacheDirectory);

            cachedDirectoryTimestampAfter.ShouldBe(cacheDirectoryTimestamp, "Cache was updated when override config was set");
        }
Exemplo n.º 14
0
        public void CacheFileExistsOnDisk()
        {
            const string versionCacheFileContent = @"
        Major: 4
        Minor: 10
        Patch: 3
        PreReleaseTag: test.19
        PreReleaseTagWithDash: -test.19
        PreReleaseLabel: test
        PreReleaseLabelWithDash: -test
        PreReleaseNumber: 19
        WeightedPreReleaseNumber: 19
        BuildMetaData:
        BuildMetaDataPadded:
        FullBuildMetaData: Branch.feature/test.Sha.dd2a29aff0c948e1bdf3dabbe13e1576e70d5f9f
        MajorMinorPatch: 4.10.3
        SemVer: 4.10.3-test.19
        LegacySemVer: 4.10.3-test19
        LegacySemVerPadded: 4.10.3-test0019
        AssemblySemVer: 4.10.3.0
        AssemblySemFileVer: 4.10.3.0
        FullSemVer: 4.10.3-test.19
        InformationalVersion: 4.10.3-test.19+Branch.feature/test.Sha.dd2a29aff0c948e1bdf3dabbe13e1576e70d5f9f
        BranchName: feature/test
        EscapedBranchName: feature-test
        Sha: dd2a29aff0c948e1bdf3dabbe13e1576e70d5f9f
        ShortSha: dd2a29af
        NuGetVersionV2: 4.10.3-test0019
        NuGetVersion: 4.10.3-test0019
        NuGetPreReleaseTagV2: test0019
        NuGetPreReleaseTag: test0019
        VersionSourceSha: 4.10.2
        CommitsSinceVersionSource: 19
        CommitsSinceVersionSourcePadded: 0019
        CommitDate: 2015-11-10
        UncommittedChanges: 0
        ";

            var stringBuilder = new StringBuilder();

            void Action(string s) => stringBuilder.AppendLine(s);

            var logAppender = new TestLogAppender(Action);

            log = new Log(logAppender);

            using var fixture = new EmptyRepositoryFixture();
            fixture.Repository.MakeACommit();

            var gitVersionOptions = new GitVersionOptions {
                WorkingDirectory = fixture.RepositoryPath
            };

            var gitVersionCalculator = GetGitVersionCalculator(gitVersionOptions, log);

            var versionVariables = gitVersionCalculator.CalculateVersionVariables();

            versionVariables.AssemblySemVer.ShouldBe("0.1.0.0");

            fileSystem.WriteAllText(versionVariables.FileName, versionCacheFileContent);
            versionVariables = gitVersionCalculator.CalculateVersionVariables();
            versionVariables.AssemblySemVer.ShouldBe("4.10.3.0");

            var logsMessages = stringBuilder.ToString();

            logsMessages.ShouldContain("Deserializing version variables from cache file", Case.Insensitive, logsMessages);
        }
Exemplo n.º 15
0
    private bool HandleNonMainCommand(GitVersionOptions gitVersionOptions, out int exitCode)
    {
        if (gitVersionOptions == null)
        {
            this.helpWriter.Write();
            exitCode = 1;
            return(true);
        }


        if (gitVersionOptions.IsVersion)
        {
            var assembly = Assembly.GetExecutingAssembly();
            this.versionWriter.Write(assembly);
            exitCode = 0;
            return(true);
        }

        if (gitVersionOptions.IsHelp)
        {
            this.helpWriter.Write();
            exitCode = 0;
            return(true);
        }

        if (gitVersionOptions.Diag)
        {
            gitVersionOptions.Settings.NoCache = true;
            gitVersionOptions.Output.Add(OutputType.BuildServer);
        }

        ConfigureLogging(gitVersionOptions, this.log);

        var workingDirectory = gitVersionOptions.WorkingDirectory;

        if (gitVersionOptions.Diag)
        {
            this.log.Info("Dumping commit graph: ");
            GitExtensions.DumpGraph(workingDirectory, mess => this.log.Info(mess), 100);
        }

        if (!Directory.Exists(workingDirectory))
        {
            this.log.Warning($"The working directory '{workingDirectory}' does not exist.");
        }
        else
        {
            this.log.Info("Working directory: " + workingDirectory);
        }

        this.configFileLocator.Verify(gitVersionOptions, this.repositoryInfo);

        if (gitVersionOptions.Init)
        {
            this.configProvider.Init(workingDirectory);
            exitCode = 0;
            return(true);
        }

        if (gitVersionOptions.ConfigInfo.ShowConfig)
        {
            var config = this.configProvider.Provide(workingDirectory);
            this.console.WriteLine(config.ToString());
            exitCode = 0;
            return(true);
        }

        exitCode = 0;
        return(false);
    }