Exemplo n.º 1
0
        public static GitRepository TryParse(string directory)
        {
            var rootDirectory = FileSystemTasks.SearchDirectory(directory, x => x.GetDirectories(".git").Any());

            ControlFlow.Assert(rootDirectory != null, $"Could not locate '.git' directory while traversing up from '{directory}'.");
            var gitDirectory = Path.Combine(rootDirectory, ".git");

            var headFile = Path.Combine(gitDirectory, "HEAD");

            ControlFlow.Assert(File.Exists(headFile), $"File.Exists({headFile})");
            var headFileContent = File.ReadAllLines(headFile);
            var head            = headFileContent.First();

            var configFile        = Path.Combine(gitDirectory, "config");
            var configFileContent = File.ReadAllLines(configFile);
            var url = configFileContent
                      .Select(x => x.Trim())
                      .SkipWhile(x => x != "[remote \"origin\"]")
                      .Skip(count: 1)
                      .TakeWhile(x => !x.StartsWith("["))
                      .Single(x => x.StartsWithOrdinalIgnoreCase("url = "))
                      .Split('=')[1];

            var match = new[]
            {
                @"git@(?<endpoint>.*):(?<owner>.*)/(?<name>.*?)(\.git)?$",
                @"https://(?<endpoint>.*)/(?<owner>.*)/(?<name>.*?)(\.git)?$"
            }.Select(x => Regex.Match(url, x)).FirstOrDefault(x => x.Success);

            if (match == null)
            {
                return(null);
            }
            var branchMatch = Regex.Match(head, "^ref: refs/heads/(?<branch>.*)");

            return(new GitRepository
            {
                Endpoint = match.Groups["endpoint"].Value,
                Owner = match.Groups["owner"].Value,
                Name = match.Groups["name"].Value,
                Head = head,
                Branch = branchMatch.Success ? branchMatch.Groups["branch"].Value : null
            });
        }
Exemplo n.º 2
0
        public static GitRepository FromLocalDirectory(string directory, string branch = null, string remote = "origin")
        {
            var rootDirectory = FileSystemTasks.SearchDirectory(directory, x => x.GetDirectories(".git").Any());

            if (rootDirectory == null)
            {
                return(null);
            }
            var gitDirectory = Path.Combine(rootDirectory, ".git");

            var headFile = Path.Combine(gitDirectory, "HEAD");

            ControlFlow.Assert(File.Exists(headFile), $"File.Exists({headFile})");
            var headFileContent = File.ReadAllLines(headFile);
            var head            = headFileContent.First();
            var branchMatch     = Regex.Match(head, "^ref: refs/heads/(?<branch>.*)");

            var configFile        = Path.Combine(gitDirectory, "config");
            var configFileContent = File.ReadAllLines(configFile);
            var url = configFileContent
                      .Select(x => x.Trim())
                      .SkipWhile(x => x != $"[remote \"{remote}\"]")
                      .Skip(count: 1)
                      .TakeWhile(x => !x.StartsWith("["))
                      .SingleOrDefault(x => x.StartsWithOrdinalIgnoreCase("url = "))
                      ?.Split('=')[1];

            if (url == null)
            {
                return(null);
            }

            var(endpoint, identifier) = ParseUrl(url);

            return(new GitRepository(
                       endpoint,
                       identifier,
                       rootDirectory,
                       head,
                       branch ?? (branchMatch.Success ? branchMatch.Groups["branch"].Value : null)));
        }