aboutsummaryrefslogtreecommitdiff
path: root/src/libutil/url-name.cc
blob: 12c55db9a0ea157ba2ed9d9905c48b8fdff4af2b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <regex>

#include "url-name.hh"

namespace nix {

static std::string const attributeNamePattern("[a-zA-Z0-9_-]+");
static std::regex const lastAttributeRegex("(?:" + attributeNamePattern + "\\.)*(?!default)(" + attributeNamePattern +")(\\^.*)?");
static std::string const pathSegmentPattern("[a-zA-Z0-9_-]+");
static std::regex const lastPathSegmentRegex(".*/(" + pathSegmentPattern +")");
static std::regex const secondPathSegmentRegex("(?:" + pathSegmentPattern + ")/(" + pathSegmentPattern +")(?:/.*)?");
static std::regex const gitProviderRegex("github|gitlab|sourcehut");
static std::regex const gitSchemeRegex("git($|\\+.*)");
static std::regex const defaultOutputRegex(".*\\.default($|\\^.*)");

std::optional<std::string> getNameFromURL(ParsedURL const & url)
{
    std::smatch match;

    /* If there is a dir= argument, use its value */
    if (url.query.count("dir") > 0) {
        return url.query.at("dir");
    }

    /* If the fragment isn't a "default" and contains two attribute elements, use the last one */
    if (std::regex_match(url.fragment, match, lastAttributeRegex)) {
        return match.str(1);
    }

    /* If this is a github/gitlab/sourcehut flake, use the repo name */
    if (
        std::regex_match(url.scheme, gitProviderRegex)
        && std::regex_match(url.path, match, secondPathSegmentRegex)
    ) {
        return match.str(1);
    }

    /* If it is a regular git flake, use the directory name */
    if (
        std::regex_match(url.scheme, gitSchemeRegex)
        && std::regex_match(url.path, match, lastPathSegmentRegex)
    ) {
        return match.str(1);
    }

    /* If everything failed but there is a non-default fragment, use it in full */
    if (!url.fragment.empty() && !std::regex_match(url.fragment, defaultOutputRegex))
        return url.fragment;

    /* If there is no fragment, take the last element of the path */
    if (std::regex_match(url.path, match, lastPathSegmentRegex))
        return match.str(1);

    /* If even that didn't work, the URL does not contain enough info to determine a useful name */
    return {};
}

}