1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
use camino::Utf8Path;
use camino::Utf8PathBuf;
use crate::Error;
use crate::Result;
use crate::jj::jj;
use crate::message::created_commit;
use crate::message::message;
/// Each goal NAME has the bookmarks jeans/NAME/goal and jeans/NAME/done.
const GOALS: &str = "glob:jeans/*/goal";
const MEGAMERGE: &str = r#"bookmarks(exact:"mm")"#;
/// What `jeans init` needs to make a goal.
pub(crate) struct Init<'a> {
pub(crate) title: &'a str,
pub(crate) goal: Option<&'a str>,
pub(crate) body: Option<&'a str>,
pub(crate) team_size: i64,
pub(crate) home: Option<&'a str>,
}
/// Makes the goal commit and its done node, and returns the goal's change ID.
pub(crate) fn init(init: &Init<'_>) -> Result<String> {
let goal = init.goal.filter(|g| is_goal_name(g)).ok_or_else(|| {
Error::msg("init needs --goal NAME, made of letters, digits, '.', '_', and '-'")
})?;
let root = Utf8PathBuf::from(jj(None, true, &["root"])?.stdout.trim());
let run = |args: &[&str]| jj(Some(&root), true, args);
if goals(&root, true)?.iter().any(|g| g == goal) {
return Err(Error::msg(format!(
"this repo already has a goal named {goal}"
)));
}
let home = match init.home {
Some(home) => Utf8PathBuf::from(home),
None => root.with_file_name(format!("{}.jeans", root.file_name().unwrap_or_default())),
};
let home = Utf8PathBuf::try_from(std::path::absolute(&home)?)?;
fs_err::create_dir_all(&home)?;
let team_size = init.team_size.to_string();
for (key, value) in [
("jeans.main", root.as_str()),
("jeans.home", home.as_str()),
("jeans.team-size", &team_size),
] {
run(&["config", "set", "--repo", key, value])?;
}
let goal_message = message(init.title, init.body);
let new = run(&["new", "--no-edit", "trunk()", "-m", &goal_message])?;
let id = created_id(&new.stderr, &root, true)?;
run(&["bookmark", "create", &goal_bookmark(goal), "-r", &id])?;
let integrate = integration_node(&root)?;
run(&["config", "set", "--repo", "jeans.integrate", &integrate])?;
// Branching from the trunk the integration node already holds keeps newer
// trunk commits out of it until finished tasks bring them in.
let base = format!("heads(::({integrate}) & ::trunk())");
let done_message = format!("Finished tasks for: {}", init.title);
let new = run(&[
"new",
"--no-edit",
"-A",
&base,
"-B",
&integrate,
"-m",
&done_message,
])?;
let done = created_id(&new.stderr, &root, true)?;
run(&["bookmark", "create", &done_bookmark(goal), "-r", &done])?;
Ok(id)
}
/// The names of the repo's goals, in bookmark order.
pub(crate) fn goals(repo: &Utf8Path, snapshot: bool) -> Result<Vec<String>> {
let args = ["bookmark", "list", GOALS, "-T", r#"name ++ "\n""#];
let out = jj(Some(repo), snapshot, &args)?;
let mut names: Vec<String> = Vec::new();
for line in out.stdout.lines() {
let name = line.strip_prefix("jeans/").unwrap_or(line);
let name = name.strip_suffix("/goal").unwrap_or(name);
if !names.iter().any(|n| n == name) {
names.push(name.to_owned());
}
}
Ok(names)
}
/// The full change ID of the commit jj's stderr says it created.
pub(crate) fn created_id(stderr: &str, repo: &Utf8Path, snapshot: bool) -> Result<String> {
let Some(short) = created_commit(stderr) else {
// cov-excl-start
return Err(Error::msg(format!(
"couldn't find the new commit in:\n{stderr}"
)));
// cov-excl-stop
};
let args = ["log", "--no-graph", "-r", short, "-T", "change_id"];
let out = jj(Some(repo), snapshot, &args)?;
Ok(out.stdout.trim().to_owned())
}
pub(crate) fn goal_bookmark(name: &str) -> String {
format!("jeans/{name}/goal")
}
pub(crate) fn done_bookmark(name: &str) -> String {
format!("jeans/{name}/done")
}
/// Matches nothing once the bookmark is gone, where a bare name would fail.
pub(crate) fn bookmark_revset(bookmark: &str) -> String {
format!(r#"bookmarks(exact:"{bookmark}")"#)
}
/// Finished work goes into the megamerge when there is one, and otherwise
/// under the main workspace's working copy.
fn integration_node(root: &Utf8Path) -> Result<String> {
let run = |args: &[&str]| jj(Some(root), true, args);
let mm = run(&["log", "--no-graph", "-r", MEGAMERGE, "-T", "change_id"])?;
if !mm.stdout.trim().is_empty() {
return Ok(MEGAMERGE.to_owned());
}
let current = r#"if(target.current_working_copy(), name ++ "\n")"#;
let workspace = run(&["workspace", "list", "-T", current])?;
Ok(format!("{}@", workspace.stdout.trim()))
}
/// Letters, digits, '.', '_', and '-', starting with a letter or digit.
fn is_goal_name(name: &str) -> bool {
let mut chars = name.chars();
chars.next().is_some_and(|c| c.is_ascii_alphanumeric())
&& chars.all(|c| c.is_ascii_alphanumeric() || matches!(c, '.' | '_' | '-'))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn goal_names_start_with_a_letter_or_digit() {
assert!(is_goal_name("a1._-"));
assert!(!is_goal_name(""));
assert!(!is_goal_name("-a"));
assert!(!is_goal_name("a/b"));
}
}