예제 #1
0
        public override bool Execute()
        {
            try
            {
                VersionVariables variables;

                if (ExecuteCore.TryGetVersion(SolutionDirectory, out variables, NoFetch, new Authentication()))
                {
                    var thisType = typeof(GetVersion);
                    foreach (var variable in variables)
                    {
                        thisType.GetProperty(variable.Key).SetValue(this, variable.Value, null);
                    }
                }
                return(true);
            }
            catch (WarningException errorException)
            {
                logger.LogWarning(errorException.Message);
                return(true);
            }
            catch (Exception exception)
            {
                logger.LogError("Error occurred: " + exception);
                return(false);
            }
            finally
            {
                Logger.Reset();
            }
        }
예제 #2
0
        void InnerExecute()
        {
            VersionVariables versionVariables;

            if (!ExecuteCore.TryGetVersion(SolutionDirectory, out versionVariables, NoFetch, new Authentication()))
            {
                return;
            }

            var fileExtension = GetFileExtension();
            var fileName      = $"GitVersionInformation.g.{fileExtension}";

            if (IntermediateOutputPath == null)
            {
                fileName = $"GitVersionInformation_{Path.GetFileNameWithoutExtension(ProjectFile)}_{Path.GetRandomFileName()}.g.{fileExtension}";
            }

            var workingDirectory = IntermediateOutputPath ?? TempFileTracker.TempPath;

            GitVersionInformationFilePath = Path.Combine(workingDirectory, fileName);

            var generator = new GitVersionInformationGenerator(fileName, workingDirectory, versionVariables, new FileSystem());

            generator.Generate();
        }
예제 #3
0
        protected GitVersionTaskBase()
        {
            var fileSystem = new FileSystem();

            ExecuteCore = new ExecuteCore(fileSystem);
            GitVersion.Logger.SetLoggers(LogDebug, LogInfo, LogWarning, s => LogError(s));
        }
예제 #4
0
        public void CacheKeyForWorktree()
        {
            var versionAndBranchFinder = new ExecuteCore(fileSystem, environment);

            RepositoryScope(versionAndBranchFinder, (fixture, vv) =>
            {
                var worktreePath = Path.Combine(Directory.GetParent(fixture.RepositoryPath).FullName, Guid.NewGuid().ToString());
                try
                {
                    // create a branch and a new worktree for it
                    var repo = new Repository(fixture.RepositoryPath);
                    repo.Worktrees.Add("worktree", worktreePath, false);

                    var targetUrl         = "https://github.com/GitTools/GitVersion.git";
                    var gitPreparer       = new GitPreparer(targetUrl, null, new Authentication(), false, worktreePath);
                    var configFileLocator = new DefaultConfigFileLocator();
                    var cacheKey          = GitVersionCacheKeyFactory.Create(fileSystem, gitPreparer, null, configFileLocator);
                    cacheKey.Value.ShouldNotBeEmpty();
                }
                finally
                {
                    DirectoryHelper.DeleteDirectory(worktreePath);
                }
            });
        }
예제 #5
0
    public static VersionVariables GetVersion(string directory, Authentication authentication, bool noFetch, IFileSystem fileSystem)
    {
        var gitDir = GitDirFinder.TreeWalkForDotGitDir(directory);

        using (var repo = RepositoryLoader.GetRepo(gitDir))
        {
            var ticks = DirectoryDateFinder.GetLastDirectoryWrite(directory);
            var key   = string.Format("{0}:{1}:{2}", repo.Head.CanonicalName, repo.Head.Tip.Sha, ticks);

            CachedVersion result;
            if (versionCacheVersions.TryGetValue(key, out result))
            {
                if (result.Timestamp != ticks)
                {
                    Logger.WriteInfo("Change detected. flushing cache.");
                    result.VersionVariables = ExecuteCore.ExecuteGitVersion(fileSystem, null, null, authentication, null, noFetch, directory, null);
                }
                return(result.VersionVariables);
            }
            Logger.WriteInfo("Version not in cache. Calculating version.");

            return((versionCacheVersions[key] = new CachedVersion
            {
                VersionVariables = ExecuteCore.ExecuteGitVersion(fileSystem, null, null, authentication, null, noFetch, directory, null),
                Timestamp = ticks
            }).VersionVariables);
        }
    }
