From 47d03e398b0a87be31d78256986e675bcfb0dfd1 Mon Sep 17 00:00:00 2001 From: ditsuke Date: Sun, 19 Mar 2023 20:01:55 +0530 Subject: [PATCH 1/4] feat: diff source with destination on conflict Also silences warnings when source and destination don't really conflict. --- src/actions.rs | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/src/actions.rs b/src/actions.rs index a61e01b..e62e659 100644 --- a/src/actions.rs +++ b/src/actions.rs @@ -1,11 +1,12 @@ use std::path::Path; use anyhow::{Context, Result}; + use crossterm::style::Stylize; use handlebars::Handlebars; use crate::config::{SymbolicTarget, TemplateTarget, Variables}; -use crate::difference; +use crate::difference::{self, diff_nonempty, generate_diff, print_diff}; use crate::filesystem::{Filesystem, SymlinkComparison, TemplateComparison}; #[cfg_attr(test, mockall::automock)] @@ -533,7 +534,30 @@ pub fn update_template( .context("perform template cache")?; Ok(true) } - TemplateComparison::Changed | TemplateComparison::TargetNotRegularFile => { + TemplateComparison::Changed => { + // At this point, we're not sure if there's a difference between the rendered source + // and target, only that the target has been modified in some way. + let diff = generate_diff(source, target, handlebars, variables) + .context("diff source with target")?; + if diff_nonempty(&diff) { + error!( + "Updating template {:?} -> {:?} but {}. Skipping.", + source, target.target, comparison + ); + info!( + "{} template {:?} -> {:?}", + "[~].yellow()", source, target.target, + ); + print_diff(diff, diff_context_lines); + Ok(false) + } else { + perform_template_deploy(source, cache, target, fs, handlebars, variables) + .context("perform template acche")?; + Ok(true) + } + } + + TemplateComparison::TargetNotRegularFile => { error!( "Updating template {:?} -> {:?} but {}. Skipping.", source, target.target, comparison From 856bfe2871f4e570dd13c7da0a1dfdd70c2b074c Mon Sep 17 00:00:00 2001 From: ditsuke Date: Tue, 21 Mar 2023 08:16:06 +0530 Subject: [PATCH 2/4] chore: make difference::print_diff public Because I can't stage :) --- src/actions.rs | 8 ++++---- src/difference.rs | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/actions.rs b/src/actions.rs index e62e659..ea5f6f7 100644 --- a/src/actions.rs +++ b/src/actions.rs @@ -6,7 +6,7 @@ use crossterm::style::Stylize; use handlebars::Handlebars; use crate::config::{SymbolicTarget, TemplateTarget, Variables}; -use crate::difference::{self, diff_nonempty, generate_diff, print_diff}; +use crate::difference; use crate::filesystem::{Filesystem, SymlinkComparison, TemplateComparison}; #[cfg_attr(test, mockall::automock)] @@ -537,9 +537,9 @@ pub fn update_template( TemplateComparison::Changed => { // At this point, we're not sure if there's a difference between the rendered source // and target, only that the target has been modified in some way. - let diff = generate_diff(source, target, handlebars, variables) + let diff = difference::generate_diff(source, target, handlebars, variables) .context("diff source with target")?; - if diff_nonempty(&diff) { + if difference::diff_nonempty(&diff) { error!( "Updating template {:?} -> {:?} but {}. Skipping.", source, target.target, comparison @@ -548,7 +548,7 @@ pub fn update_template( "{} template {:?} -> {:?}", "[~].yellow()", source, target.target, ); - print_diff(diff, diff_context_lines); + difference::print_diff(diff, diff_context_lines); Ok(false) } else { perform_template_deploy(source, cache, target, fs, handlebars, variables) diff --git a/src/difference.rs b/src/difference.rs index 9cd9256..833010d 100644 --- a/src/difference.rs +++ b/src/difference.rs @@ -186,7 +186,7 @@ fn print_hunk(mut left_line: usize, mut right_line: usize, hunk: Diff, max_digit } } -fn print_diff(diff: Diff, extra_lines: usize) { +pub fn print_diff(diff: Diff, extra_lines: usize) { let mut diff = hunkify_diff(diff, extra_lines); let last_hunk = diff.pop().expect("at least one hunk"); From bd57602ffd5fc642180e2fa421e79916d9c47004 Mon Sep 17 00:00:00 2001 From: ditsuke Date: Tue, 21 Mar 2023 09:34:17 +0530 Subject: [PATCH 3/4] chore: diff template -> source --- src/actions.rs | 21 ++++++++++----------- src/difference.rs | 11 ++++++++--- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/actions.rs b/src/actions.rs index ea5f6f7..db17578 100644 --- a/src/actions.rs +++ b/src/actions.rs @@ -6,7 +6,7 @@ use crossterm::style::Stylize; use handlebars::Handlebars; use crate::config::{SymbolicTarget, TemplateTarget, Variables}; -use crate::difference; +use crate::difference::{self, diff_nonempty, generate_template_diff, print_diff}; use crate::filesystem::{Filesystem, SymlinkComparison, TemplateComparison}; #[cfg_attr(test, mockall::automock)] @@ -537,22 +537,21 @@ pub fn update_template( TemplateComparison::Changed => { // At this point, we're not sure if there's a difference between the rendered source // and target, only that the target has been modified in some way. - let diff = difference::generate_diff(source, target, handlebars, variables) - .context("diff source with target")?; - if difference::diff_nonempty(&diff) { + let diff = generate_template_diff(source, target, handlebars, variables, false) + .context("diff source and target")?; + if diff_nonempty(&diff) { error!( - "Updating template {:?} -> {:?} but {}. Skipping.", + "Updating template {:?} -> {:?} but {}. Skipping", source, target.target, comparison ); - info!( - "{} template {:?} -> {:?}", - "[~].yellow()", source, target.target, - ); - difference::print_diff(diff, diff_context_lines); + if log_enabled!(log::Level::Info) { + info!("Refusing because of the following changes in source: "); + print_diff(diff, diff_context_lines); + } Ok(false) } else { perform_template_deploy(source, cache, target, fs, handlebars, variables) - .context("perform template acche")?; + .context("perform template cache")?; Ok(true) } } diff --git a/src/difference.rs b/src/difference.rs index 833010d..3d217f8 100644 --- a/src/difference.rs +++ b/src/difference.rs @@ -19,7 +19,7 @@ pub fn print_template_diff( diff_context_lines: usize, ) { if log_enabled!(log::Level::Info) { - match generate_diff(source, target, handlebars, variables) { + match generate_template_diff(source, target, handlebars, variables, true) { Ok(diff) => { if diff_nonempty(&diff) { info!( @@ -41,11 +41,12 @@ pub fn print_template_diff( } } -pub fn generate_diff( +pub fn generate_template_diff( source: &Path, target: &TemplateTarget, handlebars: &Handlebars<'_>, variables: &Variables, + source_to_target: bool, ) -> Result { let file_contents = fs::read_to_string(source).context("read template source file")?; let file_contents = target.apply_actions(file_contents); @@ -56,7 +57,11 @@ pub fn generate_diff( let target_contents = fs::read_to_string(&target.target).context("read template target file")?; - let diff_result = diff::lines(&target_contents, &rendered); + let diff_result = if source_to_target { + diff::lines(&target_contents, &rendered) + } else { + diff::lines(&rendered, &target_contents) + }; Ok(diff_result.into_iter().map(to_owned_diff_result).collect()) } From 2bd6d4b9f56c85cd7d2aee2ec8d50be7ec0111e1 Mon Sep 17 00:00:00 2001 From: ditsuke Date: Wed, 22 Mar 2023 09:22:29 +0530 Subject: [PATCH 4/4] chore: s/source/target location Whoops --- src/actions.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/actions.rs b/src/actions.rs index db17578..c8e7f6b 100644 --- a/src/actions.rs +++ b/src/actions.rs @@ -545,7 +545,7 @@ pub fn update_template( source, target.target, comparison ); if log_enabled!(log::Level::Info) { - info!("Refusing because of the following changes in source: "); + info!("Refusing because of the following changes in target location: "); print_diff(diff, diff_context_lines); } Ok(false)