Skip to content

Commit 40f6006

Browse files
committed
fetchGit: Drop git+ from the url attribute
This was already dropped in `inputFromURL()`, but not in `inputFromAttrs()`. Now it's done in `fixGitURL()`, which is used by both. In principle, `git+` shouldn't be used in the `url` attribute, since we already know that it's a Git URL. But since it currently works, we don't want to break it. Fixes #14429.
1 parent 3f18cad commit 40f6006

File tree

4 files changed

+18
-15
lines changed

4 files changed

+18
-15
lines changed

src/libfetchers/git.cc

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,6 @@ struct GitInputScheme : InputScheme
168168
return {};
169169

170170
auto url2(url);
171-
if (hasPrefix(url2.scheme, "git+"))
172-
url2.scheme = std::string(url2.scheme, 4);
173171
url2.query.clear();
174172

175173
Attrs attrs;

src/libutil-tests/url.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ INSTANTIATE_TEST_SUITE_P(
3535
// Already proper URL with git+ssh
3636
FixGitURLParam{
3737
.input = "git+ssh://user@domain:1234/path",
38-
.expected = "git+ssh://user@domain:1234/path",
38+
.expected = "ssh://user@domain:1234/path",
3939
.parsed =
4040
ParsedURL{
41-
.scheme = "git+ssh",
41+
.scheme = "ssh",
4242
.authority =
4343
ParsedURL::Authority{
4444
.host = "domain",

src/libutil/include/nix/util/url.hh

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -330,10 +330,13 @@ struct ParsedUrlScheme
330330

331331
ParsedUrlScheme parseUrlScheme(std::string_view scheme);
332332

333-
/* Detects scp-style uris (e.g. [email protected]:NixOS/nix) and fixes
334-
them by removing the `:` and assuming a scheme of `ssh://`. Also
335-
changes absolute paths into file:// URLs. */
336-
ParsedURL fixGitURL(const std::string & url);
333+
/**
334+
* Detects scp-style uris (e.g. `[email protected]:NixOS/nix`) and fixes
335+
* them by removing the `:` and assuming a scheme of `ssh://`. Also
336+
* drops `git+` from the scheme (e.g. `git+https://` to `https://`)
337+
* and changes absolute paths into `file://` URLs.
338+
*/
339+
ParsedURL fixGitURL(std::string url);
337340

338341
/**
339342
* Whether a string is valid as RFC 3986 scheme name.

src/libutil/url.cc

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -409,21 +409,23 @@ ParsedUrlScheme parseUrlScheme(std::string_view scheme)
409409
};
410410
}
411411

412-
ParsedURL fixGitURL(const std::string & url)
412+
ParsedURL fixGitURL(std::string url)
413413
{
414414
std::regex scpRegex("([^/]*)@(.*):(.*)");
415415
if (!hasPrefix(url, "/") && std::regex_match(url, scpRegex))
416-
return parseURL(std::regex_replace(url, scpRegex, "ssh://$1@$2/$3"));
417-
if (hasPrefix(url, "file:"))
418-
return parseURL(url);
419-
if (url.find("://") == std::string::npos) {
416+
url = std::regex_replace(url, scpRegex, "ssh://$1@$2/$3");
417+
if (!hasPrefix(url, "file:") && !hasPrefix(url, "git+file:") && url.find("://") == std::string::npos)
420418
return ParsedURL{
421419
.scheme = "file",
422420
.authority = ParsedURL::Authority{},
423421
.path = splitString<std::vector<std::string>>(url, "/"),
424422
};
425-
}
426-
return parseURL(url);
423+
auto parsed = parseURL(url);
424+
// Drop the superfluous "git+" from the scheme.
425+
auto scheme = parseUrlScheme(parsed.scheme);
426+
if (scheme.application == "git")
427+
parsed.scheme = scheme.transport;
428+
return parsed;
427429
}
428430

429431
// https://www.rfc-editor.org/rfc/rfc3986#section-3.1

0 commit comments

Comments
 (0)