public void UsesGitVersionConfigWhenCreatingDynamicRepository()
    {
        var localRepoPath = PathHelper.GetTempPath();
        var repoBasePath  = Path.GetDirectoryName(PathHelper.GetTempPath());

        Directory.CreateDirectory(localRepoPath);

        try
        {
            using (var remote = new EmptyRepositoryFixture(new Config()))
            {
                remote.Repository.MakeACommit();
                var configFile = Path.Combine(localRepoPath, "GitVersionConfig.yaml");
                File.WriteAllText(configFile, "next-version: 1.0.0");

                var arguments = string.Format(" /url {0} /dynamicRepoLocation {1} /b master", remote.RepositoryPath, repoBasePath);
                var results   = GitVersionHelper.ExecuteIn(localRepoPath, arguments, false);
                results.OutputVariables.SemVer.ShouldBe("1.0.0");
            }
        }
        finally
        {
            DeleteHelper.DeleteGitRepository(localRepoPath);
            DeleteHelper.DeleteGitRepository(repoBasePath);
        }
    }
Esempio n. 2
0
    public void WorksCorrectlyWithRemoteRepository(string branchName, string expectedBranchName)
    {
        var repoName = Guid.NewGuid().ToString();
        var tempPath = Path.GetTempPath();
        var tempDir  = Path.Combine(tempPath, repoName);

        Directory.CreateDirectory(tempDir);
        string dynamicRepositoryPath = null;

        try
        {
            using (var fixture = new EmptyRepositoryFixture(new Config()))
            {
                var expectedDynamicRepoLocation = Path.Combine(tempPath, fixture.RepositoryPath.Split('\\').Last());

                fixture.Repository.MakeCommits(5);
                fixture.Repository.CreateFileAndCommit("TestFile.txt");

                fixture.Repository.CreateBranch(SpecificBranchName);

                var arguments = new Arguments
                {
                    TargetPath = tempDir,
                    TargetUrl  = fixture.RepositoryPath
                };

                // Copy contents into working directory
                File.Copy(Path.Combine(fixture.RepositoryPath, "TestFile.txt"), Path.Combine(tempDir, "TestFile.txt"));

                if (!string.IsNullOrWhiteSpace(branchName))
                {
                    arguments.TargetBranch = branchName;
                }

                var gitPreparer = new GitPreparer(arguments);
                gitPreparer.InitialiseDynamicRepositoryIfNeeded();
                dynamicRepositoryPath = gitPreparer.GetDotGitDirectory();

                gitPreparer.IsDynamicGitRepository.ShouldBe(true);
                gitPreparer.DynamicGitRepositoryPath.ShouldBe(expectedDynamicRepoLocation + "\\.git");

                using (var repository = new Repository(dynamicRepositoryPath))
                {
                    var currentBranch = repository.Head.CanonicalName;

                    currentBranch.EndsWith(expectedBranchName).ShouldBe(true);
                }
            }
        }
        finally
        {
            Directory.Delete(tempDir, true);
            if (dynamicRepositoryPath != null)
            {
                DeleteHelper.DeleteGitRepository(dynamicRepositoryPath);
            }
        }
    }
        public void WorksCorrectlyWithRemoteRepository(string branchName, string expectedBranchName)
        {
            var repoName = Guid.NewGuid().ToString();
            var tempPath = Path.GetTempPath();
            var tempDir  = Path.Combine(tempPath, repoName);

            Directory.CreateDirectory(tempDir);
            string dynamicRepositoryPath = null;

            try
            {
                using (var fixture = new EmptyRepositoryFixture())
                {
                    var expectedDynamicRepoLocation = Path.Combine(tempPath, fixture.RepositoryPath.Split(Path.DirectorySeparatorChar).Last());

                    fixture.Repository.MakeCommits(5);
                    fixture.Repository.CreateFileAndCommit("TestFile.txt");

                    fixture.Repository.CreateBranch(SpecificBranchName);

                    // Copy contents into working directory
                    File.Copy(Path.Combine(fixture.RepositoryPath, "TestFile.txt"), Path.Combine(tempDir, "TestFile.txt"));

                    var repositoryInfo = new RepositoryInfo
                    {
                        Url    = fixture.RepositoryPath,
                        Branch = branchName
                    };

                    using (var gitRepository = GitRepositoryFactory.CreateRepository(repositoryInfo))
                    {
                        dynamicRepositoryPath = gitRepository.DotGitDirectory;

                        gitRepository.IsDynamic.ShouldBe(true);
                        gitRepository.DotGitDirectory.ShouldBe(Path.Combine(expectedDynamicRepoLocation, ".git"));

                        var currentBranch = gitRepository.Repository.Head.CanonicalName;

                        currentBranch.ShouldEndWith(expectedBranchName);
                    }
                }
            }
            finally
            {
                Directory.Delete(tempDir, true);

                if (dynamicRepositoryPath != null)
                {
                    DeleteHelper.DeleteGitRepository(dynamicRepositoryPath);
                }
            }
        }
