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

Update origin_changed? to ignore protocol differences #315

Merged
merged 9 commits into from
Feb 9, 2020
Merged

Update origin_changed? to ignore protocol differences #315

merged 9 commits into from
Feb 9, 2020

Conversation

kalafut
Copy link
Contributor

@kalafut kalafut commented Jan 20, 2020

I need an insteadOf git configuration and this issue has been really slowing things down, so I decided to make an attempt at fixing it. This is literally my first pushed Crystal code so any feedback is welcome!

Fixes #288

@straight-shoota
Copy link
Member

straight-shoota commented Jan 20, 2020

Instead of hard coding the normalization for individual repository providers, it would probably be better to have a more generic solution which would work with any provider.
I think it should suffice to extract hostname and path from both ssh+git and https URLs.

The same normalization should also be used for local_path (which seems broken for ssh+git URLs btw.). But this can be left for another PR.

@kalafut
Copy link
Contributor Author

kalafut commented Jan 20, 2020

I started that way but was a little concerned about the git@... portion, and whether that should be a distinguishing characteristic. i.e. whether git@github.com:foo/bar should equal fred@github.com:foo/bar. It sounds like that's not a concern, in which case I can definitely simplify this.

@kalafut
Copy link
Contributor Author

kalafut commented Jan 21, 2020

@straight-shoota I've generalized this comparison. As it turns out there are actually a lot of allowed git URL schemes. The regexes should take care of the common ones, and exactly matching origins will always work (i.e. current behavior).

@RX14
Copy link
Contributor

RX14 commented Jan 25, 2020

Could we use URI.parse instead of the regex?

test/git_resolver_test.cr Outdated Show resolved Hide resolved
src/resolvers/git.cr Show resolved Hide resolved
src/resolvers/git.cr Outdated Show resolved Hide resolved
@kalafut
Copy link
Contributor Author

kalafut commented Jan 25, 2020

@Sija @RX14 Thanks for the review! URI.parse does not handle the SSH URIs that are in the scp style (e.g. git@github.com:example/project.git), so something additional was needed. The pair of regexes was compact, but I don't really like adding regexes either if I can avoid it.

I'd also missed @straight-shoota's comment about local_path being broken too for these patterns. As a result, I've redone things a bit to make a small git URI parser that uses URI.parse but can also deal with some other formats, while still returning a URI object for consistency. It still feels a little fragile, but the code is simple and there are no regex's, so it's probably better overall. It can be used for origin_changed? and local_path.

I need to add some other tests but will then push it up for you to look at.

This updates the PR to use a new git_uri_parser, which fixes both
origin_changed? and local_path issues.
@kalafut
Copy link
Contributor Author

kalafut commented Jan 27, 2020

I've substantially updated the PR based on feedback, and realizing that local_path is also sort of broken. No more ugly regexes either.

@straight-shoota
Copy link
Member

The implementation of parse_git_uri could be much simpler:

private def parse_git_uri(raw_uri)
  uri = URI.parse(raw_uri)
  return uri if uri.absolute?
  
  URI.parse("ssh:#{raw_uri}")
end

@kalafut
Copy link
Contributor Author

kalafut commented Jan 27, 2020

Using .absolute is a good change. I don't see how the recommendation handles the main case of git+ssh urls, however. That would just leave everything in the path. e.g. https://play.crystal-lang.org/#/r/8grl

@straight-shoota
Copy link
Member

Yeah, you're right.

@kalafut
Copy link
Contributor Author

kalafut commented Jan 27, 2020

Looks like parse + absolute can get confused too:

uri = URI.parse("github.com:foo/bar") # scheme => github.com, host => nil, path => foo/bar
p uri.absolute? # => true

But that's only If the user (git@...) isn't included. While that style is accepted by git, it would seem to be a pretty unlikely real world case. I'd still be OK with absolute and not handling those user-less ssh URLs.

@straight-shoota
Copy link
Member

I suppose shards should allow the exact same formats as accepted by git, so user-less scp-like format example.com:foo/bar should be valid. It seems like git treats opaque URLs as scp-like format, so expanding the condition to uri.absolute? && !uri.opaque? should be sufficient.

@kalafut
Copy link
Contributor Author

kalafut commented Jan 28, 2020

Yep that seems to work OK.

Copy link
Contributor

@RX14 RX14 left a comment

Choose a reason for hiding this comment

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

awesome

Copy link
Contributor

@ysbaddaden ysbaddaden left a comment

Choose a reason for hiding this comment

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

Thanks! Just a few pedantic suggestions :)

src/resolvers/git.cr Outdated Show resolved Hide resolved
src/resolvers/git.cr Outdated Show resolved Hide resolved
src/resolvers/git.cr Outdated Show resolved Hide resolved
test/git_resolver_test.cr Show resolved Hide resolved
kalafut and others added 3 commits February 5, 2020 11:44
Co-Authored-By: Julien Portalier <julien@portalier.com>
Co-Authored-By: Julien Portalier <julien@portalier.com>
@kalafut
Copy link
Contributor Author

kalafut commented Feb 6, 2020

All comments addressed.

@ysbaddaden ysbaddaden merged commit 019b82d into crystal-lang:master Feb 9, 2020
@ysbaddaden
Copy link
Contributor

Thanks!

@kalafut kalafut deleted the fix-origin branch February 15, 2020 03:54
@bcardiff bcardiff added this to the v0.10.0 milestone Mar 27, 2020
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.

shards keeps downloading dependencies
6 participants