Sha256: 3f51eee67967ad34b6e34f3430fada7d7eed9db8eaaa3f082bceb9e584eb5ec3

Contents?: true

Size: 976 Bytes

Versions: 2

Compression:

Stored size: 976 Bytes

Contents

# frozen_string_literal: true

require 'fileutils'
require 'rugged'

module GitMQ
  class Storage
    attr_reader :repo, :path

    def initialize(path)
      @path = path
      @repo = read_or_create_repo
    end

    def branches
      repo.branches
    end

    def branch(name)
      branches[name]
    end

    def tree
      Rugged::Tree::Builder.new(repo).write
    end

    def commits(branch, tag)
      walker = Rugged::Walker.new(repo)
      walker.sorting(Rugged::SORT_TOPO | Rugged::SORT_REVERSE)
      walker.push(branch(branch).target)

      tag = repo.tags[tag]
      walker.hide(tag.target) if tag
      walker
    end

    def tag(label, commit)
      repo.tags.create(label, commit, true)
    end

    private

    def read_or_create_repo
      FileUtils.mkdir_p(path) unless File.exist?(path)
      begin
        Rugged::Repository.new(path)
      rescue Rugged::RepositoryError
        Rugged::Repository.init_at(path, :bare)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
gitmq-0.1.3 lib/storage.rb
gitmq-0.1.2 lib/storage.rb