Module: Git

Defined in:
lib/git-copy.rb

Class Method Summary collapse

Class Method Details

.copy(src, dst) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/git-copy.rb', line 6

def self.copy(src, dst)
  # Copy a remote git repo to another remote destination
  #
  # Example:
  #   >> GitCopy("https://github.com/cybertk/git-copy", "https://bitbucket.com/mirror.git")
  #   >> GitCopy("https://github.com/cybertk/git-copy", "mirror.git")
  uri = Addressable::URI.parse(dst)

  # Convert to absolute path for local path
  dst = File.absolute_path(dst) unless uri.scheme

  if uri.scheme || File.exist?(dst)

    Dir.mktmpdir('git-copy-') do |dir|
      # Clone source into temp working dir
      unless `git clone --bare #{src} #{dir}`.to_i == 0
        raise 'git clone faild'
      end

      unless `cd #{dir}; git push -f --mirror #{dst}`.to_i == 0
        raise 'git push faild'
      end
    end
  else
    # Copy to local path
    unless `git clone --bare #{src} #{dst}`.to_i == 0
      raise 'git clone failed'
    end
  end

end