Module: Bcpm::Git

Defined in:
lib/bcpm/git.rb

Overview

Git repository operations.

Class Method Summary collapse

Class Method Details

.checkpoint_local_repo(repo_path, local_path) ⇒ Object

Checks out the working copy of the repository.

Returns true for success, false if something went wrong.



69
70
71
72
73
74
75
76
77
# File 'lib/bcpm/git.rb', line 69

def self.checkpoint_local_repo(repo_path, local_path)
  return false unless File.exist?(repo_path)
  FileUtils.mkdir_p local_path
  Dir.entries(repo_path).each do |entry|
    next if ['.', '..', '.git'].include? entry
    FileUtils.cp_r File.join(repo_path, entry), local_path
  end
  true
end

.checkpoint_repo(repo_uri, repo_branch, local_path) ⇒ Object

Downloads a snapshot of a repository.

Returns true for success, false if something went wrong.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/bcpm/git.rb', line 42

def self.checkpoint_repo(repo_uri, repo_branch, local_path)
  FileUtils.mkdir_p local_path
  success = nil
  Dir.chdir File.dirname(local_path) do
    zip_file = File.basename(local_path) + '.zip'
    success = Kernel.system('git', 'archive', '--format=zip',
        '--remote', repo_uri, '--output', zip_file, '-9', repo_branch)
    if success
      Dir.chdir File.basename(File.basename(local_path)) do
        success = Kernel.system 'unzip', '-qq', File.join('..', zip_file)
      end
    end
    File.unlink zip_file if File.exist?(zip_file)
  end
  unless success
    puts "Trying workaround for old git"
    success = clone_repo repo_uri, repo_branch, local_path
    FileUtils.rm_rf File.join(local_path, '.git') if success
  end
  
  FileUtils.rm_rf local_path unless success
  success
end

.clone_repo(repo_uri, repo_branch, local_path) ⇒ Object

Clones a repository.

Returns true for success, false if something went wrong.



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
37
# File 'lib/bcpm/git.rb', line 12

def self.clone_repo(repo_uri, repo_branch, local_path)
  FileUtils.mkdir_p File.dirname(local_path)
  success = nil
  Dir.chdir File.dirname(local_path) do
    FileUtils.rm_rf File.basename(local_path) if File.exists?(local_path)
    if repo_branch == 'master'
      success = Kernel.system 'git', 'clone', repo_uri,
                              File.basename(local_path)
    else
      success = Kernel.system 'git', 'clone', '--branch', repo_branch,
                              repo_uri, File.basename(local_path)
      unless success
        success = Kernel.system 'git', 'clone', repo_uri,
                                File.basename(local_path)
        if success
          Dir.chdir File.basename(local_path) do
            success = Kernel.system 'git', 'checkout', '-q',
                                    'origin/' + repo_branch
          end
        end
      end
    end
  end
  FileUtils.rm_rf local_path unless success
  success
end

.tempdirObject

Temporary directory name.



87
88
89
90
# File 'lib/bcpm/git.rb', line 87

def self.tempdir
  File.join Dir.tmpdir, 'bcpm',
      "update_#{Socket.hostname}_#{(Time.now.to_f * 1000).to_i}_#{$PID}"
end

.update_repo(local_path) ⇒ Object

Pulls the latest changes into the repository.



80
81
82
83
84
# File 'lib/bcpm/git.rb', line 80

def self.update_repo(local_path)
  Dir.chdir local_path do
    Kernel.system 'git', 'pull'
  end
end