Commit d1b9a32f authored by Alejandro Rodríguez's avatar Alejandro Rodríguez

Add `run_git!`, which throws an exception on command failure

We also add the ability to both it and the pre-existing `run_git` to
pass blocks for stdin feeding.
parent f632fbc2
...@@ -1091,13 +1091,8 @@ module Gitlab ...@@ -1091,13 +1091,8 @@ module Gitlab
raise ArgumentError, "invalid ref_path #{ref_path.inspect}" if ref_path.include?(' ') raise ArgumentError, "invalid ref_path #{ref_path.inspect}" if ref_path.include?(' ')
raise ArgumentError, "invalid ref #{ref.inspect}" if ref.include?("\x00") raise ArgumentError, "invalid ref #{ref.inspect}" if ref.include?("\x00")
command = [Gitlab.config.git.bin_path] + %w[update-ref --stdin -z]
input = "update #{ref_path}\x00#{ref}\x00\x00" input = "update #{ref_path}\x00#{ref}\x00\x00"
output, status = circuit_breaker.perform do run_git!(%w[update-ref --stdin -z]) { |stdin| stdin.write(input) }
popen(command, path) { |stdin| stdin.write(input) }
end
raise GitError, output unless status.zero?
end end
def fetch_ref(source_repository, source_ref:, target_ref:) def fetch_ref(source_repository, source_ref:, target_ref:)
...@@ -1119,14 +1114,22 @@ module Gitlab ...@@ -1119,14 +1114,22 @@ module Gitlab
end end
# Refactoring aid; allows us to copy code from app/models/repository.rb # Refactoring aid; allows us to copy code from app/models/repository.rb
def run_git(args, env: {}, nice: false) def run_git(args, chdir: path, env: {}, nice: false, &block)
cmd = [Gitlab.config.git.bin_path, *args] cmd = [Gitlab.config.git.bin_path, *args]
cmd.unshift("nice") if nice cmd.unshift("nice") if nice
circuit_breaker.perform do circuit_breaker.perform do
popen(cmd, path, env) popen(cmd, chdir, env, &block)
end end
end end
def run_git!(args, chdir: path, env: {}, nice: false, &block)
output, status = run_git(args, chdir: chdir, env: env, nice: nice, &block)
raise GitError, output unless status.zero?
output
end
# Refactoring aid; allows us to copy code from app/models/repository.rb # Refactoring aid; allows us to copy code from app/models/repository.rb
def run_git_with_timeout(args, timeout, env: {}) def run_git_with_timeout(args, timeout, env: {})
circuit_breaker.perform do circuit_breaker.perform do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment