Guest User

Untitled

a guest
Oct 19th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. # Creates a git repo with 2^N files. N is the only arg. Creates a
  2. # bare repo called "files".
  3.  
  4. require 'digest/sha1'
  5. require 'zlib'
  6.  
  7. def filename(id)
  8. "objects/#{id[0...2]}/#{id[2..-1]}"
  9. end
  10.  
  11. def read_object(id)
  12. Zlib::Inflate.inflate(File.read(filename(id)))
  13. end
  14.  
  15. def write_object(id, bytes)
  16. `mkdir -p objects/#{id[0...2]}` # boo
  17. File.open(filename(id),'w') do |f|
  18. f.write(Zlib::Deflate.deflate(bytes))
  19. end
  20. end
  21.  
  22. def hexsha(s); Digest::SHA1.hexdigest(s); end
  23. def hex2bin(s)
  24. s2 = ""
  25. while(s.length > 0)
  26. s3 = s[0...2]
  27. s2[s2.length]=s3.to_i(16).chr
  28. s = s[2..-1]
  29. end
  30. return s2
  31. end
  32.  
  33. # keys are filenames, vals are [hash, :dir or :file]
  34. def write_tree(entries)
  35. s = ""
  36. entries.each do |filename, (hash, type)|
  37. s += (type == :dir ? "40000" : "100644")
  38. s += (" #{filename}\0#{hex2bin(hash)}")
  39. end
  40. s = "tree #{s.length}\0#{s}"
  41. sha = hexsha(s)
  42. write_object(hexsha(s), s)
  43. return sha
  44. end
  45.  
  46. def write_dense_tree(depth, base_entry)
  47. entry = (depth == 0 ? base_entry : write_dense_tree(depth - 1, base_entry))
  48. type = (depth == 0 ? :file : :dir)
  49. suffix = (depth == 0 ? ".txt" : "")
  50.  
  51. h = {}
  52. (0...16).each{|i|h[("%04b#{suffix}" % [i])]=[entry, type]}
  53. write_tree(h)
  54. end
  55.  
  56. n = ARGV[0].to_i
  57.  
  58. `mkdir files`
  59. Dir.chdir 'files'
  60. `git init --bare`
  61.  
  62. file_blob = "blob 5\0file\n"
  63. file_sha = hexsha(file_blob)
  64. write_object(file_sha, file_blob)
  65.  
  66. depth = (n >> 2) - 1
  67. tree_sha = write_dense_tree(depth ,file_sha)
  68. commit = `echo 'add files' | git commit-tree #{tree_sha}`
  69. File.write("refs/heads/master", commit)
  70. puts "Done"
Add Comment
Please, Sign In to add comment