Permalink
Browse files

rewrote the index implementation to write in pure ruby, which both sp…

…eeds things up and deals with content much better
  • Loading branch information...
1 parent 28f28c5 commit 8f19810374686ad23dd63342b08d4ad1e65204c4 @schacon schacon committed May 29, 2008
Showing with 49 additions and 20 deletions.
  1. +14 −0 examples/ex_index.rb
  2. +1 −1 lib/grit/git-ruby.rb
  3. +1 −5 lib/grit/git-ruby/repository.rb
  4. +31 −12 lib/grit/index.rb
  5. +2 −2 lib/grit/ref.rb
View
@@ -0,0 +1,14 @@
+require 'lib/grit'
+
+count = 1
+Dir.chdir("/Users/schacon/projects/atest") do
+ r = Grit::Repo.new('.')
+ i = r.index
+ while(count < 10) do
+ fname = Time.now.to_i.to_s + count.to_s
+ i.add(fname, 'hello ' + fname)
+ count += 1
+ end
+ puts i.commit('my commit')
+ puts i.inspect
+end
View
@@ -69,7 +69,7 @@ def file_type(ref)
end
def ruby_git
- @ruby_git_repo ||= Repository.new(self, @git_dir, GitRuby.use_commit_db)
+ @ruby_git_repo ||= Repository.new(@git_dir)
end
end
@@ -13,7 +13,6 @@
require 'grit/git-ruby/internal/pack'
require 'grit/git-ruby/internal/loose'
require 'grit/git-ruby/object'
-require 'grit/git-ruby/commit_db'
module Grit
module GitRuby
@@ -24,10 +23,7 @@ class NoSuchShaFound < StandardError
attr_accessor :git_dir, :commit_db
- def initialize(git_dir, commit_db = nil)
- if use_commit_db
- @commit_db = CommitDb.new(git_dir)
- end
+ def initialize(git_dir)
@git_dir = git_dir
end
View
@@ -27,21 +27,36 @@ def add(file_path, data)
current[filename] = data
end
-
+
# Commit the contents of the index
# +message+ is the commit message
#
# Returns a String of the SHA1 of the commit
- def commit(message)
+ def commit(message, parents = nil, actor = nil)
tree_sha1 = write_tree(self.tree)
- message = message.gsub("'", "\\'")
- commit_sha1 = self.repo.git.run("echo '#{message}' | ", :commit_tree, '', {}, [tree_sha1])
+ contents = []
+ contents << ['tree', tree_sha1].join(' ')
+ parents.each do |p|
+ contents << ['parent', p].join(' ') if p
+ end if parents
+
+ config = Config.new(self.repo)
+ name = config['user.name']
+ email = config['user.email']
+
+ author_string = "#{name} <#{email}> #{Time.now.to_i}"
+ contents << ['author', author_string].join(' ')
+ contents << ['committer', author_string].join(' ')
+ contents << ''
+ contents << message
+
+ commit_sha1 = self.repo.git.ruby_git.put_raw_object(contents.join("\n"), 'commit')
# self.repo.git.update_ref({}, 'HEAD', commit_sha1)
File.open(File.join(self.repo.path, 'refs', 'heads', 'master'), 'w') do |f|
f.write(commit_sha1)
- end
+ end if commit_sha1
commit_sha1
end
@@ -51,26 +66,30 @@ def commit(message)
#
# Returns the SHA1 String of the tree
def write_tree(tree)
- lstree = []
+ tree_contents = ''
tree.each do |k, v|
case v
when String:
- lstree << "100644 blob #{write_blob(v)}\t#{k}"
+ sha = write_blob(v)
+ sha = [sha].pack("H*")
+ str = "%s %s\0%s" % ['100644', k, sha]
+ tree_contents += str
when Hash:
- lstree << "040000 tree #{write_tree(v)}\t#{k}"
+ sha = write_tree(v)
+ sha = [sha].pack("H*")
+ str = "%s %s\0%s" % ['040000', k, sha]
+ tree_contents += str
end
end
-
- lstree_string = lstree.join("\n").gsub("'", "\\'")
- self.repo.git.run("echo '#{lstree_string}' | ", :mktree, '', {}, []).chomp
+ self.repo.git.ruby_git.put_raw_object(tree_contents, 'tree')
end
# Write the blob to the index
# +data+ is the data to write
#
# Returns the SHA1 String of the blob
def write_blob(data)
- self.repo.git.run("echo '#{data}' | ", :hash_object, '', {:w => true, :stdin => true}, []).chomp
+ self.repo.git.ruby_git.put_raw_object(data, 'blob')
end
end # Index
View
@@ -105,6 +105,6 @@ def self.current(repo, options = {})
class Tag < Ref ; end
- class Remote < Ref ; end
-
+ class Remote < Ref; end
+
end # Grit

0 comments on commit 8f19810

Please sign in to comment.