Skip to content

add --branch flag to pr checkout#4005

Merged
mislav merged 2 commits intocli:trunkfrom
despreston:835-rename-checkout
Aug 5, 2021
Merged

add --branch flag to pr checkout#4005
mislav merged 2 commits intocli:trunkfrom
despreston:835-rename-checkout

Conversation

@despreston
Copy link
Contributor

Allows renaming the checked out branch.

Closes #835

Copy link
Contributor

@billygriffin billygriffin left a comment

Choose a reason for hiding this comment

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

Thanks! I'm wondering whether --local is the best verbiage here, and not something like --branch-name and -b. Anyone have thoughts on that? -b feels more consistent with what you'd expect using git checkout -b

@despreston
Copy link
Contributor Author

-b works for me. I'll change it.

Copy link
Contributor

@mislav mislav left a comment

Choose a reason for hiding this comment

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

Thanks for working on this!

In addition to my line comments, an important note: it seems that you have only added this functionality to cmdsForExistingRemote. However, if --branch flag was to work in all cases, then you will have to edit cmdsForMissingRemote. An existing git remote will not always be located for a PR; in fact, cmdsForMissingRemote is more common for PRs to open source repos that come from forks.

@despreston despreston changed the title add --local flag to pr checkout add --branch flag to pr checkout Jul 19, 2021
Copy link
Contributor

@mislav mislav left a comment

Choose a reason for hiding this comment

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

Thanks for the fast update!

Could there also be an added test for the case when opts.BranchName is set, but there is no existing git remote that matches the head repository of the PR?

cmds = append(cmds, []string{"git", "checkout", newBranchName})

if opts.BranchName != "" {
cmds = append(cmds, []string{"git", "checkout", "-b", opts.BranchName, "--no-track", newBranchName})
Copy link
Contributor

Choose a reason for hiding this comment

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

We should use --track instead of --no-track here, because --track is implied by git checkout if --branch wasn't specified (the else block in this conditional).

Copy link
Contributor Author

@despreston despreston Jul 19, 2021

Choose a reason for hiding this comment

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

hm. Regarding the new test, I'm not sure how to write that. Using what exists now, I thought I could just remove everything in remotes but the tests panic with what seems like an unrelated error.

		{
			name: "with local branch rename and no existing git remote",
			opts: &CheckoutOptions{
				SelectorArg: "123",
				BranchName:  "foobar",
				Finder: func() shared.PRFinder {
					baseRepo, pr := stubPR("OWNER/REPO:master", "hubot/REPO:feature")
					pr.MaintainerCanModify = true
					pr.HeadRepository = nil
					finder := shared.NewMockFinder("123", pr, baseRepo)
					return finder
				}(),
				Config: func() (config.Config, error) {
					return config.NewBlankConfig(), nil
				},
				Branch: func() (string, error) {
					return "main", nil
				},
			},
			remotes: map[string]string{},
			runStubs: func(cs *run.CommandStubber) {
				cs.Register(`git fetch origin refs/pull/123/head:feature`, 0, "")
				cs.Register(`git config branch\.feature\.merge`, 1, "")
				cs.Register(`git checkout -b foobar --no-track feature`, 0, "")
				cs.Register(`git config branch\.feature\.remote origin`, 0, "")
				cs.Register(`git config branch\.feature\.merge refs/pull/123/head`, 0, "")
			},
		},

@mislav mislav self-assigned this Jul 20, 2021
@mislav mislav added the enhancement a request to improve CLI label Aug 4, 2021
@mislav mislav force-pushed the 835-rename-checkout branch from 72665d3 to 9a485dd Compare August 5, 2021 18:44
Copy link
Contributor

@mislav mislav left a comment

Choose a reason for hiding this comment

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

I'm sorry that the tests were tricky to make— this is particularly hairy commands with lots of hard-to-track test stubs. Thank you for your hard work! I've pushed some tweaks.

@mislav mislav enabled auto-merge August 5, 2021 18:47
@mislav mislav merged commit ac13fc8 into cli:trunk Aug 5, 2021
normagenemed36

This comment was marked as spam.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement a request to improve CLI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow a local branch name to be provided when checking out a pull request

4 participants