diff --git a/lib/homesick.rb b/lib/homesick.rb index c0ebe64..b90de36 100644 --- a/lib/homesick.rb +++ b/lib/homesick.rb @@ -151,7 +151,38 @@ class Homesick < Thor FileUtils.mkdir_p castle_path end - mv absolute_path, castle_path + # Are we already tracking this or anything inside it? + target = Pathname.new(castle_path.join(file.basename)) + + if target.exist? + + if absolute_path.directory? + child_files = absolute_path.children + child_files.each do |child| + + if target.join(child.basename).exist? + next + end + + mv child, target + end + absolute_path.rmtree + manifest = Pathname.new(repos_dir.join(castle, '.manifest')) + if manifest.exist? + lines = IO.readlines(manifest).delete_if { |line| line == "#{relative_dir + file.basename}\n" } + File.open(manifest, 'w') { |manfile| manfile.puts lines } + end + + elsif absolute_path.mtime > target.mtime && !absolute_path.symlink? + target.delete + mv absolute_path, castle_path + else + shell.say_status(:track, "#{target} already exists, and is more recent than #{file}. Run 'homesick SYMLINK CASTLE' to create symlinks.") + end + + else + mv absolute_path, castle_path + end inside home_dir do absolute_path = castle_path + file.basename diff --git a/spec/homesick_spec.rb b/spec/homesick_spec.rb index 3e68559..fed4a2c 100644 --- a/spec/homesick_spec.rb +++ b/spec/homesick_spec.rb @@ -235,6 +235,20 @@ describe "homesick" do f.readlines.size.should == 1 end end + + it 'should remove the parent of a tracked file from the manifest if the parent itself is tracked' do + castle = given_castle('castle_repo') + + some_nested_file = home.file('some/nested/file.txt') + nested_parent = home.directory('some/nested/') + homesick.track(some_nested_file.to_s, 'castle_repo') + homesick.track(nested_parent.to_s, 'castle_repo') + + manifest = Pathname.new(castle.parent.join('.manifest')) + File.open(manifest, 'r') do |f| + f.each_line { |line| line.should_not == "some/nested\n" } + end + end end end