Esempio n. 4
0
    public void UpdatesExistingDynamicRepository()
    {
        var repoName = Guid.NewGuid().ToString();
        var tempPath = Path.GetTempPath();
        var tempDir  = Path.Combine(tempPath, repoName);

        Directory.CreateDirectory(tempDir);
        string dynamicRepositoryPath = null;

        try
        {
            using (var mainRepositoryFixture = new EmptyRepositoryFixture(new Config()))
            {
                mainRepositoryFixture.Repository.MakeCommits(1);

                var arguments = new Arguments
                {
                    TargetPath   = tempDir,
                    TargetUrl    = mainRepositoryFixture.RepositoryPath,
                    TargetBranch = "master"
                };

                var gitPreparer = new GitPreparer(arguments.TargetUrl, arguments.DynamicRepositoryLocation, arguments.Authentication, arguments.TargetBranch, arguments.NoFetch, arguments.TargetPath);
                gitPreparer.Initialise(false);
                dynamicRepositoryPath = gitPreparer.GetDotGitDirectory();

                var newCommit = mainRepositoryFixture.Repository.MakeACommit();
                gitPreparer.Initialise(false);

                using (var repository = new Repository(dynamicRepositoryPath))
                {
                    mainRepositoryFixture.Repository.DumpGraph();
                    repository.DumpGraph();
                    repository.Commits.ShouldContain(c => c.Sha == newCommit.Sha);
                }
            }
        }
        finally
        {
            Directory.Delete(tempDir, true);
            if (dynamicRepositoryPath != null)
            {
                DeleteHelper.DeleteGitRepository(dynamicRepositoryPath);
            }
        }
    }
        public void UpdatesExistingDynamicRepository()
        {
            var repoName = Guid.NewGuid().ToString();
            var tempPath = Path.GetTempPath();
            var tempDir  = Path.Combine(tempPath, repoName);

            Directory.CreateDirectory(tempDir);
            string dynamicRepositoryPath = null;

            try
            {
                using (var mainRepositoryFixture = new EmptyRepositoryFixture())
                {
                    mainRepositoryFixture.Repository.MakeCommits(1);

                    var repositoryInfo = new RepositoryInfo
                    {
                        Url    = mainRepositoryFixture.RepositoryPath,
                        Branch = "master"
                    };

                    using (var gitRepository = GitRepositoryFactory.CreateRepository(repositoryInfo))
                    {
                        dynamicRepositoryPath = gitRepository.DotGitDirectory;
                    }

                    var newCommit = mainRepositoryFixture.Repository.MakeACommit();

                    using (var gitRepository = GitRepositoryFactory.CreateRepository(repositoryInfo))
                    {
                        mainRepositoryFixture.Repository.DumpGraph();
                        gitRepository.Repository.DumpGraph();
                        gitRepository.Repository.Commits.ShouldContain(c => c.Sha == newCommit.Sha);
                    }
                }
            }
            finally
            {
                Directory.Delete(tempDir, true);

                if (dynamicRepositoryPath != null)
                {
                    DeleteHelper.DeleteGitRepository(dynamicRepositoryPath);
                }
            }
        }
        public void PicksAnotherDirectoryNameWhenDynamicRepoFolderTaken()
        {
            var repoName = Guid.NewGuid().ToString();
            var tempPath = Path.GetTempPath();
            var tempDir  = Path.Combine(tempPath, repoName);

            Directory.CreateDirectory(tempDir);
            string expectedDynamicRepoLocation = null;

            try
            {
                using (var fixture = new EmptyRepositoryFixture())
                {
                    fixture.Repository.CreateFileAndCommit("TestFile.txt");
                    File.Copy(Path.Combine(fixture.RepositoryPath, "TestFile.txt"), Path.Combine(tempDir, "TestFile.txt"));
                    expectedDynamicRepoLocation = Path.Combine(tempPath, fixture.RepositoryPath.Split(Path.DirectorySeparatorChar).Last());
                    Directory.CreateDirectory(expectedDynamicRepoLocation);

                    var repositoryInfo = new RepositoryInfo
                    {
                        Url    = fixture.RepositoryPath,
                        Branch = "master"
                    };

                    using (var gitRepository = GitRepositoryFactory.CreateRepository(repositoryInfo))
                    {
                        gitRepository.IsDynamic.ShouldBe(true);
                        gitRepository.DotGitDirectory.ShouldBe(Path.Combine(expectedDynamicRepoLocation + "_1", ".git"));
                    }
                }
            }
            finally
            {
                DeleteHelper.DeleteDirectory(tempDir, true);
                if (expectedDynamicRepoLocation != null)
                {
                    DeleteHelper.DeleteDirectory(expectedDynamicRepoLocation, true);
                }

                if (expectedDynamicRepoLocation != null)
                {
                    DeleteHelper.DeleteGitRepository(expectedDynamicRepoLocation + "_1");
                }
            }
        }
