Class: Grit::GitRuby::FileIndex

Inherits:
Object
  • Object
show all
Defined in:
lib/grit/git-ruby/file_index.rb

Defined Under Namespace

Classes: IndexFileNotFound, UnsupportedRef

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repo_path) ⇒ FileIndex

initializes index given repo_path



34
35
36
37
38
39
40
41
# File 'lib/grit/git-ruby/file_index.rb', line 34

def initialize(repo_path)
  @index_file = File.join(repo_path, 'file-index')
  if File.file?(@index_file) && (File.size(@index_file) < Grit::GitRuby::FileIndex.max_file_size)
    read_index
  else
    raise IndexFileNotFound
  end
end

Class Attribute Details

.max_file_sizeObject

Returns the value of attribute max_file_size.



26
27
28
# File 'lib/grit/git-ruby/file_index.rb', line 26

def max_file_size
  @max_file_size
end

Instance Attribute Details

#files(commit_sha) ⇒ Object (readonly)

returns files changed at commit sha



83
84
85
# File 'lib/grit/git-ruby/file_index.rb', line 83

def files
  @files
end

Instance Method Details

#commits_for(file) ⇒ Object

returns all commits for a file



88
89
90
# File 'lib/grit/git-ruby/file_index.rb', line 88

def commits_for(file)
  @all_files[file]
end

#commits_from(commit_sha) ⇒ Object

builds a list of all commits reachable from a single commit

Raises:



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/grit/git-ruby/file_index.rb', line 56

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

  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)



51
52
53
# File 'lib/grit/git-ruby/file_index.rb', line 51

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

#count_allObject

returns count of all commits



44
45
46
# File 'lib/grit/git-ruby/file_index.rb', line 44

def count_all
  @sha_count
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



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/grit/git-ruby/file_index.rb', line 95

def last_commits(commit_sha, files_matcher)
  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

#sort_commits(sha_array) ⇒ Object



78
79
80
# File 'lib/grit/git-ruby/file_index.rb', line 78

def sort_commits(sha_array)
  sha_array.sort { |a, b| @commit_order[b].to_i <=> @commit_order[a].to_i }
end