Method: RIM::GitSession#execute

Defined in:
lib/rim/git.rb

#execute(cmd) ⇒ Object



268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/rim/git.rb', line 268

def execute(cmd)
  raise "git command has to start with 'git'" unless cmd.start_with? "git "
  cmd.slice!("git ")
  # remove any newlines as they will cause the command line to end prematurely
  cmd.gsub!("\n", "")
  options = ((!@execute_dir || @execute_dir == ".") ? "" : " -C #{@execute_dir}") \
      + (@work_dir.empty? ? "" : " --work-tree=#{File.expand_path(@work_dir)}") \
      + (@git_dir.empty? ? "" : " --git-dir=#{File.expand_path(@git_dir)}") 
  cmd = "git#{options} #{cmd} 2>&1"

  out = `#{cmd}`
  # make sure we don't run into any encoding misinterpretation issues
  out.force_encoding("binary")

  exitstatus = $?.exitstatus

  invid = self.class.next_invocation_id.to_s.ljust(4)
  @logger.debug "git##{invid} \"#{cmd}\" => #{exitstatus}" 

  out.split(/\r?\n/).each do |ol|
    @logger.debug "git##{invid} out : #{ol}"
  end

  exception = exitstatus != 0 ? GitException.new(cmd, exitstatus, out) : nil
  
  if block_given?
    yield out, exception  
  elsif exception
    raise exception
  end

  out
end