Skip to content

Instantly share code, notes, and snippets.

@glennpratt
Last active August 29, 2015 14:15
Show Gist options
  • Select an option

  • Save glennpratt/994463672b3d761142eb to your computer and use it in GitHub Desktop.

Select an option

Save glennpratt/994463672b3d761142eb to your computer and use it in GitHub Desktop.
filter git repo to only files interested in by regex
#!/usr/bin/env ruby
# filter git repo to only files interested in by regex
#
# Get all files ever in the repo.
# git log --pretty=format: --name-only --diff-filter=A | sort -u > list.txt
# ruby filter_reduce.rb | tr '\r\n' ' ' > reduced_list.txt
# git filter-branch --index-filter "git rm -rfq --cached --ignore-unmatch $(cat reduced_list.txt)" --prune-empty
list = []
File.open("#{__dir__}/list.txt").each_line do |line|
list << line.chop
end
$stderr.puts " Total: #{list.length}"
# @todo Make this an arg... or not.
keep = list.grep(/(ah-task-server\.rb|ah-run-site-hook\.rb|task-server-config\.json|taskserver\/files\/config\.json|[\w-]+\.task\.\w+)$/)
if keep.empty?
raise "Nothing to keep"
end
$stderr.puts " Keep: #{keep.length}"
# Files to delete.
delete = []
list.each do |line|
last_line = nil
loop do
# We reduce to the most general path that doesn't delete things we like, otherwise too many arguments.
if keep.grep(/#{line}/).empty?
last_line = line.clone
line = File.dirname(line)
else
break
end
end
if last_line
delete << last_line
end
end
delete.uniq!
$stderr.puts "Delete: #{delete.length}"
delete.each { |line| puts line }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment