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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
//! The operations that leave the TUI: running `jj`, and handing the
//! terminal over to another program.
use std::{
fs,
io::{self, Write},
process,
};
use jj_compose_core::{Draft, Field, Jj, Plan, Split, symbols};
use ratatui::{
DefaultTerminal,
crossterm::{
event::{DisableBracketedPaste, EnableBracketedPaste},
execute,
terminal::{EnterAlternateScreen, LeaveAlternateScreen, disable_raw_mode, enable_raw_mode},
},
};
use super::{App, Focus, Request, Status, View, ui};
use crate::{app, diff};
impl App {
/// Carry out a [`Request`] with the TUI out of the way.
pub(super) fn perform(
&mut self,
terminal: &mut DefaultTerminal,
jj: &Jj,
request: &Request,
) -> io::Result<()> {
match request {
// One `jj file show` per file, so it is done once per diff and
// only when the overview is actually wanted.
Request::Symbols => {
self.symbols =
symbols::for_patch(jj, &self.target, self.diff.patch()).unwrap_or_default();
}
Request::Split { message } => {
if let Err(e) = self.run_split(jj, message) {
self.status = Some(Status::Error(e));
}
}
Request::OpenDiff { path } => {
// With no tool there is nothing to open but jj's own diff,
// which is the one already on screen; say so instead.
let Some(tool) = self.diff_tool.clone() else {
self.status = Some(Status::Error(
"no diff tool: set ui.diff-editor in your jj config, or pass --diff-tool"
.into(),
));
return Ok(());
};
let target = self.target.clone();
let result = suspend(terminal, || {
// A GUI tool can take a moment to appear, and the screen
// is blank until it does.
println!("Opening {tool}...");
let result = jj.show_diff(&target, &tool, path.as_deref());
// jj pages a terminal tool's output and a GUI holds its
// own window, so only a failure needs the screen held:
// its message would otherwise vanish with the redraw.
if result.is_err() {
wait_for_key("Press Enter to return to jj-compose ");
}
result.map_err(|e| e.to_string())
})?;
if let Err(e) = result {
self.status = Some(Status::Error(e));
}
}
}
Ok(())
}
/// Commit the chosen hunks and carry on with what is left.
///
/// Nothing is taken from the UI until `jj split` has succeeded, so a
/// failure leaves the choice intact to try again.
fn run_split(&mut self, jj: &Jj, message: &str) -> Result<(), String> {
let Some(split) = self.split.as_ref() else {
return Ok(());
};
// Lowest box first, so the numbering is the order the commits end up
// in.
let Some(commit) = split.next_box() else {
return Ok(());
};
let added = split.count(commit);
let text = self.diff.patch().subset_with(&split.taken(commit));
// A HunkId only means anything within one diff, so whatever is still
// planned has to be recorded by content before the split moves it.
self.plan = Plan::capture(self.diff.patch(), split);
// jj runs jj-compose as its diff editor and passes it this patch.
let path = std::env::temp_dir().join(format!("jj-compose-{}.patch", process::id()));
fs::write(&path, text).map_err(|e| format!("couldn't write the patch: {e}"))?;
let exe = std::env::current_exe().map_err(|e| format!("couldn't find jj-compose: {e}"))?;
let result = jj.split(&self.target, message, &path, &exe);
let _ = fs::remove_file(&path);
result.map_err(|e| e.to_string())?;
let left = self.reload(jj)?;
let waiting = self.split.as_ref().map_or(0, Split::assigned);
// Rebuilt rather than appended: each tag needs re-anchoring once, to
// whatever the remainder is after the last split of the session.
self.recovery = self
.at_risk
.iter()
.flat_map(|t| {
[
format!(
"jj tag set {} --allow-move -r {} # was {} ({})",
t.tag, self.target.change_id, t.commit, t.summary
),
format!("git push origin {} --force", t.tag),
]
})
.collect();
self.status = Some(Status::Info(if waiting > 0 {
let next = self.split.as_ref().and_then(Split::next_box).unwrap_or(0);
let planned = self.split.as_ref().map_or(0, Split::planned);
format!(
"Committed {added} {} as commit {commit} of {planned}. Now commit {next}: {waiting} {}.",
app::plural(added, "hunk"),
app::plural(waiting, "hunk"),
)
} else {
format!(
"Committed {added} {}. {left} {} left to describe.",
app::plural(added, "hunk"),
app::plural(left, "hunk"),
)
}));
Ok(())
}
/// Move to what is left of the change after a split, and start again on
/// it: new diff, the description jj carried over, and a fresh split.
fn reload(&mut self, jj: &Jj) -> Result<usize, String> {
// The hunks that were split off keep the old change ID; the
// remainder is a new change, and the only child of that commit.
let remainder = format!("{}+", self.target.change);
self.target = jj.resolve(&remainder).map_err(|e| e.to_string())?;
self.change_id = self.target.change_id.clone();
self.diff = diff::load(jj, &self.target).map_err(|e| e.to_string())?;
// A new diff means the old symbols describe the wrong lines.
self.symbols.clear();
self.overview = None;
self.visual = None;
let description = jj.description(&self.target).map_err(|e| e.to_string())?;
let original = description.trim_end().to_owned();
let (draft, view) = Draft::parse(&original).map_or_else(
|_| (Draft::default(), View::Raw),
|draft| (draft, View::Structured),
);
self.fields = Field::ALL.map(|field| ui::field_editor(field, draft.get(field)));
self.raw = ui::raw_editor(&original);
self.view = view;
self.original = original;
self.field = Field::Type;
if let Some(mut split) = Split::new(self.splittable()) {
// Put the still-uncommitted hunks back in the boxes they were
// planned into, which their new HunkIds cannot say.
self.plan.apply(self.diff.patch(), &mut split);
let waiting = split.assigned() > 0;
self.enter_split(split);
if waiting {
// The next commit needs a message of its own; the
// description jj carried over belongs to whatever is left
// at the very end.
self.fields = Field::ALL.map(|field| ui::field_editor(field, ""));
self.raw = ui::raw_editor("");
self.view = View::Structured;
self.original = String::new();
self.focus = Focus::Editor;
self.zoomed = false;
}
} else {
self.split = None;
self.focus = Focus::Editor;
self.zoomed = false;
}
Ok(self.diff.hunks().len())
}
}
/// Run `f` with the terminal handed back to the shell (cooked mode, main
/// screen), then take it over again and redraw from scratch.
fn suspend<T>(terminal: &mut DefaultTerminal, f: impl FnOnce() -> T) -> io::Result<T> {
disable_raw_mode()?;
execute!(io::stdout(), DisableBracketedPaste, LeaveAlternateScreen)?;
let out = f();
enable_raw_mode()?;
execute!(io::stdout(), EnterAlternateScreen, EnableBracketedPaste)?;
// ratatui only redraws cells it thinks changed; after another program
// has used the screen, everything has.
terminal.clear()?;
Ok(out)
}
fn wait_for_key(prompt: &str) {
print!("\n{prompt}");
let _ = io::stdout().flush();
let _ = io::stdin().read_line(&mut String::new());
}