Class: GitStore::Commit

Inherits:
Object
  • Object
show all
Defined in:
lib/git_store/commit.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(store, id = nil, data = nil) ⇒ Commit

Returns a new instance of Commit.



6
7
8
9
10
11
12
# File 'lib/git_store/commit.rb', line 6

def initialize(store, id = nil, data = nil)
  @store = store
  @id = id
  @parent = []

  parse(data) if data
end

Instance Attribute Details

#authorObject

Returns the value of attribute author.



4
5
6
# File 'lib/git_store/commit.rb', line 4

def author
  @author
end

#committerObject

Returns the value of attribute committer.



4
5
6
# File 'lib/git_store/commit.rb', line 4

def committer
  @committer
end

#idObject

Returns the value of attribute id.



4
5
6
# File 'lib/git_store/commit.rb', line 4

def id
  @id
end

#messageObject

Returns the value of attribute message.



4
5
6
# File 'lib/git_store/commit.rb', line 4

def message
  @message
end

#parentObject

Returns the value of attribute parent.



4
5
6
# File 'lib/git_store/commit.rb', line 4

def parent
  @parent
end

#storeObject

Returns the value of attribute store.



4
5
6
# File 'lib/git_store/commit.rb', line 4

def store
  @store
end

#treeObject

Returns the value of attribute tree.



4
5
6
# File 'lib/git_store/commit.rb', line 4

def tree
  @tree
end

Instance Method Details

#==(other) ⇒ Object



14
15
16
# File 'lib/git_store/commit.rb', line 14

def ==(other)
  Commit === other and id == other.id
end

#diff(commit, path = nil) ⇒ Object



41
42
43
44
# File 'lib/git_store/commit.rb', line 41

def diff(commit, path = nil)
  commit = commit.id if Commit === commit
  Diff.exec(store, "git diff --full-index #{commit} #{id} -- '#{path}'")
end

#diffs(path = nil) ⇒ Object



46
47
48
# File 'lib/git_store/commit.rb', line 46

def diffs(path = nil)
  diff(parent.first, path)
end

#dumpObject



54
55
56
57
58
59
60
61
# File 'lib/git_store/commit.rb', line 54

def dump
  [ "tree #{ tree.id }",
    parent.map { |parent| "parent #{parent}" },
    "author #{ author.dump }",
    "committer #{ committer.dump }",
    '',
    message ].flatten.join("\n")
end

#parse(data) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/git_store/commit.rb', line 18

def parse(data)
  headers, @message = data.split(/\n\n/, 2)

  headers.split(/\n/).each do |header|
    key, value = header.split(/ /, 2)
    case key
    when 'parent'
      @parent << value

    when 'author'
      @author = User.parse(value)

    when 'committer'
      @committer = User.parse(value)

    when 'tree'
      @tree = store.get(value)
    end
  end

  self
end

#writeObject



50
51
52
# File 'lib/git_store/commit.rb', line 50

def write
  @id = store.put(self)
end