class Leg::Commands::Repo

Public Class Methods

name() click to toggle source
# File lib/leg/commands/repo.rb, line 2
def self.name
  "repo"
end
summary() click to toggle source
# File lib/leg/commands/repo.rb, line 6
def self.summary
  "Convert steps folder into a version controlled repository"
end

Public Instance Methods

run() click to toggle source
# File lib/leg/commands/repo.rb, line 10
def run
  needs! :config, :steps_folder, :steps, not: :repo

  FileUtils.cd(@config[:path])

  FileUtils.mkdir("repo")
  repo = Rugged::Repository.init_at("repo")

  steps.each do |step|
    index = repo.index
    index.read_tree(repo.head.target.tree) unless repo.empty?

    FileUtils.cd(step_path(step)) do
      Dir["**/*"].each do |path|
        unless File.directory?(path)
          oid = repo.write(File.read(path), :blob)
          index.add(path: path, oid: oid, mode: 0100644)
        end
      end
    end

    options = {}
    options[:tree] = index.write_tree(repo)
    options[:message] = step_name(step) || "-"
    options[:parents] = repo.empty? ? [] : [repo.head.target]
    options[:update_ref] = 'HEAD'

    Rugged::Commit.create(repo, options)
  end

  repo.checkout_head(strategy: :force)
end