Class: Takeoff::Stage::StashChanges

Inherits:
Base
  • Object
show all
Defined in:
lib/takeoff/stage/stash_changes.rb

Instance Method Summary collapse

Methods inherited from Base

#initialize

Methods included from Helpers

#branches_up_to_date?, #diff, #execute, #file_has_changed_locally?, #files_have_changed?, #latest_commit, #log

Constructor Details

This class inherits a constructor from Takeoff::Stage::Base

Instance Method Details

#call(env) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 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