mirror of
https://github.com/NixOS/nix.git
synced 2025-11-14 14:32:42 +01:00
Fix the parsing of the sourcehut refs file
Since a26be9f3b8, the same parser is used
to parse the result of sourcehut’s `HEAD` endpoint (coming from [git
dumb protocol]) and the output of `git ls-remote`. However, they are very
slightly different (the former doesn’t specify the current reference
since it’s implied to be `HEAD`).
Unify both, and make the parser a bit more robust and understandable (by
making it more typed and adding tests for it)
[git dumb protocol]: https://git-scm.com/book/en/v2/Git-Internals-Transfer-Protocols#_the_dumb_protocol
This commit is contained in:
parent
470e27ce80
commit
e68676e6c8
7 changed files with 116 additions and 63 deletions
25
src/libutil/git.cc
Normal file
25
src/libutil/git.cc
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
#include "git.hh"
|
||||
|
||||
#include <regex>
|
||||
|
||||
namespace nix {
|
||||
namespace git {
|
||||
|
||||
std::optional<LsRemoteRefLine> parseLsRemoteLine(std::string_view line)
|
||||
{
|
||||
const static std::regex line_regex("^(ref: *)?([^\\s]+)(?:\\t+(.*))?$");
|
||||
std::match_results<std::string_view::const_iterator> match;
|
||||
if (!std::regex_match(line.cbegin(), line.cend(), match, line_regex))
|
||||
return std::nullopt;
|
||||
|
||||
return LsRemoteRefLine {
|
||||
.kind = match[1].length() == 0
|
||||
? LsRemoteRefLine::Kind::Object
|
||||
: LsRemoteRefLine::Kind::Symbolic,
|
||||
.target = match[2],
|
||||
.reference = match[3].length() == 0 ? std::nullopt : std::optional<std::string>{ match[3] }
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
40
src/libutil/git.hh
Normal file
40
src/libutil/git.hh
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <optional>
|
||||
|
||||
namespace nix {
|
||||
|
||||
namespace git {
|
||||
|
||||
// A line from the output of `git ls-remote --symref`.
|
||||
//
|
||||
// These can be of two kinds:
|
||||
//
|
||||
// - Symbolic references of the form
|
||||
//
|
||||
// ref: {target} {reference}
|
||||
//
|
||||
// where {target} is itself a reference and {reference} is optional
|
||||
//
|
||||
// - Object references of the form
|
||||
//
|
||||
// {target} {reference}
|
||||
//
|
||||
// where {target} is a commit id and {reference} is mandatory
|
||||
struct LsRemoteRefLine {
|
||||
enum struct Kind {
|
||||
Symbolic,
|
||||
Object
|
||||
};
|
||||
Kind kind;
|
||||
std::string target;
|
||||
std::optional<std::string> reference;
|
||||
};
|
||||
|
||||
std::optional<LsRemoteRefLine> parseLsRemoteLine(std::string_view line);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
33
src/libutil/tests/git.cc
Normal file
33
src/libutil/tests/git.cc
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
#include "git.hh"
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
namespace nix {
|
||||
|
||||
TEST(GitLsRemote, parseSymrefLineWithReference) {
|
||||
auto line = "ref: refs/head/main HEAD";
|
||||
auto res = git::parseLsRemoteLine(line);
|
||||
ASSERT_TRUE(res.has_value());
|
||||
ASSERT_EQ(res->kind, git::LsRemoteRefLine::Kind::Symbolic);
|
||||
ASSERT_EQ(res->target, "refs/head/main");
|
||||
ASSERT_EQ(res->reference, "HEAD");
|
||||
}
|
||||
|
||||
TEST(GitLsRemote, parseSymrefLineWithNoReference) {
|
||||
auto line = "ref: refs/head/main";
|
||||
auto res = git::parseLsRemoteLine(line);
|
||||
ASSERT_TRUE(res.has_value());
|
||||
ASSERT_EQ(res->kind, git::LsRemoteRefLine::Kind::Symbolic);
|
||||
ASSERT_EQ(res->target, "refs/head/main");
|
||||
ASSERT_EQ(res->reference, std::nullopt);
|
||||
}
|
||||
|
||||
TEST(GitLsRemote, parseObjectRefLine) {
|
||||
auto line = "abc123 refs/head/main";
|
||||
auto res = git::parseLsRemoteLine(line);
|
||||
ASSERT_TRUE(res.has_value());
|
||||
ASSERT_EQ(res->kind, git::LsRemoteRefLine::Kind::Object);
|
||||
ASSERT_EQ(res->target, "abc123");
|
||||
ASSERT_EQ(res->reference, "refs/head/main");
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue