Sha256: fbcf23529be0db1d75dae2fe2a5ce7284f1851f108876c5e6ca0f3f89b7aaf90

Contents?: true

Size: 1.25 KB

Versions: 1

Compression:

Stored size: 1.25 KB

Contents

require 'rugged'

require 'method_log/source_file'

module MethodLog
  class Commit
    attr_reader :sha

    def initialize(repository: repository, sha: sha)
      @repository = repository
      @sha = sha
      @index = Rugged::Index.new
    end

    def add(source_file)
      oid = @repository.write(source_file.source, :blob)
      @index.add(path: source_file.path, oid: oid, mode: 0100644)
    end

    def apply
      tree = @index.write_tree(@repository)
      parents = @repository.empty? ? [] : [@repository.head.target].compact
      user = { email: 'test@example.com', name: 'test', time: Time.now }
      @sha = Rugged::Commit.create(@repository, tree: tree, parents: parents, update_ref: 'HEAD', author: user, committer: user, message: 'commit-message')
    end

    def source_files
      commit = @repository.lookup(sha)
      source_files = []
      commit.tree.walk_blobs do |root, blob_hash|
        path = root.empty? ? blob_hash[:name] : File.join(root, blob_hash[:name])
        source = @repository.lookup(blob_hash[:oid]).text
        source_files << SourceFile.new(path: path, source: source)
      end
      source_files
    end

    def ==(other)
      sha == other.sha
    end

    def hash
      sha.hash
    end

    def to_s
      sha
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
method_log-0.0.1 lib/method_log/commit.rb