Sha256: edc15fcc231d7d53ff3e939f8324458ead219fb0085726b228c7ae1f6de97dca

Contents?: true

Size: 1.86 KB

Versions: 10

Compression:

Stored size: 1.86 KB

Contents

require 'pathname'

module Pronto
  module Git
    class Repository
      def initialize(path)
        @repo = Rugged::Repository.new(path)
      end

      def diff(commit, options = nil)
        if commit == :index
          patches = @repo.index.diff(options)
          Patches.new(self, head, patches)
        else
          merge_base = merge_base(commit)
          patches = @repo.diff(merge_base, head, options)
          Patches.new(self, merge_base, patches)
        end
      end

      def show_commit(sha)
        return empty_patches(sha) unless sha

        commit = @repo.lookup(sha)
        return empty_patches(sha) if commit.parents.count != 1

        # TODO: Rugged does not seem to support diffing against multiple parents
        diff = commit.diff(reverse: true)
        return empty_patches(sha) if diff.nil?

        Patches.new(self, sha, diff.patches)
      end

      def commits_until(sha)
        result = []
        @repo.walk(head, Rugged::SORT_TOPO).take_while do |commit|
          result << commit.oid
          !commit.oid.start_with?(sha)
        end
        result
      end

      def path
        Pathname.new(@repo.path).parent
      end

      def blame(path, lineno)
        Rugged::Blame.new(@repo, path, min_line: lineno, max_line: lineno,
                                       track_copies_same_file: true,
                                       track_copies_any_commit_copies: true)[0]
      end

      def branch
        @repo.head.name.sub('refs/heads/', '') if @repo.head.branch?
      end

      def remote_urls
        @repo.remotes.map(&:url)
      end

      def head_commit_sha
        head.oid
      end

      private

      def empty_patches(sha)
        Patches.new(self, sha, [])
      end

      def merge_base(commit)
        @repo.merge_base(commit, head)
      end

      def head
        @repo.head.target
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
pronto-0.8.2 lib/pronto/git/repository.rb
pronto-0.8.1 lib/pronto/git/repository.rb
pronto-0.8.0 lib/pronto/git/repository.rb
pronto-0.7.1 lib/pronto/git/repository.rb
pronto-0.7.0 lib/pronto/git/repository.rb
pronto-0.6.0 lib/pronto/git/repository.rb
pronto-0.5.3 lib/pronto/git/repository.rb
pronto-0.5.2 lib/pronto/git/repository.rb
pronto-0.5.1 lib/pronto/git/repository.rb
pronto-0.5.0 lib/pronto/git/repository.rb