Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
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...
commit 8f19810374686ad23dd63342b08d4ad1e65204c4 1 parent 28f28c5
Scott Chacon schacon authored
14 examples/ex_index.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
2  lib/grit/git-ruby.rb
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
6 lib/grit/git-ruby/repository.rb
View
@@ -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
43 lib/grit/index.rb
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,18 +66,22 @@ 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
@@ -70,7 +89,7 @@ def write_tree(tree)
#
# 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
4 lib/grit/ref.rb
View
@@ -105,6 +105,6 @@ def self.current(repo, options = {})
class Tag < Ref ; end
- class Remote < Ref ; end
-
+ class Remote < Ref; end
+
end # Grit
Please sign in to comment.
Something went wrong with that request. Please try again.