From ac66d7fc79dbc44dcd5be0fbc7c3257cd4a7671b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20S=C3=A1enz?= Date: Tue, 7 Apr 2026 20:29:17 +0200 Subject: [PATCH] Skip apply phase when no differences detected (#41) Closes #41 --- CHANGELOG.md | 9 +++++ Gemfile.lock | 2 +- lib/dotsync/actions/pull_action.rb | 11 ++++-- lib/dotsync/actions/push_action.rb | 9 ++++- lib/dotsync/version.rb | 2 +- spec/dotsync/actions/pull_action_spec.rb | 44 ++++++++++++++++++++++ spec/dotsync/actions/push_action_spec.rb | 47 ++++++++++++++++++++++++ 7 files changed, 117 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a09ca4e..abdeba8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,12 @@ +## [0.4.5] - 2026-04-07 + +### Fixed + +- Skip transfer, orphan cleanup, and hooks when no differences detected (#42) + - `pull` and `push` actions now early-exit the apply phase when no differences exist + - Eliminates expensive `Find.find` directory scans from `cleanup_orphans` on every run + - `force_hooks` option still executes hooks even without differences + ## [0.4.4] - 2026-04-07 ### Fixed diff --git a/Gemfile.lock b/Gemfile.lock index cc08878..9c1d221 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - dotsync (0.4.4) + dotsync (0.4.5) fileutils (~> 1.7.3) find (~> 0.2.0) listen (~> 3.9.0) diff --git a/lib/dotsync/actions/pull_action.rb b/lib/dotsync/actions/pull_action.rb index 6566c21..5e378c0 100644 --- a/lib/dotsync/actions/pull_action.rb +++ b/lib/dotsync/actions/pull_action.rb @@ -33,9 +33,14 @@ def execute(options = {}) end end - transfer_mappings - cleanup_orphans - execute_hooks(force: options[:force_hooks]) + if has_differences? + transfer_mappings + cleanup_orphans + execute_hooks(force: options[:force_hooks]) + elsif options[:force_hooks] + execute_hooks(force: true) + end + action("Mappings pulled", icon: :done) end diff --git a/lib/dotsync/actions/push_action.rb b/lib/dotsync/actions/push_action.rb index 8612ad0..2815db5 100644 --- a/lib/dotsync/actions/push_action.rb +++ b/lib/dotsync/actions/push_action.rb @@ -23,8 +23,13 @@ def execute(options = {}) return unless confirm_action end - transfer_mappings - execute_hooks(force: options[:force_hooks]) + if has_differences? + transfer_mappings + execute_hooks(force: options[:force_hooks]) + elsif options[:force_hooks] + execute_hooks(force: true) + end + action("Mappings pushed", icon: :done) end end diff --git a/lib/dotsync/version.rb b/lib/dotsync/version.rb index 214ef1c..334d0d2 100644 --- a/lib/dotsync/version.rb +++ b/lib/dotsync/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module Dotsync - VERSION = "0.4.4" + VERSION = "0.4.5" end diff --git a/spec/dotsync/actions/pull_action_spec.rb b/spec/dotsync/actions/pull_action_spec.rb index 2ba8252..3a9425a 100644 --- a/spec/dotsync/actions/pull_action_spec.rb +++ b/spec/dotsync/actions/pull_action_spec.rb @@ -116,6 +116,26 @@ expect(file_transfer2).to have_received(:transfer) end + context "without differences" do + before do + File.write(File.join(mapping1.dest, "file1"), "src content") + File.write(mapping2.dest, "#{mapping2.src} content") + end + + it "skips transfer, orphan cleanup, and hooks" do + subject + + expect(file_transfer1).not_to have_received(:transfer) + expect(file_transfer2).not_to have_received(:transfer) + end + + it "still shows mappings pulled message" do + expect(logger).to receive(:action).with("Mappings pulled", icon: :done) + + subject + end + end + context "with user confirmation" do context "when user accepts" do before do @@ -244,6 +264,30 @@ end end + context "with hooks and no differences" do + let(:mapping_with_hooks) do + Dotsync::Mapping.new( + "src" => file2_src, + "dest" => file2_dest, + "hooks" => ["echo hook_ran"] + ) + end + let(:mappings) { [mapping1, mapping_with_hooks] } + + before do + File.write(File.join(mapping1.dest, "file1"), "src content") + File.write(mapping_with_hooks.dest, "#{mapping_with_hooks.src} content") + end + + context "with force_hooks option" do + it "executes hooks even without differences" do + expect_any_instance_of(Dotsync::HookRunner).to receive(:execute).and_return([]) + + action.execute(apply: true, yes: true, force_hooks: true) + end + end + end + context "hooks in dry-run mode" do let(:mapping_with_hooks) do Dotsync::Mapping.new( diff --git a/spec/dotsync/actions/push_action_spec.rb b/spec/dotsync/actions/push_action_spec.rb index b6bdf85..474655d 100644 --- a/spec/dotsync/actions/push_action_spec.rb +++ b/spec/dotsync/actions/push_action_spec.rb @@ -114,6 +114,26 @@ expect(file_transfer2).to have_received(:transfer) end + context "without differences" do + before do + File.write(mapping1.dest, "src1 content") + File.write(mapping2.dest, "#{mapping2.src} content") + end + + it "skips transfer and hooks" do + subject + + expect(file_transfer1).not_to have_received(:transfer) + expect(file_transfer2).not_to have_received(:transfer) + end + + it "still shows mappings pushed message" do + expect(logger).to receive(:action).with("Mappings pushed", icon: :done) + + subject + end + end + context "with user confirmation" do context "when user accepts" do before do @@ -246,6 +266,33 @@ end end + context "with hooks and no differences" do + let(:mapping_with_hooks) do + Dotsync::Mapping.new( + "src" => File.join(root, "src2"), + "dest" => File.join(root, "dest2"), + "force" => false, + "ignore" => [], + "hooks" => ["echo hook_ran"] + ) + end + let(:mappings) { [mapping1, mapping_with_hooks] } + + before do + File.write(mapping1.dest, "src1 content") + File.write(mapping_with_hooks.src, "#{mapping_with_hooks.src} content") + File.write(mapping_with_hooks.dest, "#{mapping_with_hooks.src} content") + end + + context "with force_hooks option" do + it "executes hooks even without differences" do + expect_any_instance_of(Dotsync::HookRunner).to receive(:execute).and_return([]) + + action.execute(apply: true, yes: true, force_hooks: true) + end + end + end + context "hooks in dry-run mode" do let(:mapping_with_hooks) do Dotsync::Mapping.new(