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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
|
use std::{
collections::HashMap,
env::current_dir,
ffi::OsStr,
fs,
io::Write,
path::{Path, PathBuf},
process::{Command, Stdio},
};
use anyhow::{Context, Result, bail};
use clap::{Parser, Subcommand};
use crate::{
config::{DROP_IN_NAME, DropInConfig, MAIN_CONFIG_NAME, MainConfig, Owner},
parse::{FileDiff, Patch},
};
mod config;
mod parse;
#[derive(Parser)]
struct Cli {
/// Don't actually do anything, just check'
#[arg(long, global = true)]
dry_run: bool,
#[command(subcommand)]
subcommand: Cmd,
}
#[derive(Subcommand)]
enum Cmd {
/// Apply patches to a rollup target.
Apply {
/// The files to pull the patches to rollup from.
patch_files: Vec<PathBuf>,
/// Which repository to use as a target for the rollup
/// It is specified via the owner, declared in the rollup.toml config.
///
/// The repository needs to be clean.
#[arg(short, long)]
to: String,
/// Show debug information related to the parsing of patch files.
#[arg(short, long)]
debug_parse: bool,
},
/// Create the patch files (based on the given tag)
Create {
/// Which repository to use for the tag to base created tags on.
/// It is specified via the owner, declared in the rollup.toml config.
///
/// The repository needs to be clean.
#[arg(short, long)]
to: String,
},
/// First create the patches, and then automatically apply them.
CreateApply {
/// Which repository to use as a target for the rollup and the tag for new
/// patches.
/// It is specified via the owner, declared in the rollup.toml config.
///
/// The repository needs to be clean.
#[arg(short, long)]
to: String,
/// Show debug information related to the parsing of patch files.
#[arg(short, long)]
debug_parse: bool,
},
}
macro_rules! git {
(@_base for $finalizer:ident in $to:expr; $($args:expr),*) => {
Command::new("git")
.args([
"-C",
$to.display().to_string().as_str(),
$($args),*
])
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.$finalizer()
.with_context(|| format!("Failed to run {}", git!(@_fmt_cmd $($args),*)))?
};
(@_handle_status $output:expr; $($args:expr),*) => {{
if !$output.status.success() {
bail!(
"{} failed!\n\nStderr was:\n {}",
git!(@_fmt_cmd $($args),*),
String::from_utf8($output.stderr).unwrap()
)
}
if !$output.stderr.is_empty() {
let stderr = String::from_utf8($output.stderr).expect("git should always produce utf8");
bail!("{} returned some errors:\n{stderr}", git!(@_fmt_cmd $($args),*));
}
}};
(@_fmt_cmd $($args:expr),*) => {{
let all = ["git", $($args),*].join(" ");
format!("`{}`", all)
}};
(in $to:expr; $($args:expr),*) => {{
let output = git!(@_base for output in $to; $($args),*);
git!(@_handle_status output; $($args),*);
let stdout = String::from_utf8(output.stdout).expect("git should always produce utf8");
let stderr = String::from_utf8(output.stderr).expect("git should always produce utf8");
(stdout, stderr)
}};
(@set_stdin_to $stdin:expr; in $to:expr; $($args:expr),*) => {{
let mut child = git!(@_base for spawn in $to; $($args),*);
let mut stdin = child.stdin.take().expect("is piped");
write!(stdin, "{}", $stdin)?;
drop(stdin);
let output = child.wait_with_output()
.with_context(|| format!("Failed to run `{}`", git!(@_fmt_cmd $($args),*)))?;
git!(@_handle_status output; $($args),*);
let stdout = String::from_utf8(output.stdout).expect("git should always produce utf8");
let stderr = String::from_utf8(output.stderr).expect("git should always produce utf8");
(stdout, stderr)
}};
}
fn main() -> Result<()> {
let args = Cli::parse();
let root = {
let (stdout, _) = git!(in current_dir()?; "rev-parse", "--show-toplevel");
PathBuf::from(stdout.trim())
};
let config = config::load(&root).with_context(|| {
format!(
"Failed to load the config (`{MAIN_CONFIG_NAME}`), in root: `{}`",
root.display()
)
})?;
match args.subcommand {
Cmd::Apply {
patch_files,
to,
debug_parse,
} => {
let owner = config.get_owner(&to)?;
check_owner(owner)?;
apply_patches(patch_files, owner, debug_parse, &config, root, args.dry_run)?
}
Cmd::Create { to } => {
let owner = config.get_owner(&to)?;
create_patches(&owner.last_applied_tag, args.dry_run)?;
}
Cmd::CreateApply { to, debug_parse } => {
let owner = config.get_owner(&to)?;
check_owner(owner)?;
let Some(patches_directory) = create_patches(&owner.last_applied_tag, args.dry_run)?
else {
return Ok(());
};
let patch_files = {
let iter = fs::read_dir(&patches_directory)?;
let mut output = vec![None; iter.count()];
let iter = fs::read_dir(patches_directory)?;
for entry in iter {
let entry = entry?;
if !entry.file_type()?.is_file() {
unreachable!("Should not be able to get non-files in patches directory");
}
let path = entry.path();
let idx: usize = {
let mut file_name = path
.file_name()
.expect("Should all have a filename")
.to_str()
.expect("Should be valid utf8")
.chars();
let number = file_name.clone().take(4).collect::<String>();
assert_eq!(file_name.nth(4), Some('-'));
number.parse().expect("Should parse as usize number")
};
output[idx - 1] = Some(path);
}
output.sort();
output
};
apply_patches(
patch_files.into_iter().map(|s| s.unwrap()).collect(),
owner,
debug_parse,
&config,
root,
args.dry_run,
)?;
}
}
Ok(())
}
fn create_patches(tag: &str, dry_run: bool) -> Result<Option<PathBuf>> {
let current_dir = current_dir()?;
let (stdout, _) = git!(in current_dir; "tag", "--contains", "HEAD", tag);
if !stdout.is_empty() {
println!(
"There are no tags to generate. Tag {} already points to HEAD.",
tag
);
return Ok(None);
}
let mktemp = Command::new("mktemp")
.args(["-t", "rollup_patches_XXXXXX", "-d"])
.output()
.context("Failed to run `mktemp -t rollup_patches_XXXXXX -d`")?;
if !mktemp.status.success() {
bail!("Failed to generate temp-directory for generated patches.");
}
let tempdir = PathBuf::from(String::from_utf8(mktemp.stdout).unwrap().trim());
git!(in current_dir; "format-patch", format!("{}..", tag).as_str(), "-o", tempdir.display().to_string().as_str());
if dry_run {
eprintln!("Dry-run is on; Not actually moving patch tag `{tag}` forward.")
} else {
git!(in current_dir; "tag", "--delete", tag);
git!(in current_dir; "tag", "--annotate", tag, "-m", "");
}
println!("Patches have been generated here: `{}`", tempdir.display());
Ok(Some(tempdir))
}
fn apply_patches(
patch_files: Vec<PathBuf>,
owner: &Owner,
debug_parse: bool,
config: &MainConfig,
root: PathBuf,
dry_run: bool,
) -> Result<()> {
let to = &owner.applies_to;
let mut patches = vec![];
for file in &patch_files {
patches.push(parse::parse_file(file, debug_parse)?);
}
eprintln!("Successfully parsed all {} patch files.", patches.len());
{
let (stdout, _) = git!(in to; "status", "--porcelain");
if !stdout.is_empty() {
bail!(
"`--to` (`{}`) workspace is dirty, refusing to continue.",
to.display()
);
}
}
for (idx, patch) in patches.iter().enumerate() {
eprintln!(
"Applying {}. patch: `{}`... ",
idx + 1,
patch_files[idx].display()
);
let mut final_diffs = vec![];
for file_diff in &patch.files {
match file_diff {
FileDiff::Add { path, .. } => {
if to.join(path).exists() {
bail!("A file was added, that already existed. This should be impossible.")
}
if drop_in_applies(&config.drop_ins, owner, &root, path)? {
final_diffs.push(file_diff.to_owned());
}
}
FileDiff::Remove { path, .. } => {
if to.join(path).exists() {
if drop_in_applies(&config.drop_ins, owner, &root, path)? {
final_diffs.push(file_diff.to_owned());
}
} else {
eprintln!(
" > Dropping remove of `{}`, as the file does not exist in the rollup target.",
path.display()
);
}
}
FileDiff::RenameNoChange { from, to, .. }
| FileDiff::RenameWithChange { from, to, .. } => {
if to.join(from).exists() {
if to.join(to).exists() {
bail!(
"A rename (`{}` -> `{}`) tried to rename a path to an already existing path. This should be impossible.",
from.display(),
to.display()
)
}
if drop_in_applies(&config.drop_ins, owner, &root, from)? {
final_diffs.push(file_diff.to_owned());
}
} else {
eprintln!(
" > Dropping rename from `{}` to `{}`, as the from file does not exist in the rollup target.",
from.display(),
to.display()
);
}
}
FileDiff::Change { path, .. } => {
if to.join(path).exists() {
if drop_in_applies(&config.drop_ins, owner, &root, path)? {
final_diffs.push(file_diff.to_owned());
}
} else {
eprintln!(
" > Dropping change to `{}`, as the file does not exist in the rollup target.",
path.display()
);
}
}
}
}
if final_diffs.is_empty() {
eprintln!(" -> Patch is empty after dropping, skipping it.",);
continue;
}
let filtered_patch = Patch {
files: final_diffs,
..patch.to_owned()
};
if dry_run {
eprintln!("Dry-run is on; Not actually applying, adding or commiting");
} else {
git!(@set_stdin_to filtered_patch; in to; "apply", "-", "--whitespace=nowarn");
git!(in to; "add", ".");
git!(@set_stdin_to filtered_patch.commit_message; in to; "commit", "--file=-", format!("--date={}", filtered_patch.date).as_str(), "--quiet");
}
}
Ok(())
}
fn check_owner(owner: &Owner) -> Result<()> {
let to = &owner.applies_to;
let to = to.join(".git");
// NOTE: We cannot just try to stat() the path, because landlock might be in use and does not
// currently deny that (tracked in: https://github.com/landlock-lsm/linux/issues/52) <2026-11-09>
// So we instead try to list it.
let is_listable = {
let dir_entries = fs::read_dir(&to).with_context(|| {
format!(
"Failed check regarding `--to` directory (`{}`) readability. Are you in a sandbox?",
to.display()
)
})?;
dir_entries.count() > 0
};
if !is_listable {
bail!("To directory (`{}`) is not readable", to.display());
}
Ok(())
}
fn drop_in_applies(
drop_ins: &HashMap<PathBuf, DropInConfig>,
owner: &Owner,
root: &Path,
path: &Path,
) -> Result<bool> {
let path = &path.canonicalize()?;
let file_name = path.file_name().expect("All paths are to files");
if file_name == OsStr::new(DROP_IN_NAME) || file_name == OsStr::new(MAIN_CONFIG_NAME) {
eprintln!(
"Not applying change to `{}`, because it's a rollup config or drop in.",
path.display()
);
return Ok(false);
}
if path.parent() == Some(root) {
return Ok(true);
}
let path = PathBuf::from(
&path
.display()
.to_string()
.strip_prefix(format!("{}/", root.display()).as_str())
.expect("All paths are in root"),
);
if let Some(conf) = drop_ins.get(&path)
&& conf.owner == owner.name
{
return Ok(true);
}
let mut parent: &Path = &path;
while let Some(next) = parent.parent() {
// eprintln!("Checking `{}` for drop-ins..", next.display());
if let Some(conf) = drop_ins.get(&root.join(next)) {
// eprint!("Found a drop-in for `{}`", next.display());
if conf.owner != owner.name {
// eprintln!(", but it's for `{}`, so ignoring path.", conf.owner);
eprintln!(
"`{}` says this dir is for `{}`, so ignoring path.",
next.join(DROP_IN_NAME).display(),
conf.owner
);
return Ok(false);
} else {
// eprintln!(", which matches.");
return Ok(true);
}
}
parent = next;
}
bail!(
"There was no drop-in config defining path ownership for: `{}`",
path.display()
);
}
|