Created
March 26, 2018 21:42
-
-
Save trevor-vaughan/e6a02b9b6131a8511889c4423b3e1f6c to your computer and use it in GitHub Desktop.
Find symlinks replaced by directories and the reverse in git
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
git_log = %x{git log --name-status --diff-filter=AD}.lines.select{ |x| | |
x =~ /\s*(A|D)\s+/ | |
}.map(&:strip) | |
deleted = [] | |
added = [] | |
git_log.each do |logline| | |
k,v = logline.split(/\s+/) | |
if k == 'D' | |
deleted << v | |
else | |
added << v | |
end | |
end | |
replaced_symlink_with_dir = {} | |
replaced_dir_with_symlink = {} | |
def replaced_with_dir(to_find, added) | |
added.grep(%r{^#{to_find}/.+}) | |
end | |
def replaced_with_symlink(to_find, added) | |
added.grep(%r{^#{to_find}$}) | |
end | |
deleted.each do |file| | |
matching_files = replaced_with_dir(file, added) | |
unless matching_files.empty? | |
replaced_symlink_with_dir[file] ||= [] | |
replaced_symlink_with_dir[file] += matching_files | |
next | |
end | |
matching_files = replaced_with_symlink(File.dirname(file), added) | |
unless matching_files.empty? | |
replaced_dir_with_symlink[path] ||= [] | |
replaced_dir_with_symlink[path] += matching_files | |
break | |
end | |
end | |
require 'json' | |
puts 'Symlinks replaced with directories:' | |
puts JSON.pretty_generate(replaced_symlink_with_dir) | |
puts '' | |
puts 'Directories replaced with symlinks:' | |
puts JSON.pretty_generate(replaced_dir_with_symlink) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment