class Takeoff::Stage::StashChanges

Public Instance Methods

call(env) click to toggle source
# File lib/takeoff/stage/stash_changes.rb, line 6
def call(env)
  status = `git status --untracked-files --short`
  return @app.call(env) if status.blank?

  stash_name = "Takeoff Auto-Stash: #{Time.now}"

  log     "Stashing uncommitted changes"
  execute "git stash save -u #{Shellwords.escape(stash_name)}"

  begin
    @app.call(env)
  ensure
    log "Applying previously stashed uncommitted changes"

    stashes       = `git stash list`
    matched_stash = stashes.split("\n").find { |stash| stash.include?(stash_name) }
    label         = matched_stash.match(/^([^:]+)/)

    execute "git clean -fd"
    execute "git stash apply #{label}"
    execute "git stash drop #{label}"
  end
end