Skip to content

Commit af4f204

Browse files
references: a git reference follows the default branch; git+ on the command line (SPEC-001 §5, SPEC-016 §8)
- A git dependency names at most one of rev, tag, branch; none follows the remote's default branch, announced by `git ls-remote --symref` (main, then master, otherwise), locked by commit and read offline from the lock. - The string form of a reference takes `git+<url>[#tag=|rev=|branch=]`: `--platform git+<url>#tag=<t>` selects a platform from its repository; a repository named by URL alone is the package its last segment names. - A row a platform defines is known whether or not the project writes a section for it. Co-authored-by: speak-agent <248744407+speak-agent@users.noreply.github.com>
1 parent a3a24db commit af4f204

9 files changed

Lines changed: 225 additions & 6 deletions

File tree

‎modules/manifest/src/platform.cppm‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,8 +404,31 @@ inline const Carrier* carrier_for(const Statement& s, std::string_view pair) {
404404

405405
// `"[<namespace>.]<name>@<version>"` -> the ref; the error names what is
406406
// missing. `none` is not a ref; callers test for it first.
407+
// THE STRING FORM OF A REFERENCE (SPEC-016 §8, SPEC-001 §5): `[<ns>.]<name>@<version>`,
408+
// or `git+<url>[#tag=<t>|rev=<r>|branch=<b>]` -- no fragment is the remote's
409+
// default branch. A path is read by the caller, which knows the directory.
407410
inline std::expected<PackageRef, std::string> parse_ref_text(std::string_view text) {
408411
PackageRef r;
412+
if (text.starts_with("git+")) {
413+
auto rest = text.substr(4);
414+
const auto hash = rest.find('#');
415+
r.spec.git = std::string(rest.substr(0, hash));
416+
if (r.spec.git.empty())
417+
return std::unexpected(std::format("'{}' names no repository after `git+`", text));
418+
r.spec.gitRefKind = "branch"; // the default branch, resolved when it is fetched
419+
if (hash != std::string_view::npos) {
420+
const auto frag = rest.substr(hash + 1);
421+
const auto eq = frag.find('=');
422+
const auto kind = frag.substr(0, eq);
423+
if (eq == std::string_view::npos || eq + 1 == frag.size()
424+
|| (kind != "tag" && kind != "rev" && kind != "branch"))
425+
return std::unexpected(std::format(
426+
"'{}': after `#` write tag=<tag>, rev=<commit> or branch=<branch>", text));
427+
r.spec.gitRefKind = std::string(kind);
428+
r.spec.gitRev = std::string(frag.substr(eq + 1));
429+
}
430+
return r;
431+
}
409432
const auto at = text.rfind('@');
410433
if (at == std::string_view::npos || at == 0 || at + 1 == text.size())
411434
return std::unexpected(std::format(

‎modules/manifest/src/toml.cppm‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2604,6 +2604,12 @@ std::expected<Manifest, ManifestError> parse_document(const t::Document& documen
26042604
if (auto it = sub.find("backend"); it != sub.end() && it->second.is_string()) {
26052605
spec.features.push_back("backend-" + it->second.as_string());
26062606
}
2607+
// AT MOST ONE OF `rev`, `tag`, `branch` (SPEC-001 §5); none is the
2608+
// remote's default branch, resolved when the dependency is fetched.
2609+
if (int refs = int(sub.contains("rev")) + int(sub.contains("tag")) + int(sub.contains("branch")); refs > 1)
2610+
return std::unexpected(error(origin, std::format(
2611+
"[{}.\"{}\"] names {} of rev / tag / branch; a git dependency names at most one",
2612+
section, fqName, refs)));
26072613
if (auto it = sub.find("rev"); it != sub.end() && it->second.is_string()) {
26082614
spec.gitRev = it->second.as_string();
26092615
spec.gitRefKind = "rev";
@@ -2636,10 +2642,7 @@ std::expected<Manifest, ManifestError> parse_document(const t::Document& documen
26362642
return std::unexpected(error(origin, std::format(
26372643
"[{}.\"{}\"] must specify 'path', 'version', or 'git'", section, fqName)));
26382644
}
2639-
if (!spec.git.empty() && spec.gitRev.empty()) {
2640-
return std::unexpected(error(origin, std::format(
2641-
"[{}.\"{}\"] git dep requires one of: rev / tag / branch", section, fqName)));
2642-
}
2645+
if (!spec.git.empty() && spec.gitRefKind.empty()) spec.gitRefKind = "branch"; // the default branch
26432646
return {};
26442647
};
26452648

‎src/build/prepare/graph.cpp‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1472,6 +1472,42 @@ step4b_acquire_dependency_source(PrepareState& state, WorklistItemCtx& ctx) {
14721472
const std::string lockKey = rootDeclared ? name
14731473
: mcpp::pm::compat::qualified_name(
14741474
spec.namespace_, spec.shortName.empty() ? name : spec.shortName);
1475+
// THE DEFAULT BRANCH (SPEC-001 §5): a git reference that names no
1476+
// `tag`, `rev` or `branch` follows the remote's default branch,
1477+
// whose name the remote announces (`git ls-remote --symref`); the
1478+
// lock's entry for the same remote answers it offline. A remote
1479+
// that announces none is tried for `main`, then `master`.
1480+
if (spec.gitRefKind == "branch" && spec.gitRev.empty()) {
1481+
if (auto it = state.gitLockAnchors.find(lockKey);
1482+
it != state.gitLockAnchors.end() && it->second.url == spec.git
1483+
&& it->second.refKind == "branch" && !it->second.ref.empty())
1484+
spec.gitRev = it->second.ref;
1485+
else if (offline)
1486+
return refuse_offline("`git ls-remote`",
1487+
"its default branch is resolved from the remote, and mcpp.lock records none",
1488+
"resolve");
1489+
else {
1490+
auto head = run_with_network_retry({ "git", "ls-remote", "--symref", spec.git, "HEAD" });
1491+
if (head.exit_code == 0)
1492+
if (auto p = head.output.find("ref: refs/heads/"); p != std::string::npos) {
1493+
const auto from = p + 16;
1494+
const auto end = head.output.find_first_of("\t \r\n", from);
1495+
spec.gitRev = head.output.substr(from, end == std::string::npos ? end : end - from);
1496+
}
1497+
for (auto candidate : { "main", "master" }) {
1498+
if (!spec.gitRev.empty()) break;
1499+
auto r = run_with_network_retry({ "git", "ls-remote", spec.git,
1500+
std::format("refs/heads/{}", candidate) });
1501+
if (r.exit_code == 0 && !r.output.empty()) spec.gitRev = candidate;
1502+
}
1503+
if (spec.gitRev.empty())
1504+
return std::unexpected(std::format(
1505+
"git dependency '{}': '{}' announces no default branch and has neither "
1506+
"`main` nor `master`; name one with tag, rev or branch", name, spec.git));
1507+
mcpp::ui::info("Resolved", std::format("{}: the default branch of {} is {}",
1508+
name, spec.git, spec.gitRev));
1509+
}
1510+
}
14751511
if (spec.gitRefKind == "branch") {
14761512
auto it = state.gitLockAnchors.find(lockKey);
14771513
if (it != state.gitLockAnchors.end()

‎src/build/prepare/platform.cpp‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,16 @@ std::string known_short_name(const mp::PackageRef& r) {
6666
.shortName;
6767
return {};
6868
}
69+
// A repository named by its URL alone is the package its last segment
70+
// names (`.../openkal-platform.git` is `openkal-platform`); the fetched
71+
// manifest's own name is checked against it as any dependency's is.
72+
if (!r.spec.git.empty()) {
73+
std::string_view url = r.spec.git;
74+
while (!url.empty() && (url.back() == '/' || url.back() == '\\')) url.remove_suffix(1);
75+
if (url.ends_with(".git")) url.remove_suffix(4);
76+
const auto cut = url.find_last_of("/\\:");
77+
return std::string(cut == std::string_view::npos ? url : url.substr(cut + 1));
78+
}
6979
return {};
7080
}
7181

‎src/build/prepare/toolchain.cpp‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -706,7 +706,9 @@ step1_validate_target_tier(PrepareState& state, TargetOverrideCtx& ctx) {
706706
" Name one of them.",
707707
ctx.requestedSpelling, opts));
708708
}
709-
if (!ctx.known && !ctx.hasExplicitSection) {
709+
// A row a platform defines is known to this build by its definition
710+
// (SPEC-016 §2), whether or not the project writes a section for it.
711+
if (!ctx.known && !ctx.hasExplicitSection && !(ctx.parsed && ctx.parsed->defined())) {
710712
// "UNKNOWN" IS A CLAIM ABOUT THE VOCABULARY, AND IT WAS FALSE FOR
711713
// A WHOLE arch+os FAMILY.
712714
//

‎src/platform/platform.cppm‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ namespace {
200200
namespace mp = mcpp::manifest::platform;
201201

202202
bool names_a_path(std::string_view v) {
203-
if (v.empty()) return false;
203+
if (v.empty() || v.starts_with("git+")) return false; // a repository (SPEC-016 §8)
204204
if (v.starts_with("path:")) return true;
205205
if (v.starts_with("/") || v.starts_with("./") || v.starts_with("../") || v.starts_with("~")
206206
|| v == "." || v == "..")
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#!/usr/bin/env bash
2+
# requires: elf llvm
3+
source "$(dirname "${BASH_SOURCE[0]}")/_toolchain_env.sh"
4+
# 1048 -- a git reference with no tag, rev or branch follows the remote's default branch; a
5+
# reference on the command line takes the same forms (SPEC-001 §5; SPEC-016 §8).
6+
#
7+
# The default branch is the one the remote announces (`git ls-remote --symref`), here `trunk`; the
8+
# lock records it and its commit, and an offline build reads them. A dependency that names two of
9+
# rev / tag / branch is refused. `--platform git+<url>#tag=<t>` selects a platform from a
10+
# repository as a manifest's `{ git = ..., tag = ... }` does.
11+
#
12+
# Criteria: the build reports the default branch and runs; mcpp.lock names `#branch=trunk@<sha>`;
13+
# an offline rebuild succeeds; two refs are refused; the platform from git builds its row.
14+
set -e
15+
t=$(mktemp -d); trap 'rm -rf "$t"' EXIT
16+
fail() { echo "FAIL: $1"; [ -n "${2:-}" ] && printf '%s\n' "$2" | tail -20; exit 1; }
17+
g() { git -c user.name=e2e -c user.email=e2e@example.invalid -c init.defaultBranch=trunk "$@"; }
18+
mkdir -p "$t/lib/src" "$t/app/src" "$t/toy-platform" "$t/bare/src"
19+
cat > "$t/lib/mcpp.toml" <<'EOF'
20+
[package]
21+
name = "gitlib"
22+
version = "0.1.0"
23+
24+
[targets.gitlib]
25+
kind = "lib"
26+
EOF
27+
echo 'int gitlib_value() { return 9; }' > "$t/lib/src/lib.cpp"
28+
(cd "$t/lib" && g init -q . && g add -A && g commit -qm init)
29+
cat > "$t/app/mcpp.toml" <<EOF
30+
[package]
31+
name = "app"
32+
version = "0.1.0"
33+
34+
[dependencies]
35+
gitlib = { git = "$t/lib" }
36+
EOF
37+
echo 'int gitlib_value(); int main() { return gitlib_value(); }' > "$t/app/src/main.cpp"
38+
cd "$t/app"
39+
40+
out=$("$MCPP" build 2>&1) || fail "the build failed" "$out"
41+
grep -q "the default branch of .* is trunk" <<<"$out" || fail "the default branch is not reported" "$out"
42+
set +e; "$(find target -path '*/bin/app' -type f | head -1)"; rc=$?; set -e
43+
[ "$rc" = 9 ] || fail "the program returned $rc, not 9"
44+
grep -Eq 'source = "git\+.*#branch=trunk@[0-9a-f]{40}"' mcpp.lock || fail "the lock does not record the branch and its commit" "$(cat mcpp.lock)"
45+
rm -rf target
46+
out=$("$MCPP" build --offline 2>&1) || fail "the offline build did not read the lock" "$out"
47+
echo " ok the default branch is followed, locked, and read offline"
48+
49+
sed -i "s|gitlib = { git = \"$t/lib\" }|gitlib = { git = \"$t/lib\", tag = \"v1\", branch = \"trunk\" }|" mcpp.toml
50+
out=$("$MCPP" build 2>&1 || true)
51+
grep -q "names 2 of rev / tag / branch; a git dependency names at most one" <<<"$out" || fail "two refs were not refused" "$out"
52+
echo " ok a dependency that names two refs is refused"
53+
54+
cat > "$t/toy-platform/mcpp.toml" <<EOF
55+
[package]
56+
name = "toy-platform"
57+
version = "0.1.0"
58+
59+
[platform]
60+
toolchain = "llvm@${LLVM_VERSION}"
61+
62+
[[platform.define]]
63+
triple = "x86_64-toyos"
64+
clang-triple = "x86_64-unknown-linux-gnu"
65+
object-format = "elf"
66+
execution = "linux"
67+
68+
[[platform.provider]]
69+
serves = ["x86_64-toyos"]
70+
71+
[platform.build]
72+
cflags = ["-ffreestanding", "-fno-stack-protector", "-fno-pic"]
73+
74+
[platform.link]
75+
driver = "compiler"
76+
77+
[platform.layers]
78+
"c-abi" = false
79+
"c++-abi" = false
80+
"compiler-runtime" = false
81+
"kernel-abi" = false
82+
EOF
83+
(cd "$t/toy-platform" && g init -q . && g add -A && g commit -qm toy && g tag v1)
84+
cat > "$t/bare/mcpp.toml" <<'EOF'
85+
[package]
86+
name = "bare"
87+
version = "0.1.0"
88+
89+
[build]
90+
sources = ["src/*.c"]
91+
ldflags = ["-nostdlib", "-static", "-no-pie"]
92+
93+
[targets.bare]
94+
kind = "bin"
95+
main = "src/main.c"
96+
EOF
97+
printf 'void _start(void) { __asm__ volatile("mov $60, %%eax\\n mov $7, %%edi\\n syscall"); }\n' > "$t/bare/src/main.c"
98+
cd "$t/bare"
99+
out=$("$MCPP" build --target x86_64-toyos --platform "git+$t/toy-platform#tag=v1" 2>&1) || fail "the platform from git did not build its row" "$out"
100+
grep -q "Platform toy-platform 0.1.0" <<<"$out" || fail "the platform is not reported" "$out"
101+
echo " ok --platform git+<url>#tag=<t> selects the platform from its repository"
102+
echo "PASS: 1048 a git reference follows the default branch"

‎tests/unit/test_manifest.cpp‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7090,3 +7090,30 @@ target = "x86_64-openkal"
70907090
)");
70917091
if (auto&& failed = expect(fatal(ok.has_value())); !failed) failed << ok.error().format();
70927092
}};
7093+
7094+
// A GIT DEPENDENCY NAMES AT MOST ONE OF rev, tag, branch (SPEC-001 §5); none is
7095+
// the remote's default branch.
7096+
UTest a_git_dependency_names_at_most_one_ref{"Manifest/AGitDependencyNamesAtMostOneRef", [] {
7097+
auto m = mcpp::manifest::parse_string(R"(
7098+
[package]
7099+
name = "x"
7100+
version = "0.1.0"
7101+
7102+
[dependencies]
7103+
bar = { git = "https://example.com/bar.git" }
7104+
)");
7105+
if (auto&& failed = expect(fatal(m.has_value())); !failed) failed << m.error().format();
7106+
auto const& bar = m->dependencies.at("bar");
7107+
expect(eq(bar.gitRefKind, "branch"));
7108+
expect(bar.gitRev.empty());
7109+
auto two = mcpp::manifest::parse_string(R"(
7110+
[package]
7111+
name = "x"
7112+
version = "0.1.0"
7113+
7114+
[dependencies]
7115+
bar = { git = "https://example.com/bar.git", tag = "v1", rev = "abc" }
7116+
)");
7117+
expect(fatal(!two.has_value()));
7118+
expect(ne(two.error().format().find("a git dependency names at most one"), std::string::npos));
7119+
}};

‎tests/unit/test_platform.cpp‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,3 +613,19 @@ targets = ["x86_64-none-elf"]
613613
expect(eq(march->origin.tier, mp::Tier::Manifest));
614614
expect(eq(merged.replace.size(), 2u));
615615
}};
616+
617+
// THE STRING FORM OF A REPOSITORY (SPEC-016 §8): `git+<url>` with at most one of
618+
// tag, rev, branch after `#`; none is the remote's default branch.
619+
UTest a_repository_reference_has_a_string_form{"PlatformManifest/ARepositoryReferenceHasAStringForm", [] {
620+
auto tagged = mp::parse_ref_text("git+https://example.org/acme/toy-platform.git#tag=v1");
621+
expect(fatal(tagged.has_value()));
622+
expect(eq(tagged->spec.git, "https://example.org/acme/toy-platform.git"));
623+
expect(eq(tagged->spec.gitRefKind, "tag"));
624+
expect(eq(tagged->spec.gitRev, "v1"));
625+
auto deflt = mp::parse_ref_text("git+git@example.org:acme/toy.git");
626+
expect(fatal(deflt.has_value()));
627+
expect(eq(deflt->spec.gitRefKind, "branch"));
628+
expect(deflt->spec.gitRev.empty());
629+
expect(!mp::parse_ref_text("git+https://example.org/x.git#sha=1").has_value());
630+
expect(!mp::parse_ref_text("git+").has_value());
631+
}};

0 commit comments

Comments
 (0)