Permalink
Browse files

code restructured, find git using which, remove with_git_dir, proper

handling of argument array
  • Loading branch information...
1 parent 12c23d3 commit a270d51d85009bf3909002231ded56b3ff25da36 @minad minad committed Sep 2, 2010
Showing with 83 additions and 92 deletions.
  1. +37 −41 lib/gitrb/repository.rb
  2. +1 −1 lib/gitrb/tree.rb
  3. +3 −6 test/bare_repository_test.rb
  4. +5 −5 test/benchmark.rb
  5. +4 −7 test/commit_test.rb
  6. +11 −1 test/helper.rb
  7. +4 −4 test/profile.rb
  8. +15 −21 test/repository_test.rb
  9. +3 −5 test/tree_test.rb
  10. +0 −1 test/trie_test.rb
View
@@ -17,6 +17,14 @@ def initialize(command, args, output)
class Repository
attr_reader :path, :root, :branch, :head, :encoding
+ def self.git_path
+ @git_path ||= begin
+ path = `which git`.chomp
+ raise 'git not found' if $?.exitstatus != 0
+ path
+ end
+ end
+
SHA_PATTERN = /^[A-Fa-f0-9]{5,40}$/
REVISION_PATTERN = /^[\w\-\.]+([\^~](\d+)?)*$/
DEFAULT_ENCODING = 'utf-8'
@@ -176,8 +184,8 @@ def log(opts = {})
:message => message.strip)
end
commits
- rescue => ex
- return [] if ex.message =~ /bad default revision 'HEAD'/i
+ rescue CommandError => ex
+ return [] if ex.output =~ /bad default revision 'HEAD'/i
raise
end
@@ -249,6 +257,8 @@ def get_commit(id) get_type(id, :commit) end
#
# Returns the object.
def put(object)
+ raise ArgumentError unless object && GitObject === object
+
content = object.dump
data = "#{object.type} #{content.bytesize rescue content.length}\0#{content}"
id = sha(data)
@@ -277,55 +287,41 @@ def method_missing(name, *args)
cmd = name.to_s
if cmd[0..3] == 'git_'
cmd = cmd[4..-1].tr('_', '-')
- cmdline = ['/usr/bin/git', cmd, args.map { |a| a.to_s }].flatten
-
- @logger.debug "gitrb: #{cmdline}"
-
- with_git_dir do
- out = IO.popen('-', 'rb') do |io|
- if io
- # Read in binary mode (ascii-8bit) and convert afterwards
- block_given? ? yield(io) : set_encoding(io.read)
- else
- $stderr.reopen($stdout) # child's stderr goes to stdout
- exec(*cmdline)
- raise "exec failed and didn't throw? that's inconceivable!"
- end
- end
-
- if $?.exitstatus > 0
- return '' if $?.exitstatus == 1 && out == ''
- raise CommandError.new("git #{cmd}", args, out)
+ args = args.flatten.compact.map {|a| a.to_s }
+
+ @logger.debug "gitrb: #{self.class.git_path} #{cmd} #{args.inspect}"
+
+ out = IO.popen('-', 'rb') do |io|
+ if io
+ # Read in binary mode (ascii-8bit) and convert afterwards
+ block_given? ? yield(io) : set_encoding(io.read)
+ else
+ # child's stderr goes to stdout
+ STDERR.reopen(STDOUT)
+ ENV['GIT_DIR'] = path
+ exec(self.class.git_path, cmd, *args)
end
+ end
- out
+ if $?.exitstatus > 0
+ return '' if $?.exitstatus == 1 && out == ''
+ raise CommandError.new("git #{cmd}", args, out)
end
+
+ out
else
super
end
end
def default_user
- name = git_config('user.name').chomp
- email = git_config('user.email').chomp
- if name.empty?
- require 'etc'
- user = Etc.getpwnam(Etc.getlogin)
- name = user.gecos
- end
- if email.empty?
- require 'etc'
- email = Etc.getlogin + '@' + `hostname -f`.chomp
+ @default_user ||= begin
+ name = git_config('user.name').chomp
+ email = git_config('user.email').chomp
+ name = ENV['USER'] if name.empty?
+ email = ENV['USER'] + '@' + `hostname -f`.chomp if email.empty?
+ User.new(name, email)
end
- User.new(name, email)
- end
-
- def with_git_dir
- old_path = ENV['GIT_DIR']
- ENV['GIT_DIR'] = path
- yield
- ensure
- ENV['GIT_DIR'] = old_path
end
private
View
@@ -73,7 +73,7 @@ def [](path)
# Write an entry on specified path.
def []=(path, entry)
- raise ArgumentError if !entry
+ raise ArgumentError unless entry && GitObject === entry
path = normalize_path(path)
if path.empty?
raise 'Empty path'
@@ -1,14 +1,11 @@
require 'helper'
describe Gitrb do
-
- REPO = '/tmp/gitrb_test.git'
-
before do
- FileUtils.rm_rf REPO
- Dir.mkdir REPO
+ FileUtils.rm_rf REPO_PATH
+ Dir.mkdir REPO_PATH
- @repo = Gitrb::Repository.new(:path => REPO, :create => true, :bare => true)
+ @repo = Gitrb::Repository.new(:path => REPO_PATH, :create => true, :bare => true)
end
it 'should save and load entries' do
View
@@ -3,13 +3,13 @@
require 'benchmark'
require 'fileutils'
-REPO = '/tmp/gitrb'
+REPO_PATH = '/tmp/gitrb_test'
-FileUtils.rm_rf REPO
-FileUtils.mkpath REPO
-Dir.chdir REPO
+FileUtils.rm_rf REPO_PATH
+FileUtils.mkpath REPO_PATH
+Dir.chdir REPO_PATH
-repo = Gitrb::Repository.new(:path => REPO, :create => true)
+repo = Gitrb::Repository.new(:path => REPO_PATH, :create => true)
grit = nil
gitrb = nil
View
@@ -1,14 +1,11 @@
require 'helper'
describe Gitrb::Commit do
-
- REPO = '/tmp/gitrb_test'
-
before do
- FileUtils.rm_rf REPO
- Dir.mkdir REPO
+ FileUtils.rm_rf REPO_PATH
+ Dir.mkdir REPO_PATH
- @repo = Gitrb::Repository.new(:path => REPO, :create => true)
+ @repo = Gitrb::Repository.new(:path => REPO_PATH, :create => true)
end
it "should dump in right format" do
@@ -36,7 +33,7 @@
repo.root['a'] = Gitrb::Blob.new(:data => "Yay")
commit = repo.commit("Commit Message", author, author)
- repo.with_git_dir do
+ with_git_dir do
IO.popen("git log") do |io|
io.gets.should.equal "commit #{commit.id}\n"
io.gets.should.equal "Author: hans <hans@email.de>\n"
View
@@ -11,13 +11,23 @@ def file(file, data)
open(file, 'w') { |io| io << data }
repo.git_add(file)
- repo.git_commit('-m', 'added #{file}')
+ repo.git_commit('-m', "added #{file}")
File.unlink(file)
end
end
+
+ def with_git_dir
+ old_path = ENV['GIT_DIR']
+ ENV['GIT_DIR'] = repo.path
+ yield
+ ensure
+ ENV['GIT_DIR'] = old_path
+ end
end
class Bacon::Context
include TestHelper
attr_reader :repo
end
+
+REPO_PATH = '/tmp/gitrb_test'
View
@@ -3,12 +3,12 @@
require 'grit'
require 'ruby-prof'
-REPO = '/tmp/gitrb'
+REPO_PATH = '/tmp/gitrb_test'
-FileUtils.rm_rf REPO
-FileUtils.mkpath REPO
+FileUtils.rm_rf REPO_PATH
+FileUtils.mkpath REPO_PATH
-repo = Gitrb::Repository.new(:path => REPO, :create => true)
+repo = Gitrb::Repository.new(:path => REPO_PATH, :create => true)
repo.transaction { 'aaa'.upto('jjj') { |key| repo.root[key] = Gitrb::Blob.new(:data => rand.to_s) } }
result = RubyProf.profile do
View
@@ -1,21 +1,10 @@
require 'helper'
describe Gitrb do
-
- REPO = '/tmp/gitrb_test'
-
before do
- FileUtils.rm_rf REPO
- Dir.mkdir REPO
- @repo = Gitrb::Repository.new(:path => REPO, :create => true)
- end
-
- it 'should set the GIT_DIR environment variable' do
- ENV['GIT_DIR'].should.equal nil
- repo.with_git_dir do
- ENV['GIT_DIR'].should.equal REPO + "/.git"
- end
- ENV['GIT_DIR'].should.equal nil
+ FileUtils.rm_rf REPO_PATH
+ Dir.mkdir REPO_PATH
+ @repo = Gitrb::Repository.new(:path => REPO_PATH, :create => true)
end
it 'should put and get objects by sha' do
@@ -153,11 +142,12 @@
begin
repo.transaction do
- repo.root['a/b'] = 'Changed'
- repo.root['x/a'] = 'Added'
- raise
+ repo.root['a/b'] = Gitrb::Blob.new(:data => 'Changed')
+ repo.root['x/a'] = Gitrb::Blob.new(:data => 'Added')
+ raise 'boo'
end
- rescue
+ rescue RuntimeError => ex
+ ex.message.should.equal 'boo'
end
repo.root['a/b'].data.should.equal 'Hello'
@@ -205,25 +195,27 @@
ready = false
+ # This test case produces a deadlock in old ruby versions
+ # (Works in 1.8.7_p302 and 1.9)
repo.transaction do
Thread.start do
repo.transaction do
+ sleep 0.1
repo.root['a/b'] = Gitrb::Blob.new(:data => 'Changed by second thread')
end
ready = true
end
repo.root['a/b'] = Gitrb::Blob.new(:data => 'Changed')
end
- sleep 0.01 until ready
+ repo.root['a/b'].data.should.equal 'Changed'
- repo.refresh
+ sleep 0.01 until ready
repo.root['a/b'].data.should.equal 'Changed by second thread'
end
it 'should find all objects' do
- repo.refresh
repo.root['c'] = Gitrb::Blob.new(:data => 'Hello')
repo.root['d'] = Gitrb::Blob.new(:data => 'World')
repo.commit
@@ -233,6 +225,8 @@
end
it "should load log" do
+ repo.log.should.be.empty
+
repo.root['a'] = Gitrb::Blob.new(:data => 'a')
repo.commit 'added a'
View
@@ -1,13 +1,11 @@
require 'helper'
describe Gitrb::Tree do
- REPO = '/tmp/gitrb_test'
-
before do
- FileUtils.rm_rf REPO
- Dir.mkdir REPO
+ FileUtils.rm_rf REPO_PATH
+ Dir.mkdir REPO_PATH
- @repo = Gitrb::Repository.new(:path => REPO, :create => true)
+ @repo = Gitrb::Repository.new(:path => REPO_PATH, :create => true)
end
it "should write a table" do
View
@@ -1,7 +1,6 @@
require 'gitrb'
describe Gitrb::Trie do
-
before do
@trie = Gitrb::Trie.new
end

0 comments on commit a270d51

Please sign in to comment.