class Cir::GitRepository

Class wrapping underlying Git library (rugged) and making simple certain operations that cir needs to do the most.

Public Class Methods

create(rootPath) click to toggle source

Create new git repository

# File lib/cir/git_repository.rb, line 23
def self.create(rootPath)
  raise Cir::Exception::RepositoryExists, "Path #{rootPath} already exists." if Dir.exists?(rootPath)

  # Without remote we will create blank new repository
  Rugged::Repository.init_at(rootPath)

  # And return our own wrapper on top of the underlying Rugged object
  Cir::GitRepository.new(rootPath)
end
new(rootPath) click to toggle source

Open given existing repository on disk. You might need to {#create} one if needed.

# File lib/cir/git_repository.rb, line 35
def initialize(rootPath)
  @repo = Rugged::Repository.new(rootPath)
end

Public Instance Methods

add_file(file) click to toggle source

Adds given file to index, so that it's properly tracked for next commit. This file must contain local path relative to the root of working directory.

# File lib/cir/git_repository.rb, line 42
def add_file(file)
  index = @repo.index
  index.add path: file, oid: (Rugged::Blob.from_workdir @repo, file), mode: 0100644
  index.write
end
commit(message = nil) click to toggle source

Commit all staged changes to the git repository

# File lib/cir/git_repository.rb, line 64
def commit(message = nil)
  # Write current index back to the repository
  index = @repo.index
  commit_tree = index.write_tree @repo

  # Commit message
  files = []
  index.each {|i| files << File.basename(i[:path]) }
  message = "Affected files: #{files.join(', ')}" if message.nil?

  # User handling
  user = ENV['USER']
  user = "cir-out-commit" if user.nil?

  # Commit author structure for git
  commit_author = {
    email: 'cir-auto-commit@nowhere.cz',
    name: user,
    time: Time.now
  }

  # And finally commit itself
  Rugged::Commit.create @repo,
    author: commit_author,
    committer: commit_author,
    message: message,
    parents: @repo.empty? ? [] : [ @repo.head.target ].compact,
    tree: commit_tree,
    update_ref: 'HEAD'
end
remove_file(file) click to toggle source

Remove given file from the repository. The path must have the same characteristics as it have for add_file method.

# File lib/cir/git_repository.rb, line 51
def remove_file(file)
  index = @repo.index
  index.remove file
end
repository_root() click to toggle source

Path to the root of the repository

# File lib/cir/git_repository.rb, line 58
def repository_root
  File.expand_path(@repo.path + "/../")
end