git_miner 0.2.2 → 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f0189bf9c342c04a3575700876f73dc9132eeab57ecd5127619ed591d4a9486a
4
- data.tar.gz: 01db41129cc22cd8c4fd2ba3251603925613fe23ab028fa4b57cb88d05151b15
3
+ metadata.gz: 19036e9a860b64229bdf5abee8d489b155e325c2520b709707a4417821b93f92
4
+ data.tar.gz: 1b79a6eeab4b439777646ce8763a3c35cc9c3f776794cf0d828871c6609ebae9
5
5
  SHA512:
6
- metadata.gz: a1d345cc49d44ef896a5875b7b4ab12679e72ed581301d9d5ef1500b84eb2a707af14e3d632da8d5e8a3b5654d54c2adc81e66a01b1454ffde2b16f2b8b3664d
7
- data.tar.gz: f1ace38aefce766c9265269888b1c6f625461095ef5327a35723af392879d8c21e1c56a4c6d908b7f8eeda077bf5750e714b11477355b879449cce3d706aea13
6
+ metadata.gz: be3d54581beda0968cc66199a8d67de09633f7fa4af3073c590fe18990c1e51419deb14cd2a49e3e792ab47160368f907eac17372154659d1de2b0c9935eafe4
7
+ data.tar.gz: 185f6efc588f2aed729856911a5bd34af54e3b3a01c5018354440ad3ce560cd662e5dfbcb4147af49ce2bb80c8a4996674b82d2886cf6693aa89e44a9ed987d7
data/Gemfile.lock CHANGED
@@ -1,32 +1,32 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- git_miner (0.2.1)
4
+ git_miner (0.2.4)
5
5
  concurrent-ruby (~> 1.1)
6
6
  parallel (~> 1.17)
7
7
 
8
8
  GEM
9
9
  remote: https://rubygems.org/
10
10
  specs:
11
- concurrent-ruby (1.1.9)
12
- diff-lcs (1.3)
13
- parallel (1.21.0)
14
- rake (13.0.1)
15
- rake-compiler (1.0.7)
11
+ concurrent-ruby (1.1.10)
12
+ diff-lcs (1.5.0)
13
+ parallel (1.22.1)
14
+ rake (13.0.6)
15
+ rake-compiler (1.2.1)
16
16
  rake
17
- rspec (3.8.0)
18
- rspec-core (~> 3.8.0)
19
- rspec-expectations (~> 3.8.0)
20
- rspec-mocks (~> 3.8.0)
21
- rspec-core (3.8.0)
22
- rspec-support (~> 3.8.0)
23
- rspec-expectations (3.8.3)
17
+ rspec (3.12.0)
18
+ rspec-core (~> 3.12.0)
19
+ rspec-expectations (~> 3.12.0)
20
+ rspec-mocks (~> 3.12.0)
21
+ rspec-core (3.12.0)
22
+ rspec-support (~> 3.12.0)
23
+ rspec-expectations (3.12.1)
24
24
  diff-lcs (>= 1.2.0, < 2.0)
25
- rspec-support (~> 3.8.0)
26
- rspec-mocks (3.8.0)
25
+ rspec-support (~> 3.12.0)
26
+ rspec-mocks (3.12.1)
27
27
  diff-lcs (>= 1.2.0, < 2.0)
28
- rspec-support (~> 3.8.0)
29
- rspec-support (3.8.0)
28
+ rspec-support (~> 3.12.0)
29
+ rspec-support (3.12.0)
30
30
 
31
31
  PLATFORMS
32
32
  ruby
data/README.md CHANGED
@@ -4,28 +4,29 @@ Pet project I built to experiment with different concepts.
4
4
 
5
5
  GitMiner allow "mining" of vanity Git SHA1 prefixes.
6
6
 
7
- This gem is a work in progress (core functionality is working).
7
+ The HEAD commit is altered via variations over committer and author timestamp adjustments. Other commit metadata such as commit message or description are left as their original.
8
8
 
9
9
 
10
10
  ## Installation
11
11
 
