66require 'shellwords'
77require 'tempfile'
88
9+ require_relative '../errors'
10+
911module OctocatalogDiff
1012 module CatalogUtil
1113 # Class to perform a git checkout (via 'git archive') of a branch from the base git
1214 # directory into another targeted directory.
1315 class Git
14- # Trapped errors
15- class GitCheckoutError < RuntimeError
16- end
17-
1816 # Check out a branch via 'git archive' from one directory into another.
1917 # @param options [Hash] Options hash:
2018 # - :branch => Branch name to check out
@@ -28,8 +26,12 @@ def self.check_out_git_archive(options = {})
2826 logger = options . fetch ( :logger )
2927
3028 # Validate parameters
31- raise GitCheckoutError , "Source directory #{ dir . inspect } does not exist" if dir . nil? || !File . directory? ( dir )
32- raise GitCheckoutError , "Target directory #{ path . inspect } does not exist" if dir . nil? || !File . directory? ( path )
29+ if dir . nil? || !File . directory? ( dir )
30+ raise OctocatalogDiff ::Errors ::GitCheckoutError , "Source directory #{ dir . inspect } does not exist"
31+ end
32+ if dir . nil? || !File . directory? ( path )
33+ raise OctocatalogDiff ::Errors ::GitCheckoutError , "Target directory #{ path . inspect } does not exist"
34+ end
3335
3436 # To get the options working correctly (-o pipefail in particular) this needs to run under
3537 # bash. It's just creating a script, rather than figuring out all the shell escapes...
@@ -44,7 +46,9 @@ def self.check_out_git_archive(options = {})
4446
4547 logger . debug ( "Begin git archive #{ dir } :#{ branch } -> #{ path } " )
4648 output , status = Open3 . capture2e ( tmp_script . path , chdir : dir )
47- raise GitCheckoutError , "Git archive #{ branch } ->#{ path } failed: #{ output } " unless status . exitstatus . zero?
49+ unless status . exitstatus . zero?
50+ raise OctocatalogDiff ::Errors ::GitCheckoutError , "Git archive #{ branch } ->#{ path } failed: #{ output } "
51+ end
4852 logger . debug ( "Success git archive #{ dir } :#{ branch } " )
4953 ensure
5054 FileUtils . rm_f tmp_script . path if File . exist? ( tmp_script . path )
@@ -58,7 +62,9 @@ def self.check_out_git_archive(options = {})
5862 def self . branch_sha ( options = { } )
5963 branch = options . fetch ( :branch )
6064 dir = options . fetch ( :basedir )
61- raise GitCheckoutError , "Git directory #{ dir . inspect } does not exist" if dir . nil? || !File . directory? ( dir )
65+ if dir . nil? || !File . directory? ( dir )
66+ raise OctocatalogDiff ::Errors ::GitCheckoutError , "Git directory #{ dir . inspect } does not exist"
67+ end
6268 repo = Rugged ::Repository . new ( dir )
6369 repo . branches [ branch ] . target_id
6470 end
0 commit comments