예제 #6
0
    string RepositoryScope(ExecuteCore executeCore = null, Action <EmptyRepositoryFixture, VersionVariables> fixtureAction = null)
    {
        // Make sure GitVersion doesn't trigger build server mode when we are running the tests
        Environment.SetEnvironmentVariable("APPVEYOR", null);
        var             infoBuilder = new StringBuilder();
        Action <string> infoLogger  = s =>
        {
            infoBuilder.AppendLine(s);
            Console.WriteLine(s);
        };

        executeCore = executeCore ?? new ExecuteCore(fileSystem);

        Logger.SetLoggers(infoLogger, Console.WriteLine, Console.WriteLine);

        using (var fixture = new EmptyRepositoryFixture())
        {
            fixture.Repository.MakeACommit();
            var vv = executeCore.ExecuteGitVersion(null, null, null, null, false, fixture.RepositoryPath, null);

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

            if (fixtureAction != null)
            {
                fixtureAction(fixture, vv);
            }
        }

        return(infoBuilder.ToString());
    }
 public void CreateTemporaryRepository()
 {
     workDirectory = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
     gitDirectory  = Repository.Init(workDirectory)
                     .TrimEnd(Path.DirectorySeparatorChar);
     executeCore = new ExecuteCore(new TestFileSystem());
     Assert.NotNull(gitDirectory);
 }
예제 #8
0
    public void CacheFileExistsOnDiskWhenOverrideConfigIsSpecifiedVersionShouldBeDynamicallyCalculatedWithoutSavingInCache()
    {
        const string versionCacheFileContent = @"
Major: 4
Minor: 10
Patch: 3
PreReleaseTag: test.19
PreReleaseTagWithDash: -test.19
PreReleaseLabel: 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
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
";

        var versionAndBranchFinder = new ExecuteCore(fileSystem);

        RepositoryScope(versionAndBranchFinder, (fixture, vv) =>
        {
            fileSystem.WriteAllText(vv.FileName, versionCacheFileContent);

            var gitPreparer    = new GitPreparer(null, null, null, false, fixture.RepositoryPath);
            var cacheDirectory = GitVersionCache.GetCacheDirectory(gitPreparer);

            var cacheDirectoryTimestamp = fileSystem.GetLastDirectoryWrite(cacheDirectory);

            vv = versionAndBranchFinder.ExecuteGitVersion(null, null, null, null, false, fixture.RepositoryPath, null, new Config()
            {
                TagPrefix = "prefix"
            });

            vv.AssemblySemVer.ShouldBe("0.1.0.0");

            var cachedDirectoryTimestampAfter = fileSystem.GetLastDirectoryWrite(cacheDirectory);
            cachedDirectoryTimestampAfter.ShouldBe(cacheDirectoryTimestamp, () => "Cache was updated when override config was set");
        });

        // TODO info.ShouldContain("Override config from command line", () => info);
    }
예제 #9
0
    public void DynamicRepositoriesShouldNotErrorWithFailedToFindGitDirectory()
    {
        var versionAndBranchFinder = new ExecuteCore(fileSystem);

        RepositoryScope(versionAndBranchFinder, (fixture, vv) =>
        {
            versionAndBranchFinder.ExecuteGitVersion("https://github.com/GitTools/GitVersion.git", null, new Authentication(), "refs/head/master", false, fixture.RepositoryPath, null);
        });
    }
예제 #10
0
    public void WorkingDirectoryWithoutGit()
    {
        var versionAndBranchFinder = new ExecuteCore(fileSystem);

        RepositoryScope(versionAndBranchFinder, (fixture, vv) =>
        {
            var exception = Assert.Throws <DirectoryNotFoundException>(() => versionAndBranchFinder.ExecuteGitVersion(null, null, null, null, false, Environment.SystemDirectory, null));
            exception.Message.ShouldContain("Can't find the .git directory in");
        });
    }
예제 #11
0
        void InnerExecute()
        {
            VersionVariables result;

            if (!ExecuteCore.TryGetVersion(SolutionDirectory, out result, NoFetch, new Authentication()))
            {
                return;
            }

            WriteIntegrationParameters(BuildServerList.GetApplicableBuildServers(), result);
        }
