1. carbonica
  2. amp

Commits

adgar  committed 292ea02

Added a basic git data structure object for loose objects. Will refine soon.

  • Participants
  • Parent commits 090b60d
  • Branches default

Comments (0)

Files changed (6)

File Rakefile

View file
   self.summary = "Version Control in Ruby. Mercurial Compatible. Big Ideas."
   self.readme_file  = "README.md"
   # Only used by the server
-  extra_deps << ["sinatra", ">= 1.0.0"]  << ["haml", ">= 3.0"]
+  extra_deps     << ["sinatra", ">= 1.0.0"]  << ["haml", ">= 3.0"]
   extra_dev_deps << ["rtfm", ">= 0.5.1"] << ["yard", ">= 0.4.0"] << ["minitest", ">= 1.5.0"] << ["yard-amp", ">= 0.4.0"]
 end
  

File lib/amp.rb

View file
       autoload :LocalRepository,         "amp/repository/git/repositories/local_repository.rb"
       autoload :GitPicker,               "amp/repository/git/repository.rb"
       autoload :StagingArea,             "amp/repository/git/repo_format/staging_area.rb"
+      autoload :RawObject,               "amp/repository/git/repo_format/raw_object.rb"
+      autoload :LooseObject,             "amp/repository/git/repo_format/loose_object.rb"
     end
     
     module Mercurial

File lib/amp/repository/git/repo_format/loose_object.rb

View file
+##################################################################
+#                  Licensing Information                         #
+#                                                                #
+#  The following code is licensed, as standalone code, under     #
+#  the Ruby License, unless otherwise directed within the code.  #
+#                                                                #
+#  For information on the license of this code when distributed  #
+#  with and used in conjunction with the other modules in the    #
+#  Amp project, please see the root-level LICENSE file.          #
+#                                                                #
+#  © Michael J. Edgar and Ari Brown, 2009-2010                   #
+#                                                                #
+##################################################################
+
+# This was written by reading the Git Book. No source code was
+# examined to produce this code. It is the original work of its
+# creators, Michael Edgar and Ari Brown.
+#
+# http://book.git-scm.com/7_how_git_stores_objects.html
+
+module Amp
+  module Repositories
+    module Git
+      ##
+      # = LooseObject
+      #
+      # A single loose object (tree, tag, commit, etc.) in the Git system.
+      # Its type and content will be determined after we read the file.
+      #
+      # It is uniquely identified by a SHA1 hash.
+      class LooseObject < RawObject
+        ##
+        # Initializes the RawObject. Needs a hash to identify it and
+        # an opener. The opener should point to the .git directory.
+        #
+        # @param [String] hsh the hash to use to find the object
+        # @param [Amp::Opener] opener the opener to use to open the
+        #   object file
+        def initialize(hsh, opener, create = false)
+          require 'scanf'
+          path = File.join("objects", hsh[0..1], hsh[2..40])
+          mode = "r"
+          begin
+            opener.open(path, mode) do |fp|
+              @type, content_size = fp.scanf("%s %d")
+              fp.seek(@type.size + 1 + content_size.to_s.size + 1, IO::SEEK_SET)
+              @content = fp.read(content_size)
+            end
+          rescue SystemCallError
+            if create
+              FileUtils.mkdir_p(opener.join("objects", hsh[0..1]))
+              mode = "w+"
+              retry
+            else
+              raise
+            end
+          end
+        end
+      
+      end
+    end
+  end
+end

File lib/amp/repository/git/repo_format/raw_object.rb

View file
+##################################################################
+#                  Licensing Information                         #
+#                                                                #
+#  The following code is licensed, as standalone code, under     #
+#  the Ruby License, unless otherwise directed within the code.  #
+#                                                                #
+#  For information on the license of this code when distributed  #
+#  with and used in conjunction with the other modules in the    #
+#  Amp project, please see the root-level LICENSE file.          #
+#                                                                #
+#  © Michael J. Edgar and Ari Brown, 2009-2010                   #
+#                                                                #
+##################################################################
+
+# This was written by reading the Git Book. No source code was
+# examined to produce this code. It is the original work of its
+# creators, Michael Edgar and Ari Brown.
+#
+# http://book.git-scm.com/7_how_git_stores_objects.html
+
+module Amp
+  module Repositories    
+    module Git
+      ##
+      # = LooseObject
+      #
+      # A single loose object (tree, tag, commit, etc.) in the Git system.
+      # Its type and content will be determined after we read the file.
+      #
+      # It is uniquely identified by a SHA1 hash.
+      class RawObject
+        attr_reader :type, :content
+      
+      end
+    end
+  end
+end

File lib/amp/repository/mercurial/repositories/http_repository.rb

View file
                 @capabilities[k] = true
               end
             end
-          rescue
-            @capabilities = []
+          rescue StandardError => err
+            UI.debug(err.to_s)
+            @capabilities = {}
           end
           @capabilities
         end

File test/git_tests/test_loose_object.rb

View file
+##################################################################
+#                  Licensing Information                         #
+#                                                                #
+#  The following code is licensed, as standalone code, under     #
+#  the Ruby License, unless otherwise directed within the code.  #
+#                                                                #
+#  For information on the license of this code when distributed  #
+#  with and used in conjunction with the other modules in the    #
+#  Amp project, please see the root-level LICENSE file.          #
+#                                                                #
+#  © Michael J. Edgar and Ari Brown, 2009-2010                   #
+#                                                                #
+##################################################################
+
+require File.join(File.expand_path(File.dirname(__FILE__)), '../testutilities')
+require File.expand_path(File.join(File.dirname(__FILE__), "../../lib/amp"))
+
+class TestGitLooseObject < AmpTestCase
+  def setup
+    super
+    @simple_file = self.write_file "objects/ab/looseobject1" do |io|
+      io << "commit 10\0abcdefghij"
+    end
+    @extra_garbage_file = self.write_file "objects/ab/looseobject2" do |io|
+      io << "tree 4\0mikeedgarisacoolguy"
+    end
+    @empty_file = self.write_file "objects/ab/looseobject3" do |io|
+      io << "tag 0\0hellogarbage"
+    end
+    @opener = Amp::Opener.new(tempdir)
+    @opener.default = :open_file
+  end
+  
+  def test_simple_file
+    obj = Amp::Repositories::Git::LooseObject.new("ablooseobject1", @opener)
+    assert obj.type == "commit"
+    assert obj.content == "abcdefghij"
+  end
+
+end