Class: GitDB::Protocol

Inherits:
Object
  • Object
show all
Defined in:
lib/git-db/protocol.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io = nil) ⇒ Protocol

Returns a new instance of Protocol.



6
7
8
9
10
11
12
13
14
# File 'lib/git-db/protocol.rb', line 6

def initialize(io=nil)
  if io
    @reader = io
    @writer = io
  else
    @reader = STDIN
    @writer = STDOUT
  end
end

Instance Attribute Details

#readerObject (readonly)

Returns the value of attribute reader.



3
4
5
# File 'lib/git-db/protocol.rb', line 3

def reader
  @reader
end

#writerObject (readonly)

Returns the value of attribute writer.



4
5
6
# File 'lib/git-db/protocol.rb', line 4

def writer
  @writer
end

Instance Method Details

#flushObject

commands ##################################################################



18
19
20
# File 'lib/git-db/protocol.rb', line 18

def flush
  writer.flush
end

#read_commandObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/git-db/protocol.rb', line 22

def read_command
  # length is stored in the first 4 bytes
  length = reader.read(4)
  return nil unless length

  # length is stored as hex, convert back to decimal and return if it's 0
  length = length.to_i(16)
  return if length.zero?

  # length includes the 4 bytes of the length itself, subtract for data
  length -= 4

  # read and return the data
  data = reader.read(length)
  GitDB.log("RECEIVED COMMAND: #{data.inspect}")
  data
end

#read_packObject

packs #####################################################################



62
63
64
# File 'lib/git-db/protocol.rb', line 62

def read_pack
  GitDB::Pack.new(reader).read
end

#write(data) ⇒ Object



50
51
52
53
# File 'lib/git-db/protocol.rb', line 50

def write(data)
  writer.write data
  writer.flush
end

#write_command(command) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/git-db/protocol.rb', line 40

def write_command(command)
  # output the length
  writer.print length_as_hex(command)

  # output the data
  GitDB.log("SENDING COMMAND: #{command.inspect}")
  writer.print command
  writer.flush
end

#write_eofObject



55
56
57
58
# File 'lib/git-db/protocol.rb', line 55

def write_eof
  writer.print '0000'
  writer.flush
end

#write_pack(entries) ⇒ Object



66
67
68
# File 'lib/git-db/protocol.rb', line 66

def write_pack(entries)
  GitDB::Pack.new(writer).write(entries)
end