Skip to content

Instantly share code, notes, and snippets.

@blocknotes
Last active August 14, 2017 16:10
Show Gist options
  • Save blocknotes/4520d1801754a8671166ff76c345da89 to your computer and use it in GitHub Desktop.
Save blocknotes/4520d1801754a8671166ff76c345da89 to your computer and use it in GitHub Desktop.
Ruby - Access to Git repositories
# Look for a specific string (optionally filtering files with a pattern) of my projects
require 'git'
require 'pathname'
BASE_PATH='/projects'
search='class_eval'
filter_files='*.rb' # Empty for every file
Pathname.new( BASE_PATH ).children.each do |path|
if path.directory?
begin
git = Git.open path.to_s
files = filter_files ? git.gblob( filter_files ).grep( search ) : git.grep( search )
if files.any?
puts '### ' + path.to_s
files.each do |file, matches|
puts " - #{file}"
matches.each do |match|
puts " [#{match[0]}] #{match[1]}"
end
end
end
rescue Exception => e
puts "> git - #{e.message}: #{path.to_s}"
end
end
end
# Sometimes I need to find a specific file in every project in the Git folder
require 'git'
require 'pathname'
BASE_PATH='/projects'
search='app/models/*page*.rb'
Pathname.new( BASE_PATH ).children.each do |path|
if path.directory?
begin
git = Git.open path.to_s
files = git.ls_files( search ).keys
if files.any?
puts '> ' + path.to_s
p files
end
rescue Exception => e
puts "> git - #{e.message}: #{path.to_s}"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment