Module: Overcommit::HookContext::Helpers::StashUnstagedChanges

Included in:
CommitMsg, PreCommit
Defined in:
lib/overcommit/hook_context/helpers/stash_unstaged_changes.rb

Overview

This module contains behavior for stashing unstaged changes before hooks are ran and restoring them afterwards

Instance Method Summary collapse

Instance Method Details

#cleanup_environmentObject

Restore unstaged changes and reset file modification times so it appears as if nothing ever changed.

We want to restore the modification times for each of the files after every step to ensure as little time as possible has passed while the modification time on the file was newer. This helps us play more nicely with file watchers.



40
41
42
43
44
45
46
47
48
49
# File 'lib/overcommit/hook_context/helpers/stash_unstaged_changes.rb', line 40

def cleanup_environment
  if @changes_stashed
    clear_working_tree
    restore_working_tree
    restore_modified_times
  end

  Overcommit::GitRepo.restore_merge_state
  Overcommit::GitRepo.restore_cherry_pick_state
end

#initial_commit?Boolean

Returns whether the current git branch is empty (has no commits).

Returns:

  • (Boolean)


27
28
29
30
31
# File 'lib/overcommit/hook_context/helpers/stash_unstaged_changes.rb', line 27

def initial_commit?
  return @initial_commit unless @initial_commit.nil?

  @initial_commit = Overcommit::GitRepo.initial_commit?
end

#setup_environmentObject

Stash unstaged contents of files so hooks don’t see changes that aren’t about to be committed.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/overcommit/hook_context/helpers/stash_unstaged_changes.rb', line 10

def setup_environment
  store_modified_times
  Overcommit::GitRepo.store_merge_state
  Overcommit::GitRepo.store_cherry_pick_state

  # Don't attempt to stash changes if all changes are staged, as this
  # prevents us from modifying files at all, which plays better with
  # editors/tools which watch for file changes.
  if !initial_commit? && unstaged_changes?
    stash_changes

    # While running hooks make it appear as if nothing changed
    restore_modified_times
  end
end