Module: Gpr::GitHelper

Defined in:
lib/gpr/git_helper.rb

Class Method Summary collapse

Class Method Details

.fetch(path, remote, branch) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/gpr/git_helper.rb', line 54

def fetch(path, remote, branch)
  Dir.chdir(path)
  if branch.nil?
    `git fetch #{remote}`
  else
    `git fetch #{remote} #{branch}`
  end
end

.log_by_date(path, user = nil) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/gpr/git_helper.rb', line 45

def log_by_date(path, user = nil)
  Dir.chdir(path)
  if user.nil?
    `git log --date=short --pretty=format:"%cd"`.split("\n")
  else
    `git log --author=#{user} --date=short --pretty=format:"%cd"`.split("\n")
  end
end

.ls_files(path) ⇒ Object



8
9
10
11
# File 'lib/gpr/git_helper.rb', line 8

def ls_files(path)
  Dir.chdir(path)
  `git ls-files`.split("\n")
end

.status(path) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/gpr/git_helper.rb', line 13

def status(path)
  Dir.chdir(path)
  result = {}
  git_status = `git status -sb`.split("\n")
  branch_name = git_status.shift.match(/## (?<branch>.+)/)[:branch]
  unstaged_changes = []
  untracked_files = []

  git_status.each do |file|
    if match_object = file.match(/^\sM (.+)$/)
      unstaged_changes << match_object[1]
    elsif match_object = file.match(/^\?\? (.+)$/)
      untracked_files << match_object[1]
    end
  end

  # Means no branch || ahead || behind
  if branch_name =~ /HEAD|\[.+\]/
    result[:branch] = branch_name.color(:red)
  else
    result[:branch] = branch_name.color(:green)
  end

  if unstaged_changes.empty? && untracked_files.empty?
    result[:directory] = 'Working directory clean'.color(:green)
  else
    result[:directory] = "Modified[#{unstaged_changes.size}] ??[#{untracked_files.size}]".color(:red)
  end

  result
end