From a77b2811a76dc1393c330bc5dc6a373dd3f0c343 Mon Sep 17 00:00:00 2001 From: DisposaBoy Date: Fri, 27 Apr 2018 12:14:15 +0100 Subject: [PATCH] Fix host redirect handling If you try to clone from a url that redirects to another host e.g. `example.com/repo` -> `github.com/org/repo.git`, it results in a redirect to `example.com/org/repo.git` because ModifyEndpointIfRedirect only takes into account the scheme and path. Signed-off-by: DisposaBoy --- plumbing/transport/http/common.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/plumbing/transport/http/common.go b/plumbing/transport/http/common.go index 2c337b77a..8f09ac470 100644 --- a/plumbing/transport/http/common.go +++ b/plumbing/transport/http/common.go @@ -4,6 +4,7 @@ package http import ( "bytes" "fmt" + "net" "net/http" "strconv" "strings" @@ -151,7 +152,14 @@ func (s *session) ModifyEndpointIfRedirect(res *http.Response) { return } + host, port, err := net.SplitHostPort(r.URL.Host) + if err != nil { + host = r.URL.Host + } + s.endpoint.Protocol = r.URL.Scheme + s.endpoint.Host = host + s.endpoint.Port, _ = strconv.Atoi(port) s.endpoint.Path = r.URL.Path[:len(r.URL.Path)-len(infoRefsPath)] }