Class: Braid::Operations::Git
- Inherits:
-
Proxy
- Object
- Proxy
- Braid::Operations::Git
show all
- Defined in:
- lib/braid/operations.rb
Instance Method Summary
collapse
-
#apply(diff, *args) ⇒ Object
-
#branch ⇒ Object
-
#checkout(treeish) ⇒ Object
-
#clone(*args) ⇒ Object
-
#commit(message, *args) ⇒ Object
-
#config(args) ⇒ Object
-
#diff_to_stdout(*args) ⇒ Object
-
#diff_tree(src_tree, dst_tree, prefix = nil) ⇒ Object
-
#ensure_clean! ⇒ Object
-
#fetch(remote = nil, *args) ⇒ Object
-
#head ⇒ Object
-
#is_inside_worktree ⇒ Object
If the current directory is not inside a git repository at all, this command will fail with “fatal: Not a git repository” and that will be propagated as a ShellExecutionError.
-
#make_tree_with_subtree(main_content, subtree_path, subtree_content) ⇒ Object
-
#merge_base(target, source) ⇒ Object
Returns the base commit or nil.
-
#merge_ours(opt) ⇒ Object
-
#merge_subtree(opt) ⇒ Object
-
#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.
-
#read_ls_files(prefix) ⇒ Object
-
#read_tree_im(treeish) ⇒ Object
Read tree into the root of the index.
-
#read_tree_prefix_i(treeish, prefix) ⇒ Object
Read tree into the index, regardless of the state of the working tree.
-
#read_tree_prefix_u(treeish, prefix) ⇒ Object
Read tree into the index and working tree.
-
#relative_working_dir ⇒ Object
Get the prefix of the current directory relative to the worktree.
-
#remote_add(remote, path) ⇒ Object
-
#remote_rm(remote) ⇒ Object
-
#remote_url(remote) ⇒ Object
-
#repo_file_path(path) ⇒ Object
Get the physical path to a file in the git repository (e.g., ‘MERGE_MSG’), taking into account worktree configuration.
-
#reset_hard(target) ⇒ Object
-
#rev_parse(opt) ⇒ Object
-
#rm_r(path) ⇒ Object
-
#rm_r_cached(path) ⇒ Object
-
#status_clean? ⇒ Boolean
-
#tree_hash(path, treeish = 'HEAD') ⇒ Object
-
#with_temporary_index ⇒ Object
Execute a block using a temporary git index file, initially empty.
-
#write_tree ⇒ Object
Write a tree object for the current index and return its ID.
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
|
# File 'lib/braid/operations.rb', line 405
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
status = wait_thread.value if wait_thread
end
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
|
#branch ⇒ Object
400
401
402
403
|
# File 'lib/braid/operations.rb', line 400
def branch
status, out, err = exec!("git branch | grep '*'")
out[2..-1]
end
|
#checkout(treeish) ⇒ Object
230
231
232
233
|
# File 'lib/braid/operations.rb', line 230
def checkout(treeish)
invoke(:checkout, treeish)
true
end
|
#clone(*args) ⇒ Object
432
433
434
435
|
# File 'lib/braid/operations.rb', line 432
def clone(*args)
invoke(:clone, *args)
end
|
#commit(message, *args) ⇒ Object
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
|
# File 'lib/braid/operations.rb', line 203
def commit(message, *args)
cmd = 'git commit --no-verify'
if message
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
|
#config(args) ⇒ Object
354
355
356
|
# File 'lib/braid/operations.rb', line 354
def config(args)
invoke(:config, args) rescue nil
end
|
#diff_to_stdout(*args) ⇒ Object
381
382
383
384
385
|
# File 'lib/braid/operations.rb', line 381
def diff_to_stdout(*args)
system("git diff #{args.join(' ')}")
end
|
#diff_tree(src_tree, dst_tree, prefix = nil) ⇒ Object
374
375
376
377
378
379
|
# File 'lib/braid/operations.rb', line 374
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
392
393
394
|
# File 'lib/braid/operations.rb', line 392
def ensure_clean!
status_clean? || raise(LocalChangesPresent)
end
|
#fetch(remote = nil, *args) ⇒ Object
225
226
227
228
|
# File 'lib/braid/operations.rb', line 225
def fetch(remote = nil, *args)
args.unshift "-n #{remote}" if remote
exec!("git fetch #{args.join(' ')}")
end
|
#head ⇒ Object
396
397
398
|
# File 'lib/braid/operations.rb', line 396
def head
rev_parse('HEAD')
end
|
#is_inside_worktree ⇒ Object
If the current directory is not inside a git repository at all, this command will fail with “fatal: Not a git repository” and that will be propagated as a ShellExecutionError. is_inside_worktree can return false when inside a bare repository and in certain other rare cases such as when the GIT_WORK_TREE environment variable is set.
190
191
192
|
# File 'lib/braid/operations.rb', line 190
def is_inside_worktree
invoke(:rev_parse, '--is-inside-work-tree') == 'true'
end
|
#make_tree_with_subtree(main_content, subtree_path, subtree_content) ⇒ Object
342
343
344
345
346
347
348
349
350
351
352
|
# File 'lib/braid/operations.rb', line 342
def make_tree_with_subtree(main_content, subtree_path, subtree_content)
with_temporary_index do
if main_content
read_tree_im(main_content)
rm_r_cached(subtree_path)
end
read_tree_prefix_i(subtree_content, subtree_path)
write_tree
end
end
|
#merge_base(target, source) ⇒ Object
Returns the base commit or nil.
236
237
238
239
240
|
# File 'lib/braid/operations.rb', line 236
def merge_base(target, source)
invoke(:merge_base, target, source)
rescue ShellExecutionError
nil
end
|
#merge_ours(opt) ⇒ Object
273
274
275
276
|
# File 'lib/braid/operations.rb', line 273
def merge_ours(opt)
invoke(:merge, '--allow-unrelated-histories -s ours --no-commit', opt)
true
end
|
#merge_subtree(opt) ⇒ Object
279
280
281
282
283
284
285
|
# File 'lib/braid/operations.rb', line 279
def merge_subtree(opt)
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.
295
296
297
298
299
300
301
|
# File 'lib/braid/operations.rb', line 295
def merge_trees(base_treeish, local_treeish, remote_treeish)
invoke(:merge_recursive, base_treeish, "-- #{local_treeish} #{remote_treeish}")
true
rescue ShellExecutionError => error
raise MergeError, error.out
end
|
#read_ls_files(prefix) ⇒ Object
303
304
305
|
# File 'lib/braid/operations.rb', line 303
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.
322
323
324
325
|
# File 'lib/braid/operations.rb', line 322
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.
315
316
317
318
|
# File 'lib/braid/operations.rb', line 315
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.
308
309
310
311
|
# File 'lib/braid/operations.rb', line 308
def read_tree_prefix_u(treeish, prefix)
invoke(:read_tree, "--prefix=#{prefix}/ -u", treeish)
true
end
|
#relative_working_dir ⇒ Object
Get the prefix of the current directory relative to the worktree. Empty string if it’s the root of the worktree, otherwise ends with a slash. In some cases in which the current directory is not inside a worktree at all, this will successfully return an empty string, so it may be desirable to check is_inside_worktree first.
199
200
201
|
# File 'lib/braid/operations.rb', line 199
def relative_working_dir
invoke(:rev_parse, '--show-prefix')
end
|
#remote_add(remote, path) ⇒ Object
249
250
251
252
|
# File 'lib/braid/operations.rb', line 249
def remote_add(remote, path)
invoke(:remote, 'add', remote, path)
true
end
|
#remote_rm(remote) ⇒ Object
254
255
256
257
|
# File 'lib/braid/operations.rb', line 254
def remote_rm(remote)
invoke(:remote, 'rm', remote)
true
end
|
#remote_url(remote) ⇒ Object
260
261
262
263
264
265
|
# File 'lib/braid/operations.rb', line 260
def remote_url(remote)
key = "remote.#{remote}.url"
invoke(:config, key)
rescue ShellExecutionError
nil
end
|
#repo_file_path(path) ⇒ Object
Get the physical path to a file in the git repository (e.g., ‘MERGE_MSG’), taking into account worktree configuration. The returned path may be absolute or relative to the current working directory.
176
177
178
179
180
181
182
183
|
# File 'lib/braid/operations.rb', line 176
def repo_file_path(path)
if require_version('2.5')
invoke(:rev_parse, '--git-path', path)
else
File.join(invoke(:rev_parse, '--git-dir'), path)
end
end
|
#reset_hard(target) ⇒ Object
267
268
269
270
|
# File 'lib/braid/operations.rb', line 267
def reset_hard(target)
invoke(:reset, '--hard', target)
true
end
|
#rev_parse(opt) ⇒ Object
242
243
244
245
246
|
# File 'lib/braid/operations.rb', line 242
def rev_parse(opt)
invoke(:rev_parse, opt)
rescue ShellExecutionError
raise UnknownRevision, opt
end
|
#rm_r(path) ⇒ Object
358
359
360
361
|
# File 'lib/braid/operations.rb', line 358
def rm_r(path)
invoke(:rm, '-r', path)
true
end
|
#rm_r_cached(path) ⇒ Object
364
365
366
367
|
# File 'lib/braid/operations.rb', line 364
def rm_r_cached(path)
invoke(:rm, '-r', '--cached', path)
true
end
|
#status_clean? ⇒ Boolean
387
388
389
390
|
# File 'lib/braid/operations.rb', line 387
def status_clean?
status, out, err = exec('git status')
!out.split("\n").grep(/nothing to commit/).empty?
end
|
#tree_hash(path, treeish = 'HEAD') ⇒ Object
369
370
371
372
|
# File 'lib/braid/operations.rb', line 369
def tree_hash(path, treeish = 'HEAD')
out = invoke(:ls_tree, treeish, '-d', path)
out.split[2]
end
|
#with_temporary_index ⇒ Object
Execute a block using a temporary git index file, initially empty.
333
334
335
336
337
338
339
340
|
# File 'lib/braid/operations.rb', line 333
def with_temporary_index
Dir.mktmpdir('braid_index') do |dir|
Operations::with_modified_environment(
{'GIT_INDEX_FILE' => File.join(dir, 'index')}) do
yield
end
end
end
|
#write_tree ⇒ Object
Write a tree object for the current index and return its ID.
328
329
330
|
# File 'lib/braid/operations.rb', line 328
def write_tree
invoke(:write_tree)
end
|