From 07ebb951368ed31bdaebc2e820c62ced22c8bbe4 Mon Sep 17 00:00:00 2001 From: scott Chacon Date: Tue, 27 Nov 2007 08:06:51 -0800 Subject: added Matthias and Simon to credits for the gitrb code fixed an issue with raw object tree formatting added ls_tree implementation in raw git --- camping/gitweb.rb | 10 +++++----- lib/git/lib.rb | 16 +++++++++++----- lib/git/raw/internal/loose.rb | 10 ++++++++++ lib/git/raw/internal/mmap.rb | 10 ++++++++++ lib/git/raw/internal/object.rb | 10 ++++++++++ lib/git/raw/internal/pack.rb | 10 ++++++++++ lib/git/raw/object.rb | 28 ++++++++++++++++++++++++++-- lib/git/raw/repository.rb | 19 +++++++++++++++++-- tests/test_helper.rb | 10 ++++++++++ tests/units/test_index_ops.rb | 2 +- tests/units/test_lib.rb | 9 +++++++++ tests/units/test_raw_internals.rb | 26 ++++++++++++++++++++------ 12 files changed, 139 insertions(+), 21 deletions(-) diff --git a/camping/gitweb.rb b/camping/gitweb.rb index 8dc40ba..3d1b5d6 100644 --- a/camping/gitweb.rb +++ b/camping/gitweb.rb @@ -107,10 +107,7 @@ module GitWeb::Controllers class Commit < R '/commit/(\d+)/(\w+)' def get repo_id, sha @repo = Repository.find repo_id - logger = Logger.new('/tmp/git.log') - logger.level = Logger::INFO - - @git = Git.bare(@repo.path, :log => logger) + @git = Git.bare(@repo.path) @commit = @git.gcommit(sha) render :commit end @@ -119,7 +116,10 @@ module GitWeb::Controllers class Tree < R '/tree/(\d+)/(\w+)' def get repo_id, sha @repo = Repository.find repo_id - @git = Git.bare(@repo.path) + logger = Logger.new('/tmp/git.log') + logger.level = Logger::INFO + + @git = Git.bare(@repo.path, :log => logger) @tree = @git.gtree(sha) render :tree end diff --git a/lib/git/lib.rb b/lib/git/lib.rb index 120d9ce..decd6d4 100644 --- a/lib/git/lib.rb +++ b/lib/git/lib.rb @@ -183,16 +183,22 @@ module Git def object_contents(sha) #command('cat-file', ['-p', sha]) - get_raw_repo.cat_file(revparse(sha)) + get_raw_repo.cat_file(revparse(sha)).chomp end def ls_tree(sha) data = {'blob' => {}, 'tree' => {}} - command_lines('ls-tree', sha.to_s).each do |line| - (info, filenm) = line.split("\t") - (mode, type, sha) = info.split - data[type][filenm] = {:mode => mode, :sha => sha} + + get_raw_repo.object(revparse(sha)).entry.each do |e| + data[e.format_type][e.name] = {:mode => e.format_mode, :sha => e.sha1} end + + #command_lines('ls-tree', sha.to_s).each do |line| + # (info, filenm) = line.split("\t") + # (mode, type, sha) = info.split + # data[type][filenm] = {:mode => mode, :sha => sha} + #end + data end diff --git a/lib/git/raw/internal/loose.rb b/lib/git/raw/internal/loose.rb index d8ec6fb..53d4334 100644 --- a/lib/git/raw/internal/loose.rb +++ b/lib/git/raw/internal/loose.rb @@ -1,3 +1,13 @@ +# +# converted from the gitrb project +# +# authors: +# Matthias Lederhofer +# Simon 'corecode' Schubert +# +# provides native ruby access to git objects and pack files +# + require 'zlib' require 'digest/sha1' diff --git a/lib/git/raw/internal/mmap.rb b/lib/git/raw/internal/mmap.rb index 15b5628..78de164 100644 --- a/lib/git/raw/internal/mmap.rb +++ b/lib/git/raw/internal/mmap.rb @@ -1,3 +1,13 @@ +# +# converted from the gitrb project +# +# authors: +# Matthias Lederhofer +# Simon 'corecode' Schubert +# +# provides native ruby access to git objects and pack files +# + begin require 'mmap' rescue LoadError diff --git a/lib/git/raw/internal/object.rb b/lib/git/raw/internal/object.rb index 7f95685..172b917 100644 --- a/lib/git/raw/internal/object.rb +++ b/lib/git/raw/internal/object.rb @@ -1,3 +1,13 @@ +# +# converted from the gitrb project +# +# authors: +# Matthias Lederhofer +# Simon 'corecode' Schubert +# +# provides native ruby access to git objects and pack files +# + require 'digest/sha1' module Git diff --git a/lib/git/raw/internal/pack.rb b/lib/git/raw/internal/pack.rb index 6980a98..8d5141e 100644 --- a/lib/git/raw/internal/pack.rb +++ b/lib/git/raw/internal/pack.rb @@ -1,3 +1,13 @@ +# +# converted from the gitrb project +# +# authors: +# Matthias Lederhofer +# Simon 'corecode' Schubert +# +# provides native ruby access to git objects and pack files +# + require 'zlib' require 'git/raw/internal/object' require 'git/raw/internal/mmap' diff --git a/lib/git/raw/object.rb b/lib/git/raw/object.rb index f10d853..5c3969d 100644 --- a/lib/git/raw/object.rb +++ b/lib/git/raw/object.rb @@ -1,3 +1,13 @@ +# +# converted from the gitrb project +# +# authors: +# Matthias Lederhofer +# Simon 'corecode' Schubert +# +# provides native ruby access to git objects and pack files +# + require 'digest/sha1' module Git @@ -132,6 +142,21 @@ module Git end end + def format_type + case type + when :link + 'link' + when :directory + 'tree' + when :file + 'blob' + end + end + + def format_mode + "%06o" % @mode + end + def raw "%o %s\0%s" % [@mode, @name, [@sha1].pack("H*")] end @@ -160,8 +185,7 @@ module Git def raw_content # TODO: sort correctly #@entry.sort { |a,b| a.name <=> b.name }. - @entry. - collect { |e| e.raw }.join + @entry.collect { |e| [[e.format_mode, e.format_type, e.sha1].join(' '), e.name].join("\t") }.join("\n") end end diff --git a/lib/git/raw/repository.rb b/lib/git/raw/repository.rb index 4a1c897..bd5e971 100644 --- a/lib/git/raw/repository.rb +++ b/lib/git/raw/repository.rb @@ -1,3 +1,13 @@ +# +# converted from the gitrb project +# +# authors: +# Matthias Lederhofer +# Simon 'corecode' Schubert +# +# provides native ruby access to git objects and pack files +# + require 'git/raw/internal/object' require 'git/raw/internal/pack' require 'git/raw/internal/loose' @@ -28,9 +38,14 @@ module Git puts end end - + + def object(sha) + o = get_raw_object_by_sha1(sha) + c = Git::Raw::Object.from_raw(o) + end + def cat_file(sha) - get_raw_object_by_sha1(sha).content rescue nil + object(sha).raw_content end def log(sha, count = 30) diff --git a/tests/test_helper.rb b/tests/test_helper.rb index 9907ffe..e40174a 100644 --- a/tests/test_helper.rb +++ b/tests/test_helper.rb @@ -28,6 +28,16 @@ class Test::Unit::TestCase end end + + def with_temp_bare + in_temp_dir do |path| + g = Git.clone(@wbare, 'new') + Dir.chdir('new') do + yield g + end + end + end + def create_temp_repo(clone_path) filename = 'git_test' + Time.now.to_i.to_s + rand(300).to_s.rjust(3, '0') @tmp_path = File.join("/tmp/", filename) diff --git a/tests/units/test_index_ops.rb b/tests/units/test_index_ops.rb index f92ae9e..dcd8498 100644 --- a/tests/units/test_index_ops.rb +++ b/tests/units/test_index_ops.rb @@ -10,7 +10,7 @@ class TestIndexOps < Test::Unit::TestCase end def test_add - in_temp_dir(false) do |path| + in_temp_dir do |path| g = Git.clone(@wbare, 'new') Dir.chdir('new') do assert_equal('100644', g.status['example.txt'].mode_index) diff --git a/tests/units/test_lib.rb b/tests/units/test_lib.rb index f9170e6..7cbbeef 100644 --- a/tests/units/test_lib.rb +++ b/tests/units/test_lib.rb @@ -102,6 +102,15 @@ class TestLib < Test::Unit::TestCase assert_equal('+refs/heads/*:refs/remotes/working/*', config['fetch']) end + + def test_ls_tree + tree = @lib.ls_tree('94c827875e2cadb8bc8d4cdd900f19aa9e8634c7') + assert_equal("3aac4b445017a8fc07502670ec2dbf744213dd48", tree['blob']['example.txt'][:sha]) + assert_equal("100644", tree['blob']['example.txt'][:mode]) + assert(tree['tree']) + end + + # options this will accept # :treeish # :path_limiter diff --git a/tests/units/test_raw_internals.rb b/tests/units/test_raw_internals.rb index 1437845..03a7916 100644 --- a/tests/units/test_raw_internals.rb +++ b/tests/units/test_raw_internals.rb @@ -9,14 +9,16 @@ class TestRawInternals < Test::Unit::TestCase end def test_raw_log - g = Git.bare(@wbare) - t_log(g) + with_temp_bare do |g| + t_log(g) + end end def test_packed_log - g = Git.bare(@wbare) - g.repack - t_log(g) + with_temp_bare do |g| + g.repack + t_log(g) + end end def test_commit_object @@ -26,15 +28,27 @@ class TestRawInternals < Test::Unit::TestCase assert_equal('test', c.message) end + def test_lstree + g = Git.bare(@wbare) + c = g.object("v2.5").gtree + sha = c.sha + + repo = Git::Raw::Repository.new(@wbare) + puts repo.object(sha).inspect + end + def t_log(g) c = g.object("v2.5") sha = c.sha - repo = Git::Raw::Repository.new(@wbare) + repo = Git::Raw::Repository.new(g.repo.path) raw_out = repo.log(sha) assert_equal('commit 546bec6f8872efa41d5d97a369f669165ecda0de', raw_out.split("\n").first) assert_equal('546bec6f8872efa41d5d97a369f669165ecda0de', c.log(30).first.sha) end + + + end \ No newline at end of file -- cgit