Module: Gpr::GitHelper

Defined in:
lib/gpr/git_helper.rb

Class Method Summary collapse

Class Method Details

.clone(url, path) ⇒ Object



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

def clone(url, path)
  `git clone #{url} #{path}`
end

.fetch(path, remote, branch) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/gpr/git_helper.rb', line 17

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



58
59
60
61
62
63
64
65
# File 'lib/gpr/git_helper.rb', line 58

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



12
13
14
15
# File 'lib/gpr/git_helper.rb', line 12

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

.status(path) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/gpr/git_helper.rb', line 26

def status(path)
  Dir.chdir(path)
  result = {}
  git_status = `git status`
  branch_status = `git status -sb`.split("\n")[0].match(/## (.+)/)[1]

  case branch_status
  # Means ahead || behind
  when /\[.+\]/
    result[:branch] = branch_status.color(:red)
  when /master/
    result[:branch] = git_status.match(/Your branch (is|and) (.+?)(,|\.)/)[2].color(:green)
  else
    result[:branch] = git_status.match(/(On branch .+)/)[1].color(:green)
  end

  case git_status
  when /nothing to commit, working directory clean/
    result[:directory] = 'Working directory clean'.color(:green)

  when /Changes to be committed:/
    result[:directory] = 'Exist staged changes'.color(:red)

  when /Changes not staged for commit:/
    result[:directory] = 'Exist unstaged changes'.color(:red)

  when /Untracked files:/
    result[:directory] = 'Exist untracked files'.color(:red)
  end
  result
end