Class: GitDS::StageIndex

Inherits:
Index
  • Object
show all
Defined in:
lib/git-ds/index.rb

Overview

Index object for the Git staging index.

Direct Known Subclasses

StageMemIndex

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Index

#add, #add_db, #add_fs, #delete, #include?, #local_write_tree, #path_to_object

Constructor Details

#initialize(repo, treeish = nil) ⇒ StageIndex

Returns a new instance of StageIndex.



156
157
158
159
160
161
162
163
# File 'lib/git-ds/index.rb', line 156

def initialize(repo, treeish=nil)
  super(repo)
  @parent_commit = repo.commits(repo.current_branch, 1).first
  treeish = (@parent_commit ? @parent_commit.tree.id : 'master') if \
            not treeish
  read_tree(treeish)
  @sha = self.current_tree.id
end

Instance Attribute Details

#parent_commitObject (readonly)

Returns the value of attribute parent_commit.



152
153
154
# File 'lib/git-ds/index.rb', line 152

def parent_commit
  @parent_commit
end

#shaObject (readonly)

Returns the value of attribute sha.



151
152
153
# File 'lib/git-ds/index.rb', line 151

def sha
  @sha
end

Class Method Details

.read(repo) ⇒ Object

Read staging index from disk and create a StagingIndex object for it.

This can be used to access index contents created by command-line tools.



225
226
227
228
# File 'lib/git-ds/index.rb', line 225

def self.read(repo)
  sha = repo.exec_in_git_dir{`git write-tree`}.chomp
  new(repo, sha)
end

Instance Method Details

#buildObject

Write, read tree. Done when a tree is requested.



197
198
199
200
# File 'lib/git-ds/index.rb', line 197

def build
  return @sha if self.tree.empty?
  self.read_tree(self.write)
end

#commit(msg, author = nil) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/git-ds/index.rb', line 167

def commit(msg, author=nil)
  parents = @parent_commit ? [@parent_commit] : []
  # TODO : why does last_tree cause some commits to fail?
  #          test_transaction(TC_GitDatabaseTest)
  #          transaction commit has wrong message.
  #          <"SUCCESS"> expected but was <"auto-commit on transaction">
  #        Possible bug in Grit::last_tree?
  #last_tree = @parent_commit ? @parent_commit.tree.id : nil
  #sha = super(msg, parents, author, last_tree, @repo.current_branch)
  sha = super(msg, parents, author, nil, @repo.current_branch)
  if sha
    # set index parent_commit to the new commit
    @parent_commit = @repo.commit(sha)
    # read tree back into index
    read_tree(@parent_commit.tree.id)
    sync
  end
  sha
end

#read_tree(sha) ⇒ Object



202
203
204
# File 'lib/git-ds/index.rb', line 202

def read_tree(sha)
  super
end

#syncObject Also known as: force_sync

Sync with staging index. This causes the Git index (used by command-line tools) to be filled with the contents of this index.

This can be instead of a commit to ensure that command-line tools can access the index contents.



213
214
215
216
# File 'lib/git-ds/index.rb', line 213

def sync
  self.build
  @repo.exec_in_git_dir { `git read-tree #{@sha}` }
end

#writeObject

Write tree object for index to object database.



190
191
192
# File 'lib/git-ds/index.rb', line 190

def write
  @sha = super
end