Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make hub compare work on forks #2221

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

sj26
Copy link

@sj26 sj26 commented Aug 9, 2019

I use git compare without any arguments all the time when working on feature branches in a one repository workflow - when pushing branches directly to origin and using git compare to open a comparison for review before opening a pull request. But it doesn't work for branches pushed to a fork.

This change makes it understand the current branch might be pushed to another remote and starts the comparison from there, then GitHub.com can figure out the default base.

It still works great for single-repo workflows and still respects the base flag for explicitly providing a base if required.

Before:

     git clone hub
     git fork
     git checkout -b some-feature
     ...
     git commit -m '...'
     git push $USER HEAD -u
     git compare

     > Usage: hub compare [-uc] [<USER>] [[<START>...]<END>]
     > hub compare [-uc] [-b <BASE>]

     git compare -b origin:master

     > Usage: hub compare [-uc] [<USER>] [[<START>...]<END>]
     > hub compare [-uc] [-b <BASE>]

     git remote -v

     > origin	https://github.com/github/hub.git (fetch)
     > origin	https://github.com/github/hub.git (push)
     > sj26	https://github.com/sj26/hub.git (fetch)
     > sj26	https://github.com/sj26/hub.git (push)

     git compare github

     > Usage: hub compare [-uc] [<USER>] [[<START>...]<END>]
     > hub compare [-uc] [-b <BASE>]

     /me gets frustrated

     git compare github HEAD

     # opens browser at:
     # https://github.com/github/hub/compare/HEAD

     git compare sj26 HEAD

     # opens browser at:
     # https://github.com/sj26/hub/compare/HEAD

     /me tears out hair

After:

     git clone hub
     git fork
     git checkout -b some-feature
     ...
     git commit -m '...'
     git push $USER HEAD -u
     git compare
     # opens browser at:
     # https://github.com/sj26/hub/compare/some-feature
     # which redirects to:
     # https://github.com/github/hub/compare/master...sj26:some-feature
     /me happily reviews commits then opens a pull request

(I used this branch to open this PR!)

@mislav
Copy link
Member

mislav commented Aug 19, 2019

Thank you for attempting to fix this, but this breaks CI checks. Can you look into that?

@mislav mislav added the bug label Aug 19, 2019
@sj26 sj26 force-pushed the implicit-compare-from-forks branch from 1ecd7ce to fcc5056 Compare Aug 20, 2019
@sj26
Copy link
Author

sj26 commented Aug 20, 2019

Sorry for the test oversight. I have rebased this on laster master and adjusted the tests. I also improved the implementation slightly.

"""
Usage: hub compare [-uc] [<USER>] [[<START>...]<END>]
hub compare [-uc] [-b <BASE>]
"""
Copy link
Author

@sj26 sj26 Aug 20, 2019

Choose a reason for hiding this comment

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

These tests no longer made sense. If I'm on github.com and I fork a repository, then I jump in a terminal and clone my fork, then I push some commits on the default branch, then run git compare I should be sent to a comparison of the default branch on my fork, which might suggest I pull request those changes back to the forked repository. So opening a comparison from the default branch is now valid.

We might be able to do something more sensible if we knew the project was a fork or not, but I couldn't find a nice way to do this already in hub (project.IsFork()?), and without that information this seems the best decision.


if flagCompareBase != "" {
if r == flagCompareBase {
utils.Check(command.UsageError(""))
Copy link
Author

@sj26 sj26 Aug 20, 2019

Choose a reason for hiding this comment

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

This was to satisfy an existing test.

I use `git compare` without any arguments all the time when working on
feature branches in a one repository workflow - when pushing branches
directly to origin and using `git compare` to open a comparison for
review before opening a pull request. But it doesn't work for branches
pushed to a fork.

This change makes it understand the current branch might be pushed to
another remote and starts the comparison from there, then GitHub.com can
figure out the default base.

It still works great for single-repo workflows and still respects the
base flag for explicitly providing a base if required.

Before:

         git clone hub
         git fork
         git checkout -b some-feature
         ...
         git commit -m '...'
         git push $USER HEAD -u
         git compare

         > Usage: hub compare [-uc] [<USER>] [[<START>...]<END>]
         > hub compare [-uc] [-b <BASE>]

         git compare -b origin:master

         > Usage: hub compare [-uc] [<USER>] [[<START>...]<END>]
         > hub compare [-uc] [-b <BASE>]

         git remote -v

         > origin	https://github.com/github/hub.git (fetch)
         > origin	https://github.com/github/hub.git (push)
         > sj26	https://github.com/sj26/hub.git (fetch)
         > sj26	https://github.com/sj26/hub.git (push)

         git compare github

         > Usage: hub compare [-uc] [<USER>] [[<START>...]<END>]
         > hub compare [-uc] [-b <BASE>]

         /me gets frustrated

         git compare github HEAD

         # opens browser at:
         # https://github.com/github/hub/compare/HEAD

         git compare sj26 HEAD

         # opens browser at:
         # https://github.com/sj26/hub/compare/HEAD

         /me tears out hair

After:

         git clone hub
         git fork
         git checkout -b some-feature
         ...
         git commit -m '...'
         git push $USER HEAD -u
         git compare
         # opens browser at:
         # https://github.com/sj26/hub/compare/some-feature
         # which redirects to:
         # github/hub@master...sj26:some-feature
         /me happily reviews commits then opens a pull request
@sj26 sj26 force-pushed the implicit-compare-from-forks branch from fcc5056 to cea50a5 Compare Aug 20, 2019
And I am on the "feature" branch with upstream "sj26/experimental"
When I successfully run `hub compare`
Then there should be no output
And "open https://github.com/sj26/hub/compare/experimental" should be run
Copy link
Author

@sj26 sj26 Aug 20, 2019

Choose a reason for hiding this comment

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

I've also added a test for the new functionality.

@sj26
Copy link
Author

sj26 commented Jul 6, 2021

This has gone super stale. Would you consider merging if I rebase it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants