├── .yardopts ├── .gitignore ├── CHANGELOG.md ├── .travis.yml ├── appveyor.yml ├── gemfiles ├── release.gemfile └── master.gemfile ├── lib ├── dco │ ├── version.rb │ └── cli.rb └── dco.rb ├── Gemfile ├── bin └── dco ├── Rakefile ├── spec ├── baseline_spec.rb ├── disable_spec.rb ├── spec_helper.rb ├── enable_spec.rb ├── process_commit_message_spec.rb ├── check_spec.rb └── sign_spec.rb ├── dco.gemspec ├── README.md └── LICENSE /.yardopts: -------------------------------------------------------------------------------- 1 | --hide-api private 2 | --markup markdown 3 | --hide-void-return 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | Gemfile.lock 2 | gemfiles/*.lock 3 | coverage/ 4 | **/pkg 5 | .yardoc/ 6 | doc/ 7 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # DCO Changelog 2 | 3 | ## v1.0.1 4 | 5 | * Fix `dco sign` when there is a backup ref from a different branch than the 6 | current one. 7 | 8 | ## v1.0.0 9 | 10 | * Initial release! 11 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | cache: bundler 3 | language: ruby 4 | rvm: 5 | - "2.2.10" 6 | - "2.3.7" 7 | - "2.4.4" 8 | - "2.5.1" 9 | gemfile: 10 | - gemfiles/release.gemfile 11 | - gemfiles/master.gemfile 12 | before_install: 13 | - if [[ $BUNDLE_GEMFILE == *master.gemfile ]]; then gem update --system; fi 14 | - gem --version 15 | - gem install bundler 16 | - bundle --version 17 | script: bundle exec rake spec 18 | -------------------------------------------------------------------------------- /appveyor.yml: -------------------------------------------------------------------------------- 1 | version: "{build}" 2 | 3 | cache: 4 | - vendor/bundle 5 | 6 | environment: 7 | matrix: 8 | - RUBY_VERSION: 22 9 | - RUBY_VERSION: 23 10 | 11 | install: 12 | - ps: Invoke-WebRequest -Uri https://download.sysinternals.com/files/Handle.zip -OutFile C:\handle.zip; Expand-Archive c:\handle.zip -dest c:\ 13 | - set PATH=C:\Ruby%RUBY_VERSION%\bin;%PATH% 14 | - gem install bundler 15 | - bundle install --path vendor/bundle 16 | 17 | build: off 18 | 19 | before_test: 20 | - ruby -v 21 | - gem -v 22 | - bundle -v 23 | 24 | test_script: 25 | - bundle exec rake spec 26 | -------------------------------------------------------------------------------- /gemfiles/release.gemfile: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2016, Noah Kantrowitz 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | 17 | eval_gemfile File.expand_path('../../Gemfile', __FILE__) 18 | -------------------------------------------------------------------------------- /lib/dco/version.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2016, Noah Kantrowitz 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | 17 | 18 | module Dco 19 | # dco gem version. 20 | VERSION = '1.0.2.pre' 21 | end 22 | -------------------------------------------------------------------------------- /lib/dco.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2016, Noah Kantrowitz 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | 17 | 18 | # A toolkit for managing projects using DCO. 19 | # 20 | # @since 1.0.0 21 | module Dco 22 | autoload :CLI, 'dco/cli' 23 | autoload :VERSION, 'dco/version' 24 | end 25 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2016, Noah Kantrowitz 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | 17 | source 'https://rubygems.org/' 18 | 19 | gemspec path: File.expand_path(__dir__) 20 | 21 | group :debug do 22 | gem 'pry' 23 | end 24 | 25 | group :docs do 26 | gem 'yard', '~> 0.8' 27 | end 28 | -------------------------------------------------------------------------------- /gemfiles/master.gemfile: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2016, Noah Kantrowitz 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | 17 | eval_gemfile File.expand_path('../../Gemfile', __FILE__) 18 | 19 | gem 'git', github: 'schacon/ruby-git' 20 | gem 'rspec-command', github: 'coderanger/rspec-command' 21 | gem 'thor', github: 'erikhuda/thor' 22 | -------------------------------------------------------------------------------- /bin/dco: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | # 3 | # Copyright 2014-2016, Chef Software Inc. 4 | # Copyright 2016, Noah Kantrowitz 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # http://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | 19 | # Set a trap for ctrl-C right away so we don't surface interrupt exceptions inside of rubygems or whatever. 20 | Kernel.trap(:INT) { print("\n"); exit 1 } 21 | 22 | $:.unshift(File.expand_path(File.join(File.dirname(__FILE__), "..", "lib"))) 23 | require 'dco/cli' 24 | 25 | Dco::CLI.start(ARGV.clone) 26 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2016, Noah Kantrowitz 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | 17 | require 'bundler/gem_tasks' 18 | 19 | require 'rspec/core/rake_task' 20 | RSpec::Core::RakeTask.new(:spec) do |t| 21 | t.rspec_opts = [].tap do |a| 22 | a << '--color' 23 | a << "--format #{ENV['CI'] ? 'documentation' : 'Fuubar'}" 24 | a << '--backtrace' if ENV['DEBUG'] 25 | a << "--seed #{ENV['SEED']}" if ENV['SEED'] 26 | end.join(' ') 27 | end 28 | 29 | desc 'Run all tests' 30 | task :test => [:spec] 31 | 32 | task :default => [:test] 33 | -------------------------------------------------------------------------------- /spec/baseline_spec.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2016, Noah Kantrowitz 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | 17 | require 'spec_helper' 18 | 19 | describe 'baseline' do 20 | # The most recent commit object. 21 | subject { repo.log.first } 22 | 23 | # Check that the test harness is working. 24 | git_init 25 | file 'testing' 26 | before do 27 | command 'git add testing' 28 | command 'git commit -m "harness test"' 29 | end 30 | 31 | its(:message) { is_expected.to eq 'harness test' } 32 | its('author.name') { is_expected.to eq 'Alan Smithee' } 33 | its('author.email') { is_expected.to eq 'asmithee@example.com' } 34 | end 35 | -------------------------------------------------------------------------------- /dco.gemspec: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2016, Noah Kantrowitz 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | 17 | lib = File.expand_path('../lib', __FILE__) 18 | $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) 19 | require 'dco/version' 20 | 21 | Gem::Specification.new do |spec| 22 | spec.name = 'dco' 23 | spec.version = Dco::VERSION 24 | spec.authors = ['Noah Kantrowitz'] 25 | spec.email = %w{noah@coderanger.net} 26 | spec.description = 'A command line tool to help manage Developer Certificate of Origin projects.' 27 | spec.summary = spec.description 28 | spec.homepage = 'https://github.com/coderanger/dco' 29 | spec.license = 'Apache-2.0' 30 | 31 | spec.files = `git ls-files`.split($/) 32 | spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } 33 | spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) 34 | spec.require_paths = %w{lib} 35 | 36 | spec.add_dependency 'git', '~> 1.3' 37 | spec.add_dependency 'thor', '~> 0.19' 38 | 39 | spec.add_development_dependency 'rake', '~> 11.0' 40 | spec.add_development_dependency 'rspec', '~> 3.2' 41 | spec.add_development_dependency 'rspec-command', '~> 1.0' 42 | spec.add_development_dependency 'fuubar', '~> 2.0' 43 | spec.add_development_dependency 'simplecov', '~> 0.9' 44 | spec.add_development_dependency 'codecov', '~> 0.0', '>= 0.0.2' 45 | end 46 | -------------------------------------------------------------------------------- /spec/disable_spec.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2016, Noah Kantrowitz 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | 17 | require 'spec_helper' 18 | 19 | describe 'dco disable' do 20 | # The most recent commit object. 21 | subject { repo.log.first } 22 | 23 | context 'with a normal commit' do 24 | git_init 25 | file 'testing' 26 | before do 27 | dco_command 'enable -y' 28 | dco_command 'disable' 29 | command 'git add testing' 30 | command 'git commit -m "test commit"' 31 | end 32 | 33 | its(:message) { is_expected.to eq "test commit" } 34 | end # /context with a normal commit 35 | 36 | context 'with disable called twice' do 37 | git_init 38 | file 'testing' 39 | before do 40 | dco_command 'enable -y' 41 | dco_command 'disable' 42 | dco_command 'disable' 43 | command 'git add testing' 44 | command 'git commit -m "test commit"' 45 | end 46 | 47 | its(:message) { is_expected.to eq "test commit" } 48 | end # /context with disable called twice 49 | 50 | context 'with an external commit-msg script' do 51 | git_init 52 | file '.git/hooks/commit-msg', 'SOMETHING ELSE' 53 | dco_command 'disable' 54 | 55 | it do 56 | expect(subject.exitstatus).to eq 1 57 | expect(subject.stdout).to eq '' 58 | expect(subject.stderr).to match /^commit-msg hook is external, not removing$/ 59 | end 60 | end # /context with an existing commit-msg script 61 | end 62 | -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2016, Noah Kantrowitz 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | 17 | require 'rspec' 18 | require 'rspec_command' 19 | require 'simplecov' 20 | require 'git' 21 | require 'shellwords' 22 | 23 | # Check for coverage stuffs 24 | if ENV['CODECOV_TOKEN'] || ENV['TRAVIS'] 25 | require 'codecov' 26 | SimpleCov.formatter = SimpleCov::Formatter::Codecov 27 | end 28 | 29 | SimpleCov.start do 30 | # Don't get coverage on the test cases themselves. 31 | add_filter '/spec/' 32 | add_filter '/test/' 33 | # Codecov doesn't automatically ignore vendored files. 34 | add_filter '/vendor/' 35 | end 36 | 37 | require 'dco' 38 | 39 | module DcoSpecHelper 40 | extend RSpec::SharedContext 41 | 42 | def dco_command *args 43 | cwd = Dir.pwd 44 | begin 45 | Dir.chdir(temp_path) 46 | capture_output do 47 | args = Shellwords.split(args.first) if args.length == 1 && args.first.is_a?(String) 48 | Dco::CLI.start(args) 49 | end 50 | ensure 51 | Dir.chdir(cwd) 52 | end 53 | rescue Exception => e 54 | status = e.is_a?(SystemExit) ? e.status : 1 55 | e.output_so_far.define_singleton_method(:exitstatus) { status } 56 | e.output_so_far 57 | end 58 | 59 | def git_init(name: 'Alan Smithee', email: 'asmithee@example.com') 60 | command "git init && git config user.name \"#{name}\" && git config user.email \"#{email}\"" 61 | end 62 | 63 | let(:repo) { Git.open(temp_path) } 64 | 65 | module ClassMethods 66 | def dco_command *args 67 | subject { dco_command(*args) } 68 | end 69 | 70 | def git_init(*args) 71 | before { git_init(*args) } 72 | end 73 | end 74 | end 75 | 76 | RSpec.configure do |config| 77 | # Basic configuraiton 78 | config.run_all_when_everything_filtered = true 79 | config.filter_run(:focus) 80 | 81 | # Run specs in random order to surface order dependencies. If you find an 82 | # order dependency and want to debug it, you can fix the order by providing 83 | # the seed, which is printed after each run. 84 | # --seed 1234 85 | config.order = 'random' 86 | 87 | config.include RSpecCommand 88 | 89 | config.include DcoSpecHelper 90 | config.extend DcoSpecHelper::ClassMethods 91 | end 92 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DCO 2 | 3 | [![Build Status](https://img.shields.io/travis/coderanger/dco.svg)](https://travis-ci.org/coderanger/dco) 4 | [![Windows Build Status](https://img.shields.io/appveyor/ci/coderanger/dco.svg?label=windows)](https://ci.appveyor.com/project/coderanger/dco) 5 | [![Gem Version](https://img.shields.io/gem/v/dco.svg)](https://rubygems.org/gems/dco) 6 | [![Coverage](https://img.shields.io/codecov/c/github/coderanger/dco.svg)](https://codecov.io/github/coderanger/dco) 7 | [![Gemnasium](https://img.shields.io/gemnasium/coderanger/dco.svg)](https://gemnasium.com/coderanger/dco) 8 | [![License](https://img.shields.io/badge/license-Apache_2-blue.svg)](https://www.apache.org/licenses/LICENSE-2.0) 9 | 10 | A command line tool to help manage projects using the [Developer Certificate of Origin](http://developercertificate.org/) 11 | contributor workflow. 12 | 13 | ## Quick Start 14 | 15 | To install: 16 | 17 | ```bash 18 | gem install dco 19 | ``` 20 | 21 | To enable auto-sign-off for all commits in a repository: 22 | 23 | ```bash 24 | cd /my/repository 25 | dco enable 26 | ``` 27 | 28 | ## Commands 29 | 30 | ### `dco enable` 31 | 32 | The `dco enable` command turns on auto-sign-off for all future commits in a 33 | repository. This means any commit message that does not have `Signed-off-by:` 34 | will have the declaration added. This is equivalent to always using `git commit -s` 35 | even in git tools that do not support it. 36 | 37 | The auto-sign-off is implemented using a `commit-msg` hook to rewrite the log 38 | message as needed. Unlike `git alias commit "commit -s"`, this will work with 39 | any Git interface, including GUIs and IDE integration. 40 | 41 | ### `dco disable` 42 | 43 | The `dco disable` command turns off auto-sign-off, removing the hook script 44 | created by `dco enable`. 45 | 46 | ### `dco sign` 47 | 48 | The `dco sign` command retroactively applies the sign-off commit message to 49 | every commit in a branch. By default it will update the current branch, but you 50 | can provide a branch name via `dco sign BRANCH`. If you want to use something other 51 | than `master` as the base branch, pass `--base BRANCH`. 52 | 53 | This can also be used to sign-off a branch on the behalf of another contributor. 54 | You must provide a link to a public declaration that the user is agreeing to the 55 | DCO: `dco sign --behalf 'https://github.com/me/myproject/pulls/1#issuecomment-238042611'`. 56 | 57 | When signing on the behalf of another user, you will get a comment like: 58 | 59 | ``` 60 | Signed-off-by: Alan Smithee 61 | Sign-off-executed-by: Commiter McCommiterface 62 | Approved-at: https://github.com/me/myproject/pulls/1#issuecomment-238042611 63 | ``` 64 | 65 | ### `dco check` 66 | 67 | The `dco check` verifies the DCO sign-off for all commits in a branch. By default 68 | it also checks that the sign-off for each commit matches that commit's author. 69 | 70 | ## Sponsors 71 | 72 | Development sponsored by [Bloomberg](http://www.bloomberg.com/company/technology/). 73 | 74 | ## License 75 | 76 | Copyright 2016, Noah Kantrowitz 77 | 78 | Licensed under the Apache License, Version 2.0 (the "License"); 79 | you may not use this file except in compliance with the License. 80 | You may obtain a copy of the License at 81 | 82 | http://www.apache.org/licenses/LICENSE-2.0 83 | 84 | Unless required by applicable law or agreed to in writing, software 85 | distributed under the License is distributed on an "AS IS" BASIS, 86 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 87 | See the License for the specific language governing permissions and 88 | limitations under the License. 89 | -------------------------------------------------------------------------------- /spec/enable_spec.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2016, Noah Kantrowitz 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | 17 | require 'spec_helper' 18 | 19 | describe 'dco enable' do 20 | # The most recent commit object. 21 | subject { repo.log.first } 22 | 23 | context 'without a git repository' do 24 | dco_command 'enable -y' 25 | 26 | it do 27 | expect(subject.exitstatus).to eq 1 28 | expect(subject.stdout).to eq '' 29 | expect(subject.stderr).to match /does not appear to be a git repository$/ 30 | end 31 | end # /context without a git repository 32 | 33 | context 'with an unwritable git repository' do 34 | git_init 35 | before { File.chmod(00544, File.join(temp_path, '.git')) } 36 | dco_command 'enable -y' 37 | after { File.chmod(00744, File.join(temp_path, '.git')) } 38 | 39 | it do 40 | expect(subject.exitstatus).to eq 1 41 | expect(subject.stdout).to eq '' 42 | expect(subject.stderr).to match /^Git repository at.*? is read-only$/ 43 | end 44 | end # /context with an unwritable git repository 45 | 46 | context 'with an existing commit-msg script' do 47 | git_init 48 | file '.git/hooks/commit-msg', 'SOMETHING ELSE' 49 | dco_command 'enable -y' 50 | 51 | it do 52 | expect(subject.exitstatus).to eq 1 53 | expect(subject.stdout).to eq '' 54 | expect(subject.stderr).to match /^commit-msg hook already exists, not overwriting$/ 55 | end 56 | end # /context with an existing commit-msg script 57 | 58 | context 'with a normal commit' do 59 | git_init 60 | file 'testing' 61 | before do 62 | dco_command 'enable -y' 63 | command 'git add testing' 64 | command 'git commit -m "test commit"' 65 | end 66 | 67 | its(:message) { is_expected.to eq "test commit\n\nSigned-off-by: Alan Smithee " } 68 | end # /context with a normal commit 69 | 70 | context 'with a signed-off commit' do 71 | git_init 72 | file 'testing' 73 | before do 74 | dco_command 'enable -y' 75 | command 'git add testing' 76 | command 'git commit -s -m "test commit"' 77 | end 78 | 79 | its(:message) { is_expected.to eq "test commit\n\nSigned-off-by: Alan Smithee " } 80 | end # /context with a signed-off commit 81 | 82 | context 'with enable called twice' do 83 | git_init 84 | file 'testing' 85 | before do 86 | dco_command 'enable -y' 87 | dco_command 'enable -y' 88 | command 'git add testing' 89 | command 'git commit -m "test commit"' 90 | end 91 | 92 | its(:message) { is_expected.to eq "test commit\n\nSigned-off-by: Alan Smithee " } 93 | end # /context with enable called twice 94 | 95 | context 'without -y' do 96 | git_init 97 | dco_command 'enable' 98 | 99 | it do 100 | expect(subject.exitstatus).to eq 1 101 | expect(subject.stderr).to eq "Not enabling auto-sign-off without approval\n" 102 | end 103 | end # /context without -y 104 | end 105 | -------------------------------------------------------------------------------- /spec/process_commit_message_spec.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2016, Noah Kantrowitz 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | 17 | require 'spec_helper' 18 | 19 | describe 'dco process_commit_message' do 20 | around do |ex| 21 | begin 22 | ENV['GIT_COMMIT'] = 'abcd123' 23 | ENV['GIT_AUTHOR_NAME'] = 'Alan Smithee' 24 | ENV['GIT_AUTHOR_EMAIL'] = 'asmithee@example.com' 25 | ex.run 26 | ensure 27 | ENV.delete('GIT_COMMIT') 28 | ENV.delete('GIT_AUTHOR_NAME') 29 | ENV.delete('GIT_AUTHOR_EMAIL') 30 | end 31 | end 32 | 33 | context 'hook mode' do 34 | dco_command 'process_commit_message msg' 35 | 36 | RSpec.shared_examples 'process_commit_message hook mode' do |input, output| 37 | file 'msg', input 38 | 39 | it do 40 | expect(subject.exitstatus).to eq 0 41 | expect(subject.stdout).to eq '' 42 | expect(subject.stderr).to eq '' 43 | expect(IO.read(File.join(temp_path, 'msg'))).to eq output 44 | end 45 | end 46 | 47 | context 'with a normal commit' do 48 | it_behaves_like 'process_commit_message hook mode', "test commit\n", "test commit\n\nSigned-off-by: Alan Smithee \n" 49 | end # /context with a normal commit 50 | 51 | context 'with no trailing newline' do 52 | it_behaves_like 'process_commit_message hook mode', "test commit", "test commit\n\nSigned-off-by: Alan Smithee \n" 53 | end # /context with no trailing newline 54 | 55 | context 'with existing sign-off' do 56 | it_behaves_like 'process_commit_message hook mode', "test commit\n\nSigned-off-by: Someone Else \n", "test commit\n\nSigned-off-by: Someone Else \n" 57 | end # /context with existing sign-off 58 | 59 | context 'with two existing sign-offs' do 60 | it_behaves_like 'process_commit_message hook mode', "test commit\n\nSigned-off-by: Alan Smithee \nSigned-off-by: Someone Else \n", "test commit\n\nSigned-off-by: Alan Smithee \nSigned-off-by: Someone Else \n" 61 | end # /context with two existing sign-offs 62 | end # /context hook mode 63 | 64 | context 'filter mode' do 65 | let(:input) { '' } 66 | let(:git_ident) { {} } 67 | let(:stdin) { double('STDIN', read: input) } 68 | before do 69 | # Use a let variable instead of calling git_init again in a later before 70 | # block because we need to all command running before the STDIN stub. 71 | git_init git_ident 72 | stub_const('STDIN', stdin) 73 | end 74 | 75 | context 'with a normal commit' do 76 | let(:input) { "test commit\n" } 77 | dco_command 'process_commit_message' 78 | 79 | it do 80 | expect(subject.exitstatus).to eq 0 81 | expect(subject.stdout).to eq "test commit\n\nSigned-off-by: Alan Smithee \n" 82 | expect(subject.stderr).to eq '' 83 | end 84 | end # /context with a normal commit 85 | 86 | context 'with existing sign-off' do 87 | let(:input) { "test commit\n\nSigned-off-by: Someone Else \n" } 88 | dco_command 'process_commit_message' 89 | 90 | it do 91 | expect(subject.exitstatus).to eq 0 92 | expect(subject.stdout).to eq "test commit\n\nSigned-off-by: Someone Else \n" 93 | expect(subject.stderr).to eq '' 94 | end 95 | end # /context with existing sign-off 96 | 97 | context 'with --behalf' do 98 | let(:input) { "test commit\n" } 99 | let(:git_ident) { {name: 'Someone Else', email: 'other@example.com'} } 100 | dco_command 'process_commit_message --behalf http://example.com/' 101 | 102 | it do 103 | expect(subject.exitstatus).to eq 0 104 | expect(subject.stdout).to eq "test commit\n\nSigned-off-by: Alan Smithee \nSign-off-executed-by: Someone Else \nApproved-at: http://example.com/\n" 105 | expect(subject.stderr).to eq '' 106 | end 107 | end # /context with --behalf 108 | 109 | context 'with someone elses commit' do 110 | let(:input) { "test commit\n" } 111 | let(:git_ident) { {name: 'Someone Else', email: 'other@example.com'} } 112 | dco_command 'process_commit_message' 113 | 114 | it do 115 | expect(subject.exitstatus).to eq 1 116 | expect(subject.stdout).to eq "test commit\n" 117 | expect(subject.stderr).to eq "Author mismatch on commit abcd123: asmithee@example.com vs other@example.com\n" 118 | end 119 | end # /context with someone elses commit 120 | 121 | context 'with --repo' do 122 | let(:input) { "test commit\n" } 123 | subject { dco_command "process_commit_message --repo '#{temp_path}'" } 124 | 125 | it do 126 | expect(subject.exitstatus).to eq 0 127 | expect(subject.stdout).to eq "test commit\n\nSigned-off-by: Alan Smithee \n" 128 | expect(subject.stderr).to eq '' 129 | end 130 | end # /context with --repo 131 | end # /context filter mode 132 | end 133 | -------------------------------------------------------------------------------- /spec/check_spec.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2016, Noah Kantrowitz 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | 17 | require 'spec_helper' 18 | 19 | describe 'dco check' do 20 | # Create a branch structure for all tests. 21 | git_init 22 | file 'testing', 'one' 23 | before do 24 | cmds = [ 25 | 'git add testing', 26 | 'git commit -s -m "first commit"', 27 | 'echo two > testing', 28 | 'git commit -s -a -m "second commit"', 29 | 'git checkout -b mybranch', 30 | ] 31 | command cmds.join(' && ') 32 | end 33 | dco_command 'check mybranch' 34 | 35 | context 'with no commits in the branch' do 36 | it do 37 | expect(subject.exitstatus).to eq 0 38 | expect(subject.stdout).to eq "All commits are signed off\n" 39 | end 40 | end # /context with no commits in the branch 41 | 42 | context 'with one signed commit in the branch' do 43 | before do 44 | command 'echo three > testing && git commit -s -a -m "first branch commit"' 45 | end 46 | 47 | it do 48 | expect(subject.exitstatus).to eq 0 49 | expect(subject.stdout).to eq "All commits are signed off\n" 50 | end 51 | end # /context with one signed commit in the branch 52 | 53 | context 'with one unsigned commit in the branch' do 54 | before do 55 | command 'echo three > testing && git commit -a -m "first branch commit" && echo four > testing && git commit -s -a -m "second branch commit"' 56 | end 57 | 58 | it do 59 | expect(subject.exitstatus).to eq 1 60 | expect(subject.stdout).to match /^N \h{7} Alan Smithee first branch commit$/ 61 | end 62 | end # /context with one unsigned commit in the branch 63 | 64 | context 'with two signed commits in the branch' do 65 | before do 66 | command 'echo three > testing && git commit -s -a -m "first branch commit" && echo four > testing && git commit -s -a -m "second branch commit"' 67 | end 68 | 69 | it do 70 | expect(subject.exitstatus).to eq 0 71 | expect(subject.stdout).to eq "All commits are signed off\n" 72 | end 73 | end # /context with two signed commits in the branch 74 | 75 | context 'with an implicit branch' do 76 | before do 77 | command 'echo three > testing && git commit -a -m "first branch commit" && echo four > testing && git commit -s -a -m "second branch commit"' 78 | end 79 | dco_command 'check' 80 | 81 | it do 82 | expect(subject.exitstatus).to eq 1 83 | expect(subject.stdout).to match /^N \h{7} Alan Smithee first branch commit$/ 84 | end 85 | end # /context with an implicit branch 86 | 87 | context 'with a branch that has a merge commit' do 88 | before do 89 | command('echo three > other && ' \ 90 | 'git add other && ' \ 91 | 'git commit -s -a -m "first branch commit" && ' \ 92 | 'git checkout master && ' \ 93 | 'echo three > testing && ' \ 94 | 'git commit -a -m "third commit" && ' \ 95 | 'git checkout mybranch && ' \ 96 | 'git merge master && ' \ 97 | 'echo four > other && ' \ 98 | 'git commit -a -m "second branch commit"') 99 | end 100 | 101 | it do 102 | expect(subject.exitstatus).to eq 1 103 | expect(subject.stdout).to match /^N \h{7} Alan Smithee second branch commit$/ 104 | expect(subject.stdout).to_not match /^N \h{7} Alan Smithee third commit$/ 105 | # TODO What should the check behavior on merge commits be? https://github.com/coderanger/dco/issues/1 106 | end 107 | end # /context with a branch that has a merge commit 108 | 109 | context 'with --quiet' do 110 | dco_command 'check --quiet' 111 | 112 | context 'with no bad commits' do 113 | before { command 'echo three > testing && git commit -s -a -m "first branch commit"' } 114 | 115 | it do 116 | expect(subject.exitstatus).to eq 0 117 | expect(subject.stdout).to eq '' 118 | expect(subject.stderr).to eq '' 119 | end 120 | end # /context with no bad commits 121 | 122 | context 'with bad commits' do 123 | before { command 'echo three > testing && git commit -a -m "first branch commit"' } 124 | 125 | it do 126 | expect(subject.exitstatus).to eq 1 127 | expect(subject.stdout).to eq '' 128 | expect(subject.stderr).to eq '' 129 | end 130 | end # /context with bad commits 131 | end # /context with --quiet 132 | 133 | context 'with the master branch' do 134 | before do 135 | command('echo three > other && ' \ 136 | 'git add other && ' \ 137 | 'git commit -s -a -m "first branch commit" && ' \ 138 | 'git checkout master && ' \ 139 | 'echo three > testing && ' \ 140 | 'git commit -a -m "third commit"') 141 | end 142 | dco_command 'check master' 143 | 144 | it do 145 | expect(subject.exitstatus).to eq 1 146 | expect(subject.stdout).to match /^N \h{7} Alan Smithee third commit$/ 147 | expect(subject.stdout).to_not match /^N \h{7} Alan Smithee first branch commit$/ 148 | end 149 | end # /context with the master branch 150 | 151 | context 'with an author mismatch' do 152 | file 'msg', "first branch commit\n\nSigned-off-by: Commiter McCommiterface \n" 153 | before do 154 | command 'echo three > testing && git commit -a -F msg' 155 | end 156 | 157 | it do 158 | expect(subject.exitstatus).to eq 1 159 | expect(subject.stdout).to match /^M \h{7} Alan Smithee first branch commit$/ 160 | end 161 | 162 | context 'with --allow-author-mismatch' do 163 | dco_command 'check --allow-author-mismatch mybranch' 164 | 165 | it do 166 | expect(subject.exitstatus).to eq 0 167 | expect(subject.stdout).to eq "All commits are signed off\n" 168 | end 169 | 170 | end # /context with --allow-author-mismatch 171 | end # /context with an author mismatch 172 | end 173 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /spec/sign_spec.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2016, Noah Kantrowitz 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | 17 | require 'spec_helper' 18 | 19 | describe 'dco sign' do 20 | # Create a branch structure for all tests. 21 | git_init 22 | file 'testing', 'one' 23 | before do 24 | cmds = [ 25 | 'git add testing', 26 | 'git commit -m "first commit"', 27 | 'echo two > testing', 28 | 'git commit -a -m "second commit"', 29 | 'git checkout -b mybranch', 30 | ] 31 | command cmds.join(' && ') 32 | end 33 | 34 | context 'with no commits in the branch' do 35 | dco_command 'sign -y mybranch' 36 | 37 | it do 38 | expect(subject.exitstatus).to eq 1 39 | expect(subject.stderr).to eq "Branch mybranch has no commits which require sign-off\n" 40 | end 41 | end # /context with no commits in the branch 42 | 43 | context 'with one commit in the branch' do 44 | before do 45 | command 'echo three > testing && git commit -a -m "first branch commit"' 46 | end 47 | dco_command 'sign -y mybranch' 48 | 49 | it do 50 | expect(subject.exitstatus).to eq 0 51 | expect(subject.stdout).to match /^Developer's Certificate of Origin 1\.1$/ 52 | expect(subject.stdout).to match /^Going to sign-off the following commits:\n\* \h{7} Alan Smithee first branch commit$/ 53 | expect(repo.log[0].message).to eq "first branch commit\n\nSigned-off-by: Alan Smithee " 54 | expect(repo.log[1].message).to eq "second commit" 55 | expect(repo.log[2].message).to eq "first commit" 56 | end 57 | end # /context with one commit in the branch 58 | 59 | context 'with one commit in the branch without -y' do 60 | before do 61 | command 'echo three > testing && git commit -a -m "first branch commit"' 62 | end 63 | dco_command 'sign mybranch' 64 | 65 | it do 66 | expect(subject.exitstatus).to eq 1 67 | expect(subject.stderr).to eq "Not signing off on commits without approval\n" 68 | end 69 | end # /context with one commit in the branch without -y 70 | 71 | context 'with two commits in the branch' do 72 | before do 73 | command 'echo three > testing && git commit -a -m "first branch commit" && echo four > testing && git commit -a -m "second branch commit"' 74 | end 75 | dco_command 'sign -y mybranch' 76 | 77 | it do 78 | expect(subject.exitstatus).to eq 0 79 | expect(subject.stdout).to match /^Developer's Certificate of Origin 1\.1$/ 80 | expect(subject.stdout).to match /^Going to sign-off the following commits:\n\* \h{7} Alan Smithee second branch commit\n\* \h{7} Alan Smithee first branch commit$/ 81 | expect(repo.log[0].message).to eq "second branch commit\n\nSigned-off-by: Alan Smithee " 82 | expect(repo.log[1].message).to eq "first branch commit\n\nSigned-off-by: Alan Smithee " 83 | expect(repo.log[2].message).to eq "second commit" 84 | expect(repo.log[3].message).to eq "first commit" 85 | end 86 | end # /context with two commits in the branch 87 | 88 | context 'with a branch that has a merge commit' do 89 | before do 90 | command('echo three > other && ' \ 91 | 'git add other && ' \ 92 | 'git commit -a -m "first branch commit" && ' \ 93 | 'git checkout master && ' \ 94 | 'echo three > testing && ' \ 95 | 'git commit -a -m "third commit" && ' \ 96 | 'git checkout mybranch && ' \ 97 | 'git merge master && ' \ 98 | 'echo four > other && ' \ 99 | 'git commit -a -m "second branch commit"') 100 | end 101 | dco_command 'sign -y mybranch' 102 | 103 | it do 104 | expect(subject.exitstatus).to eq 0 105 | expect(subject.stdout).to match /^Developer's Certificate of Origin 1\.1$/ 106 | expect(subject.stdout).to match /^Going to sign-off the following commits:\n\* \h{7} Alan Smithee second branch commit\n\* \h{7} Alan Smithee Merge branch 'master' into mybranch\n\* \h{7} Alan Smithee first branch commit$/ 107 | # Ordering is unstable because of the merge. 108 | commits = repo.log.map {|c| c.message } 109 | expect(commits.size).to eq 6 110 | expect(commits).to include "second branch commit\n\nSigned-off-by: Alan Smithee " 111 | expect(commits).to include "Merge branch 'master' into mybranch\n\nSigned-off-by: Alan Smithee " 112 | expect(commits).to include "first branch commit\n\nSigned-off-by: Alan Smithee " 113 | expect(commits).to include "third commit" 114 | expect(commits).to include "second commit" 115 | expect(commits).to include "first commit" 116 | end 117 | end # /context with a branch that has a merge commit 118 | 119 | context 'with behalf mode enabled' do 120 | before do 121 | command 'echo three > testing && git commit -a -m "first branch commit" && echo four > testing && git commit -a -m "second branch commit"' 122 | git_init(name: 'Commiter McCommiterface', email: 'other@example.com') 123 | end 124 | dco_command 'sign -y mybranch -b https://github.com/chef/chef/pulls/1234' 125 | 126 | it do 127 | expect(subject.exitstatus).to eq 0 128 | expect(subject.stdout).to_not match /^Developer's Certificate of Origin 1\.1$/ 129 | expect(subject.stdout).to match /^Going to sign-off the following commits:\n\* \h{7} Alan Smithee second branch commit\n\* \h{7} Alan Smithee first branch commit$/ 130 | expect(repo.log[0].message).to eq "second branch commit\n\nSigned-off-by: Alan Smithee \nSign-off-executed-by: Commiter McCommiterface \nApproved-at: https://github.com/chef/chef/pulls/1234" 131 | expect(repo.log[1].message).to eq "first branch commit\n\nSigned-off-by: Alan Smithee \nSign-off-executed-by: Commiter McCommiterface \nApproved-at: https://github.com/chef/chef/pulls/1234" 132 | expect(repo.log[2].message).to eq "second commit" 133 | expect(repo.log[3].message).to eq "first commit" 134 | end 135 | end # /context with behalf mode enabled 136 | 137 | context 'with someone elses commits' do 138 | before do 139 | command 'echo three > testing && git commit -a -m "first branch commit" && echo four > testing && git commit -a -m "second branch commit"' 140 | git_init(name: 'Commiter McCommiterface', email: 'other@example.com') 141 | end 142 | dco_command 'sign -y mybranch' 143 | 144 | it do 145 | expect(subject.exitstatus).to eq 1 146 | expect(subject.stderr).to eq "Branch mybranch contains commits not authored by you. Please use the --behalf flag when signing off for another contributor\n" 147 | end 148 | end # /context with someone elses commits 149 | 150 | context 'with an invalid branch' do 151 | dco_command 'sign -y master' 152 | 153 | it do 154 | expect(subject.exitstatus).to eq 1 155 | expect(subject.stderr).to eq "Cannot use master for both the base and target branch\n" 156 | end 157 | end # /context with an invalid branch 158 | 159 | context 'with an implicit branch' do 160 | before do 161 | command 'echo three > testing && git commit -a -m "first branch commit"' 162 | end 163 | dco_command 'sign -y' 164 | 165 | it do 166 | expect(subject.exitstatus).to eq 0 167 | expect(subject.stdout).to match /^Developer's Certificate of Origin 1\.1$/ 168 | expect(subject.stdout).to match /^Going to sign-off the following commits:\n\* \h{7} Alan Smithee first branch commit$/ 169 | expect(repo.log[0].message).to eq "first branch commit\n\nSigned-off-by: Alan Smithee " 170 | expect(repo.log[1].message).to eq "second commit" 171 | expect(repo.log[2].message).to eq "first commit" 172 | end 173 | end # /context with an implicit branch 174 | 175 | context 'with an implicit invalid branch' do 176 | before { command 'git checkout master' } 177 | dco_command 'sign -y' 178 | 179 | it do 180 | expect(subject.exitstatus).to eq 1 181 | expect(subject.stderr).to eq "Cannot use master for both the base and target branch\n" 182 | end 183 | end # /context with an implicit invalid branch 184 | 185 | context 'with an existing backup pointer' do 186 | before do 187 | command 'echo three > testing && git commit -a -m "first branch commit"' 188 | dco_command 'sign -y mybranch' 189 | command 'echo four > testing && git commit -a -m "second branch commit"' 190 | end 191 | dco_command 'sign -y mybranch' 192 | 193 | it do 194 | expect(subject.exitstatus).to eq 0 195 | expect(subject.stdout).to match /^Developer's Certificate of Origin 1\.1$/ 196 | expect(subject.stdout).to match /^Going to sign-off the following commits:\n\* \h{7} Alan Smithee second branch commit$/ 197 | expect(repo.log[0].message).to eq "second branch commit\n\nSigned-off-by: Alan Smithee " 198 | expect(repo.log[1].message).to eq "first branch commit\n\nSigned-off-by: Alan Smithee " 199 | expect(repo.log[2].message).to eq "second commit" 200 | expect(repo.log[3].message).to eq "first commit" 201 | end 202 | end # /context with an existing backup pointer 203 | 204 | context 'with an existing backup pointer from a different branch' do 205 | before do 206 | command 'echo three > testing && git commit -a -m "first branch commit"' 207 | dco_command 'sign -y mybranch' 208 | command 'git checkout -b mybranch2 && echo four > testing && git commit -a -m "second branch commit"' 209 | end 210 | dco_command 'sign -y mybranch2' 211 | 212 | it do 213 | expect(subject.exitstatus).to eq 0 214 | expect(subject.stdout).to match /^Developer's Certificate of Origin 1\.1$/ 215 | expect(subject.stdout).to match /^Going to sign-off the following commits:\n\* \h{7} Alan Smithee second branch commit$/ 216 | expect(repo.log[0].message).to eq "second branch commit\n\nSigned-off-by: Alan Smithee " 217 | expect(repo.log[1].message).to eq "first branch commit\n\nSigned-off-by: Alan Smithee " 218 | expect(repo.log[2].message).to eq "second commit" 219 | expect(repo.log[3].message).to eq "first commit" 220 | end 221 | end # /context with an existing backup pointer from a different branch 222 | 223 | context 'with an existing backup pointer without -y' do 224 | before do 225 | command 'echo three > testing && git commit -a -m "first branch commit"' 226 | dco_command 'sign -y mybranch' 227 | command 'echo four > testing && git commit -a -m "second branch commit"' 228 | end 229 | dco_command 'sign mybranch' 230 | 231 | it do 232 | expect(subject.exitstatus).to eq 1 233 | expect(subject.stderr).to eq "Backup ref present, not continuing\n" 234 | end 235 | end # /context with an existing backup pointer without -y 236 | 237 | context 'with uncommitted changes' do 238 | before do 239 | command 'echo three > testing && git commit -a -m "first branch commit" && echo four > testing' 240 | end 241 | dco_command 'sign -y mybranch' 242 | 243 | it do 244 | expect(subject.exitstatus).to eq 0 245 | expect(subject.stdout).to match /^Stashing uncommited changes before continuing$/ 246 | expect(IO.read(File.join(temp_path, 'testing')).strip).to eq "four" 247 | end 248 | end # /context with uncommitted changes 249 | end 250 | -------------------------------------------------------------------------------- /lib/dco/cli.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2016, Noah Kantrowitz 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | 17 | require 'shellwords' 18 | 19 | require 'git' 20 | require 'thor' 21 | 22 | 23 | module Dco 24 | class CLI < Thor 25 | # Because this isn't the default and exit statuses are what the cool kids do. 26 | def self.exit_on_failure? 27 | true 28 | end 29 | 30 | # Fix the basename display in ChefDK. 31 | # 32 | # @api private 33 | # @return [String] 34 | def self.basename 35 | ret = super 36 | if ret == 'chef' 37 | 'chef dco' 38 | else 39 | ret 40 | end 41 | end 42 | 43 | no_commands do 44 | # Return the path for the git repository we will process. Defaults to the 45 | # current working directory. 46 | # 47 | # @return [String] 48 | def repo_path 49 | @repo_path || Dir.pwd 50 | end 51 | 52 | # Set a new path for the git repository. 53 | # 54 | # @param val [String] Path 55 | # @return [void] 56 | def repo_path=(val) 57 | @repo_path = val 58 | # Force things to reload. 59 | @repo = nil 60 | @repo_config = nil 61 | end 62 | end 63 | 64 | # Internal command used by the git hook to implement the processing logic. 65 | # This is done in Ruby because writing it to work on all platforms in Bash 66 | # seems unfun. 67 | # 68 | # Design note: this should try as hard as possible to be fast, especially 69 | # in hook mode as it adds overhead time to every commit there. Currently 70 | # it should only have to touch the filesystem to read/write the message, 71 | # when in hook mode. For filter mode, it does need to load the git config 72 | # if using --behalf. 73 | desc 'process_commit_message', 'process a git commit message to add DCO signoff', hide: true 74 | options behalf: :string, repo: :string 75 | def process_commit_message(tmp_path=nil) 76 | # Set the repo path if passed. 77 | self.repo_path = options[:repo] if options[:repo] 78 | # If a path is passed use it as a tmpfile, otherwise assume filter mode. 79 | commit_msg = tmp_path ? IO.read(tmp_path) : STDIN.read 80 | unless has_sign_off?(commit_msg) 81 | # If we're in filter mode and not on-behalf-of, do a final check of the author. 82 | if !tmp_path && !options[:behalf] && ENV['GIT_AUTHOR_EMAIL'] != repo_config['user.email'] 83 | # Something went wrong, refuse to rewrite. 84 | STDOUT.write(commit_msg) 85 | raise Thor::Error.new("Author mismatch on commit #{ENV['GIT_COMMIT']}: #{ENV['GIT_AUTHOR_EMAIL']} vs #{repo_config['user.email']}") 86 | end 87 | commit_msg << "\n" unless commit_msg.end_with?("\n") 88 | commit_msg << "\nSigned-off-by: #{ENV['GIT_AUTHOR_NAME']} <#{ENV['GIT_AUTHOR_EMAIL']}>\n" 89 | if options[:behalf] 90 | # This requires loading the actual repo config, which is slower. 91 | commit_msg << "Sign-off-executed-by: #{git_identity}\n" 92 | commit_msg << "Approved-at: #{options[:behalf]}\n" 93 | end 94 | IO.write(tmp_path, commit_msg) if tmp_path 95 | end 96 | # Always display the replacement commit message if we're in filter mode. 97 | STDOUT.write(commit_msg) unless tmp_path 98 | end 99 | 100 | # Full text of the DCO. 101 | # @api private 102 | DCO_TEXT = <<-EOH 103 | Developer Certificate of Origin 104 | Version 1.1 105 | 106 | Copyright (C) 2004, 2006 The Linux Foundation and its contributors. 107 | 1 Letterman Drive 108 | Suite D4700 109 | San Francisco, CA, 94129 110 | 111 | Everyone is permitted to copy and distribute verbatim copies of this 112 | license document, but changing it is not allowed. 113 | 114 | 115 | Developer's Certificate of Origin 1.1 116 | 117 | By making a contribution to this project, I certify that: 118 | 119 | (a) The contribution was created in whole or in part by me and I 120 | have the right to submit it under the open source license 121 | indicated in the file; or 122 | 123 | (b) The contribution is based upon previous work that, to the best 124 | of my knowledge, is covered under an appropriate open source 125 | license and I have the right under that license to submit that 126 | work with modifications, whether created in whole or in part 127 | by me, under the same open source license (unless I am 128 | permitted to submit under a different license), as indicated 129 | in the file; or 130 | 131 | (c) The contribution was provided directly to me by some other 132 | person who certified (a), (b) or (c) and I have not modified 133 | it. 134 | 135 | (d) I understand and agree that this project and the contribution 136 | are public and that a record of the contribution (including all 137 | personal information I submit with it, including my sign-off) is 138 | maintained indefinitely and may be redistributed consistent with 139 | this project or the open source license(s) involved. 140 | EOH 141 | 142 | # Git commit-msg hook script to automatically apply DCO. 143 | # @api private 144 | HOOK_SCRIPT = <<-EOH 145 | #!/bin/sh 146 | # INSTALLED BY DCO GEM 147 | export #{ENV.select {|key, value| key =~ /^(bundle_|ruby|gem_)/i }.map {|key, value| "#{key}=#{value.inspect}"}.join(' ')} 148 | #{Thor::Util.ruby_command} #{File.expand_path('../../../bin/dco', __FILE__)} process_commit_message $1 149 | exit $? 150 | EOH 151 | 152 | # Path to the git hook script. 153 | # @api private 154 | HOOK_PATH = '.git/hooks/commit-msg' 155 | 156 | desc 'enable', 'Enable auto-sign-off for this repository' 157 | option :yes, aliases: 'y', type: :boolean, desc: 'Agree to all prompts' 158 | def enable 159 | assert_repo! 160 | unless our_hook? 161 | raise Thor::Error.new('commit-msg hook already exists, not overwriting') 162 | end 163 | say("#{DCO_TEXT}\n\n", :yellow) 164 | unless confirm?("Do you, #{git_identity}, certify that all future commits to this repository will be under the terms of the Developer Certificate of Origin? [yes/no]") 165 | raise Thor::Error.new('Not enabling auto-sign-off without approval') 166 | end 167 | IO.write(HOOK_PATH, HOOK_SCRIPT) 168 | # 755 is what the defaults from `git init` use so probably good enough. 169 | File.chmod(00755, HOOK_PATH) 170 | say('DCO auto-sign-off enabled', :green) 171 | end 172 | 173 | desc 'disable', 'Disable auto-sign-off for this repository' 174 | def disable 175 | assert_repo! 176 | unless our_hook? 177 | raise Thor::Error.new('commit-msg hook is external, not removing') 178 | end 179 | if File.exist?(HOOK_PATH) 180 | File.unlink(HOOK_PATH) 181 | end 182 | say('DCO auto-sign-off disabled', :green) 183 | end 184 | 185 | desc 'sign', 'Retroactively apply sign-off to the a branch' 186 | option :base, type: :string, banner: '', default: 'master', desc: 'Base branch (default: master)' 187 | option :behalf, aliases: 'b', type: :string, banner: '' 188 | option :yes, aliases: 'y', type: :boolean 189 | def sign(branch=nil) 190 | # What two branches are we using? 191 | base_branch = options[:base] 192 | branch ||= current_branch 193 | if base_branch == branch 194 | # This should also catch people trying to sign-off on master. 195 | raise Thor::Error.new("Cannot use #{branch} for both the base and target branch") 196 | end 197 | 198 | # First check for a stored ref under refs/original/. 199 | backup_refs = Dir[File.join(repo.repo.path, 'refs/original/refs/heads/*')] 200 | backup_refs_label = if backup_refs.size > 1 201 | "branches #{backup_refs.map {|p| File.basename(p) }.join(', ')} are" 202 | else 203 | "branch #{backup_refs.first} is" 204 | end 205 | unless backup_refs.empty? || confirm?("An existing backup of #{backup_refs_label} present from a previous filter-branch. Do you want to remove this backup and continue? [yes/no]") 206 | raise Thor::Error.new('Backup ref present, not continuing') 207 | end 208 | backup_refs.each do |ref| 209 | File.unlink(ref) 210 | end 211 | 212 | # Next examine all the commits we will be touching. 213 | commits = repo.log.between(base_branch, branch).to_a.select {|commit| !has_sign_off?(commit) } 214 | if commits.empty? 215 | raise Thor::Error.new("Branch #{branch} has no commits which require sign-off") 216 | end 217 | if !options[:behalf] && commits.any? {|commit| commit.author.email != repo_config['user.email'] } 218 | raise Thor::Error.new("Branch #{branch} contains commits not authored by you. Please use the --behalf flag when signing off for another contributor") 219 | end 220 | 221 | # Display the DCO text. 222 | say("#{DCO_TEXT}\n\n", :yellow) unless options[:behalf] 223 | 224 | # Display the list of commits. 225 | say("Going to sign-off the following commits:") 226 | commits.each do |commit| 227 | say("* #{format_commit(commit)}") 228 | end 229 | 230 | # Get confirmation. 231 | confirm_msg = if options[:behalf] 232 | "Do you, #{git_identity}, certify that these commits are contributed under the terms of the Developer Certificate of Origin as evidenced by #{options[:behalf]}? [yes/no]" 233 | else 234 | "Do you, #{git_identity}, certify that these commits are contributed under the terms of the Developer Certificate of Origin? [yes/no]" 235 | end 236 | unless confirm?(confirm_msg) 237 | raise Thor::Error.new('Not signing off on commits without approval') 238 | end 239 | 240 | # Stash if needed. 241 | did_stash = false 242 | status = repo.status 243 | unless status.changed.empty? && status.added.empty? && status.deleted.empty? 244 | say("Stashing uncommited changes before continuing") 245 | repo.lib.send(:command, 'stash', ['save', 'dco sign temp stash']) 246 | did_stash = true 247 | end 248 | 249 | # Run the filter branch. Here be dragons. Yes, I'm calling a private method. I'm sorry. 250 | filter_cmd = [Thor::Util.ruby_command, File.expand_path('../../../bin/dco', __FILE__), 'process_commit_message', '--repo', repo.dir.path] 251 | if options[:behalf] 252 | filter_cmd << '--behalf' 253 | filter_cmd << options[:behalf] 254 | end 255 | begin 256 | output = repo.lib.send(:command, 'filter-branch', ['--msg-filter', Shellwords.join(filter_cmd), "#{base_branch}..#{branch}"]) 257 | say(output) 258 | ensure 259 | if did_stash 260 | # If we had a stash, make sure to replay it. 261 | say("Unstashing previous changes") 262 | # For whatever reason, the git gem doesn't expose this. 263 | repo.lib.send(:command, 'stash', ['pop']) 264 | end 265 | end 266 | 267 | # Hopefully that worked. 268 | say("Sign-off complete", :green) 269 | say("Don't forget to use --force when pushing this branch to your git server (eg. git push --force origin #{branch})", :green) # TODO I could detect the actual remote for this branch, if any. 270 | end 271 | 272 | desc 'check', 'Check if a branch or repository has valid sign-off' 273 | option :all, type: :boolean, aliases: 'a', desc: 'Check commits, not just a single branch' 274 | option :base, type: :string, banner: '', default: 'master', desc: 'Base branch (default: master)' 275 | option :quiet, type: :boolean, aliases: 'q', desc: 'Quiet output' 276 | option :allow_author_mismatch, type: :boolean, desc: 'Allow author vs. sign-off mismatch' 277 | def check(branch=nil) 278 | branch ||= current_branch 279 | log = (options[:all] || branch == options[:base]) ? repo.log : repo.log.between(options[:base], branch) 280 | bad_commits = [] 281 | log.each do |commit| 282 | sign_off = has_sign_off?(commit) 283 | if !sign_off 284 | # No sign-off at all, tsk tsk. 285 | bad_commits << [commit, :no_sign_off] 286 | elsif !options[:allow_author_mismatch] && sign_off != "#{commit.author.name} <#{commit.author.email}>" 287 | # The signer-off and commit author don't match. 288 | bad_commits << [commit, :author_mismatch] 289 | end 290 | end 291 | 292 | if bad_commits.empty? 293 | # Yay! 294 | say("All commits are signed off", :green) unless options[:quiet] 295 | else 296 | # Something bad happened. 297 | unless options[:quiet] 298 | say("N: No Sign-off M: Author mismatch", :red) 299 | bad_commits.each do |commit, reason| 300 | reason_string = {no_sign_off: 'N', author_mismatch: 'M'}[reason] 301 | say("#{reason_string} #{format_commit(commit)}", :red) 302 | end 303 | end 304 | exit 1 305 | end 306 | end 307 | 308 | private 309 | 310 | # Modified version of Thor's #yes? to understand -y and non-interactive usage. 311 | # 312 | # @api private 313 | # @param msg [String] Message to show 314 | # @return [Boolean] 315 | def confirm?(msg) 316 | return true if options[:yes] 317 | unless STDOUT.isatty 318 | say(msg) 319 | return false 320 | end 321 | yes?(msg) 322 | end 323 | 324 | # Check that we are in a git repo that we have write access to. 325 | # 326 | # @api private 327 | # @return [void] 328 | def assert_repo! 329 | begin 330 | # Check if the repo fails to load at all. 331 | repo 332 | rescue Exception 333 | raise Thor::Error.new("#{repo_path} does not appear to be a git repository") 334 | end 335 | unless repo.repo.writable? 336 | raise Thor::Error.new("Git repository at #{repo.repo.path} is read-only") 337 | end 338 | end 339 | 340 | # Create a Git repository object for the current repo. 341 | # 342 | # @api private 343 | # @return [Git::Base] 344 | def repo 345 | @repo ||= Git.open(repo_path) 346 | end 347 | 348 | # Return and cache the git config for this repo because we use it a lot. 349 | # 350 | # @api private 351 | # @return [Hash] 352 | def repo_config 353 | @repo_config ||= repo.config 354 | end 355 | 356 | # Get the current branch but raise an error if it looks like we're on a detched head. 357 | # 358 | # @api private 359 | # @return [String] 360 | def current_branch 361 | repo.current_branch.tap {|b| raise Thor::Error.new("No explicit branch passed and current head looks detached: #{b}") if b[0] == '(' } 362 | end 363 | 364 | # Check if we are in control of the commit-msg hook. 365 | # 366 | # @api private 367 | # @return [Boolean] 368 | def our_hook? 369 | !File.exist?(HOOK_PATH) || IO.read(HOOK_PATH).include?('INSTALLED BY DCO GEM') 370 | end 371 | 372 | # Find the git identity string for the current user. 373 | # 374 | # @api private 375 | # @return [String] 376 | def git_identity 377 | "#{repo_config['user.name']} <#{repo_config['user.email']}>" 378 | end 379 | 380 | # Make a one-line version of a commit for use in displays. 381 | # 382 | # @api private 383 | # @param commit [Git::Commit] Commit object to format 384 | # @return [String] 385 | def format_commit(commit) 386 | "#{commit.sha[0..6]} #{commit.author.name} <#{commit.author.email}> #{commit.message.split(/\n/).first}" 387 | end 388 | 389 | # Check if a commit or commit message is already signed off. 390 | # 391 | # @api private 392 | # @param commit_or_message [String, Git::Commit] Commit object or message string. 393 | # @return [String, nil] 394 | def has_sign_off?(commit_or_message) 395 | message = commit_or_message.is_a?(String) ? commit_or_message : commit_or_message.message 396 | if message =~ /^Signed-off-by: (.+)$/ 397 | $1 398 | else 399 | nil 400 | end 401 | end 402 | end 403 | end 404 | --------------------------------------------------------------------------------