Module: Linecounter::Git

Defined in:
lib/linecounter/git.rb

Class Method Summary collapse

Class Method Details

.churn(repo_path, file, since) ⇒ Object



17
18
19
20
21
22
# File 'lib/linecounter/git.rb', line 17

def churn(repo_path, file, since)
  cmd = ["git", "-C", repo_path, "log", "--follow", "--pretty=oneline"]
  cmd += ["--since", since] if since
  cmd += ["--", file]
  run(*cmd).lines.count
end

.parse_since(str) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/linecounter/git.rb', line 24

def parse_since(str)
  return nil if str.nil?
  return str if str.strip.empty?

  normalized = str.strip.downcase
  if (m = normalized.match(/\A(\d+)\.(days?|weeks?|months?|years?|hours?)\.ago\z/))
    count = m[1].to_i
    unit = m[2]
    seconds =
      case unit
      when "day", "days" then 86_400
      when "week", "weeks" then 7 * 86_400
      when "hour", "hours" then 3_600
      when "month", "months" then 30 * 86_400
      when "year", "years" then 365 * 86_400
      else 0
      end
    return (Time.now - (count * seconds)).utc.iso8601
  end

  case normalized
  when "today" then Time.now.utc.iso8601
  when "yesterday" then (Time.now - 86_400).utc.iso8601
  else str
  end
end

.repo?(repo_path) ⇒ Boolean

Returns:

  • (Boolean)


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

def repo?(repo_path)
  system("git", "-C", repo_path, "rev-parse", "--is-inside-work-tree", out: File::NULL, err: File::NULL)
end

.run(*cmd) ⇒ Object



8
9
10
11
# File 'lib/linecounter/git.rb', line 8

def run(*cmd)
  stdout, _, ok = Open3.capture3(*cmd)
  ok ? stdout : ""
end