Sha256: e727ae9bea464873ef69e9bd5afe665944b1e63d0657ce60707df4923e5eeba4

Contents?: true

Size: 1.9 KB

Versions: 5

Compression:

Stored size: 1.9 KB

Contents

# -*- encoding : utf-8 -*-
module Lolcommits
  class GitInfo
    include Methadone::CLILogging
    attr_accessor :sha, :message, :repo_internal_path, :repo, :url,
                  :author_name, :author_email, :branch

    GIT_URL_REGEX = %r{.*[:]([\/\w\-]*).git}

    def initialize
      debug 'GitInfo: parsed the following values from commit:'
      debug "GitInfo: \t#{message}"
      debug "GitInfo: \t#{sha}"
      debug "GitInfo: \t#{repo_internal_path}"
      debug "GitInfo: \t#{repo}"
      debug "GitInfo: \t#{branch}"
      debug "GitInfo: \t#{author_name}" if author_name
      debug "GitInfo: \t#{author_email}" if author_email
    end

    def branch
      @branch ||= repository.current_branch
    end

    def message
      @message ||= begin
        message = last_commit.message || ''
        message.split("\n").first
      end
    end

    def sha
      @sha ||= last_commit.sha[0..10]
    end

    def repo_internal_path
      @repo_internal_path ||= repository.repo.path
    end

    def url
      @url ||= begin
        remote_https_url(repository.remote.url) if repository.remote
      end
    end

    def repo
      @repo ||= begin
        if repository.remote && repository.remote.url
          match = repository.remote.url.match(GIT_URL_REGEX)
        end

        if match
          match[1]
        elsif !repository.repo.path.empty?
          repository.repo.path.split(File::SEPARATOR)[-2]
        end
      end
    end

    def author_name
      @author_name ||= last_commit.author.name if last_commit.author
    end

    def author_email
      @author_email ||= last_commit.author.email if last_commit.author
    end

    private

    def remote_https_url(url)
      url.gsub(':', '/').gsub(/^git@/, 'https://').gsub(/\.git$/, '') + '/commit/'
    end

    def repository(path = '.')
      @repository ||= Git.open(path)
    end

    def last_commit
      @commit ||= repository.log.first
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
lolcommits-0.6.0 lib/lolcommits/git_info.rb
lolcommits-0.5.9 lib/lolcommits/git_info.rb
lolcommits-0.5.9.pre1 lib/lolcommits/git_info.rb
lolcommits-0.5.8 lib/lolcommits/git_info.rb
lolcommits-0.5.8.pre1 lib/lolcommits/git_info.rb