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"
fail "Don't delete my precious" if deleted

Fail really big diffs


fail "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 commits.any? { |c| c.message =~ /^Merge branch 'master'/ }
   warn 'Please rebase to get rid of the merge commits in this PR'
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.



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

def initialize(dangerfile)
  super(dangerfile)
  raise  unless dangerfile.env.scm.class == Danger::GitRepo

  @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:



37
38
39
# File 'lib/danger/danger_core/plugins/dangerfile_git_plugin.rb', line 37

def self.instance_name
  "git"
end

Instance Method Details

#added_filesFileList<String>

Paths for files that were added during the diff

Returns:



52
53
54
# File 'lib/danger/danger_core/plugins/dangerfile_git_plugin.rb', line 52

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`



100
101
102
# File 'lib/danger/danger_core/plugins/dangerfile_git_plugin.rb', line 100

def commits
  @git.log.to_a
end

#deleted_filesFileList<String>

Paths for files that were removed during the diff

Returns:



60
61
62
# File 'lib/danger/danger_core/plugins/dangerfile_git_plugin.rb', line 60

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)


84
85
86
# File 'lib/danger/danger_core/plugins/dangerfile_git_plugin.rb', line 84

def deletions
  @git.diff.deletions
end

#insertionsFixnum

The overall lines of code added in the diff

Returns:

  • (Fixnum)


92
93
94
# File 'lib/danger/danger_core/plugins/dangerfile_git_plugin.rb', line 92

def insertions
  @git.diff.insertions
end

#lines_of_codeFixnum

The overall lines of code added/removed in the diff

Returns:

  • (Fixnum)


76
77
78
# File 'lib/danger/danger_core/plugins/dangerfile_git_plugin.rb', line 76

def lines_of_code
  @git.diff.lines
end

#modified_filesFileList<String>

Paths for files that changed during the diff

Returns:



68
69
70
# File 'lib/danger/danger_core/plugins/dangerfile_git_plugin.rb', line 68

def modified_files
  Danger::FileList.new(@git.diff.stats[:files].keys)
end