Module: Git

Defined in:
lib/git.rb

Class Method Summary collapse

Class Method Details

.add_shared_objects(path, object_path) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/git.rb', line 17

def add_shared_objects path, object_path
  raise "object_path is empty" unless object_path
  Dir.chdir path do
    alernates_path = '.git/objects/info/alternates'
    content = (File.read(alernates_path) rescue '')
    return if content.include?(object_path)
    File.write alernates_path, "#{object_path}\n#{content}"
  end
end

.amend(dest, message) ⇒ Object



45
46
47
48
49
# File 'lib/git.rb', line 45

def amend dest, message
  Dir.chdir dest do
    system! 'git', 'commit', '--amend', '--message', message
  end
end

.checkout(dest, commit) ⇒ Object



32
33
34
35
36
37
# File 'lib/git.rb', line 32

def checkout dest, commit
  raise "commit is empty" unless commit
  Dir.chdir dest do
    system! 'git', 'checkout', '--quiet', '--force', '--detach', commit
  end
end

.init(dest) ⇒ Object



27
28
29
30
# File 'lib/git.rb', line 27

def init dest
  FileUtils.mkdir_p dest
  Dir.chdir dest do system! 'git', 'init' end
end

.message(dest, commit = 'HEAD') ⇒ Object



39
40
41
42
43
# File 'lib/git.rb', line 39

def message dest, commit='HEAD'
  Dir.chdir dest do
    `git log --format=%B -n 1 #{Shellwords.join([commit])}`.chomp
  end
end

.mirror(url, dest) ⇒ Object



6
7
8
9
10
11
12
13
14
15
# File 'lib/git.rb', line 6

def mirror url, dest
  if File.exists?(dest)
    Dir.chdir(dest) do
      system! 'git', 'fetch', '--tags', '--all', '--force', '--prune'
    end
  else
    FileUtils.mkdir_p dest
    system! 'git', 'clone', '--mirror', url, dest
  end
end

.write_commit_tree(repo_path, dest, commit) ⇒ Object



51
52
53
54
55
56
# File 'lib/git.rb', line 51

def write_commit_tree repo_path, dest, commit
  init dest
  object_path = Dir["#{repo_path}/{.git/,}objects"].first
  add_shared_objects dest, object_path
  checkout dest, commit
end