summaryrefslogtreecommitdiffstats
path: root/pkgs/by-name/ro/rollup/src
diff options
context:
space:
mode:
authorBenedikt Peetz <benedikt.peetz@b-peetz.de>2026-11-04 20:29:45 +0100
committerBenedikt Peetz <benedikt.peetz@b-peetz.de>2026-11-05 22:47:43 +0100
commit26b46c743994788550ca90b916c6359c855bbba7 (patch)
tree14ccdd3e9637ee82415268413da6923b7437070b /pkgs/by-name/ro/rollup/src
parentpkgs/rollup: Finish first mvp (diff)
downloadnixos-config-26b46c743994788550ca90b916c6359c855bbba7.zip
pkgs/rollup: Actually also parse renames in patches
Diffstat (limited to '')
-rw-r--r--pkgs/by-name/ro/rollup/src/main.rs23
-rw-r--r--pkgs/by-name/ro/rollup/src/parse.rs53
2 files changed, 61 insertions, 15 deletions
diff --git a/pkgs/by-name/ro/rollup/src/main.rs b/pkgs/by-name/ro/rollup/src/main.rs
index 48b395a5..f49eb81e 100644
--- a/pkgs/by-name/ro/rollup/src/main.rs
+++ b/pkgs/by-name/ro/rollup/src/main.rs
@@ -7,7 +7,7 @@ use std::{
use anyhow::{Context, Result, bail};
use clap::Parser;
-use crate::parse::Patch;
+use crate::parse::{FileDiff, Patch};
mod parse;
@@ -64,13 +64,13 @@ fn main() -> Result<()> {
let mut final_diffs = vec![];
for file_diff in &patch.files {
match file_diff {
- parse::FileDiff::Add { path, .. } => {
+ FileDiff::Add { path, .. } => {
if args.to.join(path).exists() {
bail!("A file was added, that already existed. This should be impossible.")
}
final_diffs.push(file_diff.to_owned());
}
- parse::FileDiff::Remove { path, .. } => {
+ FileDiff::Remove { path, .. } => {
if args.to.join(path).exists() {
final_diffs.push(file_diff.to_owned());
} else {
@@ -80,9 +80,9 @@ fn main() -> Result<()> {
);
}
}
- parse::FileDiff::Change { from, to, .. } => {
+ FileDiff::Rename { from, to, .. } => {
if args.to.join(from).exists() {
- if from != to && args.to.join(to).exists() {
+ if args.to.join(to).exists() {
bail!(
"A rename (`{}` -> `{}`) tried to rename a path to an already existing path. This should be impossible.",
from.display(),
@@ -92,8 +92,19 @@ fn main() -> Result<()> {
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 args.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.",
- from.display()
+ path.display()
);
}
}
diff --git a/pkgs/by-name/ro/rollup/src/parse.rs b/pkgs/by-name/ro/rollup/src/parse.rs
index ceb9d44f..fb5cd05e 100644
--- a/pkgs/by-name/ro/rollup/src/parse.rs
+++ b/pkgs/by-name/ro/rollup/src/parse.rs
@@ -36,10 +36,15 @@ pub(crate) enum FileDiff {
deleted: String,
},
Change {
+ path: PathBuf,
+ diff: String,
+ index: String,
+ },
+ Rename {
from: PathBuf,
to: PathBuf,
diff: String,
- index: String,
+ similarity: String,
},
}
impl FileDiff {
@@ -49,8 +54,9 @@ impl FileDiff {
diff: String,
index: Option<String>,
deleted: Option<String>,
+ similarity: Option<String>,
) -> Self {
- let diff = diff.trim().to_owned();
+ let diff = diff[..diff.len() - 1].to_owned();
if from.display().to_string() == "/dev/null" {
Self::Add { path: to, diff }
@@ -61,12 +67,18 @@ impl FileDiff {
index: index.unwrap(),
deleted: deleted.unwrap(),
}
- } else {
+ } else if from == to {
Self::Change {
+ path: from,
+ diff,
+ index: index.unwrap(),
+ }
+ } else {
+ Self::Rename {
from,
to,
diff,
- index: index.unwrap(),
+ similarity: similarity.unwrap(),
}
}
}
@@ -93,15 +105,22 @@ impl fmt::Display for FileDiff {
writeln!(f, "+++ /dev/null")?;
writeln!(f, "{}", diff)?;
}
- FileDiff::Change {
+ 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::Rename {
from,
to,
diff,
- index,
+ similarity,
} => {
writeln!(f, "diff --git a/{} b/{}", from.display(), to.display())?;
- writeln!(f, "index {}", index)?;
- writeln!(f, "--- a/{}", from.display())?;
+ writeln!(f, "similarity index {}", similarity)?;
+ writeln!(f, "rename from {}", from.display())?;
writeln!(f, "+++ b/{}", to.display())?;
writeln!(f, "{}", diff)?;
}
@@ -138,10 +157,12 @@ macro_rules! expect {
pub(crate) fn parse_file(file: &Path, debug: bool) -> Result<Patch> {
let data = Command::new("filterdiff")
- .arg(&file)
+ .arg(file)
.output()
.context("Failed to run `filterdiff` for patch analysis")?;
+ eprintln!("Started parsing `{}`..", file.display());
+
let filterdiff_output = String::from_utf8(data.stdout).unwrap();
if debug {
eprintln!("{}", filterdiff_output);
@@ -206,6 +227,7 @@ pub(crate) fn parse_file(file: &Path, debug: bool) -> Result<Patch> {
let mut fd_diff: Option<String> = None;
let mut fd_index: Option<String> = None;
let mut fd_deleted: Option<String> = None;
+ let mut fd_similarity: Option<String> = None;
let mut git_diff = None;
for (idx, line) in filterdiff_output.lines().enumerate() {
if debug {
@@ -220,11 +242,23 @@ pub(crate) fn parse_file(file: &Path, debug: bool) -> Result<Patch> {
fd_diff.take().unwrap(),
fd_index.take(),
fd_deleted.take(),
+ fd_similarity.take(),
));
}
git_diff = Some(line.to_owned());
continue;
}
+ if line.starts_with("similarity index") {
+ expect!(if fd_similarity -> "similarity index" from line; assign |strp_line| {
+ strp_line.to_owned()
+ });
+ }
+ if line.starts_with("rename from") {
+ expect!(if fd_from -> "rename from" from line; assign |strp_line| {
+ assert_ne!(strp_line, "/dev/null");
+ PathBuf::from(strp_line)
+ });
+ }
if line.starts_with("deleted") {
expect!(if fd_deleted -> "deleted" from line; assign |strp_line| {
@@ -279,6 +313,7 @@ pub(crate) fn parse_file(file: &Path, debug: bool) -> Result<Patch> {
fd_diff.take().unwrap(),
fd_index.take(),
fd_deleted.take(),
+ fd_similarity.take(),
));
let check_content = diff.to_string();