From c33395087da4a3fb5364c58761fa94d8ee15ca23 Mon Sep 17 00:00:00 2001 From: Ben Moss Date: Tue, 22 Oct 2024 20:06:45 -0400 Subject: [PATCH 1/2] POC: pixi diff Example: ``` pixi diff --old-lockfile <(git show HEAD~100:pixi.lock) ``` --- src/cli/diff.rs | 47 +++++++++++++++++++++++++++++++++++++++++++++++ src/cli/mod.rs | 3 +++ src/cli/update.rs | 2 +- 3 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 src/cli/diff.rs diff --git a/src/cli/diff.rs b/src/cli/diff.rs new file mode 100644 index 000000000..32bce338e --- /dev/null +++ b/src/cli/diff.rs @@ -0,0 +1,47 @@ +use std::path::{Path, PathBuf}; + +use clap::Parser; +use miette::IntoDiagnostic; +use pixi_config::{self, ConfigCli}; +use rattler_lock::LockFile; + +use crate::{ + cli::update::{LockFileDiff, LockFileJsonDiff}, + Project, +}; + +use super::cli_config::ProjectConfig; + +#[derive(Parser, Debug, Default)] +pub struct Args { + #[clap(flatten)] + pub config: ConfigCli, + + #[clap(flatten)] + pub project_config: ProjectConfig, + + #[arg(long)] + pub old_lockfile: PathBuf, +} + +pub async fn execute(args: Args) -> miette::Result<()> { + let lock = LockFile::from_path(Path::new("pixi.lock")).into_diagnostic()?; + + let input: Box = if args.old_lockfile.as_os_str() == "-" { + Box::new(std::io::stdin()) + } else { + Box::new(std::fs::File::open(&args.old_lockfile).into_diagnostic()?) + }; + + let old = LockFile::from_reader(input).into_diagnostic()?; + + let diff = LockFileDiff::from_lock_files(&old, &lock); + let config = args.config; + let project = Project::load_or_else_discover(args.project_config.manifest_path.as_deref())? + .with_cli_config(config); + let json_diff = LockFileJsonDiff::new(&project, diff); + let json = serde_json::to_string_pretty(&json_diff).expect("failed to convert to json"); + println!("{}", json); + + Ok(()) +} diff --git a/src/cli/mod.rs b/src/cli/mod.rs index 5e6764591..df1474094 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -18,6 +18,7 @@ pub mod clean; pub mod cli_config; pub mod completion; pub mod config; +pub mod diff; mod exec; pub mod global; pub mod has_specs; @@ -104,6 +105,7 @@ pub enum Command { #[clap(visible_alias = "s")] Shell(shell::Args), ShellHook(shell_hook::Args), + Diff(diff::Args), // Project modification commands Project(project::Args), @@ -281,6 +283,7 @@ pub async fn execute_command(command: Command) -> miette::Result<()> { Command::Tree(cmd) => tree::execute(cmd).await, Command::Update(cmd) => update::execute(cmd).await, Command::Exec(args) => exec::execute(args).await, + Command::Diff(args) => diff::execute(args).await, } } diff --git a/src/cli/update.rs b/src/cli/update.rs index 88636fa8e..76484dc73 100644 --- a/src/cli/update.rs +++ b/src/cli/update.rs @@ -638,7 +638,7 @@ pub struct LockFileJsonDiff { } impl LockFileJsonDiff { - fn new(project: &Project, value: LockFileDiff) -> Self { + pub fn new(project: &Project, value: LockFileDiff) -> Self { let mut environment = IndexMap::new(); for (environment_name, environment_diff) in value.environment { From 9c52d8763949bc2453a9860f7ef00bd14acbcd11 Mon Sep 17 00:00:00 2001 From: Ben Moss Date: Tue, 22 Oct 2024 20:14:16 -0400 Subject: [PATCH 2/2] Clean up lockfile loading --- src/cli/diff.rs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/cli/diff.rs b/src/cli/diff.rs index 32bce338e..46b73639a 100644 --- a/src/cli/diff.rs +++ b/src/cli/diff.rs @@ -1,4 +1,4 @@ -use std::path::{Path, PathBuf}; +use std::path::PathBuf; use clap::Parser; use miette::IntoDiagnostic; @@ -25,7 +25,9 @@ pub struct Args { } pub async fn execute(args: Args) -> miette::Result<()> { - let lock = LockFile::from_path(Path::new("pixi.lock")).into_diagnostic()?; + let project = Project::load_or_else_discover(args.project_config.manifest_path.as_deref())? + .with_cli_config(args.config); + let current_lockfile = LockFile::from_path(&project.lock_file_path()).into_diagnostic()?; let input: Box = if args.old_lockfile.as_os_str() == "-" { Box::new(std::io::stdin()) @@ -33,12 +35,9 @@ pub async fn execute(args: Args) -> miette::Result<()> { Box::new(std::fs::File::open(&args.old_lockfile).into_diagnostic()?) }; - let old = LockFile::from_reader(input).into_diagnostic()?; + let prior_lockfile = LockFile::from_reader(input).into_diagnostic()?; - let diff = LockFileDiff::from_lock_files(&old, &lock); - let config = args.config; - let project = Project::load_or_else_discover(args.project_config.manifest_path.as_deref())? - .with_cli_config(config); + let diff = LockFileDiff::from_lock_files(&prior_lockfile, ¤t_lockfile); let json_diff = LockFileJsonDiff::new(&project, diff); let json = serde_json::to_string_pretty(&json_diff).expect("failed to convert to json"); println!("{}", json);