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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
//! What:
//! One destination's journaled installation to an end, and the synchronization of every created worktree.
//! The journal records the phase, the install log records each batch's claims and creations,
//! and a finished or failed installation always removes its journal and stage.
//! Why:
//! A failed installation must not stay pending, because recovery would replay the same failure
//! before every later command; an interrupted one must resume from exactly what it recorded.
//!
//! In TS you'd write (pseudocode):
//! ```ts
//! const outcome = await completeJournal({ pending, snapshot });
//! ```
use super::transaction_git::GitContext;
use super::worktree_copy_error::{WorktreeCopyError, raw_failure};
use super::worktree_copy_install::{InstallRecorder, install_snapshot};
use super::worktree_copy_install_entry::InstalledPath;
use super::worktree_copy_install_log::{InstallLog, open_install_log};
use super::worktree_copy_journal::{
Journal, PendingJournal, Phase, create_journal, remove_pending_journal, write_journal,
};
use super::worktree_copy_manifest::{WorktreeCopyEntry, collect_entry_manifest};
use super::worktree_copy_selection::CopySelection;
use super::worktree_copy_stage::{StagedSnapshot, stage_ignored_snapshot};
use crate::json_record::quote;
use std::collections::BTreeSet;
use std::path::{Path, PathBuf};
/// What:
/// The mutable durable state of one installation: the latest journal, the open log, and every claim and creation so far.
/// Why:
/// The installer reports claims and creations through this state, so each one reaches disk before the next step.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// type JournalState = { pending; log; intended: Set<string>; createdPaths: Set<string> };
/// ```
pub struct TransactionState {
/// The latest durable journal.
pub pending: PendingJournal,
/// The open append-only log of this stage.
pub log: InstallLog,
/// Every selected path claimed so far.
pub intended: BTreeSet<String>,
/// Every path whose creation has been recorded so far.
pub created_paths: BTreeSet<String>,
}
impl InstallRecorder for TransactionState {
/// Records the claims that no earlier record holds, then remembers them.
fn record_intents(&mut self, relative_paths: &[String]) -> Result<(), WorktreeCopyError> {
let unclaimed: Vec<String> = relative_paths
.iter()
.filter(|relative_path| return !self.intended.contains(relative_path.as_str()))
.cloned()
.collect();
self.log.append_intents(unclaimed.as_slice())?;
self.intended.extend(unclaimed);
return Ok(());
}
/// Records the creations no earlier record holds, then remembers them.
fn record_creations(&mut self, entries: &[InstalledPath]) -> Result<(), WorktreeCopyError> {
let unrecorded: Vec<InstalledPath> = entries
.iter()
.filter(|entry| return !self.created_paths.contains(entry.relative_path.as_str()))
.cloned()
.collect();
self.log.append_creations(unrecorded.as_slice())?;
self.created_paths.extend(
unrecorded
.into_iter()
.map(|entry| return entry.relative_path),
);
return Ok(());
}
}
/// What:
/// Persists the installing phase before the destination is touched.
/// Why:
/// Recovery must know the installation had started, so it reads the log instead of discarding the stage.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// async function beginInstalling(state): Promise<void>;
/// ```
pub fn begin_installing(state: &mut TransactionState) -> Result<(), WorktreeCopyError> {
if state.pending.journal.phase == Phase::Installing {
return Ok(());
}
let installing: Journal = Journal {
phase: Phase::Installing,
..state.pending.journal.clone()
};
write_journal(state.pending.path.as_path(), &installing)?;
state.pending.journal = installing;
return Ok(());
}
/// What:
/// The validated staged payload a journal describes, rebuilt from its stage.
/// Why:
/// Recovery installs from the stage that exists now, and refuses a stage that lost a claimed path.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// async function snapshotFromJournal({ journal, intendedEntries }): Promise<StagedWorktreeSnapshot>;
/// ```
pub fn snapshot_from_journal(
pending: &PendingJournal,
intended: &BTreeSet<String>,
) -> Result<StagedSnapshot, WorktreeCopyError> {
let stage_root: PathBuf = PathBuf::from(pending.journal.stage_root.as_str());
let outcome: Result<Vec<WorktreeCopyEntry>, WorktreeCopyError> = (|| {
let entries: Vec<WorktreeCopyEntry> = collect_entry_manifest(
stage_root.as_path(),
pending.journal.selected_roots.as_slice(),
&[],
)?;
let present: BTreeSet<&str> = entries
.iter()
.map(|entry| return entry.relative_path.as_str())
.collect();
for claim in intended {
if !present.contains(claim.as_str()) {
let message: String = format!(
"cli-git: worktree-copy journal intent is absent from private stage {}.",
quote(pending.journal.stage_root.as_str())
);
return Err(super::worktree_copy_error::failure_with_cause(
message.as_str(),
None,
));
}
}
return Ok(entries);
})();
// Every failure here reads as a failed recovery, with its own cause after it.
let entries: Vec<WorktreeCopyEntry> = match outcome {
Ok(entries) => entries,
Err(error) => {
let message: String = format!(
"cli-git: could not recover staged ignored state at {}.",
quote(pending.journal.stage_root.as_str())
);
return Err(super::worktree_copy_error::failure_with_cause(
message.as_str(),
Some(&error.0),
));
}
};
return Ok(StagedSnapshot {
entries,
selected_roots: pending.journal.selected_roots.clone(),
source_root: PathBuf::from(pending.journal.source_root.as_str()),
stage_container: PathBuf::from(pending.journal.stage_container.as_str()),
stage_root,
});
}
/// What:
/// The result of one installation attempt.
/// Why:
/// A failed installation is an outcome the caller reports, while a journal fault is an error that keeps the state.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// type InstallationOutcome = { kind: 'installed'; copiedEntries: number } | { kind: 'ended'; failure: WorktreeCopyError };
/// ```
#[derive(Debug)]
pub enum Outcome {
/// Every selected entry exists; the count is the number this transaction created.
Installed(usize),
/// Installation failed after rollback; the journal and stage are removed.
Ended(WorktreeCopyError),
}
/// What:
/// Runs one journaled installation: opens the log, seeds the claims, installs, and reports the latest journal.
/// Why:
/// Resumption and first attempts share this path, so an interrupted run's records are always honored.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// async function installRecorded({ pending, snapshot }): Promise<{ latest; result }>;
/// ```
fn install_recorded(
pending: PendingJournal,
snapshot: &dyn Fn(&BTreeSet<String>) -> Result<StagedSnapshot, WorktreeCopyError>,
) -> Result<(PendingJournal, Outcome), WorktreeCopyError> {
let stage: PathBuf = PathBuf::from(pending.journal.stage_container.as_str());
let opened = open_install_log(stage.as_path())?;
let mut intended: BTreeSet<String> = pending.journal.intended_entries.iter().cloned().collect();
intended.extend(opened.recorded.intended.iter().cloned());
let mut created_paths: BTreeSet<String> = pending
.journal
.created_entries
.iter()
.map(|entry| return entry.relative_path.clone())
.collect();
created_paths.extend(
opened
.recorded
.created
.iter()
.map(|entry| return entry.relative_path.clone()),
);
let mut prior: Vec<InstalledPath> = pending.journal.created_entries.clone();
prior.extend(opened.recorded.created.iter().cloned());
let staged: StagedSnapshot = snapshot(&intended)?;
let mut state: TransactionState = TransactionState {
pending,
log: opened.log,
intended,
created_paths,
};
begin_installing(&mut state)?;
let claimed: BTreeSet<String> = state
.intended
.union(&state.created_paths)
.cloned()
.collect();
let destination: PathBuf = PathBuf::from(state.pending.journal.destination_root.as_str());
let result: Result<usize, WorktreeCopyError> = install_snapshot(
staged.stage_root.as_path(),
destination.as_path(),
staged.entries.as_slice(),
&claimed,
prior.as_slice(),
&mut state,
);
let TransactionState {
pending: latest, ..
} = state;
// `match` turns an installation failure into an outcome, and leaves journal faults as errors.
match result {
Ok(copied) => return Ok((latest, Outcome::Installed(copied))),
Err(error) => return Ok((latest, Outcome::Ended(error))),
}
}
/// What:
/// Completes one journaled installation and removes its journal and stage, whatever the installation's outcome.
/// Why:
/// Only a journal that cannot be read or written stays behind, for inspection.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// async function completeJournal({ pending, snapshot }): Promise<InstallationOutcome>;
/// ```
pub fn complete_journal(
pending: PendingJournal,
snapshot: &dyn Fn(&BTreeSet<String>) -> Result<StagedSnapshot, WorktreeCopyError>,
) -> Result<Outcome, WorktreeCopyError> {
let (latest, outcome) = install_recorded(pending, snapshot)?;
remove_pending_journal(&latest)?;
return Ok(outcome);
}
/// What:
/// Stages, journals and installs the ignored state of one newly created worktree.
/// Why:
/// The stage is removed if the journal cannot be created, so no unjournaled stage survives.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// async function synchronizeCreatedWorktree({ commonDir, sourceRoot, destinationRoot, registeredRoots, gitPath }): Promise<number>;
/// ```
pub fn synchronize_created_worktree(
context: &GitContext,
common_dir: &Path,
source_root: &Path,
destination_root: &Path,
registered_roots: &[PathBuf],
selection: &CopySelection,
) -> Result<usize, WorktreeCopyError> {
let snapshot: StagedSnapshot = stage_ignored_snapshot(
context,
source_root,
destination_root,
registered_roots,
selection,
)?;
let pending: PendingJournal = match create_journal(common_dir, destination_root, &snapshot) {
Ok(pending) => pending,
Err(error) => {
// A failed removal replaces the journal failure, as the incumbent's cleanup throws its own error there.
std::fs::remove_dir_all(snapshot.stage_container.as_path())
.map_err(|removal| return raw_failure(&removal))?;
return Err(error);
}
};
let staged: StagedSnapshot = snapshot.clone();
let outcome: Outcome = complete_journal(pending, &|_intended| return Ok(staged.clone()))?;
return match outcome {
Outcome::Installed(copied) => Ok(copied),
Outcome::Ended(error) => Err(error),
};
}
/// Transaction controls stay out of the release executable.
#[cfg(test)]
#[path = "worktree_copy_transaction_tests.rs"]
mod tests;