lib/braid/operations.rb in braid-1.1.7 vs lib/braid/operations.rb in braid-1.1.8

- old
+ new

@@ -1,5 +1,7 @@ +# typed: true + require 'singleton' require 'rubygems' require 'tempfile' module Braid @@ -54,11 +56,11 @@ # The command proxy is meant to encapsulate commands such as git, that work with subcommands. class Proxy include Singleton def self.command; - name.split('::').last.downcase; + T.unsafe(name).split('::').last.downcase; end # hax! def version _, out, _ = exec!("#{self.class.command} --version") @@ -87,10 +89,13 @@ def invoke(arg, *args) exec!("#{command(arg)} #{args.join(' ')}".strip)[1].strip # return stdout end def method_missing(name, *args) + # We have to use this rather than `T.unsafe` because `invoke` is + # private. See https://sorbet.org/docs/type-assertions#tbind. + T.bind(self, T.untyped) invoke(name, *args) end def exec(cmd) cmd.strip! @@ -136,16 +141,11 @@ class Git < Proxy # 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. def repo_file_path(path) - if require_version('2.5') # support for --git-path - invoke(:rev_parse, '--git-path', path) - else - # Git < 2.5 doesn't support linked worktrees anyway. - File.join(invoke(:rev_parse, '--git-dir'), path) - end + invoke(:rev_parse, '--git-path', path) end # 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 @@ -271,11 +271,11 @@ # how we remember the mode for single-file mirrors.) def get_tree_item(tree, path) if path.nil? || path == '' tree else - m = /^([^ ]*) ([^ ]*) ([^\t]*)\t.*$/.match(invoke(:ls_tree, tree, path)) + m = T.must(/^([^ ]*) ([^ ]*) ([^\t]*)\t.*$/.match(invoke(:ls_tree, tree, path))) mode = m[1] type = m[2] hash = m[3] if type == 'tree' hash @@ -291,13 +291,11 @@ # path. If update_worktree is true, then update the worktree, otherwise # disregard the state of the worktree (most useful with a temporary index # file). def add_item_to_index(item, path, update_worktree) if item.is_a?(BlobWithMode) - # Our minimum git version is 1.6.0 and the new --cacheinfo syntax - # wasn't added until 2.0.0. - invoke(:update_index, '--add', '--cacheinfo', item.mode, item.hash, path) + invoke(:update_index, '--add', '--cacheinfo', "#{item.mode},#{item.hash},#{path}") if update_worktree # XXX If this fails, we've already updated the index. invoke(:checkout_index, path) end else @@ -388,10 +386,11 @@ out[2..-1] end def clone(*args) # overrides builtin + T.bind(self, T.untyped) # Ditto the comment in `method_missing`. invoke(:clone, *args) end private @@ -405,14 +404,14 @@ def fetch(url) dir = path(url) # remove local cache if it was created with --no-checkout - if File.exists?("#{dir}/.git") + if File.exist?("#{dir}/.git") FileUtils.rm_r(dir) end - if File.exists?(dir) + if File.exist?(dir) Dir.chdir(dir) do git.fetch end else FileUtils.mkdir_p(local_cache_dir)