|
| 1 | +// Licensed to Elasticsearch B.V under one or more agreements. |
| 2 | +// Elasticsearch B.V licenses this file to you under the Apache 2.0 License. |
| 3 | +// See the LICENSE file in the project root for more information |
| 4 | + |
| 5 | +using System.Diagnostics.CodeAnalysis; |
| 6 | +using System.IO.Abstractions; |
| 7 | + |
| 8 | +namespace Elastic.Documentation.Configuration; |
| 9 | + |
| 10 | +/// <summary> |
| 11 | +/// Reads <c>remote.origin.url</c> from a local Git checkout using the file system (no subprocess). |
| 12 | +/// </summary> |
| 13 | +public static class GitRemoteConfigurationReader |
| 14 | +{ |
| 15 | + /// <summary> |
| 16 | + /// Reads <c>.git/config</c>, or the <c>config</c> file referenced by a <c>.git</c> worktree pointer file. |
| 17 | + /// </summary> |
| 18 | + public static bool TryReadOriginUrl(IFileSystem fileSystem, string repositoryRoot, [NotNullWhen(true)] out string? url) |
| 19 | + { |
| 20 | + url = null; |
| 21 | + try |
| 22 | + { |
| 23 | + var gitPath = fileSystem.Path.Combine(repositoryRoot, ".git"); |
| 24 | + if (fileSystem.Directory.Exists(gitPath)) |
| 25 | + { |
| 26 | + var configPath = fileSystem.Path.Combine(gitPath, "config"); |
| 27 | + return TryReadOriginUrlFromConfigPath(fileSystem, configPath, out url); |
| 28 | + } |
| 29 | + |
| 30 | + if (!fileSystem.File.Exists(gitPath)) |
| 31 | + return false; |
| 32 | + |
| 33 | + var gitFileText = fileSystem.File.ReadAllText(gitPath); |
| 34 | + var firstLineBreak = gitFileText.IndexOfAny(['\r', '\n']); |
| 35 | + var firstLine = firstLineBreak >= 0 ? gitFileText[..firstLineBreak] : gitFileText; |
| 36 | + firstLine = firstLine.Trim(); |
| 37 | + if (!firstLine.StartsWith("gitdir:", StringComparison.OrdinalIgnoreCase)) |
| 38 | + return false; |
| 39 | + |
| 40 | + var gitDir = firstLine["gitdir:".Length..].Trim(); |
| 41 | + if (string.IsNullOrEmpty(gitDir)) |
| 42 | + return false; |
| 43 | + |
| 44 | + var resolvedGitDir = fileSystem.Path.IsPathFullyQualified(gitDir) |
| 45 | + ? gitDir |
| 46 | + : fileSystem.Path.GetFullPath(fileSystem.Path.Combine(repositoryRoot, gitDir)); |
| 47 | + |
| 48 | + var commonDirFile = fileSystem.Path.Combine(resolvedGitDir, "commondir"); |
| 49 | + if (!fileSystem.File.Exists(commonDirFile)) |
| 50 | + return false; |
| 51 | + |
| 52 | + var commonDirRelative = fileSystem.File.ReadAllText(commonDirFile).Trim(); |
| 53 | + var commonDir = fileSystem.Path.IsPathFullyQualified(commonDirRelative) |
| 54 | + ? commonDirRelative |
| 55 | + : fileSystem.Path.GetFullPath(fileSystem.Path.Combine(resolvedGitDir, commonDirRelative)); |
| 56 | + |
| 57 | + var worktreeConfigPath = fileSystem.Path.Combine(commonDir, "config"); |
| 58 | + return TryReadOriginUrlFromConfigPath(fileSystem, worktreeConfigPath, out url); |
| 59 | + } |
| 60 | + catch (IOException) |
| 61 | + { |
| 62 | + url = null; |
| 63 | + return false; |
| 64 | + } |
| 65 | + catch (UnauthorizedAccessException) |
| 66 | + { |
| 67 | + url = null; |
| 68 | + return false; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + private static bool TryReadOriginUrlFromConfigPath(IFileSystem fileSystem, string configPath, [NotNullWhen(true)] out string? url) |
| 73 | + { |
| 74 | + url = null; |
| 75 | + try |
| 76 | + { |
| 77 | + if (!fileSystem.File.Exists(configPath)) |
| 78 | + return false; |
| 79 | + |
| 80 | + var content = fileSystem.File.ReadAllText(configPath); |
| 81 | + return GitConfigOriginParser.TryGetRemoteOriginUrl(content, out url); |
| 82 | + } |
| 83 | + catch (IOException) |
| 84 | + { |
| 85 | + url = null; |
| 86 | + return false; |
| 87 | + } |
| 88 | + catch (UnauthorizedAccessException) |
| 89 | + { |
| 90 | + url = null; |
| 91 | + return false; |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments