lib/grit/repo.rb in gitlab-grit-1.0.0 vs lib/grit/repo.rb in gitlab-grit-2.5.0

- old
+ new

@@ -410,11 +410,11 @@ end # The Commits objects that are newer than the specified date. # Commits are returned in chronological order. # +start+ is the branch/commit name (default 'master') - # +since+ is a string represeting a date/time + # +since+ is a string representing a date/time # +extra_options+ is a hash of extra options # # Returns Grit::Commit[] (baked) def commits_since(start = 'master', since = '1970-01-01', extra_options = {}) options = {:since => since}.merge(extra_options) @@ -709,9 +709,47 @@ if @bare self.git.fs_move('/', "../#{name}") else self.git.fs_move('/', "../../#{name}") end + end + + def grep(searchtext, contextlines = 3, branch = 'master') + context_arg = '-C ' + contextlines.to_s + result = git.native(:grep, {}, '-n', '-E', '-i', '-z', '--heading', '--break', context_arg, searchtext, branch).force_encoding('UTF-8') + greps = [] + filematches = result.split("\n\n") + filematches.each do |filematch| + binary = false + file = '' + matches = filematch.split("--\n") + matches.each_with_index do |match, i| + content = [] + startline = 0 + lines = match.split("\n") + if i == 0 + text = lines.first + lines.slice!(0) + file = text[/^Binary file (.+) matches$/] + if file + binary = true + else + text.slice! /^#{branch}:/ + file = text + end + end + lines.each_with_index do |line, j| + line.chomp! + number, text = line.split("\0", 2) + if j == 0 + startline = number.to_i + end + content << text + end + greps << Grit::Grep.new(self, file, startline, content, binary) + end + end + greps end # Pretty object inspection def inspect %Q{#<Grit::Repo "#{@path}">}