Class: Coral::Git

Inherits:
Grit::Repo
  • Object
show all
Defined in:
lib/coral_core/util/git.rb

Instance Method Summary collapse

Constructor Details

#initialize(path, options = {}) ⇒ Git


Constructor / Destructor



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
37
38
# File 'lib/coral_core/util/git.rb', line 7

def initialize(path, options = {})
  epath   = File.expand_path(path)
  git_dir = File.join(epath, '.git')
  
  @bare = (options[:is_bare] ? true : false)
  
  if File.exist?(git_dir)
    self.working_dir = epath
    
    if File.directory?(git_dir)
      self.path = git_dir
    else
      git_dir = Util::Disk.read(git_dir)
      unless git_dir.nil?
        git_dir = git_dir.gsub(/^gitdir\:\s*/, '').strip
        self.path = git_dir if File.directory?(git_dir)
      end
    end
    
  elsif File.directory?(epath) && (options[:is_bare] || (epath =~ /\.git$/ && File.exist?(File.join(epath, 'HEAD'))))
    self.path = epath
    @bare = true
  end

  self.git = ::Grit::Git.new(self.path)
  self.git.work_tree = epath
  
  unless File.directory?(epath) && self.git.exist?
    FileUtils.mkdir_p(epath) unless File.directory?(epath)
    self.git.init({ :bare => @bare })
  end
end