Skip to content

Instantly share code, notes, and snippets.

@mtancoigne
Created August 26, 2024 12:23
Show Gist options
  • Save mtancoigne/63ad7b4333dfd91dc8025b844aae745c to your computer and use it in GitHub Desktop.
Save mtancoigne/63ad7b4333dfd91dc8025b844aae745c to your computer and use it in GitHub Desktop.
Searches for a committed string in project with multiple repositories
#!/usr/bin/env ruby
# frozen_string_literal: true
# Searches for a committed string in project repositories
#
# Usage:
# git-grep-apps <string>
#
# This script will search for a configuration file on the current directory or its parents when not found.
#
# Configuration:
# Create a file named ".git-grep-apps" somewhere with relative paths to project repositories
#
# Configuration example:
# some_dir
# another/dir
# # Comments are accepted
# one_last_dir
require 'pathname'
require 'shellwords'
string = ARGV[0]
CONFIGURATION_FILE = '.git-grep-apps'
def read_configuration
path = Pathname.new Dir.pwd
while path.to_s != '/'
file = path.join(CONFIGURATION_FILE)
puts "searching #{file}"
return { path: path.to_s, dirs: parse(File.read(file)) } if File.file? file
path = path.parent
end
puts "#{CONFIGURATION_FILE} not found in directory and its parent"
end
def parse(content)
content.split("\n").filter_map(&:strip).reject { |line| line.match?(/^#/) || line == '' }
end
config = read_configuration
puts config
exit 1 unless config
Dir.chdir config[:path] do |root|
config[:dirs].each do |path|
Dir.chdir path do
puts "#{path}\n#{'=' * path.size}"
results = `git --no-pager grep -n --color #{Shellwords.escape(string)}`
results.split("\n").each { |line| puts "#{root}/#{path}/#{line}" }
puts ''
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment