Class: Nucleon::Util::Git

Inherits:
Object
  • Object
show all
Defined in:
lib/core/util/git.rb

Class Method Summary collapse

Class Method Details

.load(path, options = {}) ⇒ Object


Git repo loader



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
39
40
41
42
43
44
45
# File 'lib/core/util/git.rb', line 9

def self.load(path, options = {})
  epath   = File.expand_path(path)
  git_dir = File.join(epath, '.git')
  git     = nil
  
  begin
    if File.exist?(git_dir)
      if File.directory?(git_dir)
        git = Rugged::Repository.new(git_dir)
      else
        # TODO: Find out if this is actually necessary with Rugged / LibGit2
        git_dir = Util::Disk.read(git_dir)
        unless git_dir.nil?
          git_dir = git_dir.gsub(/^gitdir\:\s*/, '').strip
        
          if File.directory?(git_dir)
            git         = Rugged::Repository.new(git_dir)
            git.workdir = epath  
          end
        end
      end      
    elsif File.directory?(epath) && (options[:bare] || (epath =~ /\.git$/ && File.exist?(File.join(epath, 'HEAD'))))
      git = Rugged::Repository.bare(epath)
    end
  rescue
  end
  
  if git.nil? && options[:create]
    FileUtils.mkdir_p(epath) unless File.directory?(epath)
    if options[:bare]
      git = Rugged::Repository.init_at(epath, :bare)
    else
      git = Rugged::Repository.init_at(epath)
    end
  end        
  git
end