diff options
Diffstat (limited to '')
| -rw-r--r-- | pkgs/by-name/ro/rollup/src/config.rs | 107 | ||||
| -rw-r--r-- | pkgs/by-name/ro/rollup/src/main.rs | 459 | ||||
| -rw-r--r-- | pkgs/by-name/ro/rollup/src/parse.rs | 437 |
3 files changed, 1003 insertions, 0 deletions
diff --git a/pkgs/by-name/ro/rollup/src/config.rs b/pkgs/by-name/ro/rollup/src/config.rs new file mode 100644 index 00000000..66bd2366 --- /dev/null +++ b/pkgs/by-name/ro/rollup/src/config.rs @@ -0,0 +1,107 @@ +use std::{ + collections::HashMap, + ffi::OsStr, + fs::{self}, + path::PathBuf, +}; + +use anyhow::{Result, bail}; +use serde::{Deserialize, Serialize}; + +pub(crate) const MAIN_CONFIG_NAME: &str = "rollup.toml"; +pub(crate) const DROP_IN_NAME: &str = ".rollup.owner"; + +#[derive(Serialize, Deserialize, Debug)] +pub(crate) struct MainConfig { + pub(crate) owners: Vec<Owner>, + pub(crate) drop_ins: HashMap<PathBuf, DropInConfig>, +} + +impl MainConfig { + pub(crate) fn get_owner(&self, owner: &str) -> Result<&Owner> { + if let Some(own) = self.owners.iter().find(|o| o.name == owner) { + Ok(own) + } else { + bail!("Failed to find owner with name: `{}`", owner) + } + } +} + +#[derive(Serialize, Deserialize, Debug)] +pub(crate) struct Owner { + pub(crate) name: String, + pub(crate) applies_to: PathBuf, + pub(crate) last_applied_tag: String, +} + +#[derive(Serialize, Deserialize, Debug)] +pub(crate) struct DropInConfig { + pub(crate) owners: Vec<String>, +} + +pub(crate) fn load(root: &PathBuf) -> Result<MainConfig> { + let mut main: MainConfig = { + let path = root.join(MAIN_CONFIG_NAME); + let content = fs::read_to_string(path)?; + toml::from_str(&content)? + }; + + let drop_ins = collect_drop_ins(root)?; + + for (path, config) in drop_ins { + assert!(main.drop_ins.insert(path, config).is_none()); + } + + Ok(main) +} + +fn collect_drop_ins(path: &PathBuf) -> Result<Vec<(PathBuf, DropInConfig)>> { + let mut output = vec![]; + for entry in fs::read_dir(path)? { + let entry = entry?; + let ft = entry.file_type()?; + + if ft.is_dir() { + output.extend(collect_drop_ins(&entry.path())?); + } else if ft.is_file() && entry.file_name() == OsStr::new(DROP_IN_NAME) { + let drop_in: toml::Value = { + let content = fs::read_to_string(entry.path())?; + toml::from_str(&content)? + }; + + let drop_in_config = DropInConfig { + owners: { + let Some(owner) = drop_in.get("owner") else { + bail!("Drop in config without `owner` field") + }; + if owner.is_str() { + vec![owner.as_str().expect("We checked").to_owned()] + } else if owner.is_array() { + let mut output = vec![]; + for v in owner.as_array().expect("We checked") { + if let Some(v) = v.as_str() { + output.push(v.to_owned()) + } else { + bail!("Owner value in arry not string: {}", v); + } + } + output + } else { + bail!("Owner value in drop in config not string or array") + } + }, + }; + + output.push(( + entry + .path() + .parent() + .expect("All drop-ins have a parent") + .to_owned(), + drop_in_config, + )); + } + } + + Ok(output) +} 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..28ef0fea --- /dev/null +++ b/pkgs/by-name/ro/rollup/src/main.rs @@ -0,0 +1,459 @@ +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) + }}; +} + +macro_rules! drop_msg { + ($fmt:literal, $($args:expr),*) => { + eprintln!(" > {}", format!($fmt, $($args),*)); + }; +} + +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 applies_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 applies_to; "status", "--porcelain"); + if !stdout.is_empty() { + bail!( + "`--to` (`{}`) workspace is dirty, refusing to continue.", + applies_to.display() + ); + } + } + + for (idx, patch) in patches.iter().enumerate() { + eprintln!( + "Applying {}. patch: `{}`... ", + idx + 1, + patch_files[idx] + .file_name() + .expect("Has a file name") + .display() + ); + + let mut final_diffs = vec![]; + for file_diff in &patch.files { + match file_diff { + FileDiff::Add { path, .. } => { + if applies_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 applies_to.join(path).exists() { + if drop_in_applies(&config.drop_ins, owner, &root, path)? { + final_diffs.push(file_diff.to_owned()); + } + } else { + drop_msg!( + "Dropping remove of `{}`, as the file does not exist in the rollup target.", + path.display() + ); + } + } + FileDiff::RenameNoChange { from, to, .. } + | FileDiff::RenameWithChange { from, to, .. } => { + if applies_to.join(from).exists() { + if applies_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, to)? { + final_diffs.push(file_diff.to_owned()); + } + } else { + drop_msg!( + "Dropping rename from `{}` to `{}`, as the from file does not exist in the rollup target.", + from.display(), + to.display() + ); + } + } + FileDiff::Change { path, .. } => { + if applies_to.join(path).exists() { + if drop_in_applies(&config.drop_ins, owner, &root, path)? { + final_diffs.push(file_diff.to_owned()); + } + } else { + drop_msg!( + "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 applying.)"); + } else { + git!(@set_stdin_to filtered_patch; in applies_to; "apply", "-", "--whitespace=nowarn"); + git!(in applies_to; "add", "."); + git!(@set_stdin_to filtered_patch.commit_message; in applies_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, + original_path: &Path, +) -> Result<bool> { + let path = &original_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) { + drop_msg!( + "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.owners.contains(&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.owners.contains(&owner.name) { + // eprintln!(", but it's for `{}`, so ignoring path.", conf.owner); + drop_msg!( + "Dropping `{}` as `{}` says this dir is for `{}` (but we look for `{}`).", + original_path.display(), + next.join(DROP_IN_NAME).display(), + conf.owners.join(", "), + owner.name + ); + 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() + ); +} 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) +} |
