diff --git a/src/libfetchers/git-lfs-fetch.cc b/src/libfetchers/git-lfs-fetch.cc index 0333570ea80..f1ad99fc7c7 100644 --- a/src/libfetchers/git-lfs-fetch.cc +++ b/src/libfetchers/git-lfs-fetch.cc @@ -53,14 +53,6 @@ struct LfsApiInfo } // namespace -static std::string lfsEndpointUrl(const ParsedURL & url) -{ - auto endpoint = url.to_string(); - if (!endpoint.ends_with(".git")) - endpoint += ".git"; - return endpoint + "/info/lfs"; -} - static LfsApiInfo getLfsApi(const ParsedURL & url) { assert(url.authority.has_value()); @@ -95,7 +87,12 @@ static LfsApiInfo getLfsApi(const ParsedURL & url) if (authIt == headerIt->end()) throw Error("no Authorization in git-lfs-authenticate response"); - return {queryResp.at("href").get(), authIt->get()}; + auto href = url.to_string(); + auto hrefIt = queryResp.find("href"); + if (hrefIt != queryResp.end()) + href = hrefIt->get(); + + return {href, authIt->get()}; } else { std::ostringstream inputCredDescr; @@ -125,10 +122,10 @@ static LfsApiInfo getLfsApi(const ParsedURL & url) if (username.empty() || password.empty()) throw Error("git-credential-fill: no credentials returned (cmd: 'git credential fill' for protocol=%s, host=%s, path=%s)", url.scheme, url.authority->host, url.renderPath(true)); - return {lfsEndpointUrl(url), "Basic " + base64::encode(std::as_bytes(std::span{username + ":" + password}))}; + return {url.to_string(), "Basic " + base64::encode(std::as_bytes(std::span{username + ":" + password}))}; } - return {lfsEndpointUrl(url), std::nullopt}; + return {url.to_string(), std::nullopt}; } typedef std::unique_ptr> GitConfig; @@ -137,7 +134,7 @@ typedef std::unique_ptr> GitCon static std::string getLfsEndpointUrl(git_repository * repo) { GitConfig config; - if (git_repository_config(Setter(config), repo)) { + if (!git_repository_config(Setter(config), repo)) { GitConfigEntry entry; if (!git_config_get_entry(Setter(entry), config.get(), "lfs.url")) { auto value = std::string(entry->value); @@ -149,14 +146,18 @@ static std::string getLfsEndpointUrl(git_repository * repo) } git_remote * remote = nullptr; - if (git_remote_lookup(&remote, repo, "origin")) - return ""; - - const char * url_c_str = git_remote_url(remote); - if (!url_c_str) - return ""; + if (!git_remote_lookup(&remote, repo, "origin")) { + const char * url_c_str = git_remote_url(remote); + if (url_c_str) { + auto remote = std::string(url_c_str); + if (remote.ends_with(".git")) + return remote + "/info/lfs"; + else + return remote + ".git/info/lfs"; + } + } - return std::string(url_c_str); + return ""; } static std::optional parseLfsPointer(std::string_view content, std::string_view filename)