Class: CopyrightCheckHook
- Inherits:
-
RubyGitHooks::Hook
- Object
- RubyGitHooks::Hook
- CopyrightCheckHook
- Defined in:
- lib/ruby_git_hooks/copyright_check.rb
Overview
This hook checks whether a proper copyright notice exists at the top of each of your files. Right now it hardcodes most of the Copyright notice format, though you can customize the company name. If you’d prefer a more flexible format, we accept pull requests!
Constant Summary collapse
- COPYRIGHT_REGEXP =
/Copyright\s+\(C\)\s*(?<pre_year>.*)-?(?<cur_year>\d{4})\s+(?<company>.+)/i- EXTENSIONS =
Only check files with known checkable extensions
[ "c", "cpp", "cc", "cp", "h", "hp", "hpp", "m", "mm", "java", "bat", "sh", "ps1", "rb", ]
- OPTIONS =
[ "domain", "from", "subject", "via", "via_options", "intro", "no_send", "company_check", "exclude_files" ]
- Hook =
RubyGitHooks::Hook
Constants inherited from RubyGitHooks::Hook
RubyGitHooks::Hook::HOOK_INFO, RubyGitHooks::Hook::HOOK_TYPE_SETUP
Instance Attribute Summary collapse
-
#cur_year ⇒ Object
Returns the value of attribute cur_year.
Instance Method Summary collapse
-
#build_description(no_notice, outdated_notice, outdated_company) ⇒ Object
protected
Return an appropriate email based on the set of files with problems.
- #check ⇒ Object
- #commit_list ⇒ Object protected
- #current_repo ⇒ Object protected
-
#initialize(options = {}) ⇒ CopyrightCheckHook
constructor
A new instance of CopyrightCheckHook.
Methods inherited from RubyGitHooks::Hook
get_hooks_to_run, initial_setup, register, run, run_as_specific_githook, #setup, shell!
Constructor Details
#initialize(options = {}) ⇒ CopyrightCheckHook
Returns a new instance of CopyrightCheckHook.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/ruby_git_hooks/copyright_check.rb', line 32 def initialize( = {}) = .keys - OPTIONS raise "CopyrightCheckHook created with unrecognized options: " + "#{.inspect}!" if .size > 0 @options = @options["domain"] ||= "mydomain.com" @options["from"] ||= "Copyright Cop <noreply@#{@options["domain"]}>" @options["subject"] ||= "Copyright Your Files, Please!" @options["via"] ||= "no_send" @options["via_options"] ||= {} @options["exclude_files"] ||= [] # for generated files, etc. @cur_year = Time.now.strftime("%Y") end |
Instance Attribute Details
#cur_year ⇒ Object
Returns the value of attribute cur_year.
30 31 32 |
# File 'lib/ruby_git_hooks/copyright_check.rb', line 30 def cur_year @cur_year end |
Instance Method Details
#build_description(no_notice, outdated_notice, outdated_company) ⇒ Object (protected)
Return an appropriate email based on the set of files with problems. If you need a different format, please inherit from CopyrightCheckHook and override this method.
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/ruby_git_hooks/copyright_check.rb', line 138 def build_description(no_notice, outdated_notice, outdated_company) description = @options["intro"] || "" description.concat <<DESCRIPTION In repository: #{current_repo} commit(s): #{commit_list} You have outdated, inaccurate or missing copyright notices. Specifically: ============= DESCRIPTION if outdated_notice.size > 0 description.concat <<DESCRIPTION The following files do not list #{cur_year} as the most recent copyright year: #{outdated_notice.join("\n ")} ----- DESCRIPTION end if outdated_company.size > 0 description.concat <<DESCRIPTION The following files do not properly list your company as the holder of copyright: #{outdated_company.join("\n ")} DESCRIPTION end if no_notice.size > 0 description.concat <<DESCRIPTION The following files have no notice or a notice I didn't recognize: #{no_notice.join("\n ")} DESCRIPTION end description end |
#check ⇒ Object
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 108 109 |
# File 'lib/ruby_git_hooks/copyright_check.rb', line 47 def check no_notice = [] outdated_notice = [] outdated_company = [] cur_year = Time.now.strftime("%Y") files_changed.each do |filename| extension = (filename.split(".") || [])[-1] next unless EXTENSIONS.include?(extension) next if file_contents[filename] == "" # for now this is how we recognize a deleted file. next if @options["exclude_files"].include? filename if file_contents[filename] =~ COPYRIGHT_REGEXP parsed_cur_year = $~["cur_year"] parsed_company = $~["company"] unless parsed_cur_year == cur_year outdated_notice << filename end # If there is a "company_check" option, either a string # or regexp, make sure that the detected company name # matches it. if @options["company_check"] && !(parsed_company[@options["company_check"]]) outdated_company << filename end else no_notice << filename end end bad_num = no_notice.size + outdated_notice.size + outdated_company.size return true if bad_num < 1 desc = build_description(no_notice, outdated_notice, outdated_company) recipients = {} self.commits.each do |commit| = Hook.shell!("git log -n 1 --pretty=format:\"%aE %aN\" #{commit}") email, name = .chomp.split(" ", 2) recipients[name] = email end STDERR.puts "Warnings for commit from Copyright Check:\n--#{desc}\n--" unless @options["no_send"] || @options["via"] == "no_send" require "pony" # wait until we need it # NOTE: Pony breaks on Windows so don't use this option in Windows. recipients.each do |name, email| email_str = "#{name} <#{email}>" STDERR.puts "Sending warning email to #{email_str}" ret = Pony.mail :to => email_str, :from => @options["from"], :subject => @options["subject"], :body => desc, :via => @options["via"], :via_options => @options["via_options"] end end # Block commit if installed as a pre-commit or pre-receive hook false end |
#commit_list ⇒ Object (protected)
113 114 115 116 117 118 119 120 121 |
# File 'lib/ruby_git_hooks/copyright_check.rb', line 113 def commit_list # return the list of commits to display. We don't want to show them all # (it looks scary when there's a lot) # return a list of the shortened SHAH1s for readability # tried listing commit.last..commit.first but that didn't seem right either # problem is really when there's dozens of commits. self.commits.map {|c| c[0..6]}.join(", ") end |
#current_repo ⇒ Object (protected)
124 125 126 127 128 129 130 131 132 |
# File 'lib/ruby_git_hooks/copyright_check.rb', line 124 def current_repo # which repository are these commits in # Since we are running as a hook, we may get back the .git directory full_path = Hook.shell!("git rev-parse --show-toplevel").chomp # like "/path/to/repos/test-repo" or maybe "/path/to/repos/test-repo/.git" # return "test-repo" File.basename full_path.sub(/.git\z/, "") end |