예제 #12
0
        public void GetDotGitDirectory_NoWorktree()
        {
            var versionAndBranchFinder = new ExecuteCore(fileSystem, environment);

            RepositoryScope(versionAndBranchFinder, (fixture, vv) =>
            {
                var targetUrl    = "https://github.com/GitTools/GitVersion.git";
                var gitPreparer  = new GitPreparer(targetUrl, null, new Authentication(), false, fixture.RepositoryPath);
                var expectedPath = Path.Combine(fixture.RepositoryPath, ".git");
                gitPreparer.GetDotGitDirectory().ShouldBe(expectedPath);
            });
        }
예제 #13
0
    LogMessages RepositoryScope(ExecuteCore executeCore = null, Action <EmptyRepositoryFixture, VersionVariables> fixtureAction = null)
    {
        // Make sure GitVersion doesn't trigger build server mode when we are running the tests
        Environment.SetEnvironmentVariable(AppVeyor.EnvironmentVariableName, null);
        Environment.SetEnvironmentVariable(TravisCI.EnvironmentVariableName, null);
        Environment.SetEnvironmentVariable(VsoAgent.EnvironmentVariableName, null);
        var             debugBuilder = new StringBuilder();
        Action <string> debugLogger  = s =>
        {
            debugBuilder.AppendLine(s);
        };
        var             infoBuilder = new StringBuilder();
        Action <string> infoLogger  = s =>
        {
            infoBuilder.AppendLine(s);
        };
        var             warnBuilder = new StringBuilder();
        Action <string> warnLogger  = s =>
        {
            warnBuilder.AppendLine(s);
        };
        var             errorBuilder = new StringBuilder();
        Action <string> errorLogger  = s =>
        {
            errorBuilder.AppendLine(s);
        };

        executeCore = executeCore ?? new ExecuteCore(fileSystem);

        using (Logger.AddLoggersTemporarily(debugLogger, infoLogger, warnLogger, errorLogger))
            using (var fixture = new EmptyRepositoryFixture())
            {
                fixture.Repository.MakeACommit();
                var vv = executeCore.ExecuteGitVersion(null, null, null, null, false, fixture.RepositoryPath, null);

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

                if (fixtureAction != null)
                {
                    fixtureAction(fixture, vv);
                }
            }

        return(new LogMessages
        {
            Debug = debugBuilder.ToString(),
            Info = infoBuilder.ToString(),
            Warn = warnBuilder.ToString(),
            Error = errorBuilder.ToString()
        });
    }
예제 #14
0
        void InnerExecute()
        {
            TempFileTracker.DeleteTempFiles();

            InvalidFileChecker.CheckForInvalidFiles(CompileFiles, ProjectFile);

            VersionVariables versionVariables;

            if (!ExecuteCore.TryGetVersion(SolutionDirectory, out versionVariables, NoFetch, new Authentication()))
            {
                return;
            }
            CreateTempAssemblyInfo(versionVariables);
        }
예제 #15
0
    public void ConfigChangeInvalidatesCache()
    {
        const string versionCacheFileContent = @"
Major: 4
Minor: 10
Patch: 3
PreReleaseTag: test.19
PreReleaseTagWithDash: -test.19
PreReleaseLabel: 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
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
";

        var versionAndBranchFinder = new ExecuteCore(fileSystem);

        RepositoryScope(versionAndBranchFinder, (fixture, vv) =>
        {
            fileSystem.WriteAllText(vv.FileName, versionCacheFileContent);
            vv = versionAndBranchFinder.ExecuteGitVersion(null, null, null, null, false, fixture.RepositoryPath, null);
            vv.AssemblySemVer.ShouldBe("4.10.3.0");

            var configPath = Path.Combine(fixture.RepositoryPath, "GitVersionConfig.yaml");
            fileSystem.WriteAllText(configPath, "next-version: 5.0");

            vv = versionAndBranchFinder.ExecuteGitVersion(null, null, null, null, false, fixture.RepositoryPath, null);
            vv.AssemblySemVer.ShouldBe("5.0.0.0");
        });
    }
        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

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

            var executeCore = new ExecuteCore(new TestFileSystem(), new TestEnvironment(), new NullLog());

            var versionVariables = executeCore.ExecuteGitVersion(url, dynamicDirectory, null, targetBranch,
                                                                 false, workingDirectory, commitId);

            Assert.AreEqual(expectedFullSemVer, versionVariables.FullSemVer);
        }
