class Itamae::Resource::Git

Constants

DEPLOY_BRANCH

Public Instance Methods

action_sync(options) click to toggle source
# File lib/itamae/resource/git.rb, line 24
def action_sync(options)
  ensure_git_available

  new_repository = false

  if check_empty_dir
    cmd = ['git', 'clone']
    cmd << '--recursive' if attributes.recursive
    cmd += ['--depth', attributes.depth.to_s] if attributes.depth
    cmd << attributes.repository << attributes.destination
    run_command(cmd)
    new_repository = true
  end

  target = if attributes.revision
             get_revision(attributes.revision)
           else
             fetch_origin!
             run_command_in_repo("git ls-remote origin HEAD | cut -f1").stdout.strip
           end

  if new_repository || target != get_revision('HEAD')
    updated!

    deploy_old_created = false
    if current_branch == DEPLOY_BRANCH
      run_command_in_repo("git branch -m deploy-old")
      deploy_old_created = true
    end

    fetch_origin!
    run_command_in_repo(["git", "checkout", target, "-b", DEPLOY_BRANCH])

    if deploy_old_created
      run_command_in_repo("git branch -d deploy-old")
    end
  end
end
pre_action() click to toggle source
# File lib/itamae/resource/git.rb, line 13
def pre_action
  case @current_action
  when :sync
    attributes.exist = true
  end
end
set_current_attributes() click to toggle source
# File lib/itamae/resource/git.rb, line 20
def set_current_attributes
  current.exist = run_specinfra(:check_file_is_directory, attributes.destination)
end

Private Instance Methods

check_empty_dir() click to toggle source
# File lib/itamae/resource/git.rb, line 70
def check_empty_dir
  run_command("test -z \"$(ls -A #{shell_escape(attributes.destination)})\"", error: false).success?
end
current_branch() click to toggle source
# File lib/itamae/resource/git.rb, line 82
def current_branch
  run_command_in_repo("git rev-parse --abbrev-ref HEAD").stdout.strip
end
ensure_git_available() click to toggle source
# File lib/itamae/resource/git.rb, line 64
def ensure_git_available
  unless run_command("which git", error: false).exit_status == 0
    raise "`git` command is not available. Please install git."
  end
end
fetch_origin!() click to toggle source
# File lib/itamae/resource/git.rb, line 94
def fetch_origin!
  return if @origin_fetched
  @origin_fetched = true
  run_command_in_repo(['git', 'fetch', 'origin'])
end
get_revision(branch) click to toggle source
# File lib/itamae/resource/git.rb, line 86
def get_revision(branch)
  result = run_command_in_repo("git rev-parse #{shell_escape(branch)}", error: false)
  return result.stdout.strip if result.exit_status == 0

  fetch_origin!
  run_command_in_repo("git rev-parse #{shell_escape(branch)}").stdout.strip
end
run_command_in_repo(*args) click to toggle source
# File lib/itamae/resource/git.rb, line 74
def run_command_in_repo(*args)
  unless args.last.is_a?(Hash)
    args << {}
  end
  args.last[:cwd] = attributes.destination
  run_command(*args)
end