-
Notifications
You must be signed in to change notification settings - Fork 231
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
use kpt git parsing in rpkg clone
#3080
Conversation
internal/cmdrpkgclone/command.go
Outdated
@@ -154,11 +145,15 @@ func (r *runner) preRunE(cmd *cobra.Command, args []string) error { | |||
} | |||
|
|||
case strings.Contains(source, "/"): | |||
gitArgs, err := parse.GitParseArgs(context.Background(), args) |
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.
will this try to clone the repository locally (or try to contact it in any way), or is this purely syntax-level parsing?
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.
From what I can tell, GitParseArgs
will try to contact the repository only if there is no ref/version provided in the URL. It tries to make contact in this case to determine what the default branch is.
What is the correct behavior for kpt alpha rpkg clone
if no version is provided? I can extract the syntax-level parsing out of GitParseArgs
and use that here instead if we want to do something different, like throw an error or leave the ref empty.
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.
empty ref would be ok, or we can default to main
. Porch defaults to main
in other places where ref is not specified.
Error may be OK too.
I actually thought (though may be wrong) that another case where we contact the server is if the .git/ part is missing and the parser can't tell where the repository path ends and directory path begins:
domain.com/some/path/here
is the some/path
repo? or some
?
regardless, the rpkg commands should not interact with the repository and only operate at the level of syntax of the URL provided, even if there are cases we can't handle and that will result in errors (IMO)
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.
empty ref would be ok, or we can default to main. Porch defaults to main in other places where ref is not specified.
I'll default to main
to be consistent with other places.
I actually thought (though may be wrong) that another case where we contact the server is if the .git/ part is missing and the parser can't tell where the repository path ends and directory path begins
Ah you are correct, I missed that it contacts the repo while attempting to convert the URL to a URL containing .git
.
Is it a requirement for kpt alpha rpkg clone
that the URL contains .git
for git URLs? I'm thinking about moving the logic in GitParseArgs
that handles URLs with a .git suffix out to its own helper function so that we can use it here without worrying about communication with the repo.
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.
Presence of the .git
part of the URL is not requirement. I'd say the requirement is that the parser doesn't modify the URL.
So if customer gave us https://github.com/org/repo.git/path/to/package
we should include the .git
if they provided https://github.com/org/repo/path/to/package
we should not artificially introduce .git
part into the URL.
c.Flags().StringVar(&r.directory, "directory", "/", "Directory within the repository where the upstream package is located.") | ||
c.Flags().StringVar(&r.ref, "ref", "main", "Branch in the repository where the upstream package is located.") | ||
c.Flags().StringVar(&r.directory, "directory", "", "Directory within the repository where the upstream package is located.") | ||
c.Flags().StringVar(&r.ref, "ref", "", "Branch in the repository where the upstream package is located.") |
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.
I removed these defaults because otherwise it is impossible to tell whether the user set the flag explicitly or not. Since we are trying to infer the ref from the URL, we need to be able to throw an error if the user sets the flag and also includes the ref in the URL and they contradict each other. I set the default manually below in L182-L187 after this error check occurs.
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.
good call, thank you!
(some flag libraries have a way to tell whether a flag was explicitly set, but from top of my head I don't know if Cobra is one of them; this is a good approach though)
- https://github.com/platkrm/test-blueprints.git | ||
- --directory=basens | ||
- --ref=basens/v1 | ||
- https://github.com/platkrm/test-blueprints.git/basens@basens/v1 |
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.
Updated this test to demonstrate the new inferring behavior. There is another similar command that I haven't modified in rpkg-edit/config.yaml
, that uses the --directory
and --ref
flags, demonstrating that the user can still use the flags when needed.
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.
thank you! this is great!
rpkg clone
- https://github.com/platkrm/test-blueprints.git | ||
- --directory=basens | ||
- --ref=basens/v1 | ||
- https://github.com/platkrm/test-blueprints.git/basens@basens/v1 |
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.
thank you! this is great!
* kpt alpha rpkg clone http://git-repository.git/package-name@main target --repository=repo | ||
* kpt alpha rpkg clone http://git-repository.git target --directory=package-name --ref=main --repository=repo |
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.
@mortent is working on rationalizing all flags and group and command names in the alpha
group; one thing I noticed (and it on me) that I picked suboptimal flag names for the clone command - there are 2 repositories involved (from, to) and the flag name is not directional (only --repository
) so it may be confusing to customers. That said, let's not fix it here, just something to keep in mind for the big rename Morten is working on.
c.Flags().StringVar(&r.directory, "directory", "/", "Directory within the repository where the upstream package is located.") | ||
c.Flags().StringVar(&r.ref, "ref", "main", "Branch in the repository where the upstream package is located.") | ||
c.Flags().StringVar(&r.directory, "directory", "", "Directory within the repository where the upstream package is located.") | ||
c.Flags().StringVar(&r.ref, "ref", "", "Branch in the repository where the upstream package is located.") |
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.
good call, thank you!
(some flag libraries have a way to tell whether a flag was explicitly set, but from top of my head I don't know if Cobra is one of them; this is a good approach though)
@@ -287,3 +285,9 @@ func getDest(v, repo, subdir string) (string, error) { | |||
} | |||
return v, nil | |||
} | |||
|
|||
// HasGitSuffix returns true if the provided pkgURL is a git repo containing the ".git" suffix | |||
func HasGitSuffix(pkgURL string) bool { |
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.
[nit] we are actually not looking for suffix per se, but (more accurately) checking for a URL path segment that ends with '.git'.
(and, I must admit I can't come up with a better name, so feel free to leave it :)
Thanks for the reviews and answering all my questions! |
For
kpt alpha rpkg clone
, make using the--ref
and--directory
flags optional when using git URLs, instead allow something likeThrow errors if the ref/directory are both present in the URL and set via flags, and they don't match.