Class: Gollum::Git::Git

Inherits:
Object
  • Object
show all
Defined in:
lib/grit_adapter/git_layer_grit.rb

Overview

Note that in Grit, the methods grep, rm, checkout, ls_files are all passed to native via method_missing. Hence the uniform method signatures.

Instance Method Summary collapse

Constructor Details

#initialize(git, repo_bare = false) ⇒ Git

Returns a new instance of Git.


130
131
132
133
# File 'lib/grit_adapter/git_layer_grit.rb', line 130

def initialize(git, repo_bare = false)
  @git = git
  @repo_bare = repo_bare
end

Instance Method Details

#cat_file(options, sha) ⇒ Object

@repo.git.cat_file(=> true, sha)


194
195
196
# File 'lib/grit_adapter/git_layer_grit.rb', line 194

def cat_file(options, sha)
  @git.cat_file(options, sha)
end

#checkout(path, ref, options = {}, &block) ⇒ Object

git.checkout({}, 'HEAD', '–', path)


161
162
163
# File 'lib/grit_adapter/git_layer_grit.rb', line 161

def checkout(path, ref, options = {}, &block)
  @git.checkout(options, ref, '--', path, &block)
end

#exist?Boolean

Returns:

  • (Boolean)

135
136
137
# File 'lib/grit_adapter/git_layer_grit.rb', line 135

def exist?
  @git.exist?
end

#grep(query, options = {}) ⇒ Object


139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/grit_adapter/git_layer_grit.rb', line 139

def grep(query, options={})
  ref = options[:ref] ? options[:ref] : "HEAD"
  query = Shellwords.shellescape(query)
  query = Shellwords.split(query).select {|q| !(q =~ /^(-O)|(--open-files-in-pager)/) }
  query = Shellwords.join(query)
  args = [{}, '-I', '-i', '-c', query, ref, '--']
  args << options[:path] if options[:path]
  result = @git.native(:grep, *args).split("\n")
  result.map do |line|
    branch_and_name, _, count = line.rpartition(":")
    branch, _, name = branch_and_name.partition(':')
    {:name => name, :count => count}
  end
end

#ls_files(query, options = {}) ⇒ Object


177
178
179
180
181
182
183
184
185
186
187
# File 'lib/grit_adapter/git_layer_grit.rb', line 177

def ls_files(query, options = {})
  ref = options[:ref] ? options[:ref] : "HEAD"
  path = options.delete(:path) || "."
  options.delete(:ref)
  query = Shellwords.shellescape(query)
  if @git.work_tree && !@git.work_tree.empty? then
    @git.ls_files({}, ref, ::File.join(path, "*#{query}*")).split("\n")
  else
    ls_tree({:full_tree => true, :r => true, :name_only => true}, ref, path).split("\n").select {|line| line.match(/#{Regexp.escape(query)}/i)}
  end
end

#ls_tree(options = {}, *args, &block) ⇒ Object


189
190
191
# File 'lib/grit_adapter/git_layer_grit.rb', line 189

def ls_tree(options={}, *args, &block)
  @git.native(:ls_tree, options, *args, &block)
end

#pull(remote, branch, options = {}) ⇒ Object


214
215
216
# File 'lib/grit_adapter/git_layer_grit.rb', line 214

def pull(remote, branch, options = {})
  @git.pull(options, remote, branch)
end

#push(remote, branch, options = {}) ⇒ Object


210
211
212
# File 'lib/grit_adapter/git_layer_grit.rb', line 210

def push(remote, branch, options = {})
  @git.push(options, remote, branch)
end

#rev_list(options, *refs) ⇒ Object


165
166
167
168
169
# File 'lib/grit_adapter/git_layer_grit.rb', line 165

def rev_list(options, *refs)
  @git.rev_list(options, *refs)
rescue Grit::GitRuby::Repository::NoSuchShaFound
  raise Gollum::Git::NoSuchShaFound
end

#revert(path, sha1, sha2, ref) ⇒ Object


171
172
173
174
175
# File 'lib/grit_adapter/git_layer_grit.rb', line 171

def revert(path, sha1, sha2, ref)
  patch = path ?
    repo.diff(sha2, sha1, path).first.diff : repo.diff(sha2, sha1).map { |d| d.diff }.join("\n")
  @git.apply_patch({}, ref, patch)
end

#rm(path, options = {}, &block) ⇒ Object

git.rm(=> true, '–', path)


155
156
157
158
# File 'lib/grit_adapter/git_layer_grit.rb', line 155

def rm(path, options = {}, &block)
  options['f'] = true if options[:force]
  @git.rm(options, '--', path, &block)
end

#versions_for_path(path = nil, ref = nil, options = nil) ⇒ Object


198
199
200
201
202
203
204
205
206
207
208
# File 'lib/grit_adapter/git_layer_grit.rb', line 198

def versions_for_path(path = nil, ref = nil, options = nil)
  if options[:follow]
    options[:pretty] = 'raw'
    options.delete :max_count
    options.delete :skip
    logstr = log(path, ref, options)
    Gollum::Git::Commit.list_from_string(repo, logstr)
  else
    repo.log(ref, path, options).map {|grit_commit| Gollum::Git::Commit.new(grit_commit)}
  end
end