use std::path::Path;
pub(crate) fn seed_workspace_workflow(workspace: &Path, workflow: &str) {
seed_workspace_config(workspace, &[("workflow.yaml", workflow)]);
}
pub(crate) fn seed_workspace_config(workspace: &Path, files: &[(&str, &str)]) {
let repo = workspace.join("repo.git");
let author = workspace.join(".author");
std::fs::create_dir_all(&repo).unwrap();
let run = |args: &[&str]| {
let status = crate::git_env::status(crate::git_env::git().args(args)).unwrap();
assert!(status.success(), "git {args:?}");
};
let (repo_s, author_s) = (repo.display().to_string(), author.display().to_string());
run(&["init", "-q", "--bare", "-b", "config/default", &repo_s]);
run(&[
"-C",
&repo_s,
"worktree",
"add",
"-q",
"--orphan",
"-b",
"config/default",
&author_s,
]);
for (name, body) in files {
std::fs::write(author.join(name), body).unwrap();
}
run(&["-C", &author_s, "add", "-A"]);
run(&[
"-C",
&author_s,
"-c",
"user.email=t@t.local",
"-c",
"user.name=T",
"-c",
"commit.gpgsign=false",
"commit",
"-q",
"-m",
"config: init [config/default]",
]);
run(&["-C", &repo_s, "worktree", "remove", &author_s]);
}
pub(crate) fn seed_workspace_lineage(workspace: &Path, name: &str) {
let repo = workspace.join("repo.git").display().to_string();
let branch = format!("config/{name}");
let status = crate::git_env::status(crate::git_env::git().args([
"-C",
&repo,
"branch",
&branch,
"config/default",
]))
.unwrap();
assert!(status.success(), "git branch {branch}");
}
pub(crate) fn seed_agent_branch(workspace: &Path, agent: &str, role: Option<&str>) {
let repo = workspace.join("repo.git").display().to_string();
let tree = workspace.join("agents").join(agent);
let tree_s = tree.display().to_string();
let branch = format!("agents/{agent}");
let subject = match role {
Some(role) => format!("dispatch: {role} [{agent}]"),
None => format!("step 001: dispatch [{agent}]"),
};
let run = |args: &[&str]| {
let status = crate::git_env::status(crate::git_env::git().args(args)).unwrap();
assert!(status.success(), "git {args:?}");
};
run(&[
"-C",
&repo,
"worktree",
"add",
"-q",
"-b",
&branch,
&tree_s,
"config/default",
]);
std::fs::write(tree.join("goal.md"), "g").unwrap();
run(&["-C", &tree_s, "add", "-A"]);
run(&[
"-C",
&tree_s,
"-c",
"user.email=t@t.local",
"-c",
"user.name=T",
"-c",
"commit.gpgsign=false",
"commit",
"-q",
"-m",
&subject,
]);
}
pub(crate) fn seed_proposal(
workspace: &Path,
id: &str,
parent: &str,
subject: &str,
files: &[(&str, &str)],
) {
let repo = workspace.join("repo.git");
let tree = workspace.join(".proposal").join(id);
let (repo_s, tree_s) = (repo.display().to_string(), tree.display().to_string());
let branch = format!("proposal/{id}");
let run = |args: &[&str]| {
let status = crate::git_env::status(crate::git_env::git().args(args)).unwrap();
assert!(status.success(), "git {args:?}");
};
run(&[
"-C", &repo_s, "worktree", "add", "-q", "-b", &branch, &tree_s, parent,
]);
for (name, body) in files {
let path = tree.join(name);
if let Some(dir) = path.parent() {
std::fs::create_dir_all(dir).unwrap();
}
std::fs::write(path, body).unwrap();
}
run(&["-C", &tree_s, "add", "-A"]);
run(&[
"-C",
&tree_s,
"-c",
"user.email=t@t.local",
"-c",
"user.name=T",
"-c",
"commit.gpgsign=false",
"commit",
"-q",
"-m",
subject,
]);
run(&["-C", &repo_s, "worktree", "remove", "--force", &tree_s]);
}