Skip to content

Instantly share code, notes, and snippets.

@nisevi
Last active July 20, 2020 00:26
Show Gist options
  • Save nisevi/ad18dd58259bf40d50365147c3b9ce4d to your computer and use it in GitHub Desktop.
Save nisevi/ad18dd58259bf40d50365147c3b9ce4d to your computer and use it in GitHub Desktop.
Recursively iterate over all subfolders and execute 'git pull' on all paths that have '.git'
#!/usr/bin/env ruby
class PathManager
DEFAULT_DIRECTORY = '.'
attr_accessor :base_directory, :invalid_filenames
def initialize(args = {})
self.base_directory = expand_path(args.fetch(:base_directory, DEFAULT_DIRECTORY))
self.invalid_filenames = args[:invalid_filenames]
end
def valid?(path)
path.match(/#{invalid_filenames.join('|')}/).nil?
end
def directories
entries.map { |filename| expand_path(filename) }
entries << base_directory
end
def entries
filenames = Dir.glob('**/*', base: base_directory)
filenames.select { |filename| is_directory?(filename) && valid?(filename) }
end
def is_directory?(path)
Dir.exist?(path)
end
def expand_path(filename)
return File.expand_path(filename) if is_directory?(filename)
raise "Path not present on current directory!"
end
end
class Parser
GIT_DIRECTORY = '.git'
attr_accessor :path_manager
def initialize(args = {})
self.path_manager = args[:path_manager]
end
def run
parse(path_manager.directories)
rescue Exception => e
puts "An error has occurred: #{e.message}"
end
def parse(directories = [])
directories.each do |directory|
next unless is_git_directory?(directory)
puts 'Git directory was found!'
puts "Directory: #{directory}"
puts 'Pulling data now....'
git_pull(directory)
puts 'Git pull finished!'
puts '...'
end
end
private
def is_git_directory?(directory)
Dir.entries(directory).include?(GIT_DIRECTORY)
end
def git_pull(directory)
Dir.chdir(directory)
`git config pull.rebase true`
`git pull origin #{current_branch}`
Dir.chdir(path_manager.base_directory)
end
def current_branch
`git rev-parse --abbrev-ref HEAD`
end
end
invalid_filenames = ['node_modules', 'git', 'vendor', 'customize', 'third_party']
path_manager = PathManager.new( invalid_filenames: invalid_filenames )
parser = Parser.new( path_manager: path_manager )
parser.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment