From 713a644c9f2c05396f3cef23ee984cd50b386d64 Mon Sep 17 00:00:00 2001 From: Angad Gill Date: Fri, 17 Jul 2020 20:05:45 +0530 Subject: [PATCH] Generate new URL string for each entry in `readmeNames` The code in `github.go` and `gitlab.go` mutates the same URL path making anything but the first attempt incorrect. This means the project at the moment only works for repositories that have `README.md` present. I added a debug log to `findGithubREADME` and tried to get the readme of one of my own projects (it has a readme.md) here is the output: ```shell $ go run . github.com/angadgill92/glow TRYING PATH /angadgill92/clean/master/README.md TRYING PATH /angadgill92/clean/master/README.md/master/README Error: can't find README in GitHub repository Usage: glow SOURCE [flags] Flags: -h, --help help for glow -p, --pager display with pager -s, --style string style name or JSON path (default "auto") --version version for glow -w, --width uint word-wrap at width exit status 255 ``` The reason for this is that we're doing ```go v := u v.Path += "/master/" + r ``` and on every iteration of the loop it adds `"/master/" + r` to whatever URL was generated in the last iteration. This makes every URL generated by `glow` after the first one invalid causing the error. --- github.go | 9 ++++----- gitlab.go | 9 ++++----- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/github.go b/github.go index 3b944cc7..a5194ebd 100644 --- a/github.go +++ b/github.go @@ -28,18 +28,17 @@ func findGitHubREADME(s string) (*source, error) { return nil, err } u.Host = "raw.githubusercontent.com" - + u.Path += "/master/" for _, r := range readmeNames { - v := u - v.Path += "/master/" + r + v := u.String() + r - resp, err := http.Get(v.String()) + resp, err := http.Get(v) if err != nil { return nil, err } if resp.StatusCode == http.StatusOK { - return &source{resp.Body, v.String()}, nil + return &source{resp.Body, v}, nil } } diff --git a/gitlab.go b/gitlab.go index 7bebc935..c0bc4777 100644 --- a/gitlab.go +++ b/gitlab.go @@ -27,18 +27,17 @@ func findGitLabREADME(s string) (*source, error) { if err != nil { return nil, err } - + u.Path += "/raw/master/" for _, r := range readmeNames { - v := u - v.Path += "/raw/master/" + r + v := u.String() + r - resp, err := http.Get(v.String()) + resp, err := http.Get(v) if err != nil { return nil, err } if resp.StatusCode == http.StatusOK { - return &source{resp.Body, v.String()}, nil + return &source{resp.Body, v}, nil } }