git-db 0.1.2 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.2
1
+ 0.2.0
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{git-db}
8
- s.version = "0.1.2"
8
+ s.version = "0.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["David Dollar"]
@@ -26,9 +26,6 @@ Gem::Specification.new do |s|
26
26
  "Rakefile",
27
27
  "VERSION",
28
28
  "bin/git-db",
29
- "features/git-db.feature",
30
- "features/step_definitions/git-db_steps.rb",
31
- "features/support/env.rb",
32
29
  "git-db.gemspec",
33
30
  "lib/git-db.rb",
34
31
  "lib/git-db/commands.rb",
@@ -48,9 +45,13 @@ Gem::Specification.new do |s|
48
45
  "lib/git-db/utility/counting_io.rb",
49
46
  "spec/git-db/commands_spec.rb",
50
47
  "spec/git-db/objects/base_spec.rb",
48
+ "spec/git-db/objects/blob_spec.rb",
49
+ "spec/git-db/objects/commit_spec.rb",
51
50
  "spec/git-db/objects/entry_spec.rb",
52
51
  "spec/git-db/objects/tag_spec.rb",
53
52
  "spec/git-db/objects/tree_spec.rb",
53
+ "spec/git-db/objects_spec.rb",
54
+ "spec/git-db/protocol_spec.rb",
54
55
  "spec/git-db/utility/counting_io_spec.rb",
55
56
  "spec/git-db_spec.rb",
56
57
  "spec/rcov.opts",
