Class: Danger::DangerfileGitPlugin

Inherits:
Plugin
  • Object
show all
Defined in:
lib/danger/danger_core/plugins/dangerfile_git_plugin.rb

Overview

Handles interacting with git inside a Dangerfile. Providing access to files that have changed, and useful statistics. Also provides access to the commits in the form of [Git::Log](github.com/schacon/ruby-git/blob/master/lib/git/log.rb) objects.

Examples:

Do something to all new and edited markdown files


markdowns = (git.added_files + git.modified_files)
do_something markdowns.select{ |file| file.end_with? "md" }

Don’t allow a file to be deleted


deleted = git.deleted_files.include? "my/favourite.file"
failure "Don't delete my precious" if deleted

Fail really big diffs


failure "We cannot handle the scale of this PR" if git.lines_of_code > 50_000

Warn when there are merge commits in the diff


if git.commits.any? { |c| c.parents.count > 1 }
  warn 'Please rebase to get rid of the merge commits in this PR'
end

Warn when somebody tries to add nokogiri to the project


diff = git.diff_for_file("Gemfile.lock")
if diff && diff.patch =~ "nokogiri"
  warn 'Please do not add nokogiri to the project. Thank you.'
end

See Also:

  • danger/danger

Git Files collapse

Git Metadata collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Plugin

all_plugins, clear_external_plugins, inherited, #method_missing

Constructor Details

#initialize(dangerfile) ⇒ DangerfileGitPlugin

Returns a new instance of DangerfileGitPlugin.



47
48
49
50
51
52
# File 'lib/danger/danger_core/plugins/dangerfile_git_plugin.rb', line 47

def initialize(dangerfile)
  super(dangerfile)
  raise unless dangerfile.env.scm.class == Danger::GitRepo # rubocop:disable Style/ClassEqualityComparison

  @git = dangerfile.env.scm
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Danger::Plugin

Class Method Details

.instance_nameString

The instance name used in the Dangerfile

Returns:



43
44
45
# File 'lib/danger/danger_core/plugins/dangerfile_git_plugin.rb', line 43

def self.instance_name
  "git"
end

Instance Method Details

#added_filesFileList<String>

Paths for files that were added during the diff

Returns:



58
59
60
# File 'lib/danger/danger_core/plugins/dangerfile_git_plugin.rb', line 58

def added_files
  Danger::FileList.new(@git.diff.select { |diff| diff.type == "new" }.map(&:path))
end

#commitsGit::Log

The log of commits inside the diff

Returns:

  • (Git::Log)

    from the gem ‘git`



122
123
124
# File 'lib/danger/danger_core/plugins/dangerfile_git_plugin.rb', line 122

def commits
  @git.log.to_a
end

#deleted_filesFileList<String>

Paths for files that were removed during the diff

Returns:



66
67
68
# File 'lib/danger/danger_core/plugins/dangerfile_git_plugin.rb', line 66

def deleted_files
  Danger::FileList.new(@git.diff.select { |diff| diff.type == "deleted" }.map(&:path))
end

#deletionsFixnum

The overall lines of code removed in the diff

Returns:

  • (Fixnum)


106
107
108
# File 'lib/danger/danger_core/plugins/dangerfile_git_plugin.rb', line 106

def deletions
  @git.diff.deletions
end

#diffGit::Diff

Whole diff

Returns:

  • (Git::Diff)

    from the gem ‘git`



90
91
92
# File 'lib/danger/danger_core/plugins/dangerfile_git_plugin.rb', line 90

def diff
  @git.diff
end

#diff_for_file(file) ⇒ Git::Diff::DiffFile

Details for a specific file in this diff

Returns:

  • (Git::Diff::DiffFile)

    from the gem ‘git`



130
131
132
# File 'lib/danger/danger_core/plugins/dangerfile_git_plugin.rb', line 130

def diff_for_file(file)
  (added_files + modified_files + deleted_files).include?(file) ? @git.diff[file] : nil
end

#info_for_file(file) ⇒ Hash

Statistics for a specific file in this diff

Returns:

  • (Hash)

    with keys ‘:insertions`, `:deletions` giving line counts, and `:before`, `:after` giving file contents, or nil if the file has no changes or does not exist



138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/danger/danger_core/plugins/dangerfile_git_plugin.rb', line 138

def info_for_file(file)
  return nil unless modified_files.include?(file) || added_files.include?(file) || deleted_files.include?(file)

  stats = @git.diff.stats[:files][file]
  diff = @git.diff[file]
  {
    insertions: stats[:insertions],
    deletions: stats[:deletions],
    before: added_files.include?(file) || deleted_files.include?(file) ? nil : diff.blob(:src).contents,
    after: added_files.include?(file) || deleted_files.include?(file) ? nil : diff.blob(:dst).contents
  }
end

#insertionsFixnum

The overall lines of code added in the diff

Returns:

  • (Fixnum)


114
115
116
# File 'lib/danger/danger_core/plugins/dangerfile_git_plugin.rb', line 114

def insertions
  @git.diff.insertions
end

#lines_of_codeFixnum

The overall lines of code added/removed in the diff

Returns:

  • (Fixnum)


98
99
100
# File 'lib/danger/danger_core/plugins/dangerfile_git_plugin.rb', line 98

def lines_of_code
  @git.diff.lines
end

#modified_filesFileList<String>

Paths for files that changed during the diff

Returns:



74
75
76
# File 'lib/danger/danger_core/plugins/dangerfile_git_plugin.rb', line 74

def modified_files
  Danger::FileList.new(@git.diff.select { |diff| diff.type == "modified" }.map(&:path))
end

#renamed_filesArray<Hash>

List of renamed files

Returns:

  • (Array<Hash>)

    with keys ‘:before` and `:after`



82
83
84
# File 'lib/danger/danger_core/plugins/dangerfile_git_plugin.rb', line 82

def renamed_files
  @git.renamed_files
end

#tagsString

List of remote tags

Returns:



155
156
157
# File 'lib/danger/danger_core/plugins/dangerfile_git_plugin.rb', line 155

def tags
  @git.tags.each_line
end