Skip to content

Instantly share code, notes, and snippets.

@dohzya
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dohzya/28f51e7a62ded6a448fb to your computer and use it in GitHub Desktop.
Save dohzya/28f51e7a62ded6a448fb to your computer and use it in GitHub Desktop.
Push without submodules
#!/usr/bin/env ruby
$dry_run = ENV['NOSUB_DRYRUN'] && %w(1 t true y yes).include?(ENV['NOSUB_DRYRUN'].downcase)
$debug = ENV['NOSUB_DEBUG'] && %w(1 t true y yes).include?(ENV['NOSUB_DEBUG'].downcase)
def usage
puts <<-USAGE
usage: $0 [-n] [-d] <remote> <ref>"
-d | --debug : Show performed operations
-n | --dry-run : Don't actually perform the operations
USAGE
end
def die(msg, show_usage=false)
warn msg
(puts ; usage) if show_usage
exit 1
end
def debug(msg)
warn msg if $debug
end
def sys(*cmd)
cmd = cmd.compact
debug cmd.join(" ")
system(*cmd) unless $dry_run
end
remote, ref, force = nil
while arg = ARGV.shift
case arg
when '-h', '--help'
usage
exit
when '-d', '--debug'
$debug = true
when '-n', '--dry-run'
$dry_run = true
else
if remote.nil?
remote = arg
elsif ref.nil?
ref = arg
if m = /^\+(.*)/.match(ref)
force = '+'
ref = m[1]
end
else
die "Too many parameters", true
end
end
end
die "Missing remote", true unless remote
die "Missing ref", true unless ref
$debug ||= $dry_run
git_working_dir = `git rev-parse --show-toplevel`.chomp
git_dir = `git rev-parse --git-dir`.chomp
Dir.chdir(git_working_dir) {
if ! `git status -s`.chomp.empty?
die "The working directory is not clean! (do you know .git/info/exclude?)"
end
cur_ref = `git rev-parse --abbrev-ref HEAD`.chomp
cur_sha1 = `git rev-parse HEAD`.chomp
submodules = File.read('.gitmodules').lines.map { |line|
if m = /^\s*path\s*=\s*(\S+)/.match(line)
m[1]
end
}.compact
quiet = $debug ? nil : '--quiet'
sys 'git', 'submodule', quiet, 'update', '--init'
sys 'git', 'checkout', quiet, cur_sha1
begin
submodules.each { |path|
sys 'git', 'rm', '--cached', quiet, path
sys 'rm', "#{path}/.git"
sys 'git', 'add', path
}
sys 'git', 'commit', quiet, '--no-verify', '-m', "Flatten submodules"
sys 'git', 'push', '--no-verify', remote, "#{force}HEAD:#{ref}"
ensure
sys 'git', 'checkout', quiet, cur_ref
sys 'git', 'submodule', quiet, 'update', '--init'
end
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment