Sha256: 263cabd8b119d10f1256aaf3e8c1ef24dd9414cb4c6548afdffbf73584e6cc77

Contents?: true

Size: 1.77 KB

Versions: 1

Compression:

Stored size: 1.77 KB

Contents

module Kosmos
  module GitAdapter
    class << self
      def init_repo(path)
        Dir.chdir(path) do
          `git init`
          `git config user.name Kosmos`
          `git config user.email kosmos@kosmos.kosmos`
          `git config core.autocrlf false`

          File.open('.gitignore', 'w') do |file|
            file.write "!*\n"
          end
        end
      end

      def commit_everything(repo_path, commit_message)
        Dir.chdir(repo_path) do
          `git add -A -f`
          `git commit --allow-empty -m "#{commit_message}"`
        end
      end

      def revert_commit(repo_path, commit)
        Dir.chdir(repo_path) do
          # Favor "ours" (which is always HEAD for our purposes) when git-revert
          # can handle that on its own.
          `git revert --no-commit --strategy=merge --strategy-option=ours #{commit.sha}`

          # When files are being created or deleted, git will not do anything.
          # In this case, keep all files alive; better to accidentally pollute
          # than accidentally delete something important.
          `git add *`
        end
      end

      def list_commits(repo_path)
        Dir.chdir(repo_path) do
          `git log --oneline`.lines.map do |line|
            sha, message = line.split(' ', 2)
            Commit.new(message, sha)
          end
        end
      end
    end

    class Commit < Struct.new(:message, :sha)
      def pre?
        type == :pre
      end

      def post?
        type == :post
      end

      def uninstall?
        type == :uninstall
      end

      def type
        # "POST: Example" --> :post
        message.split(':').first.downcase.to_sym
      end

      def subject
        # "POST: Example\n" --> "Example"
        message.split(' ', 2).last.strip
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
kosmos-0.0.5 lib/kosmos/git_adapter.rb