Class: Giblish::GitItf

Inherits:
Object
  • Object
show all
Defined in:
lib/giblish/gitrepos/gititf.rb

Overview

A home-grown interface class to git. Used for situations when the ‘official’ ruby git gem does not support an operation that is needed.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ GitItf

Returns a new instance of GitItf.



10
11
12
13
14
15
# File 'lib/giblish/gitrepos/gititf.rb', line 10

def initialize(path)
  @repo_root = GitItf.find_gitrepo_root(path)
  raise ArgumentError("The path: @{path} is not within a git repo!") if @repo_root.nil?

  @git_dir = @repo_root / ".git"
end

Instance Attribute Details

#git_dirObject (readonly)

Returns the value of attribute git_dir.



8
9
10
# File 'lib/giblish/gitrepos/gititf.rb', line 8

def git_dir
  @git_dir
end

#repo_rootObject (readonly)

Returns the value of attribute repo_root.



8
9
10
# File 'lib/giblish/gitrepos/gititf.rb', line 8

def repo_root
  @repo_root
end

Class Method Details

.find_gitrepo_root(dirpath) ⇒ Object

Find the root directory of the git repo in which the given dirpath resides.

dirpath

an absolute path to a directory that resides within a git repo.

returns

the root direcotry of the git repo or nil if the input path

does not reside within a git repo.


25
26
27
28
29
30
31
32
# File 'lib/giblish/gitrepos/gititf.rb', line 25

def self.find_gitrepo_root(dirpath)
  Pathname.new(dirpath).ascend do |p|
    next unless p.exist?

    git_dir = p.realpath.join(".git")
    return p if git_dir.directory?
  end
end

Instance Method Details

#current_branchObject



50
51
52
53
54
55
56
# File 'lib/giblish/gitrepos/gititf.rb', line 50

def current_branch
  # git rev-parse --abbrev-ref HEAD
  o, e, s = exec_cmd("rev-parse", %w[--abbrev-ref HEAD], "")
  raise "Failed to get git log for #{filename}!!\n#{e}" if s.exitstatus != 0

  o.strip
end

#file_log(filename) ⇒ Object

Get the log history of the supplied file as an array of hashes, each entry has keys:

sha date author email parent message



43
44
45
46
47
48
# File 'lib/giblish/gitrepos/gititf.rb', line 43

def file_log(filename)
  o, e, s = exec_cmd("log", %w[--follow --date=iso --], "'#{filename}'")
  raise "Failed to get git log for #{filename}!!\n#{e}" if s.exitstatus != 0

  process_log_output(o)
end