Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added some simple write ops : add, remove, commit

  • Loading branch information...
commit 28f28c504f3ebb647dadd5d504aaf8133aecadd9 1 parent 1535929
@schacon schacon authored
View
101 API.txt
@@ -0,0 +1,101 @@
+== TODO ==
+
+* Add remote branch references (Grit::Remote)
+* Add status - what is modified, staged
+
+g.checkout('new_branch')
+g.checkout(g.branch('new_branch'))
+
+g.branch(name).merge(branch2)
+g.branch(branch2).merge # merges HEAD with branch2
+
+g.branch(name).in_branch(message) { # add files } # auto-commits
+g.merge('new_branch')
+g.merge('origin/remote_branch')
+g.merge(b.branch('master'))
+g.merge([branch1, branch2])
+
+r = g.add_remote(name, uri) # Git::Remote
+r = g.add_remote(name, Git::Base) # Git::Remote
+
+g.remotes # array of Git::Remotes
+g.remote(name).fetch
+g.remote(name).remove
+g.remote(name).merge
+g.remote(name).merge(branch)
+
+g.fetch
+g.fetch(g.remotes.first)
+
+g.pull
+g.pull(Git::Repo, Git::Branch) # fetch and a merge
+
+g.add_tag('tag_name') # returns Git::Tag
+
+g.repack
+
+g.push
+g.push(g.remote('name'))
+
+g.reset # defaults to HEAD
+g.reset_hard(Git::Commit)
+
+g.branch('new_branch') # creates new or fetches existing
+g.branch('new_branch').checkout
+g.branch('new_branch').delete
+g.branch('existing_branch').checkout
+
+
+
+
+
+require 'mojombo-grit'
+
+include Grit
+Grit.debug
+Grit.use_pure_ruby
+
+repo = Repo.new("/Users/tom/dev/grit")
+
+= Commit Log
+
+repo.commits('mybranch')
+repo.commits('40d3057d09a7a4d61059bca9dca5ae698de58cbe')
+repo.commits('v0.1')
+
+repo.log('mybranch', 100, 20)
+
+head = repo.commits.first
+head.id
+# => "e80bbd2ce67651aa18e57fb0b43618ad4baf7750"
+head.parents
+# => [#<Grit::Commit "91169e1f5fa4de2eaea3f176461f5dc784796769">]
+head.tree
+# => #<Grit::Tree "3536eb9abac69c3e4db583ad38f3d30f8db4771f">
+head.author
+# => #<Grit::Actor "Tom Preston-Werner <tom@mojombo.com>">
+head.authored_date
+# => Wed Oct 24 22:02:31 -0700 2007
+head.committer
+# => #<Grit::Actor "Tom Preston-Werner <tom@mojombo.com>">
+head.committed_date
+# => Wed Oct 24 22:02:31 -0700 2007
+head.message
+# => "add Actor inspect"
+contents = tree.contents
+ # => [#<Grit::Blob "4ebc8aea50e0a67e000ba29a30809d0a7b9b2666">,
+ #<Grit::Blob "81d2c27608b352814cbe979a6acd678d30219678">,
+ #<Grit::Tree "c3d07b0083f01a6e1ac969a0f32b8d06f20c62e5">,
+ #<Grit::Tree "4d00fe177a8407dbbc64a24dbfc564762c0922d8">]
+blob.id
+# => "4ebc8aea50e0a67e000ba29a30809d0a7b9b2666"
+blob.name
+# => "README.txt"
+blob.mode
+# => "100644"
+blob.size
+# => 7726
+blob.data
+
+repo.blob("4ebc8aea50e0a67e000ba29a30809d0a7b9b2666")
+# => #<Grit::Blob "4ebc8aea50e0a67e000ba29a30809d0a7b9b2666">
View
1  PURE_TODO
@@ -8,6 +8,7 @@ Completed
** lib/grit/tree.rb:16: output = repo.git.ls_tree({}, treeish, *paths)
+
lib/grit/commit.rb:74: repo.git.rev_list({}, ref).strip.split("\n").size
lib/grit/commit.rb:92: output = repo.git.rev_list(actual_options, ref)
lib/grit/commit.rb:94: output = repo.git.rev_list(actual_options.merge(:all => true))
View
129 benchmarks.rb
@@ -0,0 +1,129 @@
+require 'fileutils'
+require 'benchmark'
+require 'rubygems'
+require 'ruby-prof'
+require 'memcache'
+require 'pp'
+
+
+#require 'grit'
+require 'lib/grit'
+
+def main
+ @wbare = File.expand_path(File.join('test', 'dot_git'))
+
+ in_temp_dir do
+ #result = RubyProf.profile do
+
+ git = Grit::Repo.new('.')
+ puts Grit::VERSION
+
+ Grit::GitRuby.use_commit_db = true
+ #Grit::GitRuby.cache_client = MemCache.new 'localhost:11211', :namespace => 'grit'
+ #Grit.debug = true
+
+ #pp Grit::GitRuby.cache_client.stats
+
+ commit1 = '5e3ee1198672257164ce3fe31dea3e40848e68d5'
+ commit2 = 'ca8a30f5a7f0f163bbe3b6f0abf18a6c83b0687a'
+
+ Benchmark.bm(8) do |x|
+
+ run_code(x, 'packobj') do
+ @commit = git.commit('5e3ee1198672257164ce3fe31dea3e40848e68d5')
+ @tree = git.tree('cd7422af5a2e0fff3e94d6fb1a8fff03b2841881')
+ @blob = git.blob('4232d073306f01cf0b895864e5a5cfad7dd76fce')
+ @commit.parents[0].parents[0].parents[0]
+ end
+
+ run_code(x, 'commits 1') do
+ git.commits.size
+ end
+
+ run_code(x, 'commits 2') do
+ log = git.commits('master', 15)
+ log.size
+ log.size
+ log.first
+ git.commits('testing').map { |c| c.message }
+ end
+
+ run_code(x, 'big revlist') do
+ c = git.commits('master', 200)
+ end
+
+ run_code(x, 'log') do
+ log = git.log('master')
+ log.size
+ log.size
+ log.first
+ end
+
+ run_code(x, 'diff') do
+ c = git.diff(commit1, commit2)
+ end
+
+ run_code(x, 'commit-diff') do
+ c = git.commit_diff(commit1)
+ end
+
+ run_code(x, 'heads') do
+ c = git.heads.collect { |b| b.commit.id }
+ end
+
+ # run_code(x, 'config', 100) do
+ # c = git.config['user.name']
+ # c = git.config['user.email']
+ # end
+
+ #run_code(x, 'commit count') do
+ # c = git.commit_count('testing')
+ #end
+
+
+ end
+ #end
+
+ #printer = RubyProf::FlatPrinter.new(result)
+ #printer.print(STDOUT, 0)
+
+ end
+
+
+end
+
+
+def run_code(x, name, times = 30)
+ x.report(name.ljust(12)) do
+ for i in 1..times do
+ yield i
+ end
+ end
+
+ #end
+
+ # Print a graph profile to text
+end
+
+def new_file(name, contents)
+ File.open(name, 'w') do |f|
+ f.puts contents
+ end
+end
+
+
+def in_temp_dir(remove_after = true)
+ filename = 'git_test' + Time.now.to_i.to_s + rand(300).to_s.rjust(3, '0')
+ tmp_path = File.join("/tmp/", filename)
+ FileUtils.mkdir(tmp_path)
+ Dir.chdir tmp_path do
+ FileUtils.cp_r(@wbare, File.join(tmp_path, '.git'))
+ yield tmp_path
+ end
+ puts tmp_path
+ #FileUtils.rm_r(tmp_path) if remove_after
+end
+
+main()
+
+##pp Grit::GitRuby.cache_client.stats
View
21 benchmarks.txt
@@ -0,0 +1,21 @@
+Grit :
+ user system total real
+packobj 0.030000 0.270000 1.380000 ( 1.507250)
+commits 1 0.030000 0.070000 0.390000 ( 0.409931)
+commits 2 0.110000 0.170000 0.860000 ( 0.896371)
+log 0.350000 0.130000 0.850000 ( 0.875035)
+diff 0.190000 0.140000 1.940000 ( 2.031911)
+commit-diff 0.540000 0.220000 1.390000 ( 1.463839)
+heads 0.010000 0.070000 0.390000 ( 0.413918)
+
+
+Grit (with GitRuby) :
+
+ user system total real
+packobj 0.050000 0.010000 0.060000 ( 0.078318)
+commits 1 0.150000 0.010000 0.160000 ( 0.174296)
+commits 2 0.440000 0.040000 0.480000 ( 0.522310)
+log 0.490000 0.040000 0.530000 ( 0.538128)
+diff 0.370000 0.230000 2.250000 ( 2.255974)
+commit-diff 0.580000 0.260000 1.500000 ( 1.553000)
+heads 0.020000 0.100000 0.430000 ( 0.455464)
View
1  lib/grit.rb
@@ -27,6 +27,7 @@
require 'grit/config'
require 'grit/repo'
require 'grit/index'
+require 'grit/status'
module Grit
View
52 lib/grit/git-ruby/commit_db.rb
@@ -0,0 +1,52 @@
+begin
+ require 'sequel'
+
+ module Grit
+
+ class CommitDb
+
+ SCHEMA_VERSION = 1
+
+ attr_accessor :db, :git
+
+ def initialize(git_obj, index_location = nil)
+ @git = git_obj
+ db_file = File.join(index_location || @git.git_dir, 'commit_db')
+ if !File.exists?(db_file)
+ @db = Sequel.open "sqlite:///#{db_file}"
+ setup_tables
+ else
+ @db = Sequel.open "sqlite:///#{db_file}"
+ end
+ end
+
+ def rev_list(branch, options)
+ end
+
+ def update_db(branch = nil)
+ # find all refs/heads, for each
+ # add branch if not there
+ # go though all commits in branch
+ # add new commit_branches a
+ # and commit_nodes for each new one
+ # stop if reach commit that already has branch and node links
+ end
+
+ def setup_tables
+ @db << "create table meta (meta_key text, meta_value text)"
+ @db[:meta] << {:meta_key => 'schema', :meta_value => SCHEMA_VERSION}
+
+ @db << "create table commits (id integer, sha text, author_date integer)"
+ @db << "create table nodes (id integer, path text, type text)"
+ @db << "create table branches (id integer, ref text, commit_id integer)"
+
+ @db << "create table commit_branches (commit_id integer, branch_id integer)"
+ @db << "create table commit_nodes (commit_id integer, node_id integer, node_sha string)"
+ end
+
+ end
+ end
+
+rescue LoadError
+ # no commit db
+end
View
25 lib/grit/repo.rb
@@ -65,9 +65,32 @@ def head
#
# Returns true/false if commit worked
def commit_index(message)
- self.git.commit({}, a, b, '--', *paths)
+ self.git.commit({}, '-m', message)
end
+ # Commits all tracked and modified files
+ #
+ # Returns true/false if commit worked
+ def commit_all(message)
+ self.git.commit({}, '-a', '-m', message)
+ end
+
+ # Adds files to the index
+ def add(*files)
+ self.git.add({}, *files.flatten)
+ end
+
+ # Adds files to the index
+ def remove(*files)
+ self.git.rm({}, *files.flatten)
+ end
+
+ # Adds files to the index
+ def status
+ Status.new(self)
+ end
+
+
# An array of Tag objects that are available in this repo
#
# Returns Grit::Tag[] (baked)
View
149 lib/grit/status.rb
@@ -0,0 +1,149 @@
+module Grit
+
+ class Status
+ include Enumerable
+
+ @base = nil
+ @files = nil
+
+ def initialize(base)
+ @base = base
+ construct_status
+ end
+
+ def changed
+ @files.select { |k, f| f.type == 'M' }
+ end
+
+ def added
+ @files.select { |k, f| f.type == 'A' }
+ end
+
+ def deleted
+ @files.select { |k, f| f.type == 'D' }
+ end
+
+ def untracked
+ @files.select { |k, f| f.untracked }
+ end
+
+ def pretty
+ out = ''
+ self.each do |file|
+ out << file.path
+ out << "\n\tsha(r) " + file.sha_repo.to_s + ' ' + file.mode_repo.to_s
+ out << "\n\tsha(i) " + file.sha_index.to_s + ' ' + file.mode_index.to_s
+ out << "\n\ttype " + file.type.to_s
+ out << "\n\tstage " + file.stage.to_s
+ out << "\n\tuntrac " + file.untracked.to_s
+ out << "\n"
+ end
+ out << "\n"
+ out
+ end
+
+ # enumerable method
+
+ def [](file)
+ @files[file]
+ end
+
+ def each
+ @files.each do |k, file|
+ yield file
+ end
+ end
+
+ class StatusFile
+ attr_accessor :path, :type, :stage, :untracked
+ attr_accessor :mode_index, :mode_repo
+ attr_accessor :sha_index, :sha_repo
+
+ @base = nil
+
+ def initialize(base, hash)
+ @base = base
+ @path = hash[:path]
+ @type = hash[:type]
+ @stage = hash[:stage]
+ @mode_index = hash[:mode_index]
+ @mode_repo = hash[:mode_repo]
+ @sha_index = hash[:sha_index]
+ @sha_repo = hash[:sha_repo]
+ @untracked = hash[:untracked]
+ end
+
+ def blob(type = :index)
+ if type == :repo
+ @base.object(@sha_repo)
+ else
+ @base.object(@sha_index) rescue @base.object(@sha_repo)
+ end
+ end
+
+ end
+
+ private
+
+ def construct_status
+ @files = ls_files
+
+ # find untracked in working dir
+ Dir.glob('**/*') do |file|
+ if !@files[file]
+ @files[file] = {:path => file, :untracked => true} if !File.directory?(file)
+ end
+ end
+
+ # find modified in tree
+ diff_files.each do |path, data|
+ @files[path] ? @files[path].merge!(data) : @files[path] = data
+ end
+
+ # find added but not committed - new files
+ diff_index('HEAD').each do |path, data|
+ @files[path] ? @files[path].merge!(data) : @files[path] = data
+ end
+
+ @files.each do |k, file_hash|
+ @files[k] = StatusFile.new(@base, file_hash)
+ end
+ end
+
+ # compares the index and the working directory
+ def diff_files
+ hsh = {}
+ @base.git.diff_files.split("\n").each do |line|
+ (info, file) = line.split("\t")
+ (mode_src, mode_dest, sha_src, sha_dest, type) = info.split
+ hsh[file] = {:path => file, :mode_file => mode_src.to_s[1, 7], :mode_index => mode_dest,
+ :sha_file => sha_src, :sha_index => sha_dest, :type => type}
+ end
+ hsh
+ end
+
+ # compares the index and the repository
+ def diff_index(treeish)
+ hsh = {}
+ @base.git.diff_index({}, treeish).split("\n").each do |line|
+ (info, file) = line.split("\t")
+ (mode_src, mode_dest, sha_src, sha_dest, type) = info.split
+ hsh[file] = {:path => file, :mode_repo => mode_src.to_s[1, 7], :mode_index => mode_dest,
+ :sha_repo => sha_src, :sha_index => sha_dest, :type => type}
+ end
+ hsh
+ end
+
+ def ls_files
+ hsh = {}
+ lines = @base.git.ls_files({:stage => true})
+ lines.split("\n").each do |line|
+ (info, file) = line.split("\t")
+ (mode, sha, stage) = info.split
+ hsh[file] = {:path => file, :mode_index => mode, :sha_index => sha, :stage => stage}
+ end
+ hsh
+ end
+ end
+
+end
View
3  test/fixtures/commit
@@ -0,0 +1,3 @@
+Created commit 005b873: my test commit
+ 1 files changed, 1 insertions(+), 0 deletions(-)
+ create mode 100644 doc
View
4 test/fixtures/diff_files
@@ -0,0 +1,4 @@
+:100644 100644 5f3988355e1c811fbf9e0ce7f4582aa4a4ae20d2 0000000000000000000000000000000000000000 M PURE_TODO
+:100644 100644 62a6be213ccc3c770b97481d0e73eb1b2ee46547 0000000000000000000000000000000000000000 M lib/grit.rb
+:100644 100644 71e930d551c413a123f43e35c632ea6ba3e3705e 0000000000000000000000000000000000000000 M lib/grit/repo.rb
+:100644 100644 48412bac5814d476ba98550b4674189ecf514087 0000000000000000000000000000000000000000 M test/test_commit.rb
View
5 test/fixtures/diff_index
@@ -0,0 +1,5 @@
+:000000 100644 0000000000000000000000000000000000000000 dd6736a01e3560e56a3c7685ddc513f9e1dc2d89 A API.txt
+:100644 100644 5f3988355e1c811fbf9e0ce7f4582aa4a4ae20d2 0000000000000000000000000000000000000000 M PURE_TODO
+:100644 100644 62a6be213ccc3c770b97481d0e73eb1b2ee46547 0000000000000000000000000000000000000000 M lib/grit.rb
+:100644 100644 71e930d551c413a123f43e35c632ea6ba3e3705e 0000000000000000000000000000000000000000 M lib/grit/repo.rb
+:100644 100644 48412bac5814d476ba98550b4674189ecf514087 0000000000000000000000000000000000000000 M test/test_commit.rb
View
17 test/fixtures/ls_files
@@ -0,0 +1,17 @@
+100644 ae0786b99fc23d5a227bcfb54f12252f109ce39f 0 test/helper.rb
+100644 9bd6016010e5ad9e787104e75ac2ab5ad7e04096 0 test/profile.rb
+100644 373baa48adf7da63ec6bddc4adfece673c8bc2ad 0 test/suite.rb
+100644 e728ef8034c6a16ff9a8dbe154b21d45b43458e6 0 test/test_actor.rb
+100644 3d7bf379e880c962d1928a12b13a488c786f57ab 0 test/test_blob.rb
+100644 48412bac5814d476ba98550b4674189ecf514087 0 test/test_commit.rb
+100644 0886764b03db9b4b767e94d819d1516f23f19d49 0 test/test_config.rb
+100644 5841f89c4aee4ec2091c3d545b01a9ca904408eb 0 test/test_diff.rb
+100644 adbdc6d61b8caba532e13af64f6c7caea801b417 0 test/test_git.rb
+100644 baaae854addf9b0443ac7f0021dda4344b868819 0 test/test_head.rb
+100644 7b6d1b20c3f46a4549c6d21912fa8158466d865b 0 test/test_real.rb
+100644 bc11beb6f14c103416914d8745529051b2100e7c 0 test/test_reality.rb
+100644 7dc25bc3da321c75066af3f7188dc70e56e2523d 0 test/test_remote.rb
+100644 8b5830199233034dd6a86e21cd0aff6a08138b23 0 test/test_repo.rb
+100644 c6506397912dc8664a9829d2b20e23f32af8582f 0 test/test_rubygit.rb
+100644 2e3b0cbcb019040f13785c113583e389a2c121b5 0 test/test_tag.rb
+100644 4d06a9930d5ee4e1568b62446d313654377bc35b 0 test/test_tree.rb
View
55 test/test_commit_db.rb
@@ -0,0 +1,55 @@
+require File.dirname(__FILE__) + '/helper'
+require 'tempfile'
+
+class TestCommitDb < Test::Unit::TestCase
+
+ def setup
+ git_dir = File.join(File.dirname(__FILE__), *%w[dot_git])
+
+ tf= Tempfile.new('tempindex')
+ puts index = tf.path
+ tf.close
+ File.unlink(index)
+ Dir.mkdir(index)
+
+ @git = Git.new(git_dir)
+ @commit_db = CommitDb.new(@git, index)
+
+ @commit_sha = '5e3ee1198672257164ce3fe31dea3e40848e68d5'
+ @tree_sha = 'cd7422af5a2e0fff3e94d6fb1a8fff03b2841881'
+ @blob_sha = '4232d073306f01cf0b895864e5a5cfad7dd76fce'
+ end
+
+ def test_update_db_new
+ @commit_db.update_db
+ end
+
+ def test_update_db_one_commit
+ end
+
+ def test_update_db_new_branch
+ end
+
+ def test_log
+ end
+
+ def test_rev_list
+ end
+
+ def test_rev_list_max_count
+ end
+
+ def test_rev_list_since
+ end
+
+ def test_rev_list_until
+ end
+
+ def test_rev_list_range
+ end
+
+ def test_rev_list_skip
+ end
+
+
+end
View
20 test/test_commit_write.rb
@@ -0,0 +1,20 @@
+require File.dirname(__FILE__) + '/helper'
+
+class TestCommitWrite < Test::Unit::TestCase
+ def setup
+ @r = Repo.new(GRIT_REPO)
+ end
+
+ def test_commit
+ Git.any_instance.expects(:commit).returns(fixture('commit'))
+ results = @r.commit_index('my message')
+ assert_match /Created commit/, results
+ end
+
+ def test_commit_all
+ Git.any_instance.expects(:commit).returns(fixture('commit'))
+ results = @r.commit_all('my message')
+ assert_match /Created commit/, results
+ end
+
+end
View
40 test/test_index_status.rb
@@ -0,0 +1,40 @@
+require File.dirname(__FILE__) + '/helper'
+
+class TestIndexStatus < Test::Unit::TestCase
+ def setup
+ @r = Repo.new(GRIT_REPO)
+ end
+
+ def test_add
+ Git.any_instance.expects(:add).with({}, 'file1', 'file2')
+ @r.add('file1', 'file2')
+ end
+
+ def test_add_array
+ Git.any_instance.expects(:add).with({}, 'file1', 'file2')
+ @r.add(['file1', 'file2'])
+ end
+
+ def test_remove
+ Git.any_instance.expects(:rm).with({}, 'file1', 'file2')
+ @r.remove('file1', 'file2')
+ end
+
+ def test_remove_array
+ Git.any_instance.expects(:rm).with({}, 'file1', 'file2')
+ @r.remove(['file1', 'file2'])
+ end
+
+ def test_status
+ Git.any_instance.expects(:diff_index).with({}, 'HEAD').returns(fixture('diff_index'))
+ Git.any_instance.expects(:diff_files).returns(fixture('diff_files'))
+ Git.any_instance.expects(:ls_files).with({:stage => true}).returns(fixture('ls_files'))
+ status = @r.status
+ stat = status['lib/grit/repo.rb']
+ assert_equal stat.sha_repo, "71e930d551c413a123f43e35c632ea6ba3e3705e"
+ assert_equal stat.mode_repo, "100644"
+ assert_equal stat.type, "M"
+ end
+
+
+end
Please sign in to comment.
Something went wrong with that request. Please try again.