Skip to content

encode gh browse output url#4663

Merged
mislav merged 5 commits intocli:trunkfrom
bchadwic:browse-path-escape-patch
Nov 18, 2021
Merged

encode gh browse output url#4663
mislav merged 5 commits intocli:trunkfrom
bchadwic:browse-path-escape-patch

Conversation

@bchadwic
Copy link
Contributor

@bchadwic bchadwic commented Nov 2, 2021

fixes #4647

  1. User's before, and with this pr can't open up files with a \ in the file's / folder's name
    • I can update the pr to allow for \ to be escaped with another \? Not sure if this would be useful. / can't be in a path name so this isn't a problem.
  2. I removed utils.DisplayUrl(), but this can be reinserted easily.
    • I'm not sure if users would rather see the encoded or nonencoded url when opening up the url in the browser. Although I'm starting to see why that function was there in the first place.

@bchadwic bchadwic requested a review from a team as a code owner November 2, 2021 00:59
@bchadwic bchadwic requested review from vilmibm and removed request for a team November 2, 2021 00:59
Comment on lines +136 to +140
// taken from cli/internal/ghrepo/repo.go
url := fmt.Sprintf("%s%s/%s", ghinstance.HostPrefix(baseRepo.RepoHost()), baseRepo.RepoOwner(), baseRepo.RepoName())
if section != "" {
url = fmt.Sprintf("%s/%s", url, section)
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also, just to be clear, I changed this because the encoded characters act like format specifiers for the fmt.Sprintf(p, args...) in GenerateRepoURL(). I copied the code as a simple hack around it?

Copy link
Contributor

Choose a reason for hiding this comment

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

You can keep using GenerateRepoURL, but like this so to avoid issues with %:

GenerateRepoURL(baseRepo, "%s", section)

Note that section should already be path-escaped here

}

if opts.IO.IsStdoutTTY() {
fmt.Fprintf(opts.IO.Out, "Opening %s in your browser.\n", utils.DisplayURL(url))
Copy link
Contributor

Choose a reason for hiding this comment

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

Please keep using DisplayURL here

// Ensure that a path using \ can be used in a URL
p = strings.ReplaceAll(p, "\\", "/")
// encode special characters and replace all encoded slashes with forward slashes
p = regexp.MustCompile(`%2F|%5C`).ReplaceAllString(url.PathEscape(p), "/")
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's only replace %2F back to /, but leave %5C intact.

if !filepath.IsAbs(p) {
p = filepath.Clean(filepath.Join(opts.PathFromRepoRoot(), p))
// Ensure that a path using \ can be used in a URL
p = strings.ReplaceAll(p, "\\", "/")
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's not replace backslashes with forward slashes. Let backslashes be encoded as %5c.

Comment on lines +136 to +140
// taken from cli/internal/ghrepo/repo.go
url := fmt.Sprintf("%s%s/%s", ghinstance.HostPrefix(baseRepo.RepoHost()), baseRepo.RepoOwner(), baseRepo.RepoName())
if section != "" {
url = fmt.Sprintf("%s/%s", url, section)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

You can keep using GenerateRepoURL, but like this so to avoid issues with %:

GenerateRepoURL(baseRepo, "%s", section)

Note that section should already be path-escaped here

p = parts[0]
if !filepath.IsAbs(p) {
p = filepath.Clean(filepath.Join(opts.PathFromRepoRoot(), p))
// Ensure that a path using \ can be used in a URL
Copy link
Contributor

Choose a reason for hiding this comment

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

It's not the responsibility of a function called parseFile to be escaping the resulting string for use in a URL later. You're on the right tracking with escaping, but it should be moved elsewhere, e.g. to just before this value gets used in a URL.

@bchadwic
Copy link
Contributor Author

bchadwic commented Nov 3, 2021

@mislav

Sorry, I'm a little lost on what to do with \ according to the review. It sounds like we should keep \ escaped after we perform PathEscape, does this pose problems for windows users?

Files, on GitHub, with \ in the name won't be able to be opened from gh if we decide not to escape them. If we do escape them, doesn't that mean this bug fix would cause a breaking change for windows users since currently \ is an acceptable path separator on gh? I think I may have missed something.

- URLs are now always generated without a trailing slash
- Windows-style paths are normalized before processing
- Special characters in branch names are escaped
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.

Sorry, I got a bit lost in our conversation because I didn't have full context about what the current behavior is of handling Windows-style paths. I've pushed some changes that normalize paths on Windows (backslash separator is converted to slash) before further processing. This way, we don't have to worry about path separators on Windows being converted to URL escape sequences. I've also made it so that special characters in branch names are escaped too ✌️

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.

gh browse doesn't account for spaces in file / folder names

2 participants