From 474d69da0b4bbe07389e5a8c05c1af4ed6cee762 Mon Sep 17 00:00:00 2001 From: mail6543210 Date: Tue, 19 Sep 2017 20:26:01 +0800 Subject: [PATCH 1/4] Revert "Use source content instead of source path (fixes: #148)" This reverts commit ed397bdaf8ccde86a4b39863f2b68254f8a5c9bc. --- lib/homesick/utils.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/homesick/utils.rb b/lib/homesick/utils.rb index 701ec2a..e18cd17 100644 --- a/lib/homesick/utils.rb +++ b/lib/homesick/utils.rb @@ -150,7 +150,7 @@ module Homesick def collision_accepted?(destination, source) fail "Arguments must be instances of Pathname, #{destination.class.name} and #{source.class.name} given" unless destination.instance_of?(Pathname) && source.instance_of?(Pathname) - options[:force] || shell.file_collision(destination) { File.binread(source) } + options[:force] || shell.file_collision(destination) { source } end def each_file(castle, basedir, subdirs) From d3d6974b7b5146fcf3b49d7dc1281088392070b8 Mon Sep 17 00:00:00 2001 From: mail6543210 Date: Tue, 19 Sep 2017 20:29:06 +0800 Subject: [PATCH 2/4] Rename `content` to `source` It is a instance of Pathname, not binary content --- lib/homesick/cli.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/homesick/cli.rb b/lib/homesick/cli.rb index 159d470..43d401f 100644 --- a/lib/homesick/cli.rb +++ b/lib/homesick/cli.rb @@ -27,13 +27,13 @@ module Homesick # Hack in support for diffing symlinks # Also adds support for checking if destination or content is a directory shell_metaclass = class << shell; self; end - shell_metaclass.send(:define_method, :show_diff) do |destination, content| + shell_metaclass.send(:define_method, :show_diff) do |destination, source| destination = Pathname.new(destination) - content = Pathname.new(content) - return 'Unable to create diff: destination or content is a directory' if destination.directory? || content.directory? - return super(destination, content) unless destination.symlink? + source = Pathname.new(source) + return 'Unable to create diff: destination or content is a directory' if destination.directory? || source.directory? + return super(destination, source) unless destination.symlink? say "- #{destination.readlink}", :red, true - say "+ #{content.expand_path}", :green, true + say "+ #{source.expand_path}", :green, true end end From 62c934774b0f5616543a40ee4d2a6924c1b1c17e Mon Sep 17 00:00:00 2001 From: mail6543210 Date: Tue, 19 Sep 2017 20:30:15 +0800 Subject: [PATCH 3/4] Real fix for #148 --- lib/homesick/cli.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/homesick/cli.rb b/lib/homesick/cli.rb index 43d401f..d339dff 100644 --- a/lib/homesick/cli.rb +++ b/lib/homesick/cli.rb @@ -31,7 +31,7 @@ module Homesick destination = Pathname.new(destination) source = Pathname.new(source) return 'Unable to create diff: destination or content is a directory' if destination.directory? || source.directory? - return super(destination, source) unless destination.symlink? + return super(destination, File.binread(source)) unless destination.symlink? say "- #{destination.readlink}", :red, true say "+ #{source.expand_path}", :green, true end From 8c2a1d0f8485d99d24296af2e1c630f9faade0de Mon Sep 17 00:00:00 2001 From: mail6543210 Date: Sat, 23 Sep 2017 00:41:33 +0800 Subject: [PATCH 4/4] Add testcase --- spec/homesick_cli_spec.rb | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/spec/homesick_cli_spec.rb b/spec/homesick_cli_spec.rb index 31fcfb4..46a9762 100644 --- a/spec/homesick_cli_spec.rb +++ b/spec/homesick_cli_spec.rb @@ -331,6 +331,40 @@ describe Homesick::CLI do expect(home.join('.some_dotfile').readlink).to eq(dotfile) end end + + context 'when call and some files conflict' do + it 'shows differences for conflicting text files' do + contents = {:castle => 'castle has new content', :home => 'home already has content'} + + dotfile = castle.file('text') + File.open(dotfile.to_s, 'w') do |f| + f.write contents[:castle] + end + File.open(home.join('text').to_s, 'w') do |f| + f.write contents[:home] + end + message = Capture.stdout { homesick.shell.show_diff(home.join('text'), dotfile) } + expect(message.b).to match(/- ?#{contents[:home]}\n.*\+ ?#{contents[:castle]}$/m) + end + it 'shows message or differences for conflicting binary files' do + # content which contains NULL character, without any parentheses, braces, ... + contents = {:castle => (0..255).step(30).map{|e| e.chr}.join(), :home => (0..255).step(30).reverse_each.map{|e| e.chr}.join()} + + dotfile = castle.file('binary') + File.open(dotfile.to_s, 'w') do |f| + f.write contents[:castle] + end + File.open(home.join('binary').to_s, 'w') do |f| + f.write contents[:home] + end + message = Capture.stdout { homesick.shell.show_diff(home.join('binary'), dotfile) } + if homesick.shell.is_a?(Thor::Shell::Color) + expect(message.b).to match(/- ?#{contents[:home]}\n.*\+ ?#{contents[:castle]}$/m) + elsif homesick.shell.is_a?(Thor::Shell::Basic) + expect(message.b).to match(/^Binary files .+ differ$/) + end + end + end end describe 'unlink' do