Class: Braid::Operations::Git

Inherits:
Proxy
  • Object
show all
Defined in:
lib/braid/operations.rb

Instance Method Summary collapse

Methods inherited from Proxy

command, #require_version, #require_version!, #version

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Braid::Operations::Proxy

Instance Method Details

#apply(diff, *args) ⇒ Object



349
350
351
352
353
354
355
356
357
358
359
# File 'lib/braid/operations.rb', line 349

def apply(diff, *args)
  err = nil
  status = Open4.popen4("git apply --index --whitespace=nowarn #{args.join(' ')} -") do |pid, stdin, stdout, stderr|
    stdin.puts(diff)
    stdin.close

    err = stderr.read
  end.exitstatus
  raise ShellExecutionError, err unless status == 0
  true
end

#branchObject



344
345
346
347
# File 'lib/braid/operations.rb', line 344

def branch
  status, out, err = exec!("git branch | grep '*'")
  out[2..-1]
end

#checkout(treeish) ⇒ Object



241
242
243
244
# File 'lib/braid/operations.rb', line 241

def checkout(treeish)
  invoke(:checkout, treeish)
  true
end

#clone(*args) ⇒ Object



361
362
363
364
# File 'lib/braid/operations.rb', line 361

def clone(*args)
  # overrides builtin
  invoke(:clone, *args)
end

#commit(message, *args) ⇒ Object



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/braid/operations.rb', line 218

def commit(message, *args)

  commit_message_file = Tempfile.new("braid_commit", ".")
  commit_message_file.print("gitnest: " + message)
  commit_message_file.flush
  status, out, err = exec("git commit -F #{commit_message_file.path} --no-verify #{args.join(' ')}")
  commit_message_file.unlink

  if status == 0
    true
  elsif out.match(/nothing.* to commit/)
    false
  else
    raise ShellExecutionError, err
  end
end

#diff_tree(src_tree, dst_tree, prefix = nil) ⇒ Object



324
325
326
327
328
329
# File 'lib/braid/operations.rb', line 324

def diff_tree(src_tree, dst_tree, prefix = nil)
  cmd = "git diff-tree -p --binary #{src_tree} #{dst_tree}"
  cmd << " --src-prefix=a/#{prefix}/ --dst-prefix=b/#{prefix}/" if prefix
  status, out, err = exec!(cmd)
  out
end

#ensure_clean!Object



336
337
338
# File 'lib/braid/operations.rb', line 336

def ensure_clean!
  status_clean? || raise(LocalChangesPresent)
end

#fetch(remote = nil) ⇒ Object



235
236
237
238
239
# File 'lib/braid/operations.rb', line 235

def fetch(remote = nil)
  args = remote && "-n #{remote}"
  # open4 messes with the pipes of index-pack
  sh("git fetch #{args} 2>&1 >/dev/null")
end

#headObject



340
341
342
# File 'lib/braid/operations.rb', line 340

def head
  rev_parse("HEAD")
end

#merge_base(target, source) ⇒ Object

Returns the base commit or nil.



247
248
249
250
251
# File 'lib/braid/operations.rb', line 247

def merge_base(target, source)
  invoke(:merge_base, target, source)
rescue ShellExecutionError
  nil
end

#merge_ours(opt) ⇒ Object

Implies no commit.



288
289
290
291
# File 'lib/braid/operations.rb', line 288

def merge_ours(opt)
  invoke(:merge, "-s ours --no-commit", opt)
  true
end

#merge_recursive(base_hash, local_hash, remote_hash) ⇒ Object



302
303
304
305
306
307
# File 'lib/braid/operations.rb', line 302

def merge_recursive(base_hash, local_hash, remote_hash)
  invoke(:merge_recursive, base_hash, "-- #{local_hash} #{remote_hash}")
  true
rescue ShellExecutionError
  raise MergeError
end

#merge_subtree(opt) ⇒ Object

Implies no commit.



294
295
296
297
298
299
300
# File 'lib/braid/operations.rb', line 294

def merge_subtree(opt)
  # TODO which options are needed?
  invoke(:merge, "-s subtree --no-commit --no-ff", opt)
  true
rescue ShellExecutionError
  raise MergeError
end

#read_tree_prefix(treeish, prefix) ⇒ Object



309
310
311
312
# File 'lib/braid/operations.rb', line 309

def read_tree_prefix(treeish, prefix)
  invoke(:read_tree, "--prefix=#{prefix}/ -u", treeish)
  true
end

#remote_add(remote, path, branch) ⇒ Object

Implies tracking.



260
261
262
263
# File 'lib/braid/operations.rb', line 260

def remote_add(remote, path, branch)
  invoke(:remote, "add", "-t #{branch} -m #{branch}", remote, path)
  true
end

#remote_rm(remote) ⇒ Object



265
266
267
268
# File 'lib/braid/operations.rb', line 265

def remote_rm(remote)
  invoke(:remote, "rm", remote)
  true
end

#remote_url(remote) ⇒ Object

Checks git and svn remotes.



271
272
273
274
275
276
277
278
279
280
# File 'lib/braid/operations.rb', line 271

def remote_url(remote)
  key = "remote.#{remote}.url"
  begin
    invoke(:config, key)
  rescue ShellExecutionError
    invoke(:config, "svn-#{key}")
  end
rescue ShellExecutionError
  nil
end

#reset_hard(target) ⇒ Object



282
283
284
285
# File 'lib/braid/operations.rb', line 282

def reset_hard(target)
  invoke(:reset, "--hard", target)
  true
end

#rev_parse(opt) ⇒ Object



253
254
255
256
257
# File 'lib/braid/operations.rb', line 253

def rev_parse(opt)
  invoke(:rev_parse, opt)
rescue ShellExecutionError
  raise UnknownRevision, opt
end

#rm_r(path) ⇒ Object



314
315
316
317
# File 'lib/braid/operations.rb', line 314

def rm_r(path)
  invoke(:rm, "-r", path)
  true
end

#status_clean?Boolean

Returns:

  • (Boolean)


331
332
333
334
# File 'lib/braid/operations.rb', line 331

def status_clean?
  status, out, err = exec("git status")
  !out.split("\n").grep(/nothing to commit/).empty?
end

#tree_hash(path, treeish = "HEAD") ⇒ Object



319
320
321
322
# File 'lib/braid/operations.rb', line 319

def tree_hash(path, treeish = "HEAD")
  out = invoke(:ls_tree, treeish, "-d", path)
  out.split[2]
end