Skip to content

Instantly share code, notes, and snippets.

@ksss
Last active March 5, 2026 02:38
Show Gist options
  • Select an option

  • Save ksss/e229b5dadc844b129cf182fbe02198f8 to your computer and use it in GitHub Desktop.

Select an option

Save ksss/e229b5dadc844b129cf182fbe02198f8 to your computer and use it in GitHub Desktop.
Result of https://github.com/mame/ai-coding-lang-bench/ . model is gpt-5.3-codex with codex.
#!/usr/bin/env ruby
# typed: false
class MiniGit
FNV_OFFSET_BASIS = 1_469_598_103_934_665_603
FNV_PRIME = 1_099_511_628_211
MASK_64 = 0xFFFFFFFFFFFFFFFF
def initialize(base_dir)
@base_dir = base_dir
end
def run(argv)
case argv[0]
when "init"
init_repo
0
when "add"
add_file(argv[1])
when "commit"
commit(argv)
when "log"
show_log
0
else
1
end
end
private
attr_reader :base_dir
def repo_dir
File.join(base_dir, ".minigit")
end
def objects_dir
File.join(repo_dir, "objects")
end
def commits_dir
File.join(repo_dir, "commits")
end
def index_path
File.join(repo_dir, "index")
end
def head_path
File.join(repo_dir, "HEAD")
end
def init_repo
if Dir.exist?(repo_dir)
puts "Repository already initialized"
return
end
mkdir_p(objects_dir)
mkdir_p(commits_dir)
File.write(index_path, "")
File.write(head_path, "")
end
def add_file(path)
unless path && File.file?(path)
puts "File not found"
return 1
end
ensure_repo_exists
data = File.binread(path)
blob_hash = mini_hash(data)
blob_path = File.join(objects_dir, blob_hash)
File.binwrite(blob_path, data) unless File.exist?(blob_path)
staged = read_index
unless staged.include?(path)
staged << path
File.write(index_path, staged.join("\n") + "\n")
end
0
end
def commit(argv)
return 1 unless argv[1] == "-m" && argv[2]
ensure_repo_exists
staged = read_index
if staged.empty?
puts "Nothing to commit"
return 1
end
message = argv[2]
parent = read_head
parent = "NONE" if parent.empty?
timestamp = Time.now.to_i
lines = [
"parent: #{parent}",
"timestamp: #{timestamp}",
"message: #{message}",
"files:"
]
staged.sort.each do |filename|
data = File.binread(filename)
blob_hash = mini_hash(data)
blob_path = File.join(objects_dir, blob_hash)
File.binwrite(blob_path, data) unless File.exist?(blob_path)
lines << "#{filename} #{blob_hash}"
end
content = lines.join("\n") + "\n"
commit_hash = mini_hash(content.b)
File.write(File.join(commits_dir, commit_hash), content)
File.write(head_path, commit_hash)
File.write(index_path, "")
puts "Committed #{commit_hash}"
0
end
def show_log
ensure_repo_exists
current = read_head
if current.empty?
puts "No commits"
return
end
until current == "NONE" || current.empty?
commit_file = File.join(commits_dir, current)
break unless File.file?(commit_file)
info = parse_commit(File.read(commit_file))
puts "commit #{current}"
puts "Date: #{info[:timestamp]}"
puts "Message: #{info[:message]}"
puts
current = info[:parent]
end
end
def parse_commit(content)
parent = "NONE"
timestamp = ""
message = ""
content.each_line do |line|
line = line.chomp
if line.start_with?("parent: ")
parent = line.delete_prefix("parent: ")
elsif line.start_with?("timestamp: ")
timestamp = line.delete_prefix("timestamp: ")
elsif line.start_with?("message: ")
message = line.delete_prefix("message: ")
elsif line == "files:"
break
end
end
{ parent: parent, timestamp: timestamp, message: message }
end
def read_index
return [] unless File.file?(index_path)
File.readlines(index_path, chomp: true).reject(&:empty?)
end
def read_head
return "" unless File.file?(head_path)
File.read(head_path).strip
end
def ensure_repo_exists
return if Dir.exist?(repo_dir)
mkdir_p(objects_dir)
mkdir_p(commits_dir)
File.write(index_path, "") unless File.exist?(index_path)
File.write(head_path, "") unless File.exist?(head_path)
end
def mkdir_p(path)
parts = path.split(File::SEPARATOR)
current = path.start_with?(File::SEPARATOR) ? File::SEPARATOR : ""
parts.each do |part|
next if part.empty?
current = current.empty? ? part : File.join(current, part)
Dir.mkdir(current) unless Dir.exist?(current)
end
end
def mini_hash(data)
h = FNV_OFFSET_BASIS
data.each_byte do |byte|
h ^= byte
h = (h * FNV_PRIME) & MASK_64
end
format("%016x", h)
end
end
exit(MiniGit.new(Dir.pwd).run(ARGV))
class MiniGit
FNV_OFFSET_BASIS: Integer
FNV_PRIME: Integer
MASK_64: Integer
def initialize: (String base_dir) -> void
def run: (Array[String] argv) -> Integer
private
attr_reader base_dir: String
def repo_dir: -> String
def objects_dir: -> String
def commits_dir: -> String
def index_path: -> String
def head_path: -> String
def init_repo: -> void
def add_file: (String? path) -> Integer
def commit: (Array[String] argv) -> Integer
def show_log: -> void
def parse_commit: (String content) -> { parent: String, timestamp: String, message: String }
def read_index: -> Array[String]
def read_head: -> String
def ensure_repo_exists: -> void
def mkdir_p: (String path) -> void
def mini_hash: (String data) -> String
end
Read SPEC-v1.txt, implement it, and make sure test-v1.sh passes.
The implementation language is Ruby. Please develop using static type checking via steep check.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment