Class: GitDS::Index

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

Overview

A Git Index.

Direct Known Subclasses

StageIndex

Instance Method Summary collapse

Instance Method Details

#add(path, data, on_fs = false) ⇒ Object



71
72
73
74
# File 'lib/git-ds/index.rb', line 71

def add(path, data, on_fs=false)
  super(path, data)
  add_fs_item(path, data) if on_fs
end

#add_dbObject

Add a DB entry at the virtual path ‘path’ with contents ‘contents’



69
# File 'lib/git-ds/index.rb', line 69

alias :add_db :add

#add_fs(path, data) ⇒ Object

Convenience function to add an on-filesystem object.



79
80
81
# File 'lib/git-ds/index.rb', line 79

def add_fs(path, data)
  add(path, data, true)
end

#delete(path) ⇒ Object

Wrapper for Grit::Index#delete that removes the file if it exists on the filesystem.



87
88
89
90
91
92
# File 'lib/git-ds/index.rb', line 87

def delete(path)
  super
  @repo.exec_in_git_dir {
    ::FileUtils.remove_entry(path) if ::File.exist?(path)
  }
end

#include?(path) ⇒ Boolean

Return true if index includes path.

Returns:

  • (Boolean)


97
98
99
# File 'lib/git-ds/index.rb', line 97

def include?(path)
  path_to_object(path) ? true : false
end

#local_write_tree(tree, now_tree = nil) ⇒ Object

Re-implemented from Grit. Grit adds a trailing / to directory names, which makes it impossible to delete Tree objects!



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/git-ds/index.rb', line 36

def local_write_tree(tree, now_tree=nil)
  tree_contents = {}
  now_tree.contents.each do |obj|
    sha = [obj.id].pack("H*")
    k = obj.name
    tree_contents[k] = "%s %s\0%s" % [obj.mode.to_i.to_s, obj.name, sha]
  end if now_tree

  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 = local_write_tree(v, ctree)
        sha = [sha].pack("H*")
        str = "%s %s\0%s" % ['40000', k, sha]
        tree_contents[k] = str
      when false
        tree_contents.delete(k)
    end
  end

  tr = tree_contents.sort.map { |k, v| v }.join('')
  self.repo.git.put_raw_object(tr, 'tree')
end

#path_to_object(path) ⇒ Object

Return the data for the object at ‘path’. This is required in order for in-memory indexes to work properly.



105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/git-ds/index.rb', line 105

def path_to_object(path)
  # Note: this algorithm is re-implemented from Grit::Index#add
  arr = path.split('/')
  fname = arr.pop
  curr = self.tree
  arr.each do |dir|
    curr = curr[dir] if curr
  end

  #TODO: it would be nice to return a Grit::Blob or Grit::Tree here.
  (curr && fname && curr[fname]) ? curr[fname] : nil
end

#writeObject

Write index to the object db, then read the object DB into the GIT staging index. Returns SHA of new tree.



27
28
29
30
# File 'lib/git-ds/index.rb', line 27

def write
  sha = local_write_tree(self.tree, self.current_tree)
  sha
end