Skip to content

Commit f2c6492

Browse files
author
Kevin Paulisse
committed
Change git extract code to use scriptrunner
1 parent 70076c7 commit f2c6492

3 files changed

Lines changed: 43 additions & 26 deletions

File tree

lib/octocatalog-diff/catalog-util/git.rb

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
require 'open3'
55
require 'rugged'
66
require 'shellwords'
7-
require 'tempfile'
87

98
require_relative '../errors'
9+
require_relative '../util/scriptrunner'
1010

1111
module OctocatalogDiff
1212
module CatalogUtil
@@ -34,31 +34,23 @@ def self.check_out_git_archive(options = {})
3434
end
3535

3636
# Create and execute checkout script
37-
script = create_git_checkout_script(branch, path)
38-
logger.debug("Begin git archive #{dir}:#{branch} -> #{path}")
39-
output, status = Open3.capture2e(script, chdir: dir)
40-
unless status.exitstatus.zero?
41-
raise OctocatalogDiff::Errors::GitCheckoutError, "Git archive #{branch}->#{path} failed: #{output}"
42-
end
43-
logger.debug("Success git archive #{dir}:#{branch}")
44-
end
37+
sr_opts = {
38+
logger: logger,
39+
default_script: 'git-extract/git-extract.sh'
40+
}
41+
script = OctocatalogDiff::Util::ScriptRunner.new(sr_opts)
4542

46-
# Create the temporary file used to interact with the git command line.
47-
# To get the options working correctly (-o pipefail in particular) this needs to run under
48-
# bash. It's just creating a script, rather than figuring out all the shell escapes...
49-
# @param branch [String] Branch name
50-
# @param path [String] Target directory
51-
# @return [String] Name of script
52-
def self.create_git_checkout_script(branch, path)
53-
tmp_script = Tempfile.new(['git-checkout', '.sh'])
54-
tmp_script.write "#!/bin/bash\n"
55-
tmp_script.write "set -euf -o pipefail\n"
56-
tmp_script.write "git archive --format=tar #{Shellwords.escape(branch)} | \\\n"
57-
tmp_script.write " ( cd #{Shellwords.escape(path)} && tar -xf - )\n"
58-
tmp_script.close
59-
FileUtils.chmod 0o755, tmp_script.path
60-
at_exit { FileUtils.rm_f tmp_script.path if File.exist?(tmp_script.path) }
61-
tmp_script.path
43+
sr_run_opts = {
44+
:working_dir => dir,
45+
'OCD_GIT_EXTRACT_BRANCH' => branch,
46+
'OCD_GIT_EXTRACT_TARGET' => path
47+
}
48+
begin
49+
script.run(sr_run_opts)
50+
logger.debug("Success git archive #{dir}:#{branch}")
51+
rescue OctocatalogDiff::Util::ScriptRunner::ScriptException
52+
raise OctocatalogDiff::Errors::GitCheckoutError, "Git archive #{branch}->#{path} failed: #{script.output}"
53+
end
6254
end
6355

6456
# Determine the SHA of origin/master (or any other branch really) in the git repo

lib/octocatalog-diff/util/scriptrunner.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,19 @@ def run(opts = {})
5959
@stderr.split(/\n/).select { |line| line =~ /\S/ }.each { |line| @logger.debug "STDERR: #{line}" }
6060
@logger.debug "Exit status: #{@exitcode}"
6161
return @stdout if @exitcode.zero?
62-
raise ScriptException, [@stdout.split(/\n/), @stderr.split(/\n/)].compact.join("\n")
62+
raise ScriptException, output
63+
end
64+
65+
# All output from the latest execution of the command.
66+
# @return [String] Combined output of STDOUT and STDERR
67+
def output
68+
return if @exitcode.nil?
69+
[
70+
'STDOUT:',
71+
@stdout.split(/\n/).map { |line| " #{line}" },
72+
'STDERR:',
73+
@stderr.split(/\n/).map { |line| " #{line}" }
74+
].flatten.compact.join("\n")
6375
end
6476

6577
private

scripts/git-extract/git-extract.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,16 @@
22

33
# This script is called from lib/octocatalog-diff/catalog-util/git.rb and is used to
44
# archive and extract a certain branch of a git repository into a target directory.
5+
6+
if [ -z "$OCD_GIT_EXTRACT_BRANCH" ]; then
7+
echo "Error: Must declare OCD_GIT_EXTRACT_BRANCH"
8+
exit 255
9+
fi
10+
11+
if [ -z "$OCD_GIT_EXTRACT_TARGET" ]; then
12+
echo "Error: Must declare OCD_GIT_EXTRACT_TARGET"
13+
exit 255
14+
fi
15+
16+
set -euf -o pipefail
17+
git archive --format=tar "$OCD_GIT_EXTRACT_BRANCH" | ( cd "$OCD_GIT_EXTRACT_TARGET" && tar -xf - )

0 commit comments

Comments
 (0)