lib/diff_dirs.rb in cmer-diff_dirs-0.1.1 vs lib/diff_dirs.rb in cmer-diff_dirs-0.1.2

- old
+ new

@@ -18,14 +18,14 @@ protected def self.diff_result_line_parse(dir1, dir2, line) # New/Deleted files match = line.match(/^Only in ([^:]*): ([^$]*)$/) if match - if match[1] == dir1 - return [:deleted, match[2]] # deleted from second dir - elsif match[1] == dir2 - return [:new, match[2]] # new in second dir + if match[1].starts_with?(dir1) + return [:deleted, remove_dir_from_path("#{match[1]}/#{match[2]}", dir1)] # deleted from second dir + elsif match[1].starts_with?(dir2) + return [:new, remove_dir_from_path("#{match[1]}/#{match[2]}", dir2)] # new in second dir else raise "#{dir1} or #{dir2} didn't match #{match[1]}" end elsif line.match(/^Files\s/) && line.match(/\sdiffer$/) @@ -41,20 +41,32 @@ def self.execute(cmd) `#{cmd}` end def self.remove_dir_from_path(path, dir) - dir += "/" unless dir[-1..-1] == "/" + dir = add_slash(dir) path.sub(Regexp.new("^#{dir}"), "") end def self.expand_path(dir) if dir[0..0] == "~" File.expand_path(dir) else dir end end + + def self.add_slash(path) + path += "/" unless path[-1..-1] == "/" + path + end +end + +class String + def starts_with?(prefix) + prefix = prefix.to_s + self[0, prefix.length] == prefix + end end public def diff_dirs(dir1, dir2); DiffDirs::diff_dirs(dir1, dir2); end \ No newline at end of file