예제 #17
0
        public void CacheFileExistsOnDisk()
        {
            const string versionCacheFileContent = @"
Major: 4
Minor: 10
Patch: 3
PreReleaseTag: test.19
PreReleaseTagWithDash: -test.19
PreReleaseLabel: 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
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
";

            var versionAndBranchFinder = new ExecuteCore(fileSystem, environment);

            var logs = RepositoryScope(versionAndBranchFinder, (fixture, vv) =>
            {
                fileSystem.WriteAllText(vv.FileName, versionCacheFileContent);
                vv = versionAndBranchFinder.ExecuteGitVersion(null, null, null, null, false, fixture.RepositoryPath, null);
                vv.AssemblySemVer.ShouldBe("4.10.3.0");
            });

            logs.Info.ShouldContain("Deserializing version variables from cache file", () => logs.Info);
        }
예제 #18
0
    public void CacheKeySameAfterReNormalizing()
    {
        var versionAndBranchFinder = new ExecuteCore(fileSystem);

        RepositoryScope(versionAndBranchFinder, (fixture, vv) =>
        {
            var targetUrl    = "https://github.com/GitTools/GitVersion.git";
            var targetBranch = "refs/head/master";
            var gitPreparer  = new GitPreparer(targetUrl, null, new Authentication(), false, fixture.RepositoryPath);
            gitPreparer.Initialise(true, targetBranch);
            var cacheKey1 = GitVersionCacheKeyFactory.Create(fileSystem, gitPreparer, null);
            gitPreparer.Initialise(true, targetBranch);
            var cacheKey2 = GitVersionCacheKeyFactory.Create(fileSystem, gitPreparer, null);

            cacheKey2.Value.ShouldBe(cacheKey1.Value);
        });
    }
    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

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

        Logger.AddLoggersTemporarily(
            x => Debug.WriteLine($"[DEBUG]   {x}"),
            x => Debug.WriteLine($"[INFO]    {x}"),
            x => Debug.WriteLine($"[WARNING] {x}"),
            x => Debug.WriteLine($"[ERROR]   {x}"));

        var executeCore = new ExecuteCore(new TestFileSystem());

        var versionVariables = executeCore.ExecuteGitVersion(url, dynamicDirectory, null, targetBranch,
                                                             false, workingDirectory, commitId);

        Assert.AreEqual(expectedFullSemVer, versionVariables.FullSemVer);
    }
예제 #20
0
    public void FindsVersionInDynamicRepo(string name, string url, string targetBranch, string commitId, string expectedFullSemVer)
    {
        var root             = Path.Combine(workDirectory, name);
        var dynamicDirectory = Path.Combine(root, "dynamic");
        var workingDirectory = Path.Combine(root, "working");

        // Clear upfront
        if (Directory.Exists(root))
        {
            Directory.Delete(root, true);
        }

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

        var executeCore = new ExecuteCore(new TestFileSystem());

        var versionVariables = executeCore.ExecuteGitVersion(url, dynamicDirectory, null, targetBranch,
                                                             false, workingDirectory, commitId);

        Assert.AreEqual(expectedFullSemVer, versionVariables.FullSemVer);
    }
예제 #21
0
        public void GetProjectRootDirectory_WorkingDirectoryWithWorktree()
        {
            var versionAndBranchFinder = new ExecuteCore(fileSystem, environment);

            RepositoryScope(versionAndBranchFinder, (fixture, vv) =>
            {
                var worktreePath = Path.Combine(Directory.GetParent(fixture.RepositoryPath).FullName, Guid.NewGuid().ToString());
                try
                {
                    // create a branch and a new worktree for it
                    var repo = new Repository(fixture.RepositoryPath);
                    repo.Worktrees.Add("worktree", worktreePath, false);

                    var targetUrl   = "https://github.com/GitTools/GitVersion.git";
                    var gitPreparer = new GitPreparer(targetUrl, null, new Authentication(), false, worktreePath);
                    gitPreparer.GetProjectRootDirectory().TrimEnd('/', '\\').ShouldBe(worktreePath);
                }
                finally
                {
                    DirectoryHelper.DeleteDirectory(worktreePath);
                }
            });
        }
예제 #22
0
 protected bool GetVersionVariables(out VersionVariables versionVariables)
 {
     return(!ExecuteCore.TryGetVersion(SolutionDirectory, out versionVariables, NoFetch, new Authentication()));
 }
예제 #23
0
        protected GitVersionTaskBase()
        {
            var fileSystem = new FileSystem();

            executeCore = new ExecuteCore(fileSystem);
        }