mirror of
https://github.com/NixOS/nix
synced 2025-06-27 12:41:15 +02:00
Extract git reference parsing to a shared library
These utility functions can be shared between the git and github fetchers.
This commit is contained in:
parent
c21afd684c
commit
9bf296c970
4 changed files with 70 additions and 35 deletions
25
src/libfetchers/git-utils.cc
Normal file
25
src/libfetchers/git-utils.cc
Normal file
|
@ -0,0 +1,25 @@
|
|||
#include "git-utils.hh"
|
||||
|
||||
#include <regex>
|
||||
|
||||
std::optional<std::string> parseListReferenceHeadRef(std::string_view line) {
|
||||
const static std::regex head_ref_regex("^ref: ([^\\s]+)\\t+HEAD$");
|
||||
std::match_results<std::string_view::const_iterator> match;
|
||||
if (std::regex_match(line.cbegin(), line.cend(), match, head_ref_regex)) {
|
||||
return match[1];
|
||||
} else {
|
||||
return std::nullopt;
|
||||
}
|
||||
}
|
||||
|
||||
std::optional<std::string> parseListReferenceForRev(std::string_view rev, std::string_view line) {
|
||||
const static std::regex rev_regex("^([^\\t]+)\\t+(.*)$");
|
||||
std::match_results<std::string_view::const_iterator> match;
|
||||
if (!std::regex_match(line.cbegin(), line.cend(), match, rev_regex)) {
|
||||
return std::nullopt;
|
||||
}
|
||||
if (rev != match[2].str()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
return match[1];
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue