Skip to content

Instantly share code, notes, and snippets.

@colstrom
Last active January 27, 2020 21:57
Show Gist options
  • Select an option

  • Save colstrom/61b6ffaf72ceda7ed79a4f08d5fb4a34 to your computer and use it in GitHub Desktop.

Select an option

Save colstrom/61b6ffaf72ceda7ed79a4f08d5fb4a34 to your computer and use it in GitHub Desktop.
Directory Comparison Utility (like comm, but for directories)
#! /usr/bin/env ruby
# SPDX-License-Identifier: MIT
# The MIT License (MIT)
# Copyright © 2020 Chris Olstrom <[email protected]>
# Copyright © 2020 SUSE Software Solutions
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the “Software”), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
require "digest/sha2" # SPDX-License-Identifier: Ruby
require "find" # SPDX-License-Identifier: Ruby
require "optparse" # SPDX-License-Identifier: Ruby
require "pathname" # SPDX-License-Identifier: Ruby
require "set" # SPDX-License-Identifier: Ruby
options = {
suppress: Set.new
}
OptionParser.new do |parser|
parser.on("-1", "suppress column 1 (entries unique to DIR1") { options[:suppress] << 1 }
parser.on("-2", "suppress column 2 (entries unique to DIR2") { options[:suppress] << 2 }
parser.on("-3", "suppress column 3 (entries that appear in both directories") { options[:suppress] << 3 }
end.parse!
unless ARGV.size == 2
STDERR.puts "Usage: #{File.basename($PROGRAM_NAME)} [ options ] DIR1 DIR2"
exit Errno::EINVAL::Errno
end
FileSystemObject = Struct.new(:location, :type, :digest, :target)
class FileSystemObject
def to_s
location.to_s
end
end
ContentsOf = lambda do |path|
Pathname(path).yield_self do |root|
Find.find(root).map do |entry|
Pathname(entry).yield_self do |pathname|
FileSystemObject.new.tap do |fso|
fso.location = pathname.relative_path_from(root)
fso.type = pathname.ftype.to_sym
case fso.type
when :link then fso.target = pathname.readlink
when :file then fso.digest = Digest::SHA256.hexdigest(pathname.read)
end
end
end
end.reduce(Set.new, &:<<)
end
end
dir1 = ContentsOf.(ARGV.pop)
dir2 = ContentsOf.(ARGV.pop)
either = dir1.union(dir2)
both = dir1.intersection(dir2)
first = dir1.difference(both)
second = dir2.difference(both)
STDOUT.puts(
either.map do |entry|
(
if both.include?(entry)
[nil, nil, entry] unless options[:suppress].include?(3)
elsif second.include?(entry)
[nil, entry, nil] unless options[:suppress].include?(2)
elsif first.include?(entry)
[entry, nil, nil] unless options[:suppress].include?(1)
end || []
).map(&:to_s).join("\t")
end.reject(&:empty?).join("\n")
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment