Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Conversation

@jcansdale
Copy link
Collaborator

@jcansdale jcansdale commented Jun 13, 2018

What this PR does

Some users like to rename their "origin" remote so they're forced to be explicit about which repository they're pushing to. When this happend the extension wasn't able to detect their repository originated from GitHub.

This PR takes advantage of the fact the order of remotes doesn't change when a remote is renamed, added or deleted. When a user renames their "origin" remote, it will remain the first defined remote. We can fall back to using the first remote as the originating remote/clone URL.

  • Use the first remote as the originating remote when no "origin" is defined

TODO

The default remote "origin" is hard-coded in a bunch of other places:

  C:\Source\github.com\github\VisualStudio\src\GitHub.App\Services\PullRequestService.cs(290):                        var remote = await gitClient.GetHttpRemote(repo, "origin");
  C:\Source\github.com\github\VisualStudio\src\GitHub.App\Services\PullRequestService.cs(374):                    var remote = await gitClient.GetHttpRemote(repo, "origin");
  C:\Source\github.com\github\VisualStudio\src\GitHub.App\Services\PullRequestService.cs(440):                        var remote = await gitClient.GetHttpRemote(repo, "origin");
  C:\Source\github.com\github\VisualStudio\src\GitHub.App\Services\PullRequestService.cs(500):                    var remote = await gitClient.GetHttpRemote(repo, "origin");
  C:\Source\github.com\github\VisualStudio\src\GitHub.App\Services\PullRequestService.cs(613):                var remote = await gitClient.GetHttpRemote(repo, "origin");
  C:\Source\github.com\github\VisualStudio\src\GitHub.App\Services\PullRequestService.cs(666):                var remote = await gitClient.GetHttpRemote(repo, "origin");
  C:\Source\github.com\github\VisualStudio\src\GitHub.App\Services\RepositoryForkService.cs(79):                            var remote = await gitClient.GetHttpRemote(activeRepo, "origin");
  C:\Source\github.com\github\VisualStudio\src\GitHub.App\Services\RepositoryForkService.cs(106):            await gitClient.SetRemote(repository, "origin", originUri);
  C:\Source\github.com\github\VisualStudio\src\GitHub.App\Services\RepositoryPublishService.cs(50):                                     await gitClient.SetRemote(repo.LocalRepo, "origin", new Uri(repo.RemoteRepo.CloneUrl));
  C:\Source\github.com\github\VisualStudio\src\GitHub.App\Services\RepositoryPublishService.cs(51):                                     await gitClient.Push(repo.LocalRepo, "master", "origin");
  C:\Source\github.com\github\VisualStudio\src\GitHub.App\Services\RepositoryPublishService.cs(52):                                     await gitClient.Fetch(repo.LocalRepo, "origin");
  C:\Source\github.com\github\VisualStudio\src\GitHub.App\Services\RepositoryPublishService.cs(53):                                     await gitClient.SetTrackingBranch(repo.LocalRepo, "master", "origin");
  C:\Source\github.com\github\VisualStudio\src\GitHub.InlineReviews\Services\PullRequestSessionService.cs(237):                    await gitClient.Fetch(repo, "origin", sha, pullHeadRef);

How to test

  1. git remote rename origin renamed_origin
  2. Confirm that GitHub section doesn't appear on Team Explorer - Home
  3. Install the build from this PR
  4. Confirm that GitHub section now appears on Team Explorer - Home

Fixes #1729

jcansdale added 16 commits June 13, 2018 18:29
When a repository is cloned, a remote named "origin" is automatically
added with the clone URL. The remote list order doesn't change when a
remote is added, deleted or renamed. This means that if a user renames
"origin", the first remote in the list will still contain the original
clone URL.
Check first remote in list is returned when there's no "origin".
This should be used instead of assuming the default remote "origin".
Find original origin using IGitService.FindOriginalRemoteName.
The original remote name will be calculated in the method.
Throw InvalidOperationException if repository contains no remotes.
Default to using GetOriginRemoteName in GetHttpRemote.
If remote isn't specified it will use a default origin remote name.
Don't hard-code origin remote as "origin".
Don't hard-code the origin name as "origin".
Should throw InvalidOperationException when no remotes.
GetOriginRemoteName should return the configured remote name.
If there is no "origin" remote, use remote from HEAD or "master" branch.
Rename method from GetOriginRemoteName to GetDefaultRemoteName.
@meaghanlewis meaghanlewis added this to the 2.5.4 milestone Jun 14, 2018
Make sure GitService.GetUri is robust.
We need the default remote to remain consistent.

Task<Remote> GetHttpRemote(IRepository repo, string remote);
/// <summary>
/// Get a <see cref="Remote"/> that used the http protocol.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be uses?

/// 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>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does "original remote" mean here? origin? I've never heard of that referred to as the "original remote". Would "default remote" maybe be better?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was supposed to find the original remote that was created when the repository was cloned. Alas libgit2 returns remotes in alphabetical order, so it's impossible to find the first created remote short of manually parsing the .git\config file. This would be too much of a hack even for my taste. 😉

if (remoteName != null)
{
// Use remote from the "master" branch
return remoteName;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem to do what the PR says in the description. The description says "We can fall back to using the first remote as the originating remote/clone URL.", instead this uses master's remote as the default remote.

I'm not sure this is a good idea as I sometimes make master track upstream/master rather than origin/master on forks.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I agree completely. Making master track upstream/master is almost certainly what people should be doing. I imagine it's pretty rare to want your own master that's independant of upstream/master.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So do you agree the logic here is wrong? Why does it not do what you say it does in the description, i.e. "fall back to using the first remote as the originating remote/clone URL"?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So do you agree the logic here is wrong?

Yup. Fixed in latest push.

Some users like to rename origin and delete master, so this wouldn't have worked for them anyway.

Why does it not do what you say it does in the description, i.e. "fall back to using the first remote as the originating remote/clone URL"?

It turns out that libgit2 sorts the list of remotes in alphabetical order, which completely messes up this logic. 😭

Chances are user would prefer to have master track upstream/master not
origin/master!
Some users like to work without an "origin" remote defined. Allow them
to set a "ghfvs.origin" config property so GHfVS knows which remote to
treat as the origin.
@jcansdale jcansdale changed the title Find original remote when there's no remote named "origin" Allow user to specify origin when there's no remote named "origin" Jun 19, 2018
@jcansdale jcansdale changed the title Allow user to specify origin when there's no remote named "origin" Allow user to specify default remote when there's no remote named "origin" Jun 19, 2018
@grokys
Copy link
Contributor

grokys commented Jun 21, 2018

@jcansdale is this PR still needed after #1752?

@meaghanlewis meaghanlewis removed this from the 2.5.4 milestone Jul 13, 2018
@jcansdale
Copy link
Collaborator Author

We're planning to warn the user with a call-out notification (see #2026) rather than allowing them to explicitly name the default remote.

@jcansdale jcansdale closed this Nov 22, 2018
@jcansdale jcansdale deleted the fixes/1729-find-original-remote-name branch November 22, 2018 11:56
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants