Skip to content

Commit

Permalink
chore: diff template -> source
Browse files Browse the repository at this point in the history
  • Loading branch information
ditsuke committed Mar 21, 2023
1 parent 856bfe2 commit bd57602
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
21 changes: 10 additions & 11 deletions src/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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)
}
}
Expand Down
11 changes: 8 additions & 3 deletions src/difference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand All @@ -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<Diff> {
let file_contents = fs::read_to_string(source).context("read template source file")?;
let file_contents = target.apply_actions(file_contents);
Expand All @@ -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())
}
Expand Down

0 comments on commit bd57602

Please sign in to comment.