Skip to content

Commit

Permalink
update script
Browse files Browse the repository at this point in the history
  • Loading branch information
broothie committed Jul 13, 2022
1 parent 9c22ebc commit bb93aa9
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 22 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
clones
1 change: 1 addition & 0 deletions .ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.1.2
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
source "https://rubygems.org"

gem "async", "~> 2.0"
21 changes: 21 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
GEM
remote: https://rubygems.org/
specs:
async (2.0.3)
console (~> 1.10)
io-event (~> 1.0.0)
timers (~> 4.1)
console (1.15.3)
fiber-local
fiber-local (1.0.0)
io-event (1.0.9)
timers (4.3.3)

PLATFORMS
x86_64-darwin-20

DEPENDENCIES
async (~> 2.0)

BUNDLED WITH
2.3.7
48 changes: 26 additions & 22 deletions script.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,35 @@
require 'async'
require 'fileutils'

CHARACTERS = ('0'..'9').to_a + ('a'..'f').to_a
NUM_WORKERS = ENV.fetch('NUM_WORKERS', 1)
CHARACTERS = (('0'..'9').to_a + ('a'..'f').to_a).freeze

def random_hex_char
CHARACTERS.sample
end
start_time = Time.now

def random_short_sha
Array.new(7) { random_hex_char }.join
end
Async do |task|
NUM_WORKERS.times do |worker|
task.async do
repo_path = File.join('clones', start_time.to_i.to_s, "#{worker}-self-referential-commit")
FileUtils.mkdir_p(repo_path)

if __FILE__ == $0
counter = 0
loop do
attempt = counter + 1
short_sha = random_short_sha
puts "attempt #{attempt}, short sha is #{short_sha}" if (counter % 1000).zero?
puts `git clone https://github.com/broothie/self-referential-commit.git #{repo_path}`

message = "attempt #{attempt}: #{short_sha}"
output = `git commit --allow-empty -m '#{message}'`.chomp
loop do
short_sha = Array.new(7) { CHARACTERS.sample }.join
puts "attempt with short sha #{short_sha}"

if output == "[main #{short_sha}] #{message}"
puts "success! the lucky short sha is #{short_sha}"
break
else
`git reset --hard HEAD~`
end
message = "short sha: #{short_sha}"
puts output = `git -C #{repo_path} commit --allow-empty -m '#{message}'`.chomp

counter += 1
if output == "[main #{short_sha}] #{message}"
File.write('short.sha', short_sha)
puts "success! the lucky short sha is #{short_sha}, under #{repo_path}"
task.stop
else
puts `git -C #{repo_path} reset --hard HEAD~`
puts `git -C #{repo_path} gc`
end
end
end
end
end

0 comments on commit bb93aa9

Please sign in to comment.