12
- Rubygem:
12
+ These options will add the `git-mine` binary which act as a Git custom command: `git mine`.
13
+
14
+ ### Rubygem
15
+
13
16
  ```ruby
14
17
  gem 'git_miner'
15
18
  ```
16
19
 
17
- Development:
20
+ ### Manual
21
+
18
22
  ```
19
23
  gem build git_miner.gemspec
20
24
  gem install --local git_miner-*.gem
21
25
  ```
22
26
 
23
- The installation will add the `git-mine` binary which act as a Git custom command: `git mine`.
24
-
25
-
26
27
  ## Usage
27
28
 
28
- Though `git`, using `git mine [DESIRED_PREFIX]` will invoke the required logic.
29
+ `git mine [DESIRED_PREFIX]` will amend the current HEAD commit with a new mined SHA.
29
30
 
30
31
  Eg.:
31
32
  ```
@@ -34,9 +35,19 @@ git mine c0ffee
34
35
 
35
36
  Some extra options are available (experimental):
36
37
  ```
37
- $ git-mine -h
38
- Usage: example.rb [options]
38
+ $ git mine -h
39
+ Usage: git mine [options]
39
40
  --engine [ruby|c] Set the engine (default: ruby)
40
41
  --dispatch [simple|parallel] Set the dispatch (default: parallel)
42
+ --verbose Run verbosely (default: false)
43
+ --register [prefix] Register automated post commit git hook
44
+ -v, --version Returns the current version
41
45
  -h, --help Show this message
