Class Grit::Index
In: lib/grit/index.rb
Parent: Object

Methods

add   commit   new   read_tree   write_blob   write_tree  

Attributes

current_tree  [RW] 
repo  [RW] 
tree  [RW] 

Public Class methods

[Source]

    # File lib/grit/index.rb, line 6
 6:     def initialize(repo)
 7:       self.repo = repo
 8:       self.tree = {}
 9:       self.current_tree = nil
10:     end

Public Instance methods

Add a file to the index

  +path+ is the path (including filename)
  +data+ is the binary contents of the file

Returns nothing

[Source]

    # File lib/grit/index.rb, line 17
17:     def add(file_path, data)
18:       path = file_path.split('/')
19:       filename = path.pop
20:       
21:       current = self.tree
22:       
23:       path.each do |dir|
24:         current[dir] ||= {}
25:         node = current[dir]
26:         current = node
27:       end
28:       
29:       current[filename] = data
30:     end

Commit the contents of the index

  +message+ is the commit message [nil]
  +parents+ is one or more commits to attach this commit to to form a new head [nil]
  +actor+ is the details of the user making the commit [nil]
  +last_tree+ is a tree to compare with - to avoid making empty commits [nil]
  +head+ is the branch to write this head to [master]

Returns a String of the SHA1 of the commit

[Source]

    # File lib/grit/index.rb, line 48
48:     def commit(message, parents = nil, actor = nil, last_tree = nil, head = 'master')
49:       tree_sha1 = write_tree(self.tree, self.current_tree)
50:       return false if tree_sha1 == last_tree # don't write identical commits
51:       
52:       contents = []
53:       contents << ['tree', tree_sha1].join(' ')
54:       parents.each do |p|
55:         contents << ['parent', p].join(' ') if p        
56:       end if parents
57: 
58:       if actor
59:         name = actor.name
60:         email = actor.email
61:       else
62:         config = Config.new(self.repo)
63:         name = config['user.name']
64:         email = config['user.email']
65:       end
66:     
67:       author_string = "#{name} <#{email}> #{Time.now.to_i} -0700" # !! TODO : gotta fix this
68:       contents << ['author', author_string].join(' ')
69:       contents << ['committer', author_string].join(' ')
70:       contents << ''
71:       contents << message
72:       
73:       commit_sha1 = self.repo.git.ruby_git.put_raw_object(contents.join("\n"), 'commit')      
74:       
75:       self.repo.update_ref(head, commit_sha1)
76:     end

Sets the current tree

  +tree+ the branch/tag/sha... to use - a string

Returns index (self)

[Source]

    # File lib/grit/index.rb, line 36
36:     def read_tree(tree)
37:       self.current_tree = self.repo.tree(tree)
38:     end

Write the blob to the index

  +data+ is the data to write

Returns the SHA1 String of the blob

[Source]

     # File lib/grit/index.rb, line 117
117:     def write_blob(data)
118:       self.repo.git.ruby_git.put_raw_object(data, 'blob')
119:     end

Recursively write a tree to the index

  +tree+ is the tree

Returns the SHA1 String of the tree

[Source]

     # File lib/grit/index.rb, line 82
 82:     def write_tree(tree, now_tree = nil)
 83:       tree_contents = {}
 84:             
 85:       # fill in original tree
 86:       now_tree.contents.each do |obj|
 87:         sha = [obj.id].pack("H*")
 88:         k = obj.name
 89:         k += '/' if (obj.class == Grit::Tree)
 90:         tree_contents[k] = "%s %s\0%s" % [obj.mode.to_s, obj.name, sha]
 91:       end if now_tree
 92:       
 93:       # overwrite with new tree contents
 94:       tree.each do |k, v|
 95:         case v
 96:           when String
 97:             sha = write_blob(v)
 98:             sha = [sha].pack("H*")
 99:             str = "%s %s\0%s" % ['100644', k, sha]
100:             tree_contents[k] = str
101:           when Hash
102:             ctree = now_tree/k if now_tree
103:             sha = write_tree(v, ctree)
104:             sha = [sha].pack("H*")
105:             str = "%s %s\0%s" % ['040000', k, sha]
106:             tree_contents[k + '/'] = str
107:         end
108:       end
109:       tr = tree_contents.sort.map { |k, v| v }.join('')
110:       self.repo.git.ruby_git.put_raw_object(tr, 'tree')
111:     end

[Validate]