pre-commit-sign 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/bin/sign-commit +24 -0
  3. data/lib/pre-commit-sign.rb +71 -0
  4. metadata +48 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7d34fd4779846250edca0606f141e3daa993e3ab
4
+ data.tar.gz: 047f0fcc034093ecdcb2cd031258cb82afc01724
5
+ SHA512:
6
+ metadata.gz: e7e8c907747a29fd1ffaad82490e9a127bbebfaf74d60e85e31cd7d74e79c991ed04fd54e02bf6dc0d5443abd24266b73f082efb4945d9d6b8f59d4ffc0d6849
7
+ data.tar.gz: 1964f4ed3d8afbfa6458e9f742d694b2bbbf44747789ac6814e5b09581fb0d27eb36393c7855afec955ca45b39ca1e82579258eddea3a65b6c79a18566af51df
data/bin/sign-commit ADDED
@@ -0,0 +1,24 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'pre-commit-sign'
5
+
6
+ if ARGV.empty?
7
+ puts "Usage: #{$PROGRAM_NAME} <file>\nWhere file is commit message path"
8
+ exit(false)
9
+ elsif !File.exist?(ARGV[0])
10
+ puts "File #{ARGV[0]} does not exist."
11
+ exit(false)
12
+ else
13
+ pc = PrecommitSign.new(ARGV[0])
14
+ if pc.commit_signature? && !pc.valid_signature?
15
+ puts "Existing signature in commit message is invalid (#{pc.existing_signature}), re-signing"
16
+ elsif pc.commit_signature? && pc.valid_signature?
17
+ puts 'Existing signature in commit message is valid, not signing'
18
+ end
19
+
20
+ unless pc.valid_signature?
21
+ pc.write_signature
22
+ puts "Signed commit with #{pc.signature}"
23
+ end
24
+ end
@@ -0,0 +1,71 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'date'
4
+ require 'digest'
5
+
6
+ # Class for signing Git commit messages with a computed hash
7
+ class PrecommitSign
8
+ SIG_KEY = 'Precommit-Verified'
9
+
10
+ attr_accessor :message
11
+ attr_writer :date # Manually set a commit date with a Time object
12
+
13
+ def initialize(message_file = nil)
14
+ @message_file = message_file
15
+ @message = IO.read(@message_file) unless @message_file.nil?
16
+ end
17
+
18
+ def self.from_message(message)
19
+ instance = new
20
+ instance.message = message
21
+ instance
22
+ end
23
+
24
+ def full_message
25
+ message
26
+ end
27
+
28
+ # Ignores comments, leading and trailing whitespace and the signature footer
29
+ def real_message
30
+ m = full_message.split("\n").reject { |l| l.start_with?('#', "#{SIG_KEY}:") }.join("\n")
31
+ /\A\s*(.*?)\s*\Z/m.match(m).captures.first
32
+ end
33
+
34
+ def date
35
+ if ENV.key?('GIT_AUTHOR_DATE')
36
+ DateTime.strptime(ENV['GIT_AUTHOR_DATE'], '@%s %z').to_time.utc
37
+ else
38
+ (@date || Time.now).utc
39
+ end
40
+ end
41
+
42
+ def commit_title
43
+ real_message.split("\n").first
44
+ end
45
+
46
+ def commit_message
47
+ real_message.split("\n")[2..-1].join("\n")
48
+ end
49
+
50
+ def signature
51
+ Digest::SHA256.hexdigest("#{real_message}#{date}")
52
+ end
53
+
54
+ def commit_signature?
55
+ /^Precommit-Verified:/.match?(full_message)
56
+ end
57
+
58
+ def valid_signature?
59
+ existing_signature == signature
60
+ end
61
+
62
+ def existing_signature
63
+ /#{SIG_KEY}:\s*([a-f0-9]+)$/.match(full_message)&.captures&.first
64
+ end
65
+
66
+ # Real message with signature
67
+ def write_signature
68
+ self.message = "#{real_message.chomp}\n\n#{SIG_KEY}: #{signature}\n"
69
+ IO.write(@message_file, message, 0, mode: 'w') unless @message_file.nil?
70
+ end
71
+ end
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pre-commit-sign
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Matt Kulka
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-06-14 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: This pre-commit plugin will hash certain fields of the commit message
14
+ and sign it.
15
+ email: matt@lqx.net
16
+ executables:
17
+ - sign-commit
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - bin/sign-commit
22
+ - lib/pre-commit-sign.rb
23
+ homepage: https://github.com/mattlqx/pre-commit-sign
24
+ licenses:
25
+ - MIT
26
+ metadata:
27
+ source_code_uri: https://github.com/mattlqx/pre-commit-sign
28
+ post_install_message:
29
+ rdoc_options: []
30
+ require_paths:
31
+ - lib
32
+ required_ruby_version: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: '2.3'
37
+ required_rubygems_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubyforge_project:
44
+ rubygems_version: 2.6.13
45
+ signing_key:
46
+ specification_version: 4
47
+ summary: Pre-commit plugin for signing commits
48
+ test_files: []