Esempio n. 7
0
    public void PicksAnotherDirectoryNameWhenDynamicRepoFolderTaken()
    {
        var repoName = Guid.NewGuid().ToString();
        var tempPath = Path.GetTempPath();
        var tempDir  = Path.Combine(tempPath, repoName);

        Directory.CreateDirectory(tempDir);
        string expectedDynamicRepoLocation = null;

        try
        {
            using (var fixture = new EmptyRepositoryFixture(new Config()))
            {
                fixture.Repository.CreateFileAndCommit("TestFile.txt");
                File.Copy(Path.Combine(fixture.RepositoryPath, "TestFile.txt"), Path.Combine(tempDir, "TestFile.txt"));
                expectedDynamicRepoLocation = Path.Combine(tempPath, fixture.RepositoryPath.Split('\\').Last());
                Directory.CreateDirectory(expectedDynamicRepoLocation);

                var arguments = new Arguments
                {
                    TargetPath = tempDir,
                    TargetUrl  = fixture.RepositoryPath
                };

                var gitPreparer = new GitPreparer(arguments);
                gitPreparer.InitialiseDynamicRepositoryIfNeeded();

                gitPreparer.IsDynamicGitRepository.ShouldBe(true);
                gitPreparer.DynamicGitRepositoryPath.ShouldBe(expectedDynamicRepoLocation + "_1\\.git");
            }
        }
        finally
        {
            Directory.Delete(tempDir, true);
            if (expectedDynamicRepoLocation != null)
            {
                Directory.Delete(expectedDynamicRepoLocation, true);
            }
            if (expectedDynamicRepoLocation != null)
            {
                DeleteHelper.DeleteGitRepository(expectedDynamicRepoLocation + "_1");
            }
        }
    }
        public void UpdatesExistingDynamicRepository()
        {
            var repoName = Guid.NewGuid().ToString();
            var tempPath = Path.GetTempPath();
            var tempDir  = Path.Combine(tempPath, repoName);

            Directory.CreateDirectory(tempDir);
            string dynamicRepositoryPath = null;

            try
            {
                using (var mainRepositoryFixture = new EmptyRepositoryFixture())
                {
                    var commit = mainRepositoryFixture.Repository.MakeACommit();

                    var repositoryInfo = new RepositoryInfo
                    {
                        Url = mainRepositoryFixture.RepositoryPath
                    };

                    using (var dynamicRepository = DynamicRepositories.CreateOrOpen(repositoryInfo, tempPath, "master", commit.Sha))
                    {
                        dynamicRepositoryPath = dynamicRepository.Repository.Info.Path;
                    }

                    var newCommit = mainRepositoryFixture.Repository.MakeACommit();

                    using (var dynamicRepository = DynamicRepositories.CreateOrOpen(repositoryInfo, tempPath, "master", newCommit.Sha))
                    {
                        dynamicRepository.Repository.Info.Path.ShouldBe(dynamicRepositoryPath);
                        dynamicRepository.Repository.Commits.ShouldContain(c => c.Sha == newCommit.Sha);
                    }
                }
            }
            finally
            {
                Directory.Delete(tempDir, true);

                if (dynamicRepositoryPath != null)
                {
                    DeleteHelper.DeleteGitRepository(dynamicRepositoryPath);
                }
            }
        }
        public void PicksAnotherDirectoryNameWhenDynamicRepoFolderIsInUse()
        {
            var tempPath = Path.GetTempPath();
            var expectedDynamicRepoLocation  = default(string);
            var expectedDynamicRepo2Location = default(string);

            try
            {
                using (var fixture = new EmptyRepositoryFixture())
                {
                    var head           = fixture.Repository.CreateFileAndCommit("TestFile.txt");
                    var repositoryInfo = new RepositoryInfo
                    {
                        Url = fixture.RepositoryPath
                    };

                    using (var dynamicRepository = DynamicRepositories.CreateOrOpen(repositoryInfo, tempPath, "master", head.Sha))
                        using (var dynamicRepository2 = DynamicRepositories.CreateOrOpen(repositoryInfo, tempPath, "master", head.Sha))
                        {
                            expectedDynamicRepoLocation  = dynamicRepository.Repository.Info.Path;
                            expectedDynamicRepo2Location = dynamicRepository2.Repository.Info.Path;
                            dynamicRepository.Repository.Info.Path.ShouldNotBe(dynamicRepository2.Repository.Info.Path);
                        }
                }
            }
            finally
            {
                if (expectedDynamicRepoLocation != null)
                {
                    DeleteHelper.DeleteDirectory(expectedDynamicRepoLocation, true);
                }

                if (expectedDynamicRepo2Location != null)
                {
                    DeleteHelper.DeleteGitRepository(expectedDynamicRepo2Location);
                }
            }
        }
