diff options
Diffstat (limited to 'pkgs/by-name/ro/rollup/src')
| -rw-r--r-- | pkgs/by-name/ro/rollup/src/main.rs | 315 | ||||
| -rw-r--r-- | pkgs/by-name/ro/rollup/src/parse.rs | 437 |
2 files changed, 752 insertions, 0 deletions
diff --git a/pkgs/by-name/ro/rollup/src/main.rs b/pkgs/by-name/ro/rollup/src/main.rs new file mode 100644 index 00000000..ed4e29b4 --- /dev/null +++ b/pkgs/by-name/ro/rollup/src/main.rs @@ -0,0 +1,315 @@ +use std::{ + env::current_dir, + fs, + io::Write, + path::{Path, PathBuf}, + process::{Command, Stdio}, +}; + +use anyhow::{Context, Result, bail}; +use clap::{Parser, Subcommand}; + +use crate::parse::{FileDiff, Patch}; + +mod parse; + +#[derive(Parser)] +struct Cli { + #[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 + /// + /// The repository needs to be clean. + #[arg(short, long)] + to: PathBuf, + + /// 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 tag to use to mark the already applied patches. + #[arg(default_value = "CONFIG_APPLIED")] + tag: String, + }, + + /// First create the patches, and then automatically apply them. + CreateApply { + /// Which tag to use to mark the already applied patches. + #[arg(default_value = "CONFIG_APPLIED")] + tag: String, + + /// Which repository to use as a target for the rollup + /// + /// The repository needs to be clean. + #[arg(short, long)] + to: PathBuf, + + /// 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(); + + match args.subcommand { + Cmd::Apply { + patch_files, + to, + debug_parse, + } => { + check_to(&to)?; + apply_patches(patch_files, to, debug_parse)? + } + Cmd::Create { tag } => { + create_patches(tag)?; + } + Cmd::CreateApply { + tag, + to, + debug_parse, + } => { + check_to(&to)?; + + let Some(patches_directory) = create_patches(tag)? else { + return Ok(()); + }; + let patch_files = { + let iter = fs::read_dir(patches_directory)?; + let mut output = vec![]; + 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"); + } + output.push(entry.path()); + } + output.sort(); + output + }; + apply_patches(patch_files, to, debug_parse)?; + } + } + + Ok(()) +} + +fn create_patches(tag: String) -> Result<Option<PathBuf>> { + let current_dir = current_dir()?; + + let (stdout, _) = git!(in current_dir; "tag", "--contains", "HEAD", tag.as_str()); + 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()); + git!(in current_dir; "tag", "--delete", tag.as_str()); + git!(in current_dir; "tag", "--annotate", tag.as_str(), "-m", ""); + + println!("Patches have been generated here: `{}`", tempdir.display()); + + Ok(Some(tempdir)) +} + +fn apply_patches(patch_files: Vec<PathBuf>, to: PathBuf, debug_parse: bool) -> Result<()> { + 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.") + } + final_diffs.push(file_diff.to_owned()); + // eprintln!(" > Keeping add of `{}`", path.display()); + } + FileDiff::Remove { path, .. } => { + if to.join(path).exists() { + final_diffs.push(file_diff.to_owned()); + // eprintln!(" > Keeping remove of `{}`", path.display()); + } 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() + ) + } + final_diffs.push(file_diff.to_owned()); + // eprintln!(" > Keeping rename of `{}` to `{}`", from.display(), to.display()); + } 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() { + 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() + }; + + 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_to(to: &Path) -> Result<()> { + 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(()) +} diff --git a/pkgs/by-name/ro/rollup/src/parse.rs b/pkgs/by-name/ro/rollup/src/parse.rs new file mode 100644 index 00000000..ac30eaa4 --- /dev/null +++ b/pkgs/by-name/ro/rollup/src/parse.rs @@ -0,0 +1,437 @@ +#![allow(clippy::no_effect)] + +use core::fmt; +use std::{ + fs, + path::{Path, PathBuf}, +}; + +use anyhow::{Result, bail}; +use pretty_assertions::assert_eq; + +#[derive(Clone)] +pub(crate) struct Patch { + pub(crate) files: Vec<FileDiff>, + pub(crate) commit_message: String, + pub(crate) date: String, + pub(crate) final_whitespace: &'static str, + pub(crate) generating_version: String, +} + +impl fmt::Display for Patch { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + for file in &self.files { + write!(f, "{}", file)?; + } + writeln!(f, "--{}", self.final_whitespace)?; + writeln!(f, "{}", self.generating_version)?; + + Ok(()) + } +} + +#[derive(Clone)] +pub(crate) enum FileDiff { + Add { + path: PathBuf, + diff: String, + index: String, + new_file_mode: String, + }, + Remove { + path: PathBuf, + diff: String, + index: String, + deleted_mode: String, + }, + Change { + path: PathBuf, + diff: String, + index: String, + }, + RenameNoChange { + from: PathBuf, + to: PathBuf, + }, + RenameWithChange { + from: PathBuf, + to: PathBuf, + similarity: String, + diff: String, + index: String, + }, +} + +impl fmt::Display for FileDiff { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + FileDiff::Add { + path, + diff, + index, + new_file_mode, + } => { + writeln!(f, "diff --git a/{} b/{}", path.display(), path.display())?; + writeln!(f, "new file mode {}", new_file_mode)?; + writeln!(f, "index {}", index)?; + writeln!(f, "--- /dev/null")?; + writeln!(f, "+++ b/{}", path.display())?; + writeln!(f, "{}", diff)?; + } + FileDiff::Remove { + path, + diff, + index, + deleted_mode, + } => { + writeln!(f, "diff --git a/{} b/{}", path.display(), path.display())?; + writeln!(f, "deleted file mode {}", deleted_mode)?; + writeln!(f, "index {}", index)?; + writeln!(f, "--- a/{}", path.display())?; + writeln!(f, "+++ /dev/null")?; + writeln!(f, "{}", diff)?; + } + FileDiff::Change { path, diff, index } => { + writeln!(f, "diff --git a/{} b/{}", path.display(), path.display())?; + writeln!(f, "index {}", index)?; + writeln!(f, "--- a/{}", path.display())?; + writeln!(f, "+++ b/{}", path.display())?; + writeln!(f, "{}", diff)?; + } + FileDiff::RenameNoChange { from, to } => { + writeln!(f, "diff --git a/{} b/{}", from.display(), to.display())?; + writeln!(f, "similarity index 100%")?; + writeln!(f, "rename from {}", from.display())?; + writeln!(f, "rename to {}", to.display())?; + } + FileDiff::RenameWithChange { + from, + to, + similarity, + diff, + index, + } => { + writeln!(f, "diff --git a/{} b/{}", from.display(), to.display())?; + writeln!(f, "similarity index {}", similarity)?; + writeln!(f, "rename from {}", from.display())?; + writeln!(f, "rename to {}", to.display())?; + writeln!(f, "index {}", index)?; + writeln!(f, "--- a/{}", from.display())?; + writeln!(f, "+++ b/{}", to.display())?; + writeln!(f, "{}", diff)?; + } + } + + Ok(()) + } +} + +macro_rules! expect { + (if $gate:ident -> $prefix:literal from $line:expr; assign |$strp_line:ident| $callback:block) => { + expect!(@base continue; if $gate -> $prefix from $line; assign |$strp_line| $callback) + }; + (@no_wrap if $gate:ident -> $prefix:literal from $line:expr; assign |$strp_line:ident| $callback:block) => { + expect!(@base (); if $gate -> $prefix from $line; assign |$strp_line| $callback) + }; + (@base $continue:expr; if $gate:ident -> $prefix:literal from $line:expr; assign |$strp_line:ident| $callback:block) => { + if $gate.is_none() { + let $strp_line: &str = $line + .strip_prefix($prefix) + .expect(concat!( + "Expectected '", + $prefix, + "' prefix, while trying to fill '", + stringify!($gate), + "'" + )) + .trim(); + $gate = Some((|$strp_line: &str| $callback)($strp_line)); + $continue; + } + }; +} + +macro_rules! parser { + ( + $iterator:ident, + $debug:ident, + $($prefix:literal @ $prefix_var:ident => {$($expr:tt)*};)* $(;)? + .. $diff:ident + ) => { + $( + let $prefix_var = { + let next = $iterator.next().unwrap(); + if $debug { + eprintln!(" {}", next); + } + next + }.strip_prefix($prefix).unwrap().to_owned(); + $( + $expr + )*; + )* + let $diff = { + let mut diff = vec![]; + let mut iter = $iterator.clone(); + while let Some(next) = iter.next() { + if next.starts_with("diff --git") { + break; + } + if (next == "-- " || next == "--") + && iter.peek().map(|v| v.starts_with("2")) == Some(true) + { + break; + } + diff.push(next); + } + diff + }; + + for _ in 0..$diff.len() { + let next = $iterator.next().unwrap(); + if $debug { + eprintln!(" {}", next); + } + } + let $diff = $diff.join("\n"); + + }; +} + +pub(crate) fn parse_file(file: &Path, debug: bool) -> Result<Patch> { + if debug { + eprintln!("Started parsing `{}`..", file.display()); + } + let patch_text = fs::read_to_string(file)?; + + let (commit_message, date, mut cut_idx) = { + let mut commit = None; + let mut date = None; + let mut author = None; + let mut final_message = None; + let mut finished_headers = false; + let mut cut_idx = None; + for (idx, line) in patch_text.lines().enumerate() { + expect!(if commit -> "From " from line; assign |strp_line| { + let (id, fake_date) = strp_line.split_once(' ').unwrap(); + assert_eq!(fake_date, "Mon Sep 17 00:00:00 2001"); + id.to_owned() + }); + expect!(if author -> "From: " from line; assign |strp_line| { + strp_line.to_owned() + }); + expect!(if date -> "Date: " from line; assign |strp_line| { + strp_line.to_owned() + }); + expect!(if final_message -> "Subject: " from line; assign |strp_line| { + if strp_line.starts_with('[') { + let chars = strp_line.chars().take_while(|ch| *ch != ']').collect::<String>(); + assert_eq!(&chars[1..=5], "PATCH"); + strp_line[chars.len()+2..].to_owned() + } else { + unreachable!("Email subjects always start with [PATCH x/y]"); + } + }); + if line.starts_with("MIME-Version: ") + || line.starts_with("Content-Type: ") + || line.starts_with("Content-Transfer-Encoding: ") + { + continue; + } + + if line.starts_with(" ") { + // this line is a continuation of the previous header. We just assume + // it's the subject header. + final_message.as_mut().unwrap().push_str(line); + continue; + } + + if line.is_empty() { + // add the newline to end the subject message + final_message.as_mut().unwrap().push('\n'); + // and one newline to mark the end of the header and the start for more text. + final_message.as_mut().unwrap().push('\n'); + finished_headers = true; + continue; + } + + if line == "---" { + cut_idx = Some(idx); + break; + } + + if finished_headers { + // If we finished all the headers, the rest must be commit message stuff. + // One per line. + + final_message.as_mut().unwrap().push_str(line); + final_message.as_mut().unwrap().push('\n'); + } else { + bail!("Failed to recognize commit message header: `{line}`, while parsing patch.") + } + } + + if debug { + eprintln!("Patch data:"); + eprintln!(" Author: {}", author.unwrap()); + eprintln!(" Commit: {}", commit.unwrap()); + eprintln!(" Date: {}", date.as_ref().unwrap()); + for line in final_message.as_ref().unwrap().lines() { + eprintln!(" {}", line); + } + eprintln!("****************************"); + } + + ( + final_message.unwrap().trim().to_owned(), + date.unwrap().trim().to_owned(), + cut_idx.unwrap(), + ) + }; + + let mut patch_files = vec![]; + let mut final_whitespace = None; + let mut generating_version = None; + let mut skipping_frontmatter = true; + let mut iterator = patch_text + .lines() + .skip(cut_idx) + // skip one more to go over the `---` line + .skip(1) + .peekable(); + while let Some(line) = iterator.peek() { + if debug { + eprintln!("{}", line); + } + + if (line.starts_with(" ") || line.is_empty()) && skipping_frontmatter { + iterator.next().unwrap(); + cut_idx += 1; + continue; + } + + if line.starts_with("diff --git") { + if skipping_frontmatter { + skipping_frontmatter = false; + } + iterator.next().unwrap(); + continue; + } + + match line { + line if line.starts_with("new file mode") => { + parser! { + iterator, debug, + "new file mode " @ mode => {let new_file_mode = mode.to_owned()}; + "index " @ index => {let index = index.to_owned()}; + "--- " @ from => {assert_eq!(from, "/dev/null")}; + "+++ b/" @ to => {let path = PathBuf::from(to)}; + ..diff + } + patch_files.push(FileDiff::Add { + path, + diff, + index, + new_file_mode, + }) + } + line if line.starts_with("index") => { + parser! { + iterator, debug, + "index " @ index => {()}; + "--- a/" @ from => {()}; + "+++ b/" @ to => {assert_eq!(from, to); let path = PathBuf::from(to)}; + ..diff + } + patch_files.push(FileDiff::Change { path, diff, index }) + } + line if line.starts_with("similarity index 100%") => { + parser! { + iterator, debug, + "similarity index 100%" @ none => {assert!(none.is_empty())}; + "rename from " @ from => {let from = PathBuf::from(from)}; + "rename to " @ to => {let to = PathBuf::from(to)}; + .. no_diff + } + assert!(no_diff.is_empty()); + + patch_files.push(FileDiff::RenameNoChange { from, to }) + } + line if line.starts_with("similarity index ") => { + parser! { + iterator, debug, + "similarity index " @ similarity => {()}; + "rename from " @ from_1 => {let from = PathBuf::from(&from_1)}; + "rename to " @ to_1 => {let to = PathBuf::from(&to_1)}; + "index " @ index => {()}; + "--- a/" @ from_2 => {assert_eq!(from_1, from_2)}; + "+++ b/" @ to_2 => {assert_eq!(to_1, to_2)}; + .. diff + } + + patch_files.push(FileDiff::RenameWithChange { + from, + to, + similarity, + diff, + index, + }) + } + line if line.starts_with("deleted") => { + parser! { + iterator, debug, + "deleted file mode " @ deleted_mode => {()}; + "index " @ index => {()}; + "--- a/" @ from => {let path = PathBuf::from(from)}; + "+++ /dev/null" @ rest => {assert!(rest.is_empty())}; + ..diff + } + patch_files.push(FileDiff::Remove { + path, + diff, + index, + deleted_mode, + }) + } + line if *line == "--" || *line == "-- " => { + final_whitespace = Some( match *line { + "-- " => " ", + "--" => "", + _ => unreachable!(), + }); + iterator.next().unwrap(); + generating_version = Some(iterator.next().unwrap().to_owned()); + assert_eq!(iterator.next(), Some("")); + assert_eq!(iterator.next(), None); + break; + } + _ => { + unreachable!("All possible line starts should be covered, but: `{line}` is not.") + } + } + } + + let patch = Patch { + files: patch_files, + commit_message, + date, + final_whitespace: final_whitespace.unwrap(), + generating_version: generating_version.unwrap(), + }; + + let patch_text = patch_text + .lines() + .skip(cut_idx) + // skip one more to go over the `---` line + .skip(1) + .collect::<Vec<_>>() + .join("\n"); + let check_content = patch.to_string(); + + assert_eq!( + check_content, patch_text, + "Expected parsed patchtext to be the same as our the original patch" + ); + + Ok(patch) +} |
