Skip to content

Commit

Permalink
Handling two object which name only differs in case. Also added dry-mode
Browse files Browse the repository at this point in the history
as default.
  • Loading branch information
kolAflash committed Mar 19, 2018
1 parent 5baf0e5 commit 4e7bfbd
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 11 deletions.
79 changes: 68 additions & 11 deletions casesync.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@

require 'find'

DEBUG = true
DEBUG = false



def main(argv)
rename_left, root_left, root_right = options(argv)
do_it, rename_left, root_left, root_right = parse_argv(argv)
for left_p in Find.find(root_left) do
left_p_short = cut_root(left_p, root_left)
if left_p_short != ''
puts("cheking: left: #{left_p_short}") if DEBUG
dir_short = cut_root(File.dirname(left_p), root_left)
right_dir = File.join(root_right, dir_short)
right_dir = sync_case(root_right, dir_short)

if File.directory?(right_dir)
for right_name in Dir.entries(right_dir) do
Expand All @@ -24,7 +24,7 @@ def main(argv)
puts(" right: #{right_p_short}") if DEBUG
if left_name != right_name
right_p = File.join(root_right, right_p_short)
rename_by_template(left_p, right_p, rename_left)
rename_by_template(left_p, right_p, do_it, rename_left, root_left, root_right)
end
end
end
Expand All @@ -37,25 +37,79 @@ def main(argv)
end


def rename_by_template(left_path, right_path, rename_left)
def rename_by_template(left_path, right_path, do_it, rename_left, root_left, root_right)
template_path = left_path
target_path = right_path
target_root = root_right
if rename_left
template_path = right_path
target_path = left_path
target_root = root_left
end
target_dir = File.dirname(target_path)
template_name = File.basename(template_path)
puts(" renaming: #{target_path} => #{File.join(target_dir, template_name)}")
File.rename(target_path, File.join(target_dir, template_name))
target_new_path = File.join(target_dir, template_name)
target_new_path_short = cut_root(target_new_path, target_root)
if File.exists?(target_new_path)
puts(" error, object exists: #{target_new_path_short}")
else
target_path_short = cut_root(target_path, target_root)
puts(" renaming: #{target_path_short} => #{target_new_path_short}")
File.rename(target_path, target_new_path) if do_it
end
end


def options(argv)
def sync_case(root, cased_dir)
cased_dir_ary = []
basename = File.basename(cased_dir)
while basename != '.'
cased_dir_ary << basename
cased_dir = File.dirname(cased_dir)
basename = File.basename(cased_dir)
end

dir = root
for sub in cased_dir_ary.reverse
found_identical = false
found_with_different_case = nil
if File.directory?(dir)
for entry in Dir.entries(dir) do
if sub == entry
found_identical = true
elsif sub.downcase == entry.downcase
found_with_different_case = entry
end
end
end
if found_identical || !found_with_different_case
dir = File.join(dir, sub)
else
dir = File.join(dir, found_with_different_case)
end
end
return dir
end


def parse_argv(argv)
do_it = false
rename_left = false

next_arg = 0

rename_left = argv[next_arg] == '--rename-left'
next_arg += 1 if rename_left
in_options = true
while in_options do
if argv[next_arg] == '--do-it'
do_it = true
next_arg += 1
elsif argv[next_arg] == '--rename-left'
rename_left = true
next_arg += 1
else
in_options = false
end
end

next_arg += 1 if argv[next_arg] == '--'

Expand All @@ -72,12 +126,15 @@ def options(argv)
"Searches trough the LEFT directory tree and syncronizes the case of\n"+
"the file and directory names in the RIGHT tree."+
"\n"+
"--do-it\n"+
" Really rename stuff. Else needed renames will only be printed."+
"\n"+
"--rename-left\n"+
" Rename files in the left directory. Default is to rename in the right directory."
)
exit(1)
end
return rename_left, root_left, root_right
return do_it, rename_left, root_left, root_right
end


Expand Down
Binary file modified example_tree.tar.xz
Binary file not shown.

0 comments on commit 4e7bfbd

Please sign in to comment.