csharp15d33a96
| 1 | namespace GitMcp.Core; |
| 2 | |
| 3 | /// <summary> |
| 4 | /// Определение корня git worktree: обычный репозиторий (<c>.git</c> — каталог) или субмодуль/secondary worktree (<c>.git</c> — файл с <c>gitdir:</c>). |
| 5 | /// </summary> |
| 6 | public static class GitWorkTree |
| 7 | { |
| 8 | /// <summary> |
| 9 | /// Нормализует путь и проверяет, что это корень рабочего дерева git. |
| 10 | /// </summary> |
| 11 | /// <exception cref="ArgumentException">В каталоге нет <c>.git</c> (ни каталога, ни файла).</exception> |
| 12 | public static string GetRepoRoot(string repoRoot) |
| 13 | { |
| 14 | var root = Path.GetFullPath(repoRoot.Trim()); |
| 15 | if (File.Exists(root)) |
| 16 | root = Path.GetDirectoryName(root) ?? root; |
| 17 | if (!IsGitWorkTreeRoot(root)) |
| 18 | throw new ArgumentException($"Not a git repository: {root}"); |
| 19 | return root; |
| 20 | } |
| 21 | |
| 22 | /// <summary> |
| 23 | /// true, если в <paramref name="directory"/> есть <c>.git</c> как каталог (обычный clone/init) или как файл (субмодуль, git worktree add). |
| 24 | /// </summary> |
| 25 | public static bool IsGitWorkTreeRoot(string directory) |
| 26 | { |
| 27 | var gitPath = Path.Combine(directory, ".git"); |
| 28 | if (Directory.Exists(gitPath)) |
| 29 | return true; |
| 30 | if (File.Exists(gitPath)) |
| 31 | return true; |
| 32 | return false; |
| 33 | } |
| 34 | } |
| 35 | |