Skip to content

Instantly share code, notes, and snippets.

@jonforums
Created September 27, 2011 15:10
Show Gist options
  • Save jonforums/1245318 to your computer and use it in GitHub Desktop.
Save jonforums/1245318 to your computer and use it in GitHub Desktop.
Custom 'git all' command in Ruby and 'hg all' extension in Python
C:\Users\Jon\Documents\WebDev>git config --global --list | grep -P "^alias\.[abs]"
alias.all=!ruby c:/tools/gitall.rb
alias.br=branch
alias.st=status -sb
C:\Users\Jon\Documents\WebDev>git all fetch
No '.gitall_ignore' found; processing all '*-git' dirs...
[FETCH] 960-gs-git
[FETCH] cramp-git
[FETCH] jekyll-git
[FETCH] lift-git
[FETCH] mustache-rails3-git
[FETCH] mustache-sinatra-git
[FETCH] nanoc-git
[FETCH] padrino-git
From https://github.com/padrino/padrino-framework
95df763..8941e4b master -> origin/master
[FETCH] quotes-git
[FETCH] rack-git
From https://github.com/rack/rack
fb0d0d4..0c26ffe master -> origin/master
698ec56..e65368f rack-1.3 -> origin/rack-1.3
[FETCH] rack-mount-git
[FETCH] rack-test-git
[FETCH] radiant-git
[FETCH] scalatra-git
[FETCH] sdj-git
[FETCH] sinatra-authentication-git
[FETCH] sinatra-contrib-git
From https://github.com/sinatra/sinatra-contrib
9a31497..35a92b6 master -> origin/master
[FETCH] sinatra-git
From https://github.com/sinatra/sinatra
27206ab..a25bff1 1.2.x -> origin/1.2.x
* [new tag] 1.2.7 -> 1.2.7
* [new tag] v1.2.7 -> v1.2.7
[FETCH] slim-git
[FETCH] toto-git
[FETCH] trinidad-git
[FETCH] warden-git
The following repos were updated:
padrino-git
rack-git
sinatra-contrib-git
sinatra-git
# Revision: 10/12/2011 9:13:05 AM
# TODO recognize more git commands
# TODO better handling of aliases as not all have my custom aliases :(
# TODO code clean this little experiment
# TODO enable full git subcommand options to be passed to git
root = File.expand_path File.dirname(__FILE__)
def usage_and_exit(msg=USAGE, code=-1)
STDERR.puts msg
exit(code)
end
USAGE = <<-EOT
usage: git all GIT_COMMAND
where GIT_COMMAND is one of:
br branch
fetch fetch
st status
EOT
VALID_CMDS = [
:br,
:fetch,
:st,
]
usage_and_exit unless ARGV.length > 0
options = {}
options[:debug] = ARGV.delete('--debug') || ARGV.delete('-d')
cmd = ARGV.shift.downcase.to_sym
usage_and_exit unless VALID_CMDS.include?(cmd)
require 'open3'
begin
require 'psych'
rescue LoadError
ensure
require 'yaml'
end
begin
excludes = YAML.load_file('.gitall_ignore')
rescue ::Exception
STDERR.puts %q[No '.gitall_ignore' found; processing all '*-git' dirs...]
end
updated = []
Dir.glob('*-git').each do |dir|
if excludes
next if excludes.include?(dir)
end
Dir.chdir(dir) do
puts "[#{cmd.upcase}] #{dir}"
results, status = Open3.capture2e("git #{cmd}", :binmode => true)
case cmd
when :st, :br
puts results
when :fetch
#unless results.empty?
if results =~ /^From/
updated << dir
puts results
end
end
print "Exit status: #{status.exitstatus}\n\n" if options[:debug]
end
end
if updated.length > 0
puts <<-EOT
The following repos were updated:
#{updated.join("\n")}
EOT
end
C:\Users\Jon\Documents\webDev>type %USERPROFILE%\.hgrc
...
[extensions]
all= C:/tools/hgall.py
...
C:\Users\Jon\Documents\WebDev>hg all fetch
[FETCH] cp-hg
pulling from https://bitbucket.org/cherrypy/cherrypy
searching for changes
no changes found
# hgall.py - execute commands on multiple hg repos
#
# Created: 10/16/2011 8:35:05 PM
# Revision: 10/20/2011 12:04:17 PM
#
# TODO fix inability to pass hg cmd options without needing `--`
"""execute commands on multiple repositories"""
from mercurial import commands, util
from mercurial.dispatch import dispatch, request
import glob, json, os
commands.norepo += ' all'
def hgall(ui, *args, **opts):
"Execute commands on multiple Mercurial repos"
if len(args) == 0:
return
try:
with open('.hgall_ignore') as f:
excludes = json.load(f)
except Exception:
print("No '.hgall_ignore' found; processing all '*%s' dirs" % opts['suffix'])
for d in glob.glob('*%s' % opts['suffix']):
if 'excludes' in locals():
if d in excludes:
continue
ui.write('[%s] %s\n' % (args[0].upper(), d))
os.chdir(d)
dispatch(request(list(args)))
os.chdir('..')
cmdtable = {
"all": (hgall,
[('s', 'suffix', '-hg', 'Mercurial repo identifier suffix'),
],
"[options] CMD [-- CMD_OPTIONS]")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment