Skip to content

Add support for git flags in gh repo fork#2282

Merged
vilmibm merged 6 commits intocli:trunkfrom
cristiand391:repo-fork-gitflags
Jan 21, 2021
Merged

Add support for git flags in gh repo fork#2282
vilmibm merged 6 commits intocli:trunkfrom
cristiand391:repo-fork-gitflags

Conversation

@cristiand391
Copy link
Contributor

Fix #2236

I added the same custom error msg from #1475 for gh repo fork when trying to use unknown git flags.

With no argument, creates a fork of the current repository. Otherwise, forks the specified repository.`,
With no argument, creates a fork of the current repository. Otherwise, forks the specified repository.

If '--clone' is specified, you can pass additional 'git clone' flags by listing them after '--'.`,
Copy link
Contributor

Choose a reason for hiding this comment

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

"If the '--clone' flag is specified, additional 'git clone' flags can be passed in by listing them after '--'."

I think this wording is a bit more clear.

cc @ampinsk

Copy link
Contributor

Choose a reason for hiding this comment

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

If --clone wasn't specified, a prompt will be shown and the user will still have an opportunity to clone. Therefore, gitflags might take effect regardless of whether --clone was explicitly used or not.

Copy link
Contributor

@samcoe samcoe 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 contributing. The code looks good to me, I just made one comment regarding the language.

cmd := &cobra.Command{
Use: "fork [<repository>]",
Args: cobra.MaximumNArgs(1),
Use: "fork [<repository>] [-- <gitflags>...]",
Copy link
Contributor

Choose a reason for hiding this comment

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

Unlike the repo clone command, the <repository> argument here is optional, and so are the gitflags. Parsing flags is therefore tricky, because if you pass any gitflags while omitting the <repository> argument, the first gitflag (e.g. --depth) will get interpreted as the repository name.

The solution would have to be explicitly detecting what is left and what is right of the -- flag separator, but I don't think the pflag parser gives us that information. What the command sees is the list of all arguments joined together in a []string slice with the -- separator removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've tried a few possible cases:

gh repo fork -- --depth 1
argument error: expected the "[HOST/]OWNER/REPO" format, got "--depth"

-----------------------------------------------------------------------------

gh repo fork --depth 1
unknown flag: --depth
Separate git clone flags with '--'.

Usage:  gh repo fork [<repository>] [-- <gitflags>...]

Flags:
  --clone    Clone the fork {true|false}
  --remote   Add remote for fork {true|false}

In the former case, ghrepo.FromFullName catches it and shows the expected format.

In the second case, any flag that's not '--clone' or '--remote' fires the custom err msg. Do you think it should show something else here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

nvm, I added a custom validator to warn the user if they is passing git flags without specifying the <repository> argument.

ArgsLenAtDash allows you to check the length of args before the -- separator.

return forkRun(opts)
},
}
cmd.SetFlagErrorFunc(func(cmd *cobra.Command, err error) error {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice to see this here! 👏

With no argument, creates a fork of the current repository. Otherwise, forks the specified repository.`,
With no argument, creates a fork of the current repository. Otherwise, forks the specified repository.

If '--clone' is specified, you can pass additional 'git clone' flags by listing them after '--'.`,
Copy link
Contributor

Choose a reason for hiding this comment

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

If --clone wasn't specified, a prompt will be shown and the user will still have an opportunity to clone. Therefore, gitflags might take effect regardless of whether --clone was explicitly used or not.

@vilmibm vilmibm self-assigned this Jan 20, 2021
@vilmibm vilmibm requested a review from mislav January 21, 2021 20:11
@vilmibm vilmibm merged commit 4860ccc into cli:trunk Jan 21, 2021
@cristiand391 cristiand391 deleted the repo-fork-gitflags branch January 21, 2021 20:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

support gitflags in gh repo fork

4 participants