|
10 | 10 | #include <fstream> |
11 | 11 | #include <functional> |
12 | 12 | #include <optional> |
| 13 | +#include <regex> |
13 | 14 | #include <string> |
14 | 15 | #include <string_view> |
15 | 16 | #include <toml.hpp> |
@@ -66,27 +67,51 @@ handleDependency( |
66 | 67 |
|
67 | 68 | static std::string |
68 | 69 | getDependencyGitUrl(const std::string_view dep) { |
69 | | - if (dep.find("://") == std::string_view::npos) { |
70 | | - // check if atleast in "user/repo" format |
71 | | - if (dep.find('/') == std::string_view::npos) { |
72 | | - logger::error("Invalid dependency: " + std::string(dep)); |
73 | | - return ""; |
74 | | - } |
| 70 | + std::regex user_repo_regex(R"(^([^/]+)/([^/]+)$)"); |
| 71 | + std::regex url_regex( |
| 72 | + R"((?:https?://)?(?:www\.)?(?:github|gitlab)\.com/([^/]+/[^/]+)(?:\.git)?)" |
| 73 | + ); |
| 74 | + std::regex generic_git_regex(R"((?:https?://)?.+/(.+)\.git)"); |
75 | 75 |
|
76 | | - return "https://github.com/" + std::string(dep) + ".git"; |
| 76 | + std::cmatch match; |
| 77 | + if (std::regex_match(dep.begin(), dep.end(), match, user_repo_regex)) { |
| 78 | + // Format is "user/repo" |
| 79 | + return "https://github.com/" + match[1].str() + "/" + match[2].str() |
| 80 | + + ".git"; |
| 81 | + } else if (std::regex_match(dep.begin(), dep.end(), match, url_regex)) { |
| 82 | + // Format is a valid GitHub or GitLab URL |
| 83 | + return match[0].str(); |
| 84 | + } else if (std::regex_match( |
| 85 | + dep.begin(), dep.end(), match, generic_git_regex |
| 86 | + )) { |
| 87 | + // Format is a generic git URL ending with .git |
| 88 | + return match[0].str(); |
| 89 | + } else { |
| 90 | + // Invalid input format |
| 91 | + return ""; |
77 | 92 | } |
78 | | - return std::string(dep); |
79 | 93 | } |
80 | 94 |
|
81 | 95 | static std::string |
82 | 96 | getDependencyName(const std::string_view dep) { |
83 | 97 | std::string name; |
84 | | - if (dep.find("://") == std::string_view::npos) { |
| 98 | + std::regex url_regex( |
| 99 | + R"((?:https?://)?(?:www\.)?(?:github|gitlab)\.com/([^/]+)/([^/]+)(?:\.git)?)" |
| 100 | + ); |
| 101 | + std::regex generic_git_regex(R"((?:https?://)?.+/(.+)\.git)"); |
| 102 | + |
| 103 | + std::cmatch match; |
| 104 | + if (std::regex_match(dep.begin(), dep.end(), match, url_regex)) { |
| 105 | + name = match[2].str(); // Extract only the repo name |
| 106 | + } else if (std::regex_match( |
| 107 | + dep.begin(), dep.end(), match, generic_git_regex |
| 108 | + )) { |
| 109 | + name = match[1].str(); |
| 110 | + } else if (dep.find('/') != std::string_view::npos) { |
| 111 | + // Assume format is User/Repo |
85 | 112 | name = dep.substr(dep.find_last_of('/') + 1); |
86 | 113 | } else { |
87 | | - name = dep.substr( |
88 | | - dep.find_last_of('/') + 1, dep.find(".git") - dep.find_last_of('/') - 1 |
89 | | - ); |
| 114 | + return ""; |
90 | 115 | } |
91 | 116 |
|
92 | 117 | // Remove trailing '.git' if it exists |
@@ -139,6 +164,7 @@ addDependencyToManifest( |
139 | 164 | const std::string depName = getDependencyName(dep); |
140 | 165 |
|
141 | 166 | if (gitUrl.empty() || depName.empty()) { |
| 167 | + logger::error("Invalid dependency: " + std::string(dep)); |
142 | 168 | return EXIT_FAILURE; |
143 | 169 | } |
144 | 170 |
|
|
0 commit comments