Class: Ginbeer

Inherits:
Object
  • Object
show all
Defined in:
lib/ginbeer/ginbeer.rb,
lib/ginbeer/version.rb

Constant Summary collapse

VERSION =
"0.0.2"

Instance Method Summary collapse

Constructor Details

#initialize(dir, from = "", to = Time.now) ⇒ Ginbeer

Returns a new instance of Ginbeer.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/ginbeer/ginbeer.rb', line 5

def initialize(dir, from="", to=Time.now)
  @dir = dir
  @from = from
  @from_d = nil
  if @from != ""
    @from_d = Time.parse(@from)
  end
  @to = to


  # error handling
  epath = File.expand_path(dir)
  if File.exist?(File.join(epath, '.git'))
  elsif File.exist?(epath) && (epath =~ /\.git$/)
  elsif File.exist?(epath)
    raise InvalidGitRepositoryError.new(epath)
  else
    raise NoSuchPathError.new(epath)
  end
end

Instance Method Details

#authorsObject



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
57
58
# File 'lib/ginbeer/ginbeer.rb', line 26

def authors
  authors = []
  commits = `#{self.shortlog_command}` 
  commits.each_line do |line|
    line.chomp!
    /\s*(\d+)\s(.+)/ =~ line

    count = $1.to_i
    name = $2
    authors.push(Author.new(name, count))
  end

  authors.each do |author|
    logs = `#{self.log_command(author)}`

    logs.each_line do |line|
      line.chomp!
      if /file/ =~ line
        /.*(\d+) insertion.*, (\d+) deletion/ =~ line
        insertion = $1.to_i
        deletion = $2.to_i

        author.insertion += insertion
        author.deletion += deletion
      elsif /\[(.+)\]/ =~ line
        data = $1 
        data = Time.parse(data)
        break if @from_d != nil && data < @from_d # 終了
      end
    end
  end
  return authors
end

#cd_commandObject



60
61
62
# File 'lib/ginbeer/ginbeer.rb', line 60

def cd_command
  return "cd #{@dir} &&"
end

#log_command(author) ⇒ Object



70
71
72
73
# File 'lib/ginbeer/ginbeer.rb', line 70

def log_command(author)
  command = "#{self.cd_command} git log --pretty=format:'[%ad]' --shortstat --author='#{author.name}' --date=short --before=#{@to}"
  return command
end

#shortlog_commandObject



64
65
66
67
68
# File 'lib/ginbeer/ginbeer.rb', line 64

def shortlog_command
  command = "#{self.cd_command} git shortlog -s --no-merges -n --before=#{@to}"
  command += " --after=#{@from}" if @from != ""
  return command
end