diff options
author | Kjetil Orbekk <kj@orbekk.com> | 2022-04-29 18:30:00 -0400 |
---|---|---|
committer | Kjetil Orbekk <kj@orbekk.com> | 2022-04-29 18:46:21 -0400 |
commit | 9bf296c970bf33b7ed53d7e2d8fbe44197482518 (patch) | |
tree | c172f61f1b5e51b6d353b99ab75d0e1e206bc54f /src/libfetchers/git-utils.cc | |
parent | c21afd684cb5f59337b879684728884fd8275ce4 (diff) |
Extract git reference parsing to a shared library
These utility functions can be shared between the git and github fetchers.
Diffstat (limited to 'src/libfetchers/git-utils.cc')
-rw-r--r-- | src/libfetchers/git-utils.cc | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/src/libfetchers/git-utils.cc b/src/libfetchers/git-utils.cc new file mode 100644 index 000000000..060077098 --- /dev/null +++ b/src/libfetchers/git-utils.cc @@ -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]; +} |