예제 #1
0
        public CommandContext()
        {
            Streams = new StandardStreams();
            Trace   = new Trace();
            Git     = new LibGit2(Trace);

            if (PlatformUtils.IsWindows())
            {
                FileSystem      = new WindowsFileSystem();
                Environment     = new WindowsEnvironment(FileSystem);
                Terminal        = new WindowsTerminal(Trace);
                CredentialStore = WindowsCredentialManager.Open();
            }
            else if (PlatformUtils.IsPosix())
            {
                if (PlatformUtils.IsMacOS())
                {
                    FileSystem      = new MacOSFileSystem();
                    CredentialStore = MacOSKeychain.Open();
                }
                else if (PlatformUtils.IsLinux())
                {
                    throw new NotImplementedException();
                }

                Environment = new PosixEnvironment(FileSystem);
                Terminal    = new PosixTerminal(Trace);
            }

            string repoPath = Git.GetRepositoryPath(FileSystem.GetCurrentDirectory());

            Settings          = new Settings(Environment, Git, repoPath);
            HttpClientFactory = new HttpClientFactory(Trace, Settings, Streams);
        }
예제 #2
0
        public CommandContext()
        {
            Streams    = new StandardStreams();
            Trace      = new Trace();
            FileSystem = new FileSystem();

            var git    = new LibGit2();
            var envars = new EnvironmentVariables(Environment.GetEnvironmentVariables());

            Settings = new Settings(envars, git)
            {
                RepositoryPath = git.GetRepositoryPath(FileSystem.GetCurrentDirectory())
            };

            HttpClientFactory = new HttpClientFactory(Trace, Settings, Streams);

            if (PlatformUtils.IsWindows())
            {
                Terminal        = new WindowsTerminal(Trace);
                CredentialStore = WindowsCredentialManager.Open();
            }
            else if (PlatformUtils.IsPosix())
            {
                Terminal = new PosixTerminal(Trace);

                if (PlatformUtils.IsMacOS())
                {
                    CredentialStore = MacOSKeychain.Open();
                }
                else if (PlatformUtils.IsLinux())
                {
                    throw new NotImplementedException();
                }
            }
        }
예제 #3
0
        public void LibGit2_GetConfiguration_ReturnsConfiguration()
        {
            var git = new LibGit2();

            using (var config = git.GetConfiguration())
            {
                Assert.NotNull(config);
            }
        }
예제 #4
0
        public void LibGit2_GetRepositoryPath_NotInsideRepository_ReturnsNull()
        {
            var    git        = new LibGit2();
            string randomPath = Path.Combine(Path.GetTempPath(), $"{Guid.NewGuid():N}");

            Directory.CreateDirectory(randomPath);

            string repositoryPath = git.GetRepositoryPath(randomPath);

            Assert.Null(repositoryPath);
        }
예제 #5
0
        public void LibGit2Configuration_GetString_Name_DoesNotExists_ThrowsException()
        {
            string repoPath = CreateRepository();

            var git = new LibGit2();

            using (var config = git.GetConfiguration(repoPath))
            {
                string randomName = $"{Guid.NewGuid():N}.{Guid.NewGuid():N}";
                Assert.Throws <KeyNotFoundException>(() => config.GetValue(randomName));
            }
        }
예제 #6
0
        public void LibGit2Configuration_GetString_SectionProperty_DoesNotExists_ThrowsException()
        {
            string repoPath = CreateRepository();

            var git = new LibGit2();

            using (var config = git.GetConfiguration(repoPath))
            {
                string randomSection  = Guid.NewGuid().ToString("N");
                string randomProperty = Guid.NewGuid().ToString("N");
                Assert.Throws <KeyNotFoundException>(() => config.GetValue(randomSection, randomProperty));
            }
        }
예제 #7
0
        public void LibGit2Configuration_TryGetValue_Name_DoesNotExists_ReturnsFalse()
        {
            string repoPath = CreateRepository();

            var git = new LibGit2();

            using (var config = git.GetConfiguration(repoPath))
            {
                string randomName = $"{Guid.NewGuid():N}.{Guid.NewGuid():N}";
                bool   result     = config.TryGetValue(randomName, out string value);
                Assert.False(result);
                Assert.Null(value);
            }
        }
예제 #8
0
        public void LibGit2Configuration_TryGetValue_SectionProperty_DoesNotExists_ReturnsFalse()
        {
            string repoPath = CreateRepository();

            var git = new LibGit2();

            using (var config = git.GetConfiguration(repoPath))
            {
                string randomSection  = Guid.NewGuid().ToString("N");
                string randomProperty = Guid.NewGuid().ToString("N");
                bool   result         = config.TryGetValue(randomSection, randomProperty, out string value);
                Assert.False(result);
                Assert.Null(value);
            }
        }
예제 #9
0
        public void LibGit2Configuration_GetString_SectionScopeProperty_NullScope_ReturnsUnscopedString()
        {
            string repoPath = CreateRepository();

            Git(repoPath, "config --local user.name john.doe").AssertSuccess();

            var git = new LibGit2();

            using (var config = git.GetConfiguration(repoPath))
            {
                string value = config.GetValue("user", null, "name");
                Assert.NotNull(value);
                Assert.Equal("john.doe", value);
            }
        }
예제 #10
0
        public void LibGit2Configuration_GetString_Name_Exists_ReturnsString()
        {
            string repoPath = CreateRepository();

            Git(repoPath, "config --local user.name john.doe").AssertSuccess();

            var git = new LibGit2();

            using (var config = git.GetConfiguration(repoPath))
            {
                string value = config.GetValue("user.name");
                Assert.NotNull(value);
                Assert.Equal("john.doe", value);
            }
        }
예제 #11
0
        public void LibGit2Configuration_TryGetValue_SectionProperty_Exists_ReturnsTrueOutString()
        {
            string repoPath = CreateRepository();

            Git(repoPath, "config --local user.name john.doe").AssertSuccess();

            var git = new LibGit2();

            using (var config = git.GetConfiguration(repoPath))
            {
                bool result = config.TryGetValue("user", "name", out string value);
                Assert.True(result);
                Assert.NotNull(value);
                Assert.Equal("john.doe", value);
            }
        }
        public void LibGit2Configuration_GetString_SectionScopeProperty_Exists_ReturnsString()
        {
            string repoPath = CreateRepository(out string workDirPath);

            Git(repoPath, workDirPath, "config --local user.example.com.name john.doe").AssertSuccess();

            var trace = new NullTrace();
            var git   = new LibGit2(trace);

            using (var config = git.GetConfiguration(repoPath))
            {
                string value = config.GetValue("user", "example.com", "name");
                Assert.NotNull(value);
                Assert.Equal("john.doe", value);
            }
        }
        public void LibGit2Configuration_TryGetValue_SectionScopeProperty_NullScope_ReturnsTrueOutUnscopedString()
        {
            string repoPath = CreateRepository(out string workDirPath);

            Git(repoPath, workDirPath, "config --local user.name john.doe").AssertSuccess();

            var trace = new NullTrace();
            var git   = new LibGit2(trace);

            using (var config = git.GetConfiguration(repoPath))
            {
                bool result = config.TryGetValue("user", null, "name", out string value);
                Assert.True(result);
                Assert.NotNull(value);
                Assert.Equal("john.doe", value);
            }
        }
        public void LibGit2Configuration_Enumerate_CallbackReturnsTrue_InvokesCallbackForEachEntry()
        {
            string repoPath = CreateRepository(out string workDirPath);

            Git(repoPath, workDirPath, "config --local foo.name lancelot").AssertSuccess();
            Git(repoPath, workDirPath, "config --local foo.quest seek-holy-grail").AssertSuccess();
            Git(repoPath, workDirPath, "config --local foo.favcolor blue").AssertSuccess();

            var expectedVisitedEntries = new List <(string name, string value)>
            {
                ("foo.name", "lancelot"),
                ("foo.quest", "seek-holy-grail"),
                ("foo.favcolor", "blue")
            };

            var trace = new NullTrace();
            var git   = new LibGit2(trace);

            using (var config = git.GetConfiguration(repoPath))
            {
                var actualVisitedEntries = new List <(string name, string value)>();

                bool cb(string name, string value)
                {
                    if (name.StartsWith("foo."))
                    {
                        actualVisitedEntries.Add((name, value));
                    }

                    // Continue enumeration
                    return(true);
                }

                config.Enumerate(cb);

                Assert.Equal(expectedVisitedEntries, actualVisitedEntries);
            }
        }
예제 #15
0
        public void LibGit2Configuration_GetRepositoryPath_ReturnsRepositoryPath()
        {
            string repoBasePath        = CreateRepository();
            string expectedRepoGitPath = Path.Combine(repoBasePath, ".git") + "/";
            string fileL0Path          = Path.Combine(repoBasePath, "file.txt");
            string directoryL0Path     = Path.Combine(repoBasePath, "directory");
            string fileL1Path          = Path.Combine(directoryL0Path, "inner-file.txt");
            string directoryL1Path     = Path.Combine(directoryL0Path, "sub-directory");

            var git = new LibGit2();

            // Create files and directories
            Directory.CreateDirectory(directoryL0Path);
            Directory.CreateDirectory(directoryL1Path);
            File.WriteAllText(fileL0Path, string.Empty);
            File.WriteAllText(fileL1Path, string.Empty);

            // Check from L0 file
            string fileL0RepoPath = git.GetRepositoryPath(fileL0Path);

            AssertPathsEquivalent(expectedRepoGitPath, fileL0RepoPath);

            // Check from L0 directory
            string dirL0RepoPath = git.GetRepositoryPath(directoryL0Path);

            AssertPathsEquivalent(expectedRepoGitPath, dirL0RepoPath);

            // Check from L1 file
            string fileL1RepoPath = git.GetRepositoryPath(fileL1Path);

            AssertPathsEquivalent(expectedRepoGitPath, fileL1RepoPath);

            // Check from L1 directory
            string dirL1RepoPath = git.GetRepositoryPath(directoryL1Path);

            AssertPathsEquivalent(expectedRepoGitPath, dirL1RepoPath);
        }