Class: GitWrapper

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/git_wrapper.rb

Instance Method Summary collapse

Instance Method Details

#[](key) ⇒ Object



50
51
52
# File 'lib/git_wrapper.rb', line 50

def [](key)
  to_hash[key]
end

#last_commitObject



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/git_wrapper.rb', line 21

def last_commit
  @last_commit ||= begin
    if repository?
      OpenStruct.new(
        author: `git show -s --format='format:%an <%ae>'`.strip,
        message: `git show -s --format='%s'`.strip,
        authored_date: Time.at(`git show -s --format='format:%at'`.to_i)
      )
    else
      OpenStruct.new()
    end
  end
end

#last_commit_hashObject



17
18
19
# File 'lib/git_wrapper.rb', line 17

def last_commit_hash
  @last_commit_hash ||= `git rev-parse HEAD`.strip
end

#repositoryObject



9
10
11
# File 'lib/git_wrapper.rb', line 9

def repository
  @repository = (Dir.new('./.git') rescue :invalid)
end

#repository?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/git_wrapper.rb', line 13

def repository?
  !repository.nil? && repository != :invalid
end

#to_hashObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/git_wrapper.rb', line 35

def to_hash
  return {
    :branch => 'Not currently on a branch.',
    :commit => (File.read("REVISION").strip rescue nil)
  } unless repository?
  
  @hash ||= {
    :branch => `git rev-parse --abbrev-ref HEAD`.strip,
    :commit => last_commit_hash,
    :last_author => last_commit.author,
    :message => last_commit.message,
    :date => last_commit.authored_date
  }
end