Class: GitStatistics::DiffSummary

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/git_statistics/diff_summary.rb

Instance Method Summary collapse

Constructor Details

#initialize(repo, patch) ⇒ DiffSummary

Returns a new instance of DiffSummary.



3
4
5
6
# File 'lib/git_statistics/diff_summary.rb', line 3

def initialize(repo, patch)
  @repo = repo
  super(patch)
end

Instance Method Details

#additionsObject

We flip these around since we are diffing in the opposite direction – new.diff(old)



9
10
11
# File 'lib/git_statistics/diff_summary.rb', line 9

def additions
  __getobj__.deletions
end

#blobObject

We flip these around since we are diffing in the opposite direction – new.diff(old)



46
47
48
49
50
51
52
53
54
55
# File 'lib/git_statistics/diff_summary.rb', line 46

def blob
  if status == :deleted
    @repo.lookup(delta.new_file[:oid])  # Look at new instead of old
  else
    @repo.lookup(delta.old_file[:oid])  # Look at old instead of new
  end
rescue Rugged::OdbError
  Log.warn 'Could not find object (most likely a submodule)'
  nil
end

#deletionsObject

We flip these around since we are diffing in the opposite direction – new.diff(old)



14
15
16
# File 'lib/git_statistics/diff_summary.rb', line 14

def deletions
  __getobj__.additions
end

#filenameObject



37
38
39
40
41
42
43
# File 'lib/git_statistics/diff_summary.rb', line 37

def filename
  if status == :deleted
    delta.old_file[:path]
  else
    delta.new_file[:path]
  end
end

#inspectObject



57
58
59
# File 'lib/git_statistics/diff_summary.rb', line 57

def inspect
  "<GitStatistics::FileStat @filename=#{filename} @status=#{status} @similarity=#{similarity} @language=#{language} @additions=#{additions}, @deletions=#{deletions}, @net=#{net}>"
end

#languageObject

Determine the language of the file from the blob



66
67
68
69
70
71
72
73
# File 'lib/git_statistics/diff_summary.rb', line 66

def language
  language = 'Unknown'
  unless blob.nil?
    detected_language = LanguageSniffer.detect(filename, content: blob.content).language
    language = detected_language.name unless detected_language.nil?
  end
  language
end

#netObject



18
19
20
# File 'lib/git_statistics/diff_summary.rb', line 18

def net
  additions - deletions
end

#similarityObject



33
34
35
# File 'lib/git_statistics/diff_summary.rb', line 33

def similarity
  delta.similarity
end

#statusObject

We flip these around since we are diffing in the opposite direction – new.diff(old)



23
24
25
26
27
28
29
30
31
# File 'lib/git_statistics/diff_summary.rb', line 23

def status
  if delta.status == :deleted
    return :added
  elsif delta.status == :added
    return :deleted
  else
    return delta.status
  end
end

#to_jsonObject



61
62
63
# File 'lib/git_statistics/diff_summary.rb', line 61

def to_json
  { filename: filename, status: status, similarity: similarity, language: language, additions: additions, deletions: deletions, net: net }
end