-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Allow user to specify default remote when there's no remote named "origin" #1730
Changes from 18 commits
ccc49e9
d1d6c4d
2a04e80
7b47f79
636b11e
b6e4a63
f725bbd
cdac730
8acf74b
e4f223f
3480932
651c5f1
afb7067
d9c0cfa
c49765c
ba14b3f
a090996
8dbeee0
fbf7c74
3944b4f
5330df2
3cfb8d2
93d3382
d9a19ce
1629695
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -152,7 +152,13 @@ public interface IGitClient | |
| /// <returns></returns> | ||
| Task UnsetConfig(IRepository repository, string key); | ||
|
|
||
| Task<Remote> GetHttpRemote(IRepository repo, string remote); | ||
| /// <summary> | ||
| /// Get a <see cref="Remote"/> that used the http protocol. | ||
| /// </summary> | ||
| /// <param name="repository">The repository</param> | ||
| /// <param name="remote">The name of the remote or null to use original remote.</param> | ||
|
||
| /// <returns>A <see cref="Remote"/> that uses the http protocol.</returns> | ||
| Task<Remote> GetHttpRemote(IRepository repo, string remote = null); | ||
|
|
||
| /// <summary> | ||
| /// Extracts a file at a specified commit from the repository. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,14 +13,16 @@ namespace GitHub.Services | |
| [PartCreationPolicy(CreationPolicy.NonShared)] | ||
| public class GitService : IGitService | ||
| { | ||
| const string defaultOriginName = "origin"; | ||
|
|
||
| /// <summary> | ||
| /// Returns the URL of the remote for the specified <see cref="repository"/>. If the repository | ||
| /// is null or no remote named origin exists, this method returns null | ||
| /// </summary> | ||
| /// <param name="repository">The repository to look at for the remote.</param> | ||
| /// <param name="remote">The name of the remote to look for</param> | ||
| /// <returns>Returns a <see cref="UriString"/> representing the uri of the remote normalized to a GitHub repository url or null if none found.</returns> | ||
| public UriString GetUri(IRepository repository, string remote = "origin") | ||
| public UriString GetUri(IRepository repository, string remote = null) | ||
| { | ||
| return UriString.ToUriString(GetRemoteUri(repository, remote)?.ToRepositoryUrl()); | ||
| } | ||
|
|
@@ -36,7 +38,7 @@ public UriString GetUri(IRepository repository, string remote = "origin") | |
| /// <param name="path">The path to start probing</param> | ||
| /// <param name="remote">The name of the remote to look for</param> | ||
| /// <returns>Returns a <see cref="UriString"/> representing the uri of the remote normalized to a GitHub repository url or null if none found.</returns> | ||
| public UriString GetUri(string path, string remote = "origin") | ||
| public UriString GetUri(string path, string remote = null) | ||
| { | ||
| using (var repo = GetRepository(path)) | ||
| { | ||
|
|
@@ -66,10 +68,21 @@ public IRepository GetRepository(string path) | |
| /// <param name="repo"></param> | ||
| /// <param name="remote">The name of the remote to look for</param> | ||
| /// <returns></returns> | ||
| public UriString GetRemoteUri(IRepository repo, string remote = "origin") | ||
| public UriString GetRemoteUri(IRepository repo, string remote = null) | ||
| { | ||
| if (repo == null) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| remote = remote ?? TryGetDefaultRemoteName(repo); | ||
| if (remote == null) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| return repo | ||
| ?.Network | ||
| .Network | ||
| .Remotes[remote] | ||
| ?.Url; | ||
| } | ||
|
|
@@ -117,5 +130,43 @@ public Task<string> GetLatestPushedSha(string path) | |
| } | ||
| }); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Find a remote named "origin" or the remote tracking "master". | ||
| /// </summary> | ||
| /// <param name="repo">The <see cref="IRepository" /> to find a remote for.</param> | ||
| /// <returns>The remote named "origin" or the remote tracking "master"</returns> | ||
| /// <exception cref="InvalidOperationException">If repository contains no "origin" remote or "master" branch.</exception> | ||
| public string GetDefaultRemoteName(IRepository repo) | ||
| { | ||
| var remoteName = TryGetDefaultRemoteName(repo); | ||
| if (remoteName != null) | ||
| { | ||
| return remoteName; | ||
| } | ||
|
|
||
| throw new InvalidOperationException("Can't get default remote name because repository contains no `origin` remote or `master` branch."); | ||
| } | ||
|
|
||
| static string TryGetDefaultRemoteName(IRepository repo) | ||
| { | ||
| var remotes = repo.Network.Remotes; | ||
| var remote = remotes[defaultOriginName]; | ||
| if (remote != null) | ||
| { | ||
| // Use remote named "origin" | ||
| return remote.Name; | ||
| } | ||
|
|
||
| var masterBranch = repo.Branches["master"]; | ||
| var remoteName = masterBranch?.RemoteName; | ||
| if (remoteName != null) | ||
| { | ||
| // Use remote from the "master" branch | ||
| return remoteName; | ||
|
||
| } | ||
|
|
||
| return null; | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be
uses?