Class: Mercurial::FileIndex

Inherits:
Object
  • Object
show all
Includes:
Helper
Defined in:
lib/mercurial-ruby/file_index.rb

Overview

This class was ported from grit.

It implements a file-based ‘file index’, a simple index of all of the reachable commits in a repo, along with the parents and which files were modified during each commit.

This class creates and reads a file named ‘[.hg]/file-index’.

Defined Under Namespace

Classes: IndexFileNotFound, IndexFileTooBig, UnsupportedRef

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helper

#hg, #hg_to_array, #shell

Constructor Details

#initialize(repository) ⇒ FileIndex

initializes index given repository



28
29
30
# File 'lib/mercurial-ruby/file_index.rb', line 28

def initialize(repository)
  @repository = repository      
end

Class Attribute Details

.max_file_sizeObject

Returns the value of attribute max_file_size.



20
21
22
# File 'lib/mercurial-ruby/file_index.rb', line 20

def max_file_size
  @max_file_size
end

Instance Attribute Details

#repositoryObject (readonly)

Returns the value of attribute repository.



25
26
27
# File 'lib/mercurial-ruby/file_index.rb', line 25

def repository
  @repository
end

Instance Method Details

#commits_for(file) ⇒ Object

returns all commits for a file



97
98
99
100
# File 'lib/mercurial-ruby/file_index.rb', line 97

def commits_for(file)
  read_if_needed
  @all_files[file] || []
end

#commits_from(commit_sha) ⇒ Object

builds a list of all commits reachable from a single commit

Raises:



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/mercurial-ruby/file_index.rb', line 63

def commits_from(commit_sha)
  raise UnsupportedRef if commit_sha.is_a? Array
  read_if_needed

  already = {}
  final = []
  left_to_do = [commit_sha]

  while commit_sha = left_to_do.shift
    next if already[commit_sha]

    final << commit_sha
    already[commit_sha] = true

    commit = @commit_index[commit_sha]
    commit[:parents].each do |sha|
      left_to_do << sha
    end if commit
  end

  sort_commits(final)
end

#count(commit_sha) ⇒ Object

returns count of all commits reachable from SHA note: originally did this recursively, but ruby gets pissed about that on really big repos where the stack level gets ‘too deep’ (thats what she said)



57
58
59
60
# File 'lib/mercurial-ruby/file_index.rb', line 57

def count(commit_sha)
  read_if_needed
  commits_from(commit_sha).size
end

#count_allObject

returns count of all commits



49
50
51
52
# File 'lib/mercurial-ruby/file_index.rb', line 49

def count_all
  read_if_needed
  @sha_count
end

#destroy!Object



131
132
133
# File 'lib/mercurial-ruby/file_index.rb', line 131

def destroy!
  FileUtils.rm_f(path)
end

#files(commit_sha) ⇒ Object

returns files changed at commit sha



87
88
89
90
91
92
93
94
# File 'lib/mercurial-ruby/file_index.rb', line 87

def files(commit_sha)
  read_if_needed
  if commit = @commit_index[commit_sha]
    commit[:files]
  else
    []
  end
end

#last_commits(commit_sha, files_matcher) ⇒ Object

returns the shas of the last commits for all the files in [] from commit_sha files_matcher can be a regexp or an array



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/mercurial-ruby/file_index.rb', line 105

def last_commits(commit_sha, files_matcher)
  read_if_needed
  acceptable = commits_from(commit_sha)

  matches = {}

  if files_matcher.is_a? Regexp
    files = @all_files.keys.select { |file| file =~ files_matcher }
    files_matcher = files
  end

  if files_matcher.is_a? Array
    # find the last commit for each file in the array
    files_matcher.each do |f|
      @all_files[f].each do |try|
        if acceptable.include?(try)
          matches[f] = try
          break
        end
      end if @all_files[f]
    end
  end

  matches
end

#pathObject



135
136
137
# File 'lib/mercurial-ruby/file_index.rb', line 135

def path
  File.join(repository.dothg_path, 'file-index')
end

#reloadObject



32
33
34
# File 'lib/mercurial-ruby/file_index.rb', line 32

def reload
  @_read_complete = false
end

#update(oldrev = nil, newrev = nil) ⇒ Object

updates file index



37
38
39
40
41
42
43
44
45
46
# File 'lib/mercurial-ruby/file_index.rb', line 37

def update(oldrev=nil, newrev=nil)
  if index_file_exists? && oldrev != "0"*40
    hg([
        "log --debug -r ?:? --style ? >> ?",
        oldrev, newrev, Style.file_index, path
      ])
  else
    hg(["log --debug -r : --style ? > ?", Style.file_index, path])
  end
end