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, #local_write_tree

Constructor Details

#initialize(repo, treeish = nil) ⇒ StageIndex

Returns a new instance of StageIndex.



132
133
134
135
136
137
138
139
# File 'lib/git-ds/index.rb', line 132

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.



128
129
130
# File 'lib/git-ds/index.rb', line 128

def parent_commit
  @parent_commit
end

#shaObject (readonly)

Returns the value of attribute sha.



127
128
129
# File 'lib/git-ds/index.rb', line 127

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.



199
200
201
202
# File 'lib/git-ds/index.rb', line 199

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.



173
174
175
176
# File 'lib/git-ds/index.rb', line 173

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

#commit(msg, author = nil) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/git-ds/index.rb', line 143

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



178
179
180
# File 'lib/git-ds/index.rb', line 178

def read_tree(sha)
  super
end

#syncObject

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.



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

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

#writeObject

Write tree object for index to object database.



166
167
168
# File 'lib/git-ds/index.rb', line 166

def write
  @sha = super
end