BVB Source Codes

grit Show loose.rb Source code

Return Download grit: download loose.rb Source code - Download grit Source code - Type:.rb
  1. #
  2. # converted from the gitrb project
  3. #
  4. # authors:
  5. #    Matthias Lederhofer <matled@gmx.net>
  6. #    Simon 'corecode' Schubert <corecode@fs.ei.tum.de>
  7. #    Scott Chacon <schacon@gmail.com>
  8. #
  9. # provides native ruby access to git objects and pack files
  10. #
  11.  
  12. require 'zlib'
  13. require 'digest/sha1'
  14. require 'grit/git-ruby/internal/raw_object'
  15. require 'tempfile'
  16.  
  17. module Grit
  18.   module GitRuby
  19.     module Internal
  20.       class LooseObjectError < StandardError
  21.       end
  22.  
  23.       class LooseStorage
  24.         def initialize(directory)
  25.           @directory = directory
  26.         end
  27.  
  28.         def [](sha1)
  29.           sha1 = sha1.unpack("H*")[0]
  30.           begin
  31.             return nil unless sha1[0...2] && sha1[2..39]
  32.             path = @directory + '/' + sha1[0...2] + '/' + sha1[2..39]
  33.             get_raw_object(open(path, 'rb') { |f| f.read })
  34.           rescue Errno::ENOENT
  35.             nil
  36.           end
  37.         end
  38.  
  39.         def get_raw_object(buf)
  40.           if buf.bytesize < 2
  41.             raise LooseObjectError, "object file too small"
  42.           end
  43.  
  44.           if legacy_loose_object?(buf)
  45.             content = Zlib::Inflate.inflate(buf)
  46.             header, content = content.split(/\0/, 2)
  47.             if !header || !content
  48.               raise LooseObjectError, "invalid object header"
  49.             end
  50.             type, size = header.split(/ /, 2)
  51.             if !%w(blob tree commit tag).include?(type) || size !~ /^\d+$/
  52.               raise LooseObjectError, "invalid object header"
  53.             end
  54.             type = type.to_sym
  55.             size = size.to_i
  56.           else
  57.             type, size, used = unpack_object_header_gently(buf)
  58.             content = Zlib::Inflate.inflate(buf[used..-1])
  59.           end
  60.           raise LooseObjectError, "size mismatch" if content.bytesize != size
  61.           return RawObject.new(type, content)
  62.         end
  63.  
  64.         # write an object to a temporary file, then atomically rename it
  65.         # into place; this ensures readers never see a half-written file
  66.         def safe_write(path, content)
  67.           f =
  68.             if RUBY_VERSION >= '1.9'
  69.               Tempfile.open("tmp_obj_", File.dirname(path), :opt => "wb")
  70.             else
  71.               Tempfile.open("tmp_obj_", File.dirname(path))
  72.             end
  73.           begin
  74.             f.write content
  75.             f.fsync
  76.             File.link(f.path, path)
  77.           rescue Errno::EEXIST
  78.             # The path already exists; we raced with another process,
  79.             # but it's OK, because by definition the content is the
  80.             # same. So we can just ignore the error.
  81.           ensure
  82.             f.unlink
  83.             f.close
  84.           end
  85.         end
  86.  
  87.         # currently, I'm using the legacy format because it's easier to do
  88.         # this function takes content and a type and writes out the loose object and returns a sha
  89.         def put_raw_object(content, type)
  90.           size = content.bytesize.to_s
  91.           LooseStorage.verify_header(type, size)
  92.  
  93.           header = "#{type} #{size}\0"
  94.           store = header + content
  95.  
  96.           sha1 = Digest::SHA1.hexdigest(store)
  97.           path = @directory+'/'+sha1[0...2]+'/'+sha1[2..40]
  98.  
  99.           if !File.exists?(path)
  100.             content = Zlib::Deflate.deflate(store)
  101.  
  102.             FileUtils.mkdir_p(@directory+'/'+sha1[0...2])
  103.             safe_write(path, content)
  104.           end
  105.           return sha1
  106.         end
  107.  
  108.         # simply figure out the sha
  109.         def self.calculate_sha(content, type)
  110.           size = content.bytesize.to_s
  111.           verify_header(type, size)
  112.           header = "#{type} #{size}\0"
  113.           store = header + content
  114.  
  115.           Digest::SHA1.hexdigest(store)
  116.         end
  117.  
  118.         def self.verify_header(type, size)
  119.           if !%w(blob tree commit tag).include?(type) || size !~ /^\d+$/
  120.             raise LooseObjectError, "invalid object header"
  121.           end
  122.         end
  123.  
  124.         # private
  125.         def unpack_object_header_gently(buf)
  126.           used = 0
  127.           c = buf.getord(used)
  128.           used += 1
  129.  
  130.           type = (c >> 4) & 7;
  131.           size = c & 15;
  132.           shift = 4;
  133.           while c & 0x80 != 0
  134.             if buf.bytesize <= used
  135.               raise LooseObjectError, "object file too short"
  136.             end
  137.             c = buf.getord(used)
  138.             used += 1
  139.  
  140.             size += (c & 0x7f) << shift
  141.             shift += 7
  142.           end
  143.           type = OBJ_TYPES[type]
  144.           if ![:blob, :tree, :commit, :tag].include?(type)
  145.             raise LooseObjectError, "invalid loose object type"
  146.           end
  147.           return [type, size, used]
  148.         end
  149.         private :unpack_object_header_gently
  150.  
  151.         def legacy_loose_object?(buf)
  152.           word = (buf.getord(0) << 8) + buf.getord(1)
  153.           buf.getord(0) == 0x78 && word % 31 == 0
  154.         end
  155.         private :legacy_loose_object?
  156.       end
  157.     end
  158.   end
  159. end
  160.  
downloadloose.rb Source code - Download grit Source code
Related Source Codes/Software:
guard-livereload - Guard::LiveReload automatically reload your browse... 2017-04-22
Begin-Latex-in-minutes - Brief Intro to LaTeX for beginners that helps you ... 2017-04-22
wicked - Use wicked to turn your controller into a wizard ... 2017-04-22
flexboxfroggy - A game for learning CSS flexbox ... 2017-04-22
phpDocumentor2 - Documentation Generator for PHP ... 2017-04-22
grr - GRR Rapid Response: remote live forensics for inci... 2017-04-22
libsvm - 2017-04-22
breakpoint - Really simple media queries in Sa 2017-04-23
CoffeeScriptRedux - 2017-04-23
Toucan - Fabulous Image Processing in Swift 2017-04-23
boto3 - AWS SDK for Python http://a... 2017-04-30
PreloadJS - PreloadJS makes preloading assets & getting ag... 2017-04-29
gotraining - Go Training Class Material : ... 2017-04-29
eigen - The Art World in Your Pocket or Your Trendy Tech C... 2017-04-29
rack-cors - Rack Middleware for handling Cross-Origin Resource... 2017-04-29
react-redux-tutorial - react-redux-tutorial in Chinese, catalog &&... 2017-04-29
swifter - Tiny http server engine written in Swift programmi... 2017-04-29
Chartbuilder - A front-end charting application that facilitates ... 2017-04-29
offline-first - 2017-04-29
plotly.py - An interactive, browser-based graphing library for... 2017-04-29

 Back to top