@@ -65,9 +66,13 @@ Gem::Specification.new do |s|
65
66
  s.test_files = [
66
67
  "spec/git-db/commands_spec.rb",
67
68
  "spec/git-db/objects/base_spec.rb",
69
+ "spec/git-db/objects/blob_spec.rb",
70
+ "spec/git-db/objects/commit_spec.rb",
68
71
  "spec/git-db/objects/entry_spec.rb",
69
72
  "spec/git-db/objects/tag_spec.rb",
70
73
  "spec/git-db/objects/tree_spec.rb",
74
+ "spec/git-db/objects_spec.rb",
75
+ "spec/git-db/protocol_spec.rb",
71
76
  "spec/git-db/utility/counting_io_spec.rb",
72
77
  "spec/git-db_spec.rb",
73
78
  "spec/spec_helper.rb"
@@ -49,7 +49,7 @@ module GitDB;
49
49
  end
50
50
 
51
51
  def self.log(message)
52
- logger.puts message
52
+ logger.puts message if ENV["DEBUG"]
53
53
  end
54
54
 
55
55
  ## database ##################################################################
@@ -37,7 +37,11 @@ class GitDB::Commands::ReceivePack
37
37
 
38
38
  unless new_shas.reject { |sha| sha == GitDB.null_sha1 }.length.zero?
39
39
  while (entries = io.read_pack)
40
- database.write_objects(entries)
40
+
41
+ # bulk in 50 at a time
42
+ while (page = entries.shift(50)).length > 0
43
+ database.write_objects(page)
44
+ end
41
45
  end
42
46
  end
43
47
 
@@ -1,16 +1,4 @@
1
1
  class GitDB::Objects::Commit < GitDB::Objects::Base
2
-
3
- def raw
4
- "commit #{data.length}\000#{data}"
5
- end
6
-
7
- def type
8
- GitDB::OBJ_COMMIT
9
- end
10
-
11
- def message
12
- data.split("\n\n", 2).last
13
- end
14
2
 
15
3
  def author
16
4
  attributes['author'].first
@@ -20,8 +8,8 @@ class GitDB::Objects::Commit < GitDB::Objects::Base
20
8
  attributes['committer'].first
21
9
  end
22
10
 
23
- def tree
24
- attributes['tree'].first
11
+ def message
12
+ data.split("\n\n", 2).last
25
13
  end
26
14
 
27
15
  def parents
@@ -32,6 +20,18 @@ class GitDB::Objects::Commit < GitDB::Objects::Base
32
20
  [:tree, :parents, :author, :committer, :message]
33
21
  end
34
22
 
23
+ def raw
24
+ "commit #{data.length}\000#{data}"
25
+ end
26
+
27
+ def type
28
+ GitDB::OBJ_COMMIT
29
+ end
30
+
31
+ def tree
32
+ attributes['tree'].first
33
+ end
34
+
35
35
  private ######################################################################
36
36
 
37
37
  def attributes
@@ -2,18 +2,18 @@ require 'stringio'
2
2
 
3
3
  class GitDB::Objects::Tree < GitDB::Objects::Base
4
4
 
5
- # TODO: memoize entries
6
-
7
5
  def entries
8
- entries = []
9
- stream = StringIO.new(data)
10
- until stream.eof?
11
- perms = read_until(stream, ' ').to_i
12
- name = read_until(stream, 0.chr)
13
- sha = GitDB.sha1_to_hex(stream.read(20))
14
- entries << GitDB::Objects::Entry.new(sha, perms, name)
6
+ @entries ||= begin
7
+ entries = []
8
+ stream = StringIO.new(data)
9
+ until stream.eof?
10
+ perms = read_until(stream, ' ').to_i
11
+ name = read_until(stream, 0.chr)
12
+ sha = GitDB.sha1_to_hex(stream.read(20))
13
+ entries << GitDB::Objects::Entry.new(sha, perms, name)
14
+ end
15
+ entries
15
16
  end
16
- entries
17
17
  end
18
18
 
19
19
  def properties
@@ -20,22 +20,30 @@ class GitDB::Protocol
20
20
  end
21
21
 
22
22
  def read_command
23
+ # length is stored in the first 4 bytes
23
24
  length = reader.read(4)
24
25
  return nil unless length
25
- length = length.to_i(16) - 4
26
- if (length == -4)
27
- GitDB.log('GOT EOF')
28
- return
29
- end
26
+
27
+ # length is stored as hex, convert back to decimal and return if it's 0
28
+ length = length.to_i(16)
29
+ return if length.zero?
30
+
31
+ # length includes the 4 bytes of the length itself, subtract for data
32
+ length -= 4
33
+
34
+ # read and return the data
30
35
  data = reader.read(length)
31
- GitDB.log("GOT DATA: #{data.inspect}")
36
+ GitDB.log("RECEIVED COMMAND: #{data.inspect}")
32
37
  data
33
38
  end
34
39
 
35
40
  def write_command(command)
36
- raw_command = encode_command(command)
37
- GitDB.log("WWRITING COMMAND: #{raw_command.inspect}")
38
- writer.print raw_command
41
+ # output the length
42
+ writer.print length_as_hex(command)
43
+
44
+ # output the data
45
+ GitDB.log("SENDING COMMAND: #{command.inspect}")
46
+ writer.print command
39
47
  writer.flush
40
48
  end
41
49
 
@@ -45,7 +53,6 @@ class GitDB::Protocol
45
53
  end
46
54
 
47
55
  def write_eof
48
- #GitDB.log("WRITING EOF")
49
56
  writer.print '0000'
50
57
  writer.flush
51
58
  end
@@ -62,10 +69,6 @@ class GitDB::Protocol
62
69
 
63
70
  private ######################################################################
64
71
 
65
- def encode_command(command)
66
- length_as_hex(command) << command
67
- end
68
-
69
72
  def length_as_hex(command)
70
73
  hex = (command.length + 4).to_s(16).rjust(4, '0')
71
74
  end
@@ -0,0 +1,18 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ describe "GitDB::Objects::Blob" do
4
+
5
+ before(:each) do
6
+ @data = "test blob"
7
+ @blob = GitDB::Objects::Blob.new(@data)
8
+ end
9
+
10
+ it "has a raw value" do
11
+ @blob.raw.should == "blob #{@data.length}\000#{@data}"
12
+ end
13
+
14
+ it "has a type" do
15
+ @blob.type.should == GitDB::OBJ_BLOB
16
+ end
17
+
18
+ end
@@ -0,0 +1,65 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ describe "GitDB::Objects::Commit" do
4
+
5
+ class CommitFactory
6
+ attr_accessor :tree, :parents, :author, :committer, :message
7
+
8
+ def to_data
9
+ raw = ""
10
+ raw << "tree #{tree}\n"
11
+ parents.each do |parent|
12
+ raw << "parent #{parent}\n"
13
+ end
14
+ raw << "author #{author}\n"
15
+ raw << "committer #{committer}\n"
16
+ raw << "\n"
17
+ raw << message
18
+ end
19
+ end
20
+
21
+ before(:each) do
22
+ @raw_commit = CommitFactory.new
23
+ @raw_commit.tree = "12a3b4d6c8d95475e3faf0e4a7c431c9609057f5"
24
+ @raw_commit.parents = ["45bef2e266991a468b02a82e7989aca680557f7b"]
25
+ @raw_commit.author = "David Dollar <ddollar@gmail.com> 1252946959 -0400"
26
+ @raw_commit.committer = "David Dollar <ddollar@gmail.com> 1252946959 -0400"
27
+ @raw_commit.message = "test message\n"
28
+ @data = @raw_commit.to_data
29
+
30
+ @commit = GitDB::Objects::Commit.new(@data)
31
+ end
32
+
33
+ it "has an author" do
34
+ @commit.author.should == @raw_commit.author
35
+ end
36
+
37
+ it "has a committer" do
38
+ @commit.committer.should == @raw_commit.committer
39
+ end
40
+
41
+ it "has a message" do
42
+ @commit.message.should == @raw_commit.message
43
+ end
44
+
45
+ it "has parents" do
46
+ @commit.parents.should == @raw_commit.parents
47
+ end
48
+
49
+ it "has properties" do
50
+ @commit.properties.should == [:tree, :parents, :author, :committer, :message]
51
+ end
52
+
53
+ it "has a raw value" do
54
+ @commit.raw.should == "commit #{@data.length}\000#{@data}"
55
+ end
56
+
57
+ it "has a tree" do
58
+ @commit.tree.should == @raw_commit.tree
59
+ end
60
+
61
+ it "has a type" do
62
+ @commit.type.should == GitDB::OBJ_COMMIT
63
+ end
64
+
65
+ end
@@ -22,4 +22,5 @@ describe "GitDB::Objects::Entry" do
22
22
  it "converts to json" do
23
23
  @entry.to_json.should == @entry.to_hash.to_json
24
24
  end
25
+
25
26
  end
@@ -11,4 +11,5 @@ describe "GitDB::Objects::Tag" do
11
11
  it "has a type" do
12
12
  @tag.type.should == GitDB::OBJ_TAG
13
13
  end
14
+
14
15
  end
@@ -2,7 +2,7 @@ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
2
 
3
3
  describe "GitDB::Objects::Tree" do
4
4
 
5
- class TreeEntry
5
+ class TreeEntryFactory
6
6
  attr_reader :perms, :name, :sha
7
7
 
8
8
  def initialize(perms, name, sha)
@@ -11,15 +11,15 @@ describe "GitDB::Objects::Tree" do
11
11
  @sha = sha
12
12
  end
13
13
 
14
- def to_entry
14
+ def to_data
15
15
  "#{perms} #{name}\000#{GitDB.hex_to_sha1(sha)}"
16
16
  end
17
17
  end
18
18
 
19
19
  before(:each) do
20
- @entry1 = TreeEntry.new(100644, "entry1", "1111111111111111111111111111111111111111")
21
- @entry2 = TreeEntry.new(100444, "entry2", "2222222222222222222222222222222222222222")
22
- @data = [ @entry1.to_entry, @entry2.to_entry ].join('')
20
+ @entry1 = TreeEntryFactory.new(100644, "entry1", "1111111111111111111111111111111111111111")
21
+ @entry2 = TreeEntryFactory.new(100444, "entry2", "2222222222222222222222222222222222222222")
22
+ @data = [ @entry1.to_data, @entry2.to_data ].join('')
23
23
  @tree = GitDB::Objects::Tree.new(@data)
24
24
  end
25
25
 
@@ -39,4 +39,5 @@ describe "GitDB::Objects::Tree" do
39
39
  it "has a type" do
40
40
  @tree.type.should == GitDB::OBJ_TREE
41
41
  end
42
+
42
43
  end
@@ -0,0 +1,28 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe "GitDB::Objects" do
4
+
5
+ describe "new_from_type" do
6
+
7
+ it "can create a commit" do
8
+ GitDB::Objects.new_from_type(GitDB::OBJ_COMMIT, '').should be_a(GitDB::Objects::Commit)
9
+ end
10
+
11
+ it "can create a tree" do
12
+ GitDB::Objects.new_from_type(GitDB::OBJ_TREE, '').should be_a(GitDB::Objects::Tree)
13
+ end
14
+
15
+ it "can create a blob" do
16
+ GitDB::Objects.new_from_type(GitDB::OBJ_BLOB, '').should be_a(GitDB::Objects::Blob)
17
+ end
18
+
19
+ it "can create a tag" do
20
+ GitDB::Objects.new_from_type(GitDB::OBJ_TAG, '').should be_a(GitDB::Objects::Tag)
21
+ end
22
+
23
+ it "raises on unknown types" do
24
+ lambda { GitDB::Objects.new_from_type(-1, '') }.should raise_error
25
+ end
26
+ end
27
+
28
+ end
@@ -0,0 +1,135 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe "GitDB::Protocol" do
4
+
5
+ describe "initialize with defaults" do
6
+ before(:each) do
7
+ @protocol = GitDB::Protocol.new
8
+ end
9
+
10
+ it "uses STDIN and STDOUT when no io is specified" do
11
+ @protocol.reader.should == STDIN
12
+ @protocol.writer.should == STDOUT
13
+ end
14
+ end
15
+
16
+ describe "initialize with io" do
17
+ before(:each) do
18
+ @io = StringIO.new
19
+ @protocol = GitDB::Protocol.new(@io)
20
+ end
21
+
22
+ it "sets both reader and writer to the io" do
23
+ @protocol.reader.should == @io
24
+ @protocol.writer.should == @io
25
+ end
26
+ end
27
+
28
+ describe "flush" do
29
+ before(:each) do
30
+ @io = StringIO.new
31
+ @protocol = GitDB::Protocol.new(@io)
32
+ end
33
+
34
+ it "can flush the writer" do
35
+ @io.should_receive(:flush)
36
+ @protocol.flush
37
+ end
38
+ end
39
+
40
+ describe "read_command" do
41
+ describe "valid command" do
42
+ before(:each) do
43
+ @io = StringIO.new("000bcommand")
44
+ @protocol = GitDB::Protocol.new(@io)
45
+ end
46
+
47
+ it "reads the command" do
48
+ @protocol.read_command.should == 'command'
49
+ end
50
+ end
51
+
52
+ describe "eof" do
53
+ before(:each) do
54
+ @io = StringIO.new("0000")
55
+ @protocol = GitDB::Protocol.new(@io)
56
+ end
57
+
58
+ it "returns nil" do
59
+ @protocol.read_command.should be_nil
60
+ end
61
+ end
62
+ end
63
+
64
+ describe "write_command" do
65
+ before(:each) do
66
+ @io = StringIO.new
67
+ @protocol = GitDB::Protocol.new(@io)
68
+ end
69
+
70
+ it "writes a command" do
71
+ @protocol.write_command('command')
72
+ @io.string.should == '000bcommand'
73
+ end
74
+ end
75
+
76
+ describe "write" do
77
+ before(:each) do
78
+ @io = StringIO.new
79
+ @protocol = GitDB::Protocol.new(@io)
80
+ @data = "test data"
81
+ end
82
+
83
+ it "can write" do
84
+ @io.should_receive(:write).with(@data)
85
+ @protocol.write(@data)
86
+ end
87
+
88
+ it "flushes after write" do
89
+ @io.should_receive(:flush)
90
+ @protocol.write(@data)
91
+ end
92
+ end
93
+
94
+ describe "write_eof" do
95
+ before(:each) do
96
+ @io = StringIO.new
97
+ @protocol = GitDB::Protocol.new(@io)
98
+ end
99
+
100
+ it "writes eof" do
101
+ @io.should_receive(:write).with("0000")
102
+ @protocol.write_eof
103
+ end
104
+ end
105
+
106
+ describe "read_pack" do
107
+ before(:each) do
108
+ @io = StringIO.new
109
+ @protocol = GitDB::Protocol.new(@io)
110
+ @pack = mock(GitDB::Pack)
111
+ end
112
+
113
+ it "uses GitDB::Pack to read a pack object" do
114
+ GitDB::Pack.should_receive(:new).with(@io).and_return(@pack)
115
+ @pack.should_receive(:read)
116
+ @protocol.read_pack
117
+ end
118
+ end
119
+
120
+ describe "write_pack" do
121
+ before(:each) do
122
+ @io = StringIO.new
123
+ @protocol = GitDB::Protocol.new(@io)
124
+ @pack = mock(GitDB::Pack)
125
+ @entries = []
126
+ end
127
+
128
+ it "uses GitDB::Pack to write a pack object" do
129
+ GitDB::Pack.should_receive(:new).with(@io).and_return(@pack)
130
+ @pack.should_receive(:write).with(@entries)
131
+ @protocol.write_pack(@entries)
132
+ end
133
+ end
134
+
135
+ end
@@ -21,14 +21,45 @@ describe "GitDB" do
21
21
  GitDB.logger.should respond_to(:puts)
22
22
  end
23
23
 
24
- it "logs messages sent to log" do
25
- @logger = mock(Logger)
26
- @message = "Log This"
24
+ describe "with DEBUG" do
25
+ before(:each) do
26
+ @old_debug = ENV["DEBUG"]
27
+ ENV["DEBUG"] = "1"
28
+ end
27
29
 
28
- GitDB.should_receive(:logger).and_return(@logger)
29
- @logger.should_receive(:puts).with(@message)
30
+ after(:each) do
31
+ ENV["DEBUG"] = @old_debug
32
+ end
30
33
 
31
- GitDB.log(@message)
34
+ it "logs messages sent to log" do
35
+ @logger = mock(Logger)
36
+ @message = "Log This"
37
+
38
+ GitDB.should_receive(:logger).and_return(@logger)
39
+ @logger.should_receive(:puts).with(@message)
40
+
41
+ GitDB.log(@message)
42
+ end
43
+ end
44
+
45
+ describe "without DEBUG" do
46
+ before(:each) do
47
+ @old_debug = ENV["DEBUG"]
48
+ ENV["DEBUG"] = nil
49
+ end
50
+
51
+ after(:each) do
52
+ ENV["DEBUG"] = @old_debug
53
+ end
54
+
55
+ it "should not log messages sent to log" do
56
+ @logger = mock(Logger)
57
+ @message = "Log This"
58
+
59
+ GitDB.should_not_receive(:logger)
60
+
61
+ GitDB.log(@message)
62
+ end
32
63
  end
33
64
  end
34
65
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: git-db
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Dollar
@@ -49,9 +49,6 @@ files:
49
49
  - Rakefile
50
50
  - VERSION
51
51
  - bin/git-db
52
- - features/git-db.feature
53
- - features/step_definitions/git-db_steps.rb
54
- - features/support/env.rb
55
52
  - git-db.gemspec
56
53
  - lib/git-db.rb
57
54
  - lib/git-db/commands.rb
@@ -71,9 +68,13 @@ files:
71
68
  - lib/git-db/utility/counting_io.rb
72
69
  - spec/git-db/commands_spec.rb
73
70
  - spec/git-db/objects/base_spec.rb
71
+ - spec/git-db/objects/blob_spec.rb
72
+ - spec/git-db/objects/commit_spec.rb
74
73
  - spec/git-db/objects/entry_spec.rb
75
74
  - spec/git-db/objects/tag_spec.rb
76
75
  - spec/git-db/objects/tree_spec.rb
76
+ - spec/git-db/objects_spec.rb
77
+ - spec/git-db/protocol_spec.rb
77
78
  - spec/git-db/utility/counting_io_spec.rb
78
79
  - spec/git-db_spec.rb
79
80
  - spec/rcov.opts
@@ -110,9 +111,13 @@ summary: Database-based git server
110
111
  test_files:
111
112
  - spec/git-db/commands_spec.rb
112
113
  - spec/git-db/objects/base_spec.rb
114
+ - spec/git-db/objects/blob_spec.rb
115
+ - spec/git-db/objects/commit_spec.rb
113
116
  - spec/git-db/objects/entry_spec.rb
114
117
  - spec/git-db/objects/tag_spec.rb
115
118
  - spec/git-db/objects/tree_spec.rb
119
+ - spec/git-db/objects_spec.rb
120
+ - spec/git-db/protocol_spec.rb
116
121
  - spec/git-db/utility/counting_io_spec.rb
117
122
  - spec/git-db_spec.rb
118
123
  - spec/spec_helper.rb
@@ -1,9 +0,0 @@
1
- Feature: something something
2
- In order to something something
3
- A user something something
4
- something something something
5
-
6
- Scenario: something something
7
- Given inspiration
8
- When I create a sweet new gem
9
- Then everyone should see how awesome I am
File without changes
@@ -1,4 +0,0 @@
1
- $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../../lib')
2
- require 'git-db'
3
-
4
- require 'spec/expectations'