Skip to content

Commit

Permalink
Merge branch 'js/master' into next
Browse files Browse the repository at this point in the history
  • Loading branch information
mojombo committed Feb 28, 2009
2 parents 6bb41e4 + cdb0d3e commit b6556f1
Show file tree
Hide file tree
Showing 26 changed files with 199 additions and 46 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1 +1,2 @@
pkg
.DS_Store
2 changes: 1 addition & 1 deletion grit.gemspec

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions lib/grit.rb
Expand Up @@ -17,8 +17,12 @@

# third party
require 'rubygems'
gem "mime-types", ">=0"
require 'mime/types'

# ruby 1.9 compatibility
require 'grit/ruby1.9'

# internal requires
require 'grit/lazy'
require 'grit/errors'
Expand Down
4 changes: 4 additions & 0 deletions lib/grit/blob.rb
Expand Up @@ -108,6 +108,10 @@ def self.blame(repo, commit, file)
blames
end

def basename
File.basename(name)
end

# Pretty object inspection
def inspect
%Q{#<Grit::Blob "#{@id}">}
Expand Down
6 changes: 5 additions & 1 deletion lib/grit/commit.rb
Expand Up @@ -178,7 +178,11 @@ def diffs
self.class.diff(@repo, parents.first.id, @id)
end
end


def stats
stats = @repo.commit_stats(self.sha, 1)[0][-1]
end

# Convert this Commit to a String which is just the SHA1 id
def to_s
@id
Expand Down
2 changes: 1 addition & 1 deletion lib/grit/git-ruby.rb
Expand Up @@ -113,7 +113,7 @@ def file_type(ref)

def blame_tree(commit, path = nil)
begin
path = path.to_a.join('/').to_s + '/' if (path && path != '')
path = [path].join('/').to_s + '/' if (path && path != '')
path = '' if !path.is_a? String
commits = file_index.last_commits(rev_parse({}, commit), looking_for(commit, path))
clean_paths(commits)
Expand Down
12 changes: 9 additions & 3 deletions lib/grit/git-ruby/git_object.rb
Expand Up @@ -115,7 +115,7 @@ class DirectoryEntry
def initialize(mode, filename, sha1o)
@mode = 0
mode.each_byte do |i|
@mode = (@mode << 3) | (i-'0'[0])
@mode = (@mode << 3) | (i-'0'.getord(0))
end
@name = filename
@sha1 = sha1o
Expand Down Expand Up @@ -179,8 +179,14 @@ def raw

def self.read_bytes_until(io, char)
string = ''
while ((next_char = io.getc.chr) != char) && !io.eof
string += next_char
if RUBY_VERSION > '1.9'
while ((next_char = io.getc) != char) && !io.eof
string += next_char
end
else
while ((next_char = io.getc.chr) != char) && !io.eof
string += next_char
end
end
string
end
Expand Down
Expand Up @@ -12,7 +12,7 @@
module Grit
module GitRuby
module Internal
class Mmap
class FileWindow
def initialize(file, version = 1)
@file = file
@offset = nil
Expand Down Expand Up @@ -46,7 +46,7 @@ def [](*idx)
end
@offset = offset + len ? len : 1
if not len
@file.read(1)[0]
@file.read(1).getord(0)
else
@file.read(len)
end
Expand Down
12 changes: 6 additions & 6 deletions lib/grit/git-ruby/internal/loose.rb
Expand Up @@ -29,7 +29,7 @@ def [](sha1)
begin
return nil unless sha1[0...2] && sha1[2..39]
path = @directory + '/' + sha1[0...2] + '/' + sha1[2..39]
get_raw_object(File.read(path))
get_raw_object(File.open(path, 'rb').read)
rescue Errno::ENOENT
nil
end
Expand Down Expand Up @@ -76,7 +76,7 @@ def put_raw_object(content, type)
content = Zlib::Deflate.deflate(store)

FileUtils.mkdir_p(@directory+'/'+sha1[0...2])
File.open(path, 'w') do |f|
File.open(path, 'wb') do |f|
f.write content
end
end
Expand All @@ -102,7 +102,7 @@ def self.verify_header(type, size)
# private
def unpack_object_header_gently(buf)
used = 0
c = buf[used]
c = buf.getord(used)
used += 1

type = (c >> 4) & 7;
Expand All @@ -112,7 +112,7 @@ def unpack_object_header_gently(buf)
if buf.length <= used
raise LooseObjectError, "object file too short"
end
c = buf[used]
c = buf.getord(used)
used += 1

size += (c & 0x7f) << shift
Expand All @@ -127,8 +127,8 @@ def unpack_object_header_gently(buf)
private :unpack_object_header_gently

def legacy_loose_object?(buf)
word = (buf[0] << 8) + buf[1]
buf[0] == 0x78 && word % 31 == 0
word = (buf.getord(0) << 8) + buf.getord(1)
buf.getord(0) == 0x78 && word % 31 == 0
end
private :legacy_loose_object?
end
Expand Down
38 changes: 19 additions & 19 deletions lib/grit/git-ruby/internal/pack.rb
Expand Up @@ -11,7 +11,7 @@

require 'zlib'
require 'grit/git-ruby/internal/raw_object'
require 'grit/git-ruby/internal/mmap'
require 'grit/git-ruby/internal/file_window'

PACK_SIGNATURE = "PACK"
PACK_IDX_SIGNATURE = "\377tOc"
Expand Down Expand Up @@ -48,9 +48,9 @@ def initialize(file)
def with_idx(index_file = nil)
if !index_file
index_file = @name
idxfile = File.open(@name[0...-4]+'idx')
idxfile = File.open(@name[0...-4]+'idx', 'rb')
else
idxfile = File.open(index_file)
idxfile = File.open(index_file, 'rb')
end

# read header
Expand All @@ -66,14 +66,14 @@ def with_idx(index_file = nil)
@version = 1
end

idx = Mmap.new(idxfile, @version)
idx = FileWindow.new(idxfile, @version)
yield idx
idx.unmap
idxfile.close
end

def with_packfile
packfile = File.open(@name)
packfile = File.open(@name, 'rb')
yield packfile
packfile.close
end
Expand Down Expand Up @@ -189,7 +189,7 @@ def each_sha1
end

def find_object_in_index(idx, sha1)
slot = sha1[0]
slot = sha1.getord(0)
return nil if !slot
first, last = @offsets[slot,2]
while first < last
Expand Down Expand Up @@ -248,13 +248,13 @@ def unpack_object(packfile, offset, options = {})
obj_offset = offset
packfile.seek(offset)

c = packfile.read(1)[0]
c = packfile.read(1).getord(0)
size = c & 0xf
type = (c >> 4) & 7
shift = 4
offset += 1
while c & 0x80 != 0
c = packfile.read(1)[0]
c = packfile.read(1).getord(0)
size |= ((c & 0x7f) << shift)
shift += 7
offset += 1
Expand All @@ -281,10 +281,10 @@ def unpack_deltified(packfile, type, offset, obj_offset, size, options = {})

if type == OBJ_OFS_DELTA
i = 0
c = data[i]
c = data.getord(i)
base_offset = c & 0x7f
while c & 0x80 != 0
c = data[i += 1]
c = data.getord(i += 1)
base_offset += 1
base_offset <<= 7
base_offset |= c & 0x7f
Expand Down Expand Up @@ -335,18 +335,18 @@ def patch_delta(base, delta)
dest_size, pos = patch_delta_header_size(delta, pos)
dest = ""
while pos < delta.size
c = delta[pos]
c = delta.getord(pos)
pos += 1
if c & 0x80 != 0
pos -= 1
cp_off = cp_size = 0
cp_off = delta[pos += 1] if c & 0x01 != 0
cp_off |= delta[pos += 1] << 8 if c & 0x02 != 0
cp_off |= delta[pos += 1] << 16 if c & 0x04 != 0
cp_off |= delta[pos += 1] << 24 if c & 0x08 != 0
cp_size = delta[pos += 1] if c & 0x10 != 0
cp_size |= delta[pos += 1] << 8 if c & 0x20 != 0
cp_size |= delta[pos += 1] << 16 if c & 0x40 != 0
cp_off = delta.getord(pos += 1) if c & 0x01 != 0
cp_off |= delta.getord(pos += 1) << 8 if c & 0x02 != 0
cp_off |= delta.getord(pos += 1) << 16 if c & 0x04 != 0
cp_off |= delta.getord(pos += 1) << 24 if c & 0x08 != 0
cp_size = delta.getord(pos += 1) if c & 0x10 != 0
cp_size |= delta.getord(pos += 1) << 8 if c & 0x20 != 0
cp_size |= delta.getord(pos += 1) << 16 if c & 0x40 != 0
cp_size = 0x10000 if cp_size == 0
pos += 1
dest += base[cp_off,cp_size]
Expand All @@ -365,7 +365,7 @@ def patch_delta_header_size(delta, pos)
size = 0
shift = 0
begin
c = delta[pos]
c = delta.getord(pos)
if c == nil
raise PackFormatError, 'invalid delta header'
end
Expand Down
10 changes: 8 additions & 2 deletions lib/grit/git-ruby/object.rb
Expand Up @@ -163,8 +163,14 @@ def raw

def self.read_bytes_until(io, char)
string = ''
while ((next_char = io.getc.chr) != char) && !io.eof
string += next_char
if RUBY_VERSION > '1.9'
while ((next_char = io.getc) != char) && !io.eof
string += next_char
end
else
while ((next_char = io.getc.chr) != char) && !io.eof
string += next_char
end
end
string
end
Expand Down
13 changes: 9 additions & 4 deletions lib/grit/git.rb
Expand Up @@ -35,6 +35,11 @@ def initialize(git_dir)
self.bytes_read = 0
end

def shell_escape(str)
str.to_s.gsub("'", "\\\\'").gsub(";", '\\;')
end
alias_method :e, :shell_escape

# Run the given git command with the specified arguments and return
# the result as a String
# +cmd+ is the command
Expand All @@ -54,9 +59,9 @@ def run(prefix, cmd, postfix, options, args)
timeout = true if timeout.nil?

opt_args = transform_options(options)
ext_args = args.reject { |a| a.empty? }.map { |a| (a == '--' || a[0].chr == '|') ? a : "'#{a}'" }
ext_args = args.reject { |a| a.empty? }.map { |a| (a == '--' || a[0].chr == '|') ? a : "'#{e(a)}'" }

call = "#{prefix}#{Git.git_binary} --git-dir='#{self.git_dir}' #{cmd.to_s.gsub(/_/, '-')} #{(opt_args + ext_args).join(' ')}#{postfix}"
call = "#{prefix}#{Git.git_binary} --git-dir='#{self.git_dir}' #{cmd.to_s.gsub(/_/, '-')} #{(opt_args + ext_args).join(' ')}#{e(postfix)}"
Grit.log(call) if Grit.debug
response, err = timeout ? sh(call) : wild_sh(call)
Grit.log(response) if Grit.debug
Expand Down Expand Up @@ -116,14 +121,14 @@ def transform_options(options)
args << "-#{opt}"
else
val = options.delete(opt)
args << "-#{opt.to_s} '#{val}'"
args << "-#{opt.to_s} '#{e(val)}'"
end
else
if options[opt] == true
args << "--#{opt.to_s.gsub(/_/, '-')}"
else
val = options.delete(opt)
args << "--#{opt.to_s.gsub(/_/, '-')}='#{val}'"
args << "--#{opt.to_s.gsub(/_/, '-')}='#{e(val)}'"
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/grit/index.rb
Expand Up @@ -93,12 +93,12 @@ def write_tree(tree, now_tree = nil)
# overwrite with new tree contents
tree.each do |k, v|
case v
when String:
when String
sha = write_blob(v)
sha = [sha].pack("H*")
str = "%s %s\0%s" % ['100644', k, sha]
tree_contents[k] = str
when Hash:
when Hash
ctree = now_tree/k if now_tree
sha = write_tree(v, ctree)
sha = [sha].pack("H*")
Expand Down
14 changes: 14 additions & 0 deletions lib/grit/repo.rb
Expand Up @@ -209,6 +209,20 @@ def commit(id)
Commit.find_all(self, id, options).first
end

# Returns a list of commits that is in +other_repo+ but not in self
#
# Returns Grit::Commit[]
def commit_deltas_from(other_repo, ref = "master", other_ref = "master")
# TODO: we should be able to figure out the branch point, rather than
# rev-list'ing the whole thing
repo_refs = self.git.rev_list({}, ref).strip.split("\n")
other_repo_refs = other_repo.git.rev_list({}, other_ref).strip.split("\n")

(other_repo_refs - repo_refs).map do |ref|
Commit.find_all(other_repo, ref, {:max_count => 1}).first
end
end

# The Tree object for the given treeish reference
# +treeish+ is the reference (default 'master')
# +paths+ is an optional Array of directory paths to restrict the tree (deafult [])
Expand Down
7 changes: 7 additions & 0 deletions lib/grit/ruby1.9.rb
@@ -0,0 +1,7 @@
class String
if ((defined? RUBY_VERSION) && (RUBY_VERSION[0..2] == "1.9"))
def getord(offset); self[offset].ord; end
else
alias :getord :[]
end
end
6 changes: 5 additions & 1 deletion lib/grit/submodule.rb
Expand Up @@ -54,7 +54,7 @@ def self.config(repo, ref = "master")
blob = commit.tree/'.gitmodules'
return {} unless blob

lines = blob.data.split("\n")
lines = blob.data.gsub(/\r\n?/, "\n" ).split("\n")

config = {}
current = nil
Expand All @@ -75,6 +75,10 @@ def self.config(repo, ref = "master")
config
end

def basename
File.basename(name)
end

# Pretty object inspection
def inspect
%Q{#<Grit::Submodule "#{@id}">}
Expand Down
4 changes: 4 additions & 0 deletions lib/grit/tree.rb
Expand Up @@ -95,6 +95,10 @@ def /(file)
end
end

def basename
File.basename(name)
end

# Pretty object inspection
def inspect
%Q{#<Grit::Tree "#{@id}">}
Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/rev_list_delta_a
@@ -0,0 +1,8 @@
e34590b7a2d186b3bb9a1170d02d52b36c791c78
8977833d74f8681aa0d9a5e84b0dd3d81519774d
6f5561530cb3a94e4c86454e84732197325be172
ee419e04a961543444be6db66aef52e6e37936d6
d845de9d438e1a249a0c2fcb778e8ea3b7e06cef
0bba4a6c10060405a94d52533af2f9bdacd4f29c
77711c0722964ead965e0ba2ee9ed4a03cb3d292
501d23cac6dd911511f15d091ee031a15b90ebde

0 comments on commit b6556f1

Please sign in to comment.