42
46
  ```
47
+
48
+
49
+ ### Development
50
+
51
+ ```
52
+ [path]/git_miner/bin/git-mine ...
53
+ ```
data/bin/git-mine CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  STDOUT.sync = true
4
4
 
5
- require 'git_miner'
5
+ require File.join(__dir__, '..', 'lib', 'git_miner')
6
6
 
7
7
  require 'optparse'
8
8
 
@@ -12,21 +12,31 @@ options = {
12
12
  }
13
13
 
14
14
  parser = OptionParser.new do |opts|
15
- opts.banner = "Usage: example.rb [options]"
15
+ opts.banner = "Usage: git mine [options]"
16
16
 
17
- opts.on("--engine [ruby|c]", [:ruby, :c], "Set the engine (default: ruby)") do |x|
18
- options[:engine] = x
17
+ opts.on("--engine [ruby|c]", [:ruby, :c], "Set the engine (default: ruby)") do |engine|
18
+ options[:engine] = engine
19
19
  end
20
20
 
21
- opts.on("--dispatch [simple|parallel]", [:simple, :parallel], "Set the dispatch (default: parallel)") do |x|
22
- options[:dispatch] = x
21
+ opts.on("--dispatch [simple|parallel]", [:simple, :parallel], "Set the dispatch (default: parallel)") do |dispatch|
22
+ options[:dispatch] = dispatch
23
23
  end
24
24
 
25
- opts.on("-v", "--[no-]verbose", "Run verbosely (default: false)") do |v|
26
- options[:verbose] = v
25
+ opts.on("--verbose", "Run verbosely (default: false)") do |verbose|
26
+ options[:verbose] = verbose
27
27
  end
28
28
 
29
- opts.on_tail("--version", "Returns the current version") do |v|
29
+ opts.on("--register [prefix]", "Register automated post commit git hook") do |prefix|
30
+ if prefix.nil?
31
+ puts parser
32
+ exit 1
33
+ end
34
+
35
+ GitMiner::RegisterHook.register(prefix: prefix)
36
+ exit
37
+ end
38
+
39
+ opts.on_tail("-v", "--version", "Returns the current version") do
30
40
  puts "GitMiner #{GitMiner::VERSION}"
31
41
  exit
32
42
  end
@@ -1,14 +1,15 @@
1
1
  #include "ruby.h"
2
- #include "openssl/sha.h"
2
+
3
3
  #include <stdio.h>
4
4
  #include <stdbool.h>
5
+ #include <openssl/sha.h>
5
6
 
6
7
  VALUE GitMinerExt = Qnil;
7
8
 
8
9
  void Init_git_miner_ext();
9
10
 
10
11
  VALUE rb_sha1_hexdigest(VALUE self, VALUE rbString);
11
- VALUE rb_sha1_mine(VALUE self, int authorOffset, int committerOffset, int qty);
12
+ VALUE rb_sha1_mine(VALUE self, VALUE vAuthorOffset, VALUE vCommitterOffset, VALUE vQty);
12
13
 
13
14
  void Init_git_miner_ext() {
14
15
  GitMinerExt = rb_define_module("GitMinerExt");
@@ -18,9 +19,9 @@ void Init_git_miner_ext() {
18
19
 
19
20
  VALUE rb_sha1_hexdigest(VALUE self, VALUE rbString) {
20
21
  unsigned char result[SHA_DIGEST_LENGTH];
21
- const char *string = StringValueCStr(rbString);
22
+ const char* string = StringValueCStr(rbString);
22
23
 
23
- SHA1(string, strlen(string), result);
24
+ SHA1( (unsigned char*) string, strlen(string), result);
24
25
 
25
26
  unsigned char resultHex[SHA_DIGEST_LENGTH*2 + 1];
26
27
  int i = 0;
@@ -28,11 +29,15 @@ VALUE rb_sha1_hexdigest(VALUE self, VALUE rbString) {
28
29
  sprintf((char*)&(resultHex[i*2]), "%02x", result[i]);
29
30
  }
30
31
 
31
- printf("ShaGenDebug: %s\n", resultHex);
32
- return rb_str_new_cstr(resultHex);
32
+ printf("ShaGenDebug: %s\n",(char*) resultHex);
33
+ return rb_str_new_cstr((char*) resultHex);
33
34
  }
34
35
 
35
- VALUE rb_sha1_mine(VALUE self, int authorOffset, int committerOffset, int qty) {
36
+ VALUE rb_sha1_mine(VALUE self, VALUE vAuthorOffset, VALUE vCommitterOffset, VALUE vQty) {
37
+ long authorOffset = FIX2LONG(vAuthorOffset);
38
+ long committerOffset = FIX2LONG(vCommitterOffset);
39
+ int qty = FIX2INT(vQty);
40
+
36
41
  VALUE rbPrefix = rb_iv_get(self, "@prefix");
37
42
  char* prefix = StringValueCStr(rbPrefix);
38
43
 
@@ -75,24 +80,25 @@ VALUE rb_sha1_mine(VALUE self, int authorOffset, int committerOffset, int qty) {
75
80
  gitHeadTree,
76
81
  gitHeadParent,
77
82
  gitHeadAuthorPrefix,
78
- timestamp - authorOffset,
83
+ (int) (timestamp - authorOffset),
79
84
  gitHeadCommitterPrefix,
80
- timestamp - committerOffset,
85
+ (int) (timestamp - committerOffset),
81
86
  gitHeadMessage
82
87
  );
83
88
 
84
- sprintf(commitPrefix, "commit %d", strlen(currentHead));
89
+ sprintf(commitPrefix, "commit %d", (int) strlen(currentHead));
85
90
  sprintf(currentCommit, "%s_%s", commitPrefix, currentHead);
86
91
  currentCommit[strlen(commitPrefix)] = '\0';
87
92
 
88
- shaLen = strlen(commitPrefix) + 1 + strlen(currentHead);
89
- SHA1(currentCommit, shaLen, currentSha);
93
+ shaLen = (int) strlen(commitPrefix) + 1 + (int) strlen(currentHead);
94
+
95
+ SHA1( (unsigned char*) currentCommit, shaLen, currentSha);
90
96
 
91
97
  for (int j = 0; j < SHA_DIGEST_LENGTH; j++) {
92
98
  sprintf((char*)&(currentHex[j*2]), "%02x", currentSha[j]);
93
99
  }
94
100
 
95
- int prefixLen = strlen(prefix);
101
+ int prefixLen = (int) strlen(prefix);
96
102
  bool match = true;
97
103
  for(int j = 0; j < prefixLen; j++) {
98
104
  match = match && (prefix[j] == currentHex[j]);
@@ -7,5 +7,8 @@ extension_name = 'git_miner_ext'
7
7
  # The destination
8
8
  dir_config(extension_name)
9
9
 
10
+ # Requirement for SHA1
11
+ have_library("crypto")
12
+
10
13
  # Do the work
11
14
  create_makefile(extension_name)
@@ -1,10 +1,11 @@
1
1
  module GitMiner
2
2
  class Core
3
3
  def initialize(engine:, dispatch:, prefix:)
4
- GitMiner.logger.info("Initializing git miner.")
5
- GitMiner.logger.info("Prefix: #{prefix}")
6
- GitMiner.logger.debug("Engine: #{engine::IDENTIFIER}")
7
- GitMiner.logger.debug("Dispatch: #{dispatch::IDENTIFIER}")
4
+ GitMiner.logger.info("Initializing")
5
+
6
+ GitMiner.logger.debug("- Prefix: #{prefix}")
7
+ GitMiner.logger.debug("- Engine: #{engine::IDENTIFIER}")
8
+ GitMiner.logger.debug("- Dispatch: #{dispatch::IDENTIFIER}")
8
9
 
9
10
  @prefix = prefix
10
11
 
@@ -38,10 +39,10 @@ module GitMiner
38
39
  end
39
40
 
40
41
  unless @prefix[/^[0-9a-f]{1,32}$/]
41
- raise "Invalid prefix, expected '^[0-9a-f]{1,32}$'"
42
+ raise(UserError, "Invalid prefix, expected '^[0-9a-f]{1,32}$'")
42
43
  end
43
44
 
44
- GitMiner.logger.debug("Validations: Successful")
45
+ GitMiner.logger.debug("Prefix validation: Accepted")
45
46
  end
46
47
 
47
48
  def mine
@@ -56,8 +57,6 @@ module GitMiner
56
57
  GitMiner.logger.debug("Mining completed.")
57
58
  GitMiner.logger.debug("Author offset: #{@mining_result.author_offset}")
58
59
  GitMiner.logger.debug("Committer offset: #{@mining_result.committer_offset}")
59
-
60
- GitMiner.logger.info("New SHA: #{@mining_result.sha}")
61
60
  end
62
61
 
63
62
  def alter
@@ -66,7 +65,10 @@ module GitMiner
66
65
  author_date = @now - @mining_result.author_offset
67
66
  committer_date = @now - @mining_result.committer_offset
68
67
 
68
+ GitMiner.logger.debug("Amending git")
69
69
  GitUtil.update_current_ref(author_date, committer_date)
70
+
71
+ GitMiner.logger.info("New SHA: #{@mining_result.sha}")
70
72
  end
71
73
  end
72
74
  end
@@ -34,9 +34,10 @@ module GitMiner
34
34
  private
35
35
 
36
36
  def shell(cmd, environment: {}, stdin_data: nil)
37
- GitMiner.logger.debug("System call: #{cmd}")
38
- GitMiner.logger.debug("environment: #{environment.inspect}") unless environment.empty?
39
- GitMiner.logger.debug("stdin_data: #{stdin_data}") if stdin_data
37
+ GitMiner.logger.debug("System call")
38
+ GitMiner.logger.debug("- cmd: #{cmd}")
39
+ GitMiner.logger.debug("- environment: #{environment.inspect}") unless environment.empty?
40
+ GitMiner.logger.debug("- stdin: #{stdin_data}") if stdin_data
40
41
 
41
42
  output, status = Open3.capture2(environment, cmd, stdin_data: stdin_data) #, chdir: @working_directory
42
43
 
@@ -44,7 +45,7 @@ module GitMiner
44
45
  raise "Error on system call: #{output}, #{status}"
45
46
  end
46
47
 
47
- GitMiner.logger.debug("result: #{output.strip}")
48
+ GitMiner.logger.debug("- result => #{output.strip}")
48
49
 
49
50
  output
50
51
  end
@@ -28,22 +28,24 @@ module GitMiner
28
28
  end
29
29
 
30
30
  def report(count)
31
- info = []
32
-
33
31
  percentage = count * 100 / @combinations.to_f
34
- info << "#{'%.2f' % percentage}%"
32
+ info = []
35
33
 
36
34
  historic_count = @historic.last[:count] - @historic.first[:count]
37
35
  historic_delay = @historic.last[:timestamp] - @historic.first[:timestamp]
38
36
  if historic_delay > 0
39
37
  per_sec = historic_count / historic_delay
40
- info << "hash: #{'%.2f' % per_sec}/s"
38
+ info << "hash: #{'%.0f' % per_sec}/s"
41
39
 
42
40
  per_sec = (@historic.last[:batch] - @historic.first[:batch]) / historic_delay
43
41
  info << "batches: #{'%.2f' % per_sec}/s"
44
42
  end
45
43
 
46
- GitMiner.logger.info(info.join(', '))
44
+ if info.empty?
45
+ GitMiner.logger.info("Mining estimate #{'%.2f' % percentage}%")
46
+ else
47
+ GitMiner.logger.info("Mining estimate #{'%.2f' % percentage}% (#{info.join(', ')})")
48
+ end
47
49
  end
48
50
  end
49
51
  end
@@ -0,0 +1,29 @@
1
+ require 'fileutils'
2
+
3
+ module GitMiner
4
+ class RegisterHook
5
+ FILENAME = ".git/hooks/post-commit"
6
+
7
+ class << self
8
+ def register(prefix:)
9
+ GitMiner.logger.info("Writing hook to #{FILENAME}")
10
+
11
+ if File.exists?(FILENAME)
12
+ File.open(FILENAME, 'a') do |file|
13
+ file.puts("")
14
+ end
15
+ else
16
+ FileUtils.touch(FILENAME)
17
+ FileUtils.chmod("+x", FILENAME)
18
+ end
19
+
20
+ File.open(FILENAME, 'a') do |file|
21
+ file.puts(
22
+ "# GitMiner hook",
23
+ "git mine #{prefix}"
24
+ )
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -1,3 +1,3 @@
1
1
  module GitMiner
2
- VERSION = "0.2.2"
2
+ VERSION = "0.2.4"
3
3
  end
data/lib/git_miner.rb CHANGED
@@ -4,13 +4,25 @@ Dir[File.join(__dir__, 'git_miner', '**', '*.rb')].each do |file|
4
4
  end
5
5
 
6
6
  module GitMiner
7
- class Error < StandardError; end
7
+ class UserError < StandardError; end
8
8
 
9
- def self.logger
10
- @logger ||= Logger.new(STDOUT)
11
- end
9
+ class << self
10
+ def logger
11
+ return @logger if defined?(@logger)
12
+
13
+ @logger = Logger.new(STDOUT)
14
+
15
+ @logger.formatter = proc do |severity, datetime, _progname, msg|
16
+ date_format = datetime.strftime("%Y-%m-%d %H:%M:%S")
17
+
18
+ if @logger.level == Logger::INFO
19
+ "[GitMiner] #{msg}\n"
20
+ else
21
+ "[GitMiner] #{date_format} #{severity}: #{msg}\n"
22
+ end
23
+ end
12
24
 
13
- def self.logger=(logger)
14
- @logger = logger
25
+ @logger
26
+ end
15
27
  end
16
28
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: git_miner
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Thierry Joyal
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-12-26 00:00:00.000000000 Z
11
+ date: 2022-12-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: parallel
@@ -128,6 +128,7 @@ files:
128
128
  - lib/git_miner/group_manager.rb
129
129
  - lib/git_miner/mining_result.rb
130
130
  - lib/git_miner/progress.rb
131
+ - lib/git_miner/utils/register_hook.rb
131
132
  - lib/git_miner/version.rb
132
133
  homepage: https://github.com/tjoyal/git_miner
133
134
  licenses: []
@@ -149,7 +150,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
149
150
  - !ruby/object:Gem::Version
150
151
  version: '0'
151
152
  requirements: []
152
- rubygems_version: 3.2.32
153
+ rubygems_version: 3.3.26
153
154
  signing_key:
154
155
  specification_version: 4
155
156
  summary: git-mine shell executable