Skip to content

Commit

Permalink
POC: pixi diff
Browse files Browse the repository at this point in the history
Example:

```
pixi diff --old-lockfile <(git show HEAD~100:pixi.lock)
```
  • Loading branch information
benmoss committed Oct 23, 2024
1 parent bae4984 commit c333950
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
47 changes: 47 additions & 0 deletions src/cli/diff.rs
Original file line number Diff line number Diff line change
@@ -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<dyn std::io::Read + 'static> = 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(())
}
3 changes: 3 additions & 0 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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,
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/cli/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit c333950

Please sign in to comment.