Method: Git::Base.root_of_worktree

Defined in:
lib/git/base.rb

.root_of_worktree(working_dir)

Raises:

  • (ArgumentError)
[View source]

88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/git/base.rb', line 88

def self.root_of_worktree(working_dir)
  result = working_dir
  status = nil

  raise ArgumentError, "'#{working_dir}' does not exist" unless Dir.exist?(working_dir)

  begin
    result, status = Open3.capture2e(Git::Base.config.binary_path, "-c", "core.quotePath=true", "-c", "color.ui=false", "rev-parse", "--show-toplevel", chdir: File.expand_path(working_dir))
    result = result.chomp
  rescue Errno::ENOENT
    raise ArgumentError, "Failed to find the root of the worktree: git binary not found"
  end

  raise ArgumentError, "'#{working_dir}' is not in a git working tree" unless status.success?
  result
end