Skip to content

Instantly share code, notes, and snippets.

@tekwiz
Created June 3, 2013 21:04
Show Gist options
  • Save tekwiz/5701396 to your computer and use it in GitHub Desktop.
Save tekwiz/5701396 to your computer and use it in GitHub Desktop.
Git Backup
#!/usr/bin/env ruby
require "tmpdir"
require "fileutils"
PWD_ORIG = Dir.pwd
TMP_DIR = Dir.mktmpdir("git-backup")
def remote_basename
if ARGV[0] =~ /\.git$/
File.basename(ARGV[0])
else
raise "Canot determine remote basename"
end
end
def target_fn
if ARGV[1] and ARGV[1] =~ /^\//
ARGV[1]
elsif ARGV[1]
File.join(PWD_ORIG, ARGV[1])
else
File.join(PWD_ORIG, remote_basename+"-bundle")
end
end
Dir.chdir(TMP_DIR)
system *(%w[git clone --mirror] + [ ARGV[0] ])
Dir.chdir(File.join(TMP_DIR, remote_basename))
system *(%w[git bundle create] + [ target_fn ] + %w[--all])
Dir.chdir(PWD_ORIG)
FileUtils.rm_r TMP_DIR, secure: true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment