diff --git a/README.md b/README.md index b864045e..73cf8144 100644 --- a/README.md +++ b/README.md @@ -32,8 +32,22 @@ origin annotate @ review --withdraw -m "Spoke too soon" # take your statement origin annotate --on flaky --neutral -m "Timeout on CI" # a statement about a statement origin statements [] # the statements about a revision, and those about them origin promote [] # carry results over from revisions with the same inputs + +origin land [] [--onto ] # land a change and the ones below it +origin land --from --onto # land a target's tip on another +origin land --status # the landings waiting for evidence or a conflict fix +origin targets # every target, its tip, authority and requirements +origin target new --from +origin new [description] --on # start a change on a target's tip ``` +Landing rebases a change onto the target's tip, file by file, checks the +statements `policy.toml` requires (promoting results whose inputs didn't +change), and appends an acceptance signed by your node, which must be the +target's authority. A landing that conflicts or lacks evidence waits: issue +the statements, or a new revision, and run `origin land` again. Policy is a +target too: edit it with `origin new --on policy`, then `origin land`. + Changes are named by a unique prefix of their id, or `@` for the working copy's. A revision is `@`, a change (for its head), or a unique prefix of the revision's id. Your name and node key live in `$ORIGIN_CONFIG_DIR`, or @@ -44,8 +58,9 @@ in its `config.toml` if `$USER` isn't the name you want. See `converge` merges the files of a change's heads. A text file two heads changed holds both versions between conflict markers (`<<<<<<<`, `=======`, `>>>>>>>`), and a binary one keeps one side. A `core.conflict` statement -records which files conflict and what the merge wrote there, until they -change: edit them, or accept a file as it is with `origin resolve `. `status` lists those left, and every new +records which files conflict and what the merge wrote there, and the change +doesn't land while they're unchanged: edit them, or accept a file as it is +with `origin resolve `. `status` lists those left, and every new revision, `describe` included, keeps them. ## Development diff --git a/apps/origin-src/src/cli.rs b/apps/origin-src/src/cli.rs index 0f26335f..b80df439 100644 --- a/apps/origin-src/src/cli.rs +++ b/apps/origin-src/src/cli.rs @@ -53,6 +53,9 @@ enum WorkspaceCommand { /// What the new change is for. #[arg(default_value = "")] description: String, + /// Start it on the tip of this target instead. + #[arg(long)] + on: Option, }, /// Describe what the current change is for. Describe { @@ -125,6 +128,44 @@ enum WorkspaceCommand { #[arg(default_value = "@")] rev: String, }, + /// Land a change on a target, with the changes below it the target + /// doesn't have yet. The queue rebases it onto the target's tip, checks + /// the evidence `policy.toml` requires, and if it's there, appends a + /// signed acceptance. A landing that conflicts or lacks evidence waits. + Land(LandArgs), + /// Manage targets. + #[command(subcommand)] + Target(TargetCommand), + /// List the targets, with their tips, authorities and requirements. + Targets, +} + +#[derive(Debug, Args)] +struct LandArgs { + /// The change: the working copy's by default. + #[arg(conflicts_with_all = ["from", "status"])] + change: Option, + /// The target to land on: by default, the one the change is on. + #[arg(long)] + onto: Option, + /// Land the tip of this target instead of a change. + #[arg(long, requires = "onto")] + from: Option, + /// List the landings that wait, and why. + #[arg(long, conflicts_with_all = ["onto", "from"])] + status: bool, +} + +#[derive(Debug, Subcommand)] +enum TargetCommand { + /// Create a target at the tip of another. + New { + /// Its name, such as `release/v3`. + name: TargetName, + /// The target it starts from. + #[arg(long)] + from: TargetName, + }, } #[derive(Debug, Args)] @@ -337,7 +378,10 @@ impl WorkspaceCommand { Self::Status => workspace.status()?, Self::Diff => workspace.diff()?, Self::Show { change } => workspace.show(change.as_deref())?, - Self::New { description } => workspace.new_change(&description)?, + Self::New { description, on } => match on { + Some(target) => workspace.new_on(&target, &description)?, + None => workspace.new_change(&description)?, + }, Self::Describe { description } => workspace.describe(&description)?, Self::Resolve { paths } => workspace.resolve(&paths)?, Self::Edit { change } => workspace.edit(&change)?, @@ -358,6 +402,15 @@ impl WorkspaceCommand { } Self::Statements { rev } => workspace.statements(&rev)?, Self::Promote { rev } => workspace.promote(&rev)?, + Self::Land(args) => match (args.from, args.onto) { + _ if args.status => workspace.land_status()?, + (Some(from), Some(onto)) => workspace.land_target(&from, &onto)?, + (_, onto) => workspace.land(args.change.as_deref(), onto)?, + }, + Self::Target(TargetCommand::New { name, from }) => { + workspace.target_new(&name, &from)?; + } + Self::Targets => workspace.targets()?, } Ok(()) } diff --git a/apps/origin-src/src/workspace/land.rs b/apps/origin-src/src/workspace/land.rs new file mode 100644 index 00000000..a87fe7cb --- /dev/null +++ b/apps/origin-src/src/workspace/land.rs @@ -0,0 +1,334 @@ +//! The target commands: `land`, `target new` and `targets`, and starting a +//! change on a target with `new --on`. +//! +//! The queue runs in this process, as this node. After a landing, the +//! working copy follows its change: when it landed, or the target already +//! had it, the working copy starts a new change on the target's tip, so +//! later edits are made on what the target has; when the queue rebased +//! it, the working copy becomes the rebased revision; and when it sits on +//! a change that landed, it is rebased onto the tip, if that merges +//! cleanly. + +use origin_shared::{ + Base, ChangeId, EncodedObject, Record, RecordId, Revision, RevisionId, Source, TargetName, + TreeId, +}; + +use super::{WorkingCopy, Workspace, WorkspaceError}; +use crate::index::Index; +use crate::policy::Policy; +use crate::queue::{Blocked, Landing, Pending, Proposal, Queue}; + +impl Workspace { + /// `origin land [] [--onto ]`: lands a change, the + /// working copy's by default, with the changes below it the target + /// doesn't have yet, on the target its stack is on by default. + /// + /// # Errors + /// + /// Fails if the change cannot be found or cannot land, this node isn't + /// the target's authority, or the records cannot be read or written. + pub fn land( + &mut self, + change: Option<&str>, + onto: Option, + ) -> Result<(), WorkspaceError> { + let working_copy = self.refresh()?; + let index = Index::open(&self.store)?; + let change = working_copy.find_change(&index, change.unwrap_or("@"))?; + let onto = match onto { + Some(onto) => onto, + None => self.target_of(Self::single_head(&index, change)?)?, + }; + let key = self.config.node_key()?; + let landings = + Queue::new(&self.store, &key, self.config.node().clone()).land_change(change, &onto)?; + for landing in &landings { + self.print_landing(landing, &onto)?; + } + self.follow(&working_copy, &onto, &landings) + } + + /// `origin land --from --onto `: lands the tip of + /// another target. + /// + /// # Errors + /// + /// Fails if a target doesn't exist, this node isn't the authority of + /// `onto`, or the records cannot be read or written. + pub fn land_target(&self, from: &TargetName, onto: &TargetName) -> Result<(), WorkspaceError> { + let key = self.config.node_key()?; + let landing = + Queue::new(&self.store, &key, self.config.node().clone()).land_target(from, onto)?; + self.print_landing(&landing, onto) + } + + /// `origin land --status`: lists the landings that wait, and why. + /// + /// # Errors + /// + /// Fails if the queues cannot be read. + pub fn land_status(&self) -> Result<(), WorkspaceError> { + let pending = Queue::pending(&self.store)?; + if pending.is_empty() { + println!("No landing waits."); + } + for pending in &pending { + Self::print_pending(pending); + } + Ok(()) + } + + /// `origin target new --from `: creates a target at the + /// tip of another. + /// + /// # Errors + /// + /// Fails if the name is taken, `from` doesn't exist, this node wouldn't + /// be the target's authority, or the records cannot be written. + pub fn target_new(&self, name: &TargetName, from: &TargetName) -> Result<(), WorkspaceError> { + let key = self.config.node_key()?; + Queue::new(&self.store, &key, self.config.node().clone()).create_target(name, from)?; + let tip = self + .store + .head(from)? + .ok_or_else(|| WorkspaceError::UnknownTarget(from.clone()))?; + println!( + "Created target `{name}` at {}", + self.describe_acceptance(tip.acceptance)? + ); + Ok(()) + } + + /// `origin targets`: lists every target with its tip, its authority and + /// what landing on it requires. + /// + /// # Errors + /// + /// Fails if the targets or the policy cannot be read. + pub fn targets(&self) -> Result<(), WorkspaceError> { + let policy = Policy::load(&self.store)?; + let mut targets = self.store.targets()?; + targets.sort(); + for target in targets { + let Some(tip) = self.store.head(&target)? else { + continue; + }; + let authority = policy + .authority(&target) + .map_or_else(|| "no authority".to_owned(), |node| format!("node:{node}")); + println!( + "{target} #{} {} ({authority})", + tip.seq, + tip.acceptance.short() + ); + for requirement in policy.requirements(&target) { + println!(" requires {requirement}"); + } + } + Ok(()) + } + + /// `origin new [description] --on `: starts a change on the tip + /// of a target. + /// + /// # Errors + /// + /// Fails if the target doesn't exist, the working copy cannot be + /// snapshotted, or the change cannot be written or checked out. + pub fn new_on(&mut self, target: &TargetName, description: &str) -> Result<(), WorkspaceError> { + let working_copy = self.refresh()?; + self.start_on(target, description, working_copy.revision.snapshot) + } + + /// Starts a change described by `description` on the tip of `target`, + /// and checks it out over the files of `files`. + fn start_on( + &mut self, + target: &TargetName, + description: &str, + files: TreeId, + ) -> Result<(), WorkspaceError> { + let tip = self + .store + .head(target)? + .ok_or_else(|| WorkspaceError::UnknownTarget(target.clone()))?; + let description = EncodedObject::blob(description.as_bytes()); + let revision = Revision { + change: ChangeId::random().map_err(WorkspaceError::Random)?, + predecessors: Vec::new(), + base: Base::Acceptance(tip.acceptance), + deps: Vec::new(), + snapshot: self.store.get(tip.acceptance)?.body().state, + description: description.id().into(), + author: self.config.user(), + }; + let encoded = revision.encode(); + self.write(&[vec![description], vec![encoded.clone()]])?; + self.checkout(files, revision.snapshot)?; + self.point_at(encoded.id().into())?; + println!("Working copy: {}", self.headline(&revision)?); + println!("On top of: {}", self.describe_base(revision.base)?); + Ok(()) + } + + /// The target a revision's stack is on: that of the acceptance at its + /// bottom. + fn target_of(&self, revision: RevisionId) -> Result { + let mut base = self.store.get(revision)?.base; + loop { + match base { + Base::Revision(id) => base = self.store.get(id)?.base, + Base::Acceptance(id) => { + return self.target_name(self.store.get(id)?.body().target); + } + } + } + } + + /// Moves the working copy after `landings` on `onto`, as the module + /// explains. + fn follow( + &mut self, + working_copy: &WorkingCopy, + onto: &TargetName, + landings: &[Landing], + ) -> Result<(), WorkspaceError> { + let revision = &working_copy.revision; + let files = revision.snapshot; + // A change the target already had is as good as landed. + let landed = |change: ChangeId| { + landings.iter().any(|landing| match landing { + Landing::Landed { proposal, .. } | Landing::Unchanged(proposal) => { + *proposal == Proposal::Change(change) + } + Landing::Pending(_) => false, + }) + }; + if landed(revision.change) { + return self.start_on(onto, "", files); + } + for landing in landings { + if let Landing::Pending(Pending { + proposal: Proposal::Change(change), + subject: RecordId::Revision(rebased), + .. + }) = landing + && *change == revision.change + && *rebased != working_copy.id + { + let rebased = *rebased; + self.checkout(files, self.store.get(rebased)?.snapshot)?; + self.point_at(rebased)?; + println!( + "Working copy: {}", + self.headline(&self.store.get(rebased)?)? + ); + return Ok(()); + } + } + let Base::Revision(below) = revision.base else { + return Ok(()); + }; + let below = self.store.get(below)?; + if !landed(below.change) { + return Ok(()); + } + self.rebase_onto_tip(working_copy, below.snapshot, onto) + } + + /// Rebases the working copy, made on `base`, onto the tip of `onto`, if + /// that merges cleanly. + fn rebase_onto_tip( + &mut self, + working_copy: &WorkingCopy, + base: TreeId, + onto: &TargetName, + ) -> Result<(), WorkspaceError> { + let tip = self + .store + .head(onto)? + .ok_or_else(|| WorkspaceError::UnknownTarget(onto.clone()))?; + let state = self.store.get(tip.acceptance)?.body().state; + let current = &working_copy.revision; + let merge = self.store.merge(Some(base), current.snapshot, state)?; + if !merge.conflicts.is_empty() { + println!( + "The working copy stays where it is: it conflicts with `{onto}` in {}", + Self::list_paths(&merge.conflicts.paths()) + ); + return Ok(()); + } + let revision = Revision { + predecessors: vec![working_copy.id], + base: Base::Acceptance(tip.acceptance), + snapshot: merge.tree, + author: self.config.user(), + ..current.clone() + }; + let encoded = revision.encode(); + self.write(&[merge.trees, vec![encoded.clone()]])?; + self.checkout(current.snapshot, revision.snapshot)?; + self.point_at(encoded.id().into())?; + println!("Working copy: {}", self.headline(&revision)?); + println!("On top of: {}", self.describe_base(revision.base)?); + Ok(()) + } + + fn print_landing(&self, landing: &Landing, onto: &TargetName) -> Result<(), WorkspaceError> { + match landing { + Landing::Landed { + proposal, + source, + acceptance, + } => { + let landed = match source { + Source::Revision(revision) => format!(", revision {},", revision.short()), + Source::Target(_) => String::new(), + }; + println!( + "Landed {proposal}{landed} on {}", + self.describe_acceptance(acceptance.acceptance)? + ); + } + Landing::Unchanged(proposal) => { + println!("Landing {proposal} would change nothing on `{onto}`"); + } + Landing::Pending(pending) => Self::print_pending(pending), + } + Ok(()) + } + + fn print_pending(pending: &Pending) { + let Pending { + onto, + proposal, + subject, + blocked, + } = pending; + let retry = match proposal { + Proposal::Change(change) => format!("origin land {} --onto {onto}", change.short()), + Proposal::Target(target) => format!("origin land --from {target} --onto {onto}"), + }; + match blocked { + Blocked::Evidence(missing) => { + let missing: Vec<_> = missing.iter().map(ToString::to_string).collect(); + println!( + "The landing of {proposal} on `{onto}` waits for {}", + missing.join(", ") + ); + println!( + " about {}: once they're issued, run `{retry}`", + subject.describe() + ); + } + Blocked::Conflict { paths, .. } => { + println!( + "The landing of {proposal} on `{onto}` conflicts in {}", + Self::list_paths(paths) + ); + println!(" resolve it in a new revision, then run `{retry}`"); + } + } + } +} diff --git a/apps/origin-src/src/workspace/mod.rs b/apps/origin-src/src/workspace/mod.rs index b3a4eefd..b3618efc 100644 --- a/apps/origin-src/src/workspace/mod.rs +++ b/apps/origin-src/src/workspace/mod.rs @@ -19,6 +19,7 @@ //! without them. mod annotate; +mod land; pub use annotate::{Annotated, Payload}; @@ -39,6 +40,7 @@ use crate::config::{ConfigError, UserConfig}; use crate::conflict::Conflicts; use crate::index::{FindChangeError, FindError, Index}; use crate::output::FileDiff; +use crate::policy::PolicyError; use crate::queue::{LandError, Queue}; use crate::store::{Store, StoreError}; use crate::worktree::{SnapshotError, StatCache, Worktree}; @@ -1077,6 +1079,9 @@ pub enum WorkspaceError { /// A statement cannot be issued. #[error(transparent)] Annotate(#[from] AnnotateError), + /// The policy cannot be read. + #[error(transparent)] + Policy(#[from] PolicyError), /// A landing failed. #[error(transparent)] Land(#[from] LandError), diff --git a/apps/origin-src/tests/cli.rs b/apps/origin-src/tests/cli.rs index 6c763244..34fe0e8f 100644 --- a/apps/origin-src/tests/cli.rs +++ b/apps/origin-src/tests/cli.rs @@ -273,12 +273,15 @@ fn divergence_shows_until_converged() { assert!(status.contains("Conflicts: file hold"), "{status}"); assert!(repo.ok(&["evolog"]).contains(" converges ")); - // Describing it keeps the conflict, an edit resolves it, whatever the - // file then holds. + // It doesn't land until the file changes: describing it keeps the + // conflict, an edit resolves it, whatever the file then holds. repo.ok(&["describe", "Shared work, converged"]); - assert!(repo.ok(&["status"]).contains("Conflicts: file hold")); + let refused = repo.fails(&["land"]); + assert!(refused.contains("unresolved conflicts"), "{refused}"); + assert!(refused.contains("edit file"), "{refused}"); repo.write("file", "Heading\n=======\nboth\n"); assert!(!repo.ok(&["status"]).contains("Conflicts")); + assert!(repo.ok(&["land"]).starts_with("Landed change")); assert!( repo.fails(&["converge", &change]) .contains("is not divergent") @@ -305,14 +308,14 @@ fn binary_conflicts_resolve_only_when_accepted() { // An edit elsewhere leaves it unresolved, even without markers. repo.write("unrelated", "docs\n"); - assert!(repo.ok(&["status"]).contains("Conflicts: image, text hold")); + assert!(repo.fails(&["land"]).contains("unresolved conflicts")); let resolved = repo.ok(&["resolve", "image", "unrelated"]); assert!(resolved.starts_with("Resolved image\n"), "{resolved}"); assert!(resolved.contains("Conflicts: text hold"), "{resolved}"); let status = repo.ok(&["status"]); assert!(status.contains("\nConflicts: text hold"), "{status}"); repo.ok(&["resolve", "text"]); - assert!(!repo.ok(&["status"]).contains("Conflicts")); + assert!(repo.ok(&["land"]).starts_with("Landed change")); } #[test] @@ -365,7 +368,8 @@ fn a_revision_is_published_only_after_its_conflicts() { // So the revision, which would hide the conflict, wasn't written. assert!(!store.contains(described.id()).unwrap()); - assert!(repo.ok(&["status"]).contains("Conflicts: file hold")); + let refused = repo.fails(&["land"]); + assert!(refused.contains("unresolved conflicts"), "{refused}"); } #[test] @@ -919,6 +923,220 @@ fn statements_are_promoted_where_their_inputs_are_unchanged() { assert!(repo.fails(&["annotate", "@", "ci.lint"]).contains("--pass")); } +#[test] +fn landing_moves_the_working_copy_onto_the_target() { + let repo = Repo::new(); + repo.write("a", "a\n"); + repo.ok(&["describe", "Add a"]); + let first = repo.change(); + repo.ok(&["new", "Add b"]); + let second = repo.change(); + repo.write("b", "b\n"); + repo.ok(&["new"]); + let empty = repo.change(); + + // The stack lands from the bottom; the empty working copy has nothing + // to land, and moves onto the new tip. + let land = repo.ok(&["land"]); + let lines: Vec<_> = land.lines().collect(); + assert!( + lines[0].starts_with(&format!("Landed change {first}, revision ")), + "{land}" + ); + assert!(lines[0].ends_with(", on main #1"), "{land}"); + assert!( + lines[1].starts_with(&format!("Landed change {second}, ")), + "{land}" + ); + assert_eq!( + lines[2], + format!("Landing change {empty} would change nothing on `main`") + ); + assert!( + repo.ok(&["status"]) + .contains("On top of: main #2\nNo changes.\n") + ); + + // Landing the working copy's change starts a new one on the tip. + repo.write("c", "c\n"); + let land = repo.ok(&["land"]); + assert!(land.contains(" on main #3\nWorking copy: "), "{land}"); + assert_ne!(repo.change(), empty); + assert!( + repo.ok(&["status"]) + .ends_with("On top of: main #3\nNo changes.\n") + ); + assert_eq!(repo.read("c"), "c\n"); + + let targets = repo.ok(&["targets"]); + assert!(targets.starts_with("main #3 "), "{targets}"); + assert!(targets.contains("(node:ada)\npolicy #0 "), "{targets}"); +} + +#[test] +fn a_change_the_target_already_has_moves_onto_its_tip() { + let repo = Repo::new(); + repo.write("f", "f\n"); + let ours = repo.change(); + repo.ok(&["new", "--on", "main", "The same file"]); + repo.write("f", "f\n"); + repo.ok(&["land"]); + repo.ok(&["edit", &ours]); + let land = repo.ok(&["land"]); + assert!(land.contains(" would change nothing on `main`\n"), "{land}"); + + // Deleting the file is then a change to what `main` has. + fs::remove_file(repo.path("f")).unwrap(); + let land = repo.ok(&["land"]); + assert!(land.starts_with("Landed change "), "{land}"); + assert!(!repo.path("f").exists()); +} + +#[test] +fn targets_fork_and_land_on_each_other() { + let repo = Repo::new(); + repo.ok(&["target", "new", "overhaul", "--from", "main"]); + assert!( + repo.fails(&["target", "new", "overhaul", "--from", "main"]) + .contains("already named `overhaul`") + ); + repo.ok(&["new", "--on", "overhaul", "Rework"]); + repo.write("rework", "new\n"); + let land = repo.ok(&["land"]); + assert!( + land.contains(" on overhaul #1\n"), + "lands where it's made: {land}" + ); + + let land = repo.ok(&["land", "--from", "overhaul", "--onto", "main"]); + assert_eq!(land, "Landed `overhaul` on main #1\n"); + assert!(repo.ok(&["log", "main"]).starts_with("◆ main #1 ")); + assert!(repo.ok(&["log", "main"]).contains("\n overhaul #1\n")); +} + +/// Lands a policy that makes `main` require `ci.test` to pass, from the +/// runners, ada among them. +fn require_tests(repo: &Repo) { + repo.ok(&["new", "--on", "policy", "Require tests"]); + let policy = repo + .read("policy.toml") + .replace("# [groups]", "[groups]") + .replace( + "# runners = [\"node:ci-1\"]", + "runners = [\"node:ci-1\", \"user:ada\"]", + ) + .replace("# require", "require"); + repo.write("policy.toml", &policy); + assert!(repo.ok(&["land"]).contains(" on policy #1\n")); +} + +#[test] +fn results_there_count_over_promoted_ones() { + let repo = Repo::new(); + require_tests(&repo); + repo.ok(&["new", "--on", "main", "Library"]); + repo.write("src/lib.rs", "fn a() {}\n"); + repo.ok(&["annotate", "@", "ci.test", "--pass", "--inputs", "src"]); + assert!(repo.ok(&["land"]).contains(" on main #1\n")); + // Each change below keeps `src`, so the pass is promoted to it. + let promoted = |description: &str| { + repo.ok(&["new", "--on", "main", description]); + repo.write(&format!("{description}.md"), "docs\n"); + assert!(repo.ok(&["promote"]).contains("Promoted ci.test pass")); + }; + let waits = || { + let landing = repo.ok(&["land"]); + assert!(landing.starts_with("The landing of change "), "{landing}"); + }; + // A pass there, then taken back, takes the promoted one with it. + promoted("withdrawn"); + repo.ok(&["annotate", "@", "ci.test", "--pass", "--inputs", "src"]); + repo.ok(&["annotate", "@", "ci.test", "--withdraw"]); + waits(); + // A failure there wins over the promoted pass. + promoted("failed"); + repo.ok(&["annotate", "@", "ci.test", "--fail", "--inputs", "src"]); + waits(); +} + +#[test] +fn landings_wait_for_the_evidence_policy_requires() { + let repo = Repo::new(); + require_tests(&repo); + assert!( + repo.ok(&["targets"]) + .contains(" requires ci.test pass from group:runners\n") + ); + + repo.ok(&["new", "--on", "main", "Feature"]); + let change = repo.change(); + repo.write("src/lib.rs", "fn a() {}\n"); + let waiting = repo.ok(&["land"]); + assert!( + waiting.starts_with(&format!( + "The landing of change {change} on `main` waits for ci.test pass from group:runners\n" + )), + "{waiting}" + ); + assert_eq!(repo.ok(&["land", "--status"]), waiting); + // A result withdrawn is no evidence. + repo.ok(&["annotate", "@", "ci.test", "--pass", "--inputs", "src"]); + repo.ok(&["annotate", "@", "ci.test", "--withdraw"]); + assert_eq!(repo.ok(&["land"]), waiting); + repo.ok(&["annotate", "@", "ci.test", "--pass", "--inputs", "src"]); + assert!(repo.ok(&["land"]).contains(" on main #1\n")); + assert_eq!(repo.ok(&["land", "--status"]), "No landing waits.\n"); + + // Only the authority lands: here, after a policy edit hands `main` to + // another node. + repo.ok(&["new", "--on", "policy"]); + let policy = repo.read("policy.toml").replace( + "[targets.main]\nauthority = \"node:ada\"", + "[targets.main]\nauthority = \"node:hosted\"", + ); + // Another node's key: any valid one will do. + let key = policy + .lines() + .find_map(|line| line.strip_prefix("\"ada\" = ")) + .unwrap(); + repo.write( + "policy.toml", + &policy.replace("[nodes]\n", &format!("[nodes]\nhosted = {key}\n")), + ); + repo.ok(&["land"]); + repo.ok(&["new", "--on", "main"]); + repo.write("x", "x\n"); + assert!( + repo.fails(&["land"]) + .contains("node:hosted is the authority of `main`") + ); +} + +#[test] +fn conflicting_landings_are_flagged_and_wait() { + let repo = Repo::new(); + repo.ok(&["describe", "One"]); + repo.write("shared", "one\n"); + let one = repo.change(); + repo.ok(&["new", "--on", "main", "Two"]); + repo.write("shared", "two\n"); + let two = repo.change(); + repo.ok(&["land", &one]); + + let conflict = repo.ok(&["land"]); + assert!( + conflict.starts_with(&format!( + "The landing of change {two} on `main` conflicts in shared\n" + )), + "{conflict}" + ); + assert!( + repo.ok(&["statements"]) + .contains(" core.conflict by node:ada\n") + ); + assert_eq!(repo.ok(&["land", "--status"]), conflict); +} + #[test] fn converge_merges_the_files_of_every_head() { let repo = Repo::new();