Class: Gollum::Git::Repo

Inherits:
Object
  • Object
show all
Defined in:
lib/rugged_adapter/git_layer_rugged.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, options) ⇒ Repo

Returns a new instance of Repo.



541
542
543
544
545
546
547
548
549
# File 'lib/rugged_adapter/git_layer_rugged.rb', line 541

def initialize(path, options)
  begin
    @repo = Rugged::Repository.new(path, options)
  #rescue Grit::InvalidGitRepositoryError
   # raise Gollum::InvalidGitRepositoryError
  #rescue Grit::NoSuchPathError
   # raise Gollum::NoSuchPathError
  end
end

Class Method Details

.init(path) ⇒ Object



551
552
553
554
# File 'lib/rugged_adapter/git_layer_rugged.rb', line 551

def self.init(path)
  Rugged::Repository.init_at(path, false)
  self.new(path, :is_bare => false)
end

.init_bare(path) ⇒ Object



556
557
558
559
# File 'lib/rugged_adapter/git_layer_rugged.rb', line 556

def self.init_bare(path)
  Rugged::Repository.init_at(path, true)
  self.new(path, :is_bare => true)
end

Instance Method Details

#bareObject



561
562
563
# File 'lib/rugged_adapter/git_layer_rugged.rb', line 561

def bare
  @repo.bare?
end

#commit(id) ⇒ Object



573
574
575
# File 'lib/rugged_adapter/git_layer_rugged.rb', line 573

def commit(id)
  git.commit_from_ref(id)
end

#commits(start = 'refs/heads/master', max_count = 10, skip = 0) ⇒ Object



577
578
579
# File 'lib/rugged_adapter/git_layer_rugged.rb', line 577

def commits(start = 'refs/heads/master', max_count = 10, skip = 0)
  git.log(start, nil, :max_count => max_count, :skip => skip)
end

#configObject



565
566
567
# File 'lib/rugged_adapter/git_layer_rugged.rb', line 565

def config
  @repo.config
end

#diff(sha1, sha2, *paths) ⇒ Object



589
590
591
592
593
594
595
596
# File 'lib/rugged_adapter/git_layer_rugged.rb', line 589

def diff(sha1, sha2, *paths)
  opts = path == nil ? {} : {:path => path}
  patches = @repo.diff(sha1, sha2, opts).patches
  if not paths.empty?
    patches.keep_if { |p| paths.include? p.delta.new_file[:path] }
  end
  patches.map  {|patch| OpenStruct.new(:diff => patch.to_s.split("\n")[2..-1].join("\n").force_encoding("UTF-8"))}.reverse # First remove two superfluous lines. Rugged seems to order the diffs differently than Grit, so reverse.
end

#files_sorted_by_created_at(sha = nil) ⇒ Object



629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
# File 'lib/rugged_adapter/git_layer_rugged.rb', line 629

def files_sorted_by_created_at(sha = nil)
  sha ||= @repo.head.target.oid

  file_renamings = {}
  sorting = Rugged::SORT_DATE | Rugged::SORT_TOPO

  @repo.walk(sha, sorting).with_object([]) do |commit, files|
    parent = commit.parents.first

    diff = commit.diff(parent, reverse: true)
    diff.find_similar!
    diff.each_delta do |delta|
      name = delta.new_file[:path]

      if delta.added?
        files << (file_renamings[name] || name)
      elsif delta.renamed?
        file_renamings[delta.old_file[:path]] = file_renamings[name] || name
      end
    end
  end
end

#gitObject



569
570
571
# File 'lib/rugged_adapter/git_layer_rugged.rb', line 569

def git
  @git ||= Gollum::Git::Git.new(@repo)
end

#headObject



581
582
583
# File 'lib/rugged_adapter/git_layer_rugged.rb', line 581

def head
  Gollum::Git::Ref.new(@repo.head)
end

#indexObject



585
586
587
# File 'lib/rugged_adapter/git_layer_rugged.rb', line 585

def index
  @index ||= Gollum::Git::Index.new(@repo.index, @repo)
end

#log(commit = 'refs/heads/master', path = nil, options = {}) ⇒ Object



598
599
600
# File 'lib/rugged_adapter/git_layer_rugged.rb', line 598

def log(commit = 'refs/heads/master', path = nil, options = {})
  git.log(commit, path, options)
end

#lstree(sha, options = {}) ⇒ Object



602
603
604
605
606
607
608
609
610
611
612
# File 'lib/rugged_adapter/git_layer_rugged.rb', line 602

def lstree(sha, options = {})
  results = []
  @repo.lookup(sha).tree.walk(:postorder) do |root, entry|
    entry[:sha] = entry[:oid]
    entry[:mode] = entry[:filemode].to_s(8)
    entry[:type] = entry[:type].to_s
    entry[:path] = "#{root}#{entry[:name]}"
    results << entry
  end
  results
end

#pathObject



614
615
616
# File 'lib/rugged_adapter/git_layer_rugged.rb', line 614

def path
  @repo.path
end

#update_ref(ref, commit_sha) ⇒ Object

Checkout branch and if necessary first create it. Currently used only in gollum-lib’s tests.



619
620
621
622
623
624
625
626
627
# File 'lib/rugged_adapter/git_layer_rugged.rb', line 619

def update_ref(ref, commit_sha)
  ref = "refs/heads/#{ref}" unless ref =~ /^refs\/heads\//
  if _ref = @repo.references[ref]
    @repo.references.update(_ref, commit_sha)
  else
    @repo.create_branch(ref, commit_sha)
    @repo.checkout(ref, :strategy => :force) unless @repo.bare?
  end
end