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



356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
# File 'lib/braid/operations.rb', line 356

def apply(diff, *args)
  status, err = nil, nil

  command = "git apply --index --whitespace=nowarn #{args.join(' ')} -"

  if USE_OPEN3
    Open3.popen3(command) do |stdin, stdout, stderr, wait_thread|
      stdin.puts(diff)
      stdin.close
      err = stderr.read
      # Under earlier jrubies this is not correctly passed so add in check
      status = wait_thread.value if wait_thread # Process::Status object returned.
    end
    # Handle earlier jrubies such as 1.6.7.2
    status = $?.exitstatus if status.nil?
  else
    status = Open4.popen4(command) do |pid, stdin, stdout, stderr|
      stdin.puts(diff)
      stdin.close
      err = stderr.read
    end.exitstatus
  end

  raise ShellExecutionError, err unless status == 0
  true
end

#branchObject



351
352
353
354
# File 'lib/braid/operations.rb', line 351

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

#checkout(treeish) ⇒ Object



200
201
202
203
# File 'lib/braid/operations.rb', line 200

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

#clone(*args) ⇒ Object



383
384
385
386
# File 'lib/braid/operations.rb', line 383

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

#commit(message, *args) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/braid/operations.rb', line 172

def commit(message, *args)
  cmd = 'git commit --no-verify'
  if message # allow nil
    message_file = Tempfile.new('braid_commit')
    message_file.print("Braid: #{message}")
    message_file.flush
    message_file.close
    cmd << " -F #{message_file.path}"
  end
  cmd << " #{args.join(' ')}" unless args.empty?
  status, out, err = exec(cmd)
  message_file.unlink if message_file

  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



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

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



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

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

#fetch(remote = nil, *args) ⇒ Object



194
195
196
197
198
# File 'lib/braid/operations.rb', line 194

def fetch(remote = nil, *args)
  args.unshift "-n #{remote}" if remote
  # open4 messes with the pipes of index-pack
  sh("git fetch #{args.join(' ')} > #{Gem.win_platform? ? 'nul' : '/dev/null'}")
end

#headObject



347
348
349
# File 'lib/braid/operations.rb', line 347

def head
  rev_parse('HEAD')
end

#merge_base(target, source) ⇒ Object

Returns the base commit or nil.



206
207
208
209
210
# File 'lib/braid/operations.rb', line 206

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

#merge_ours(opt) ⇒ Object

Implies no commit.



243
244
245
246
# File 'lib/braid/operations.rb', line 243

def merge_ours(opt)
  invoke(:merge, '--allow-unrelated-histories -s ours --no-commit', opt)
  true
end

#merge_subtree(opt) ⇒ Object

Implies no commit.



249
250
251
252
253
254
255
# File 'lib/braid/operations.rb', line 249

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

#merge_trees(base_treeish, local_treeish, remote_treeish) ⇒ Object

Merge three trees (local_treeish should match the current state of the index) and update the index and working tree.

The usage of ‘git merge-recursive’ doesn’t seem to be officially documented, but it does accept trees. When a single base is passed, the ‘recursive’ part (i.e., merge of bases) does not come into play and only the trees matter. But for some reason, Git’s smartest tree merge algorithm is only available via the ‘recursive’ strategy.



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

def merge_trees(base_treeish, local_treeish, remote_treeish)
  invoke(:merge_recursive, base_treeish, "-- #{local_treeish} #{remote_treeish}")
  true
rescue ShellExecutionError => error
  # 'CONFLICT' messages go to stdout.
  raise MergeError, error.out
end

#read_ls_files(prefix) ⇒ Object



273
274
275
# File 'lib/braid/operations.rb', line 273

def read_ls_files(prefix)
  invoke('ls-files', prefix)
end

#read_tree_im(treeish) ⇒ Object

Read tree into the root of the index. This may not be the preferred way to do it, but it seems to work.



292
293
294
295
# File 'lib/braid/operations.rb', line 292

def read_tree_im(treeish)
  invoke(:read_tree, '-im', treeish)
  true
end

#read_tree_prefix_i(treeish, prefix) ⇒ Object

Read tree into the index, regardless of the state of the working tree. Most useful with a temporary index file.



285
286
287
288
# File 'lib/braid/operations.rb', line 285

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

#read_tree_prefix_u(treeish, prefix) ⇒ Object

Read tree into the index and working tree.



278
279
280
281
# File 'lib/braid/operations.rb', line 278

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

#remote_add(remote, path, branch) ⇒ Object

Implies tracking.



219
220
221
222
# File 'lib/braid/operations.rb', line 219

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

#remote_rm(remote) ⇒ Object



224
225
226
227
# File 'lib/braid/operations.rb', line 224

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

#remote_url(remote) ⇒ Object

Checks git remotes.



230
231
232
233
234
235
# File 'lib/braid/operations.rb', line 230

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

#reset_hard(target) ⇒ Object



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

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

#rev_parse(opt) ⇒ Object



212
213
214
215
216
# File 'lib/braid/operations.rb', line 212

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

#rm_r(path) ⇒ Object



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

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

#rm_r_cached(path) ⇒ Object

Remove from index only.



321
322
323
324
# File 'lib/braid/operations.rb', line 321

def rm_r_cached(path)
  invoke(:rm, '-r', '--cached', path)
  true
end

#status_clean?Boolean

Returns:

  • (Boolean)


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

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

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



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

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

#with_temporary_indexObject

Execute a block using a temporary git index file, initially empty.



303
304
305
306
307
308
309
310
311
312
313
# File 'lib/braid/operations.rb', line 303

def with_temporary_index
  Dir.mktmpdir('braid_index') do |dir|
    orig_index_file = ENV['GIT_INDEX_FILE']
    ENV['GIT_INDEX_FILE'] = File.join(dir, 'index')
    begin
      yield
    ensure
      ENV['GIT_INDEX_FILE'] = orig_index_file
    end
  end
end

#write_treeObject

Write a tree object for the current index and return its ID.



298
299
300
# File 'lib/braid/operations.rb', line 298

def write_tree
  invoke(:write_tree)
end