Skip to content

Commit

Permalink
added module update diff display
Browse files Browse the repository at this point in the history
  • Loading branch information
PKVadde committed Sep 11, 2024
1 parent 4e4b030 commit f013c87
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ indicatif = "0.17.8"
which = "6.0.3"
regex = "1.10.6"
toml_edit = "0.22.20"
imara-diff = "0.1.7"

[build-dependencies]
cc="*"
17 changes: 17 additions & 0 deletions src/cmd/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use anyhow::Result;
use crate::cmd::{Execute, Update};
use crate::cmd::include::get_head_commit_hash;
use crate::toml::{get_repo_links, add_top_module, remove_top_module};
use imara_diff::intern::InternedInput;
use imara_diff::{diff, Algorithm, UnifiedDiffBuilder};

impl Execute for Update {
async fn execute(&self) -> Result<()> {
Expand Down Expand Up @@ -36,9 +38,24 @@ fn update_module(module_path: &str, commit: Option<&str>) -> Result<()> {
let commit_hash = commit.unwrap_or(&head_commit_hash);

println!("Updating module '{}' to commit '{}'", module_path, commit_hash);
let old_contents = std::fs::read_to_string(module_path)?;
remove_top_module(&chosen_repo, module_path)?;
add_top_module(&chosen_repo, module_path, commit_hash)?;
let new_contents = std::fs::read_to_string(module_path)?;
println!("Module '{}' updated to commit '{}'", module_path, commit_hash);

display_diff(&old_contents, &new_contents);

Ok(())
}

fn display_diff(old_contents: &str, new_contents: &str) {
let input = InternedInput::new(old_contents, new_contents);
let diff_output = diff(
Algorithm::Histogram,
&input,
UnifiedDiffBuilder::new(&input)
);

println!("Diff:\n{}", diff_output);
}

0 comments on commit f013c87

Please sign in to comment.