Sha256: c96c403adae55bea4a5af74d2bdd6512ae734d1a5bcda8bd5b497d36fba91471
Contents?: true
Size: 1.92 KB
Versions: 12
Compression:
Stored size: 1.92 KB
Contents
require 'net/ssh' class Indocker::Repositories::Cloner def initialize(configuration, logger) @configuration = configuration @logger = logger end def clone(session, repository) raise ArgumenError.new("only git repositories should be cloned") if !repository.is_git? already_cloned = repository_already_cloned?( session: session, target_path: repository.clone_path, remote_url: repository.remote_url, ) git_command = if already_cloned build_force_pull_command( target_path: repository.clone_path, branch_name: repository.branch, ) else build_clone_command( target_path: repository.clone_path, branch_name: repository.branch, remote_url: repository.remote_url, ) end session.exec!("ssh-agent bash -c 'ssh-add ~/.ssh/#{repository.ssh_key}; #{git_command}'") end private def repository_already_cloned?(session:, target_path:, remote_url:) target_remote_url = session.exec!( build_git_remote_url_command( path: target_path ) ).stdout_data.chomp @logger.debug("target remote_url: #{target_remote_url.inspect}, checked remote_url: #{remote_url.inspect}") target_remote_url == remote_url end def build_git_remote_url_command(path:) [ "mkdir -p #{path}", "cd #{path}", "git config --get remote.origin.url", ].join(" && ") end def build_clone_command(target_path:, branch_name:, remote_url:) [ "rm -rf #{target_path}", "mkdir -p #{target_path}", "git clone -b #{branch_name} --depth 1 #{remote_url} #{target_path}", ].join(" && ") end def build_force_pull_command(target_path:, branch_name:) [ "cd #{target_path}", "git add .", "git reset HEAD --hard", "git checkout #{branch_name}", "git pull --force", ].join(" && ") end end
Version data entries
12 entries across 12 versions & 1 rubygems