Esempio n. 10
0
        private string GetGitInfoFromUrl(Context context, string gitDirectory)
        {
            gitDirectory = Path.Combine(gitDirectory, ".git");
            if (Directory.Exists(gitDirectory))
            {
                Log.Info("Deleting existing .git folder from '{0}' to force new checkout from url", gitDirectory);

                DeleteHelper.DeleteGitRepository(gitDirectory);
            }

            Log.Info("Retrieving git info from url '{0}'", context.TargetUrl);

            Credentials credentials    = null;
            var         authentication = context.Authentication;

            if (!string.IsNullOrWhiteSpace(authentication.Username) && !string.IsNullOrWhiteSpace(authentication.Password))
            {
                Log.Info("Setting up credentials using name '{0}'", authentication.Username);

                credentials = new UsernamePasswordCredentials
                {
                    Username = authentication.Username,
                    Password = authentication.Password
                };
            }

            var cloneOptions = new CloneOptions
            {
                Checkout            = false,
                IsBare              = true,
                CredentialsProvider = (url, username, supportedTypes) => credentials
            };

            Repository.Clone(context.TargetUrl, gitDirectory, cloneOptions);

            if (!string.IsNullOrWhiteSpace(context.TargetBranch))
            {
                using (var repository = new Repository(gitDirectory))
                {
                    Reference newHead = null;

                    var localReference = GetLocalReference(repository, context.TargetBranch);
                    if (localReference != null)
                    {
                        newHead = localReference;
                    }

                    if (newHead == null)
                    {
                        var remoteReference = GetRemoteReference(repository, context.TargetBranch, context.TargetUrl);
                        if (remoteReference != null)
                        {
                            repository.Network.Fetch(context.TargetUrl, new[]
                            {
                                string.Format("{0}:{1}", remoteReference.CanonicalName, context.TargetBranch)
                            });

                            newHead = repository.Refs[string.Format("refs/heads/{0}", context.TargetBranch)];
                        }
                    }

                    if (newHead != null)
                    {
                        Log.Info("Switching to branch '{0}'", context.TargetBranch);

                        repository.Refs.UpdateTarget(repository.Refs.Head, newHead);
                    }
                }
            }

            return(gitDirectory);
        }