Class: Cir::GitRepository
- Inherits:
-
Object
- Object
- Cir::GitRepository
- Defined in:
- lib/cir/git_repository.rb
Overview
Class wrapping underlying Git library (rugged) and making simple certain operations that cir needs to do the most.
Class Method Summary collapse
-
.create(rootPath) ⇒ Object
Create new git repository.
Instance Method Summary collapse
-
#add_file(file) ⇒ Object
Adds given file to index, so that it’s properly tracked for next commit.
-
#commit(message = nil) ⇒ Object
Commit all staged changes to the git repository.
-
#initialize(rootPath) ⇒ GitRepository
constructor
Open given existing repository on disk.
-
#remove_file(file) ⇒ Object
Remove given file from the repository.
-
#repository_root ⇒ Object
Path to the root of the repository.
Constructor Details
#initialize(rootPath) ⇒ GitRepository
Open given existing repository on disk. You might need to #create one if needed.
35 36 37 |
# File 'lib/cir/git_repository.rb', line 35 def initialize(rootPath) @repo = Rugged::Repository.new(rootPath) end |
Class Method Details
.create(rootPath) ⇒ Object
Create new git repository
23 24 25 26 27 28 29 30 31 |
# 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 |
Instance Method Details
#add_file(file) ⇒ Object
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.
42 43 44 45 46 |
# 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) ⇒ Object
Commit all staged changes to the git repository
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/cir/git_repository.rb', line 64 def commit( = 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]) } = "Affected files: #{files.join(', ')}" if .nil? # User handling user = ENV['USER'] user = "cir-out-commit" if user.nil? # Commit author structure for git = { email: '[email protected]', name: user, time: Time.now } # And finally commit itself Rugged::Commit.create @repo, author: , committer: , message: , parents: @repo.empty? ? [] : [ @repo.head.target ].compact, tree: commit_tree, update_ref: 'HEAD' end |
#remove_file(file) ⇒ Object
Remove given file from the repository. The path must have the same characteristics as it have for #add_file method.
51 52 53 54 |
# File 'lib/cir/git_repository.rb', line 51 def remove_file(file) index = @repo.index index.remove file end |
#repository_root ⇒ Object
Path to the root of the repository
58 59 60 |
# File 'lib/cir/git_repository.rb', line 58 def repository_root File.(@repo.path + "/../") end |