|
| 1 | +using System; |
| 2 | +using System.ComponentModel.Composition; |
| 3 | +using System.Linq; |
| 4 | +using GitHub.Primitives; |
| 5 | +using LibGit2Sharp; |
| 6 | +using Microsoft.VisualStudio.TeamFoundation.Git.Extensibility; |
| 7 | + |
| 8 | +namespace GitHub.Services |
| 9 | +{ |
| 10 | + [Export(typeof(IVSServices))] |
| 11 | + [PartCreationPolicy(CreationPolicy.Shared)] |
| 12 | + public class GitService : IGitService |
| 13 | + { |
| 14 | + /// <summary> |
| 15 | + /// Returns the URL of the remote named "origin" for the specified <see cref="repository"/>. If the repository |
| 16 | + /// is null or no remote named origin exists, this method returns null |
| 17 | + /// </summary> |
| 18 | + /// <param name="repository">The repository to look at for the remote.</param> |
| 19 | + /// <returns>A <see cref="UriString"/> representing the origin or null if none found.</returns> |
| 20 | + public UriString GetUri(IRepository repository) |
| 21 | + { |
| 22 | + return UriString.ToUriString(GetUriFromRepository(repository)?.ToRepositoryUrl()); |
| 23 | + } |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// Probes for a git repository and if one is found, returns a <see cref="UriString"/> for the repository's |
| 27 | + /// remote named "origin" if one is found |
| 28 | + /// </summary> |
| 29 | + /// <remarks> |
| 30 | + /// The lookup checks to see if the specified <paramref name="path"/> is a repository. If it's not, it then |
| 31 | + /// walks up the parent directories until it either finds a repository, or reaches the root disk. |
| 32 | + /// </remarks> |
| 33 | + /// <param name="path">The path to start probing</param> |
| 34 | + /// <returns>A <see cref="UriString"/> representing the origin or null if none found.</returns> |
| 35 | + public UriString GetUri(string path) |
| 36 | + { |
| 37 | + return GetUri(GetRepo(path)); |
| 38 | + } |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// Probes for a git repository and if one is found, returns a <see cref="UriString"/> for the repository's |
| 42 | + /// remote named "origin" if one is found |
| 43 | + /// </summary> |
| 44 | + /// <remarks> |
| 45 | + /// The lookup checks to see if the path specified by the RepositoryPath property of the specified |
| 46 | + /// <see cref="repoInfo"/> is a repository. If it's not, it then walks up the parent directories until it |
| 47 | + /// either finds a repository, or reaches the root disk. |
| 48 | + /// </remarks> |
| 49 | + /// <param name="repoInfo">The repository information containing the path to start probing</param> |
| 50 | + /// <returns>A <see cref="UriString"/> representing the origin or null if none found.</returns> |
| 51 | + public UriString GetUri(IGitRepositoryInfo repoInfo) |
| 52 | + { |
| 53 | + return GetUri(GetRepo(repoInfo)); |
| 54 | + } |
| 55 | + |
| 56 | + /// <summary> |
| 57 | + /// Probes for a git repository and if one is found, returns a <see cref="IRepository"/> instance for the |
| 58 | + /// repository. |
| 59 | + /// </summary> |
| 60 | + /// <remarks> |
| 61 | + /// The lookup checks to see if the path specified by the RepositoryPath property of the specified |
| 62 | + /// <see cref="repoInfo"/> is a repository. If it's not, it then walks up the parent directories until it |
| 63 | + /// either finds a repository, or reaches the root disk. |
| 64 | + /// </remarks> |
| 65 | + /// <param name="repoInfo">The repository information containing the path to start probing</param> |
| 66 | + /// <returns>An instance of <see cref="IRepository"/> or null</returns> |
| 67 | + |
| 68 | + public IRepository GetRepo(IGitRepositoryInfo repoInfo) |
| 69 | + { |
| 70 | + return GetRepo(repoInfo?.RepositoryPath); |
| 71 | + } |
| 72 | + |
| 73 | + /// <summary> |
| 74 | + /// Probes for a git repository and if one is found, returns a <see cref="IRepository"/> instance for the |
| 75 | + /// repository. |
| 76 | + /// </summary> |
| 77 | + /// <remarks> |
| 78 | + /// The lookup checks to see if the specified <paramref name="path"/> is a repository. If it's not, it then |
| 79 | + /// walks up the parent directories until it either finds a repository, or reaches the root disk. |
| 80 | + /// </remarks> |
| 81 | + /// <param name="path">The path to start probing</param> |
| 82 | + /// <returns>An instance of <see cref="IRepository"/> or null</returns> |
| 83 | + public IRepository GetRepo(string path) |
| 84 | + { |
| 85 | + var repoPath = Repository.Discover(path); |
| 86 | + return repoPath == null ? null : new Repository(repoPath); |
| 87 | + } |
| 88 | + |
| 89 | + internal static UriString GetUriFromRepository(IRepository repo) |
| 90 | + { |
| 91 | + return repo |
| 92 | + ?.Network |
| 93 | + .Remotes |
| 94 | + .FirstOrDefault(x => x.Name.Equals("origin", StringComparison.Ordinal)) |
| 95 | + ?.Url; |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments