class ReaPack::Index::Git

Public Class Methods

new(path) click to toggle source
# File lib/reapack/index/git.rb, line 3
def initialize(path)
  @repo = Rugged::Repository.discover path.encode(Encoding::UTF_8)

  if @repo.bare?
    raise ReaPack::Index::Error,
      'reapack-index cannot be run in a repository without a work tree'
  end
end

Public Instance Methods

commits() click to toggle source
# File lib/reapack/index/git.rb, line 21
def commits
  @commits ||= commits_since nil
end
commits_since(sha) click to toggle source
# File lib/reapack/index/git.rb, line 25
def commits_since(sha)
  return [] if empty?

  walker = Rugged::Walker.new @repo
  walker.sorting Rugged::SORT_TOPO | Rugged::SORT_REVERSE
  walker.hide sha if fetch_commit sha
  walker.push @repo.head.target_id

  walker.map {|c| Commit.new c, @repo }
end
create_commit(message, files) click to toggle source
# File lib/reapack/index/git.rb, line 88
def create_commit(message, files)
  old_index = @repo.index
  target = empty? ? nil : @repo.head.target

  if target
    old_index.read_tree target.tree
  else
    old_index.clear
  end

  new_index = @repo.index
  files.each {|f|
    if File.exist? f
      new_index.add relative_path(f)
    else
      new_index.remove relative_path(f)
    end
  }

  hash = Rugged::Commit.create @repo,
    tree: new_index.write_tree(@repo),
    message: message,
    parents: [target].compact,
    update_ref: 'HEAD'

  old_index.write

  # force-reload the repository
  @repo = Rugged::Repository.discover path

  commit = get_commit hash
  @commits << commit if @commits
  commit
end
empty?() click to toggle source
# File lib/reapack/index/git.rb, line 12
def empty?
  # head_unborn = orphan branch – FIXME: add test for this case
  @repo.empty? || @repo.head_unborn?
end
get_commit(sha) click to toggle source
# File lib/reapack/index/git.rb, line 36
def get_commit(sha)
  c = fetch_commit sha
  Commit.new c, @repo if c
end
guess_url_template() click to toggle source
# File lib/reapack/index/git.rb, line 66
def guess_url_template
  remote = @repo.remotes['origin']
  return unless remote

  uri = Gitable::URI.parse remote.url
  return unless uri.path =~ /\A\/?(?<user>[^\/]+)\/(?<repo>[^\/]+)(\.git|\/)?\Z/

  tpl = uri.to_web_uri
  tpl.path = tpl.path.chomp '/'
  tpl.path += '/raw/$commit/$path'

  tpl.to_s
end
last_commit() click to toggle source
# File lib/reapack/index/git.rb, line 41
def last_commit
  c = @repo.last_commit
  Commit.new c, @repo if c
end
last_commit_for(file) click to toggle source
# File lib/reapack/index/git.rb, line 46
def last_commit_for(file)
  commits.reverse_each.find {|c|
    c.each_diff.any? {|d| d.file == file }
  }
end
last_commits_for(pattern) click to toggle source
# File lib/reapack/index/git.rb, line 52
def last_commits_for(pattern)
  dir = pattern.empty? ? '' : pattern + '/'
  out = Hash.new
  last_commit.filelist.each {|file|
    path = File.split(file).first + '/'
    next unless path.start_with?(dir) || file == pattern

    commit = last_commit_for(file)
    out[commit] ||= Array.new
    out[commit] << file
  }
  out
end
path() click to toggle source
# File lib/reapack/index/git.rb, line 17
def path
  @path ||= File.expand_path @repo.workdir
end
relative_path(path) click to toggle source
# File lib/reapack/index/git.rb, line 80
def relative_path(path)
  root = Pathname.new self.path
  file = Pathname.new File.expand_path(path)

  rel = file.relative_path_from(root).to_s
  rel == '.' ? '' : rel
end

Private Instance Methods

fetch_commit(sha) click to toggle source
# File lib/reapack/index/git.rb, line 124
def fetch_commit(sha)
  if sha && sha.size.between?(7, 40) && @repo.include?(sha)
    object = @repo.lookup sha
    object if object.is_a? Rugged::Commit
  end
rescue Rugged::InvalidError
  nil
end