Skip to content

Commit

Permalink
Breaking change: clean --all searches for broken links
Browse files Browse the repository at this point in the history
Quick hack for a more useful behavior.
  • Loading branch information
guns committed Oct 11, 2012
1 parent 487ec61 commit 6814688
Showing 1 changed file with 35 additions and 10 deletions.
45 changes: 35 additions & 10 deletions lib/ruby/haus/clean.rb
@@ -1,5 +1,7 @@
# -*- encoding: utf-8 -*-

require 'find'
require 'shellwords'
require 'haus/task'

class Haus
Expand All @@ -9,25 +11,48 @@ class Clean < Task

def options
super.tap do |opt|
opt.on '-a', '--all', 'Clean regular files and directories in addition to symlinks' do
opt.on '-a', '--all', 'Search all dotfiles for broken links (slow)' do
opt.all = true
end
end
end

def all_dotfiles dir
Dir['%s/.*' % dir.shellescape].each do |f|
b = File.basename f
next if b == '.' or b == '..'

# Find#find runs File.exist? on its arguments thereby raising on
# broken symlinks, which is exactly what we're looking for! We'll have
# to pass known extant directories instead.
if File.directory? f
Find.find(f) { |p| yield p }
else
yield f
end
end
end

def enqueue
users.each do |user|
hausfiles user do |src, dst|
begin
if options.all
if options.all
haus = Regexp.compile '\A%s/' % options.path
all_dotfiles user.dir do |dst|
if File.symlink? dst and File.expand_path(File.readlink dst) =~ haus
queue.add_deletion dst
elsif File.lstat(dst).ftype == 'link'
queue.add_deletion dst if File.expand_path(File.readlink dst) == src
end
rescue Errno::ENOENT
# We're not filtering non-extant files, so do nothing here
rescue Errno::EACCES, Errno::ENOTDIR => e
log ['!! ', :red, :bold], e.to_s
end
else
hausfiles user do |src, dst|
begin
if File.symlink? dst and File.expand_path(File.readlink dst) == src
queue.add_deletion dst
end
rescue Errno::ENOENT
# We're not filtering non-extant files, so do nothing here
rescue Errno::EACCES, Errno::ENOTDIR => e
log ['!! ', :red, :bold], e.to_s
end
end
end
end
Expand Down

0 comments on commit 6814688

Please sign in to comment.