Class: Vcs2Json::Git

Inherits:
Object
  • Object
show all
Defined in:
lib/vcs2json/git.rb

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Git

Returns a new instance of Git.



5
6
7
# File 'lib/vcs2json/git.rb', line 5

def initialize(opts)
  @opts = opts
end

Instance Method Details

#executeObject



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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/vcs2json/git.rb', line 33

def execute
  before = @opts[:before].nil? ? '' : "--before=\"#{@opts[:before]}\""
  after = @opts[:after].nil?   ? '' : "--after=\"#{@opts[:after]}\""
  number = @opts[:number].nil? ? '' : "-n #{@opts[:number]}"
  options = "#{before} #{after} #{number} --no-merges"

  # Generate separators between fields and commits
  field_sep = Digest::SHA256.hexdigest Time.new.to_s + "field_sep"
  commit_sep = Digest::SHA256.hexdigest Time.new.to_s + "commit_sep"

  # Create a new hash that defaults to creating new hashes given hash[:key]
  # so we can do 'commit[:commit][:author][:name] = .. ' without creating the :commit and :author hashes first
  commits = Hash.new {|h,k| h[k] = Hash.new(&h.default_proc) }

  #TODO: log this =>  print "Getting metadata on the commits..\r"
  raw_commits = `git log #{options} --pretty=format:'%H#{field_sep}%cn#{field_sep}%ce#{field_sep}%cd#{field_sep}%ad#{field_sep}%B#{commit_sep}'`
  commits_info = raw_commits.encode('UTF-8', :invalid => :replace).split(commit_sep)

  commits_info.each do |commit|
    fields = commit.split(field_sep)
    sha = fields[0].delete("\n") #remove astray newlines
    commits[sha][:sha]            = sha
    commits[sha][:name]           = fields[1]
    commits[sha][:email]          = fields[2]
    commits[sha][:date]           = Time.parse fields[3]
    commits[sha][:author_date]    = Time.parse fields[4]
    commits[sha][:message]        = fields[5]

    # attempt to parse an issue id from the commit message
    if @opts.issue?
      commits[commit[0]][:issue] = parse_issue(commits[sha][:message])
    end
  end


  commits_changes_type = `git log --pretty=format:'#{field_sep}%H' --name-status #{options}`.split(field_sep)
  commits_changes_type.each do |commit|
    if !commit.empty?
      lines = commit.split("\n")
      sha = lines[0]
      commits[sha][:changes][:all] = []
      if lines.size > 1
        lines[1..-1].each do |line|
          if !line.empty?
            file_info = line.split("\t")
            file_name = file_info[1]
            status = file_info[0]
            commits[sha][:changes][:all] << file_name
            commits[sha][:changes][:details][file_name][:filename] = file_name
            commits[sha][:changes][:details][file_name][:status] = parse_status(status)
          end
        end
      end
    end
  end

  # create file_name -> integer mapping
  mapping = Hash.new
  index_counter = 0
  commits.each do |sha,info|
    integer_representation = []
    info[:changes][:all].each do |file|
      if mapping[file].nil?
        mapping[file] = index_counter
        index_counter += 1
      end
      integer_representation << mapping[file]
      info[:changes][:details][file][:id] = mapping[file]
    end
    info[:changes][:all].clear
    info[:changes][:all] = integer_representation
  end

  JSON.pretty_generate(commits.sort_by {|id,commit| commit[:date]}.reverse.map {|(sha,commit)| commit})
end

#parse_issue(message) ⇒ Object

attempts to parse an issue/bug id from the given commit message



24
25
26
27
28
29
30
# File 'lib/vcs2json/git.rb', line 24

def parse_issue(message)
  if match = /(bug|issue) (?<id>\d+)/i.match(message)
    return match[:id]
  else
    return ""
  end
end

#parse_status(abbreviated_status) ⇒ Object

simply un-abbreviates the status code given by –name-status



11
12
13
14
15
16
17
18
19
20
# File 'lib/vcs2json/git.rb', line 11

def parse_status(abbreviated_status)
  case abbreviated_status
  when "A"
    "added"
  when "M"
    "modified"
  when "D"
    "deleted"
  end
end