Class: Grit::Index
- Inherits:
-
Object
- Object
- Grit::Index
- Defined in:
- lib/grit/lib/grit/index.rb
Instance Attribute Summary collapse
-
#current_tree ⇒ Object
Returns the value of attribute current_tree.
-
#repo ⇒ Object
Returns the value of attribute repo.
-
#tree ⇒ Object
Returns the value of attribute tree.
Instance Method Summary collapse
-
#add(file_path, data) ⇒ Object
Add a file to the index
path
is the path (including filename)data
is the binary contents of the file. -
#commit(message, parents = nil, actor = nil, last_tree = nil, head = 'master') ⇒ Object
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]. -
#initialize(repo) ⇒ Index
constructor
A new instance of Index.
-
#read_tree(tree) ⇒ Object
Sets the current tree
tree
the branch/tag/sha… -
#write_blob(data) ⇒ Object
Write the blob to the index
data
is the data to write. -
#write_tree(tree, now_tree = nil) ⇒ Object
Recursively write a tree to the index
tree
is the tree.
Constructor Details
#initialize(repo) ⇒ Index
Returns a new instance of Index.
6 7 8 9 10 |
# File 'lib/grit/lib/grit/index.rb', line 6 def initialize(repo) self.repo = repo self.tree = {} self.current_tree = nil end |
Instance Attribute Details
#current_tree ⇒ Object
Returns the value of attribute current_tree.
4 5 6 |
# File 'lib/grit/lib/grit/index.rb', line 4 def current_tree @current_tree end |
#repo ⇒ Object
Returns the value of attribute repo.
4 5 6 |
# File 'lib/grit/lib/grit/index.rb', line 4 def repo @repo end |
#tree ⇒ Object
Returns the value of attribute tree.
4 5 6 |
# File 'lib/grit/lib/grit/index.rb', line 4 def tree @tree end |
Instance Method Details
#add(file_path, data) ⇒ Object
Add a file to the index
+path+ is the path (including filename)
+data+ is the binary contents of the file
Returns nothing
17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/grit/lib/grit/index.rb', line 17 def add(file_path, data) path = file_path.split('/') filename = path.pop current = self.tree path.each do |dir| current[dir] ||= {} node = current[dir] current = node end current[filename] = data end |
#commit(message, parents = nil, actor = nil, last_tree = nil, head = 'master') ⇒ Object
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
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/grit/lib/grit/index.rb', line 48 def commit(, parents = nil, actor = nil, last_tree = nil, head = 'master') tree_sha1 = write_tree(self.tree, self.current_tree) return false if tree_sha1 == last_tree # don't write identical commits contents = [] contents << ['tree', tree_sha1].join(' ') parents.each do |p| contents << ['parent', p].join(' ') if p end if parents if actor name = actor.name email = actor.email else config = Config.new(self.repo) name = config['user.name'] email = config['user.email'] end = "#{name} <#{email}> #{Time.now.to_i} -0700" # !! TODO : gotta fix this contents << ['author', ].join(' ') contents << ['committer', ].join(' ') contents << '' contents << commit_sha1 = self.repo.git.put_raw_object(contents.join("\n"), 'commit') self.repo.update_ref(head, commit_sha1) end |
#read_tree(tree) ⇒ Object
Sets the current tree
+tree+ the branch/tag/sha... to use - a string
Returns index (self)
36 37 38 |
# File 'lib/grit/lib/grit/index.rb', line 36 def read_tree(tree) self.current_tree = self.repo.tree(tree) end |
#write_blob(data) ⇒ Object
Write the blob to the index
+data+ is the data to write
Returns the SHA1 String of the blob
117 118 119 |
# File 'lib/grit/lib/grit/index.rb', line 117 def write_blob(data) self.repo.git.put_raw_object(data, 'blob') end |
#write_tree(tree, now_tree = nil) ⇒ Object
Recursively write a tree to the index
+tree+ is the tree
Returns the SHA1 String of the tree
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/grit/lib/grit/index.rb', line 82 def write_tree(tree, now_tree = nil) tree_contents = {} # fill in original tree now_tree.contents.each do |obj| sha = [obj.id].pack("H*") k = obj.name k += '/' if (obj.class == Grit::Tree) tree_contents[k] = "%s %s\0%s" % [obj.mode.to_s, obj.name, sha] end if now_tree # overwrite with new tree contents tree.each do |k, v| case v when String sha = write_blob(v) sha = [sha].pack("H*") str = "%s %s\0%s" % ['100644', k, sha] tree_contents[k] = str when Hash ctree = now_tree/k if now_tree sha = write_tree(v, ctree) sha = [sha].pack("H*") str = "%s %s\0%s" % ['040000', k, sha] tree_contents[k + '/'] = str end end tr = tree_contents.sort.map { |k, v| v }.join('') self.repo.git.put_raw_object(tr, 'tree') end |