|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module Overcommit::HookContext |
| 4 | + module Helpers |
| 5 | + # This module contains behavior for stashing unstaged changes before hooks are ran and restoring |
| 6 | + # them afterwards |
| 7 | + module StashUnstagedChanges |
| 8 | + # Stash unstaged contents of files so hooks don't see changes that aren't |
| 9 | + # about to be committed. |
| 10 | + def setup_environment |
| 11 | + store_modified_times |
| 12 | + Overcommit::GitRepo.store_merge_state |
| 13 | + Overcommit::GitRepo.store_cherry_pick_state |
| 14 | + |
| 15 | + # Don't attempt to stash changes if all changes are staged, as this |
| 16 | + # prevents us from modifying files at all, which plays better with |
| 17 | + # editors/tools which watch for file changes. |
| 18 | + if !initial_commit? && unstaged_changes? |
| 19 | + stash_changes |
| 20 | + |
| 21 | + # While running hooks make it appear as if nothing changed |
| 22 | + restore_modified_times |
| 23 | + end |
| 24 | + end |
| 25 | + |
| 26 | + # Restore unstaged changes and reset file modification times so it appears |
| 27 | + # as if nothing ever changed. |
| 28 | + # |
| 29 | + # We want to restore the modification times for each of the files after |
| 30 | + # every step to ensure as little time as possible has passed while the |
| 31 | + # modification time on the file was newer. This helps us play more nicely |
| 32 | + # with file watchers. |
| 33 | + def cleanup_environment |
| 34 | + if @changes_stashed |
| 35 | + clear_working_tree |
| 36 | + restore_working_tree |
| 37 | + restore_modified_times |
| 38 | + end |
| 39 | + |
| 40 | + Overcommit::GitRepo.restore_merge_state |
| 41 | + Overcommit::GitRepo.restore_cherry_pick_state |
| 42 | + end |
| 43 | + |
| 44 | + private |
| 45 | + |
| 46 | + # Stores the modification times for all modified files to make it appear like |
| 47 | + # they never changed. |
| 48 | + # |
| 49 | + # This prevents (some) editors from complaining about files changing when we |
| 50 | + # stash changes before running the hooks. |
| 51 | + def store_modified_times |
| 52 | + @modified_times = {} |
| 53 | + |
| 54 | + staged_files = modified_files |
| 55 | + unstaged_files = Overcommit::GitRepo.modified_files(staged: false) |
| 56 | + |
| 57 | + (staged_files + unstaged_files).each do |file| |
| 58 | + next if Overcommit::Utils.broken_symlink?(file) |
| 59 | + next unless File.exist?(file) # Ignore renamed files (old file no longer exists) |
| 60 | + @modified_times[file] = File.mtime(file) |
| 61 | + end |
| 62 | + end |
| 63 | + |
| 64 | + # Returns whether the current git branch is empty (has no commits). |
| 65 | + def initial_commit? |
| 66 | + return @initial_commit unless @initial_commit.nil? |
| 67 | + @initial_commit = Overcommit::GitRepo.initial_commit? |
| 68 | + end |
| 69 | + |
| 70 | + # Returns whether there are any changes to tracked files which have not yet |
| 71 | + # been staged. |
| 72 | + def unstaged_changes? |
| 73 | + result = Overcommit::Utils.execute(%w[git --no-pager diff --quiet]) |
| 74 | + !result.success? |
| 75 | + end |
| 76 | + |
| 77 | + def stash_changes |
| 78 | + @stash_attempted = true |
| 79 | + |
| 80 | + stash_message = "Overcommit: Stash of repo state before hook run at #{Time.now}" |
| 81 | + result = Overcommit::Utils.with_environment('GIT_LITERAL_PATHSPECS' => '0') do |
| 82 | + Overcommit::Utils.execute( |
| 83 | + %w[git -c commit.gpgsign=false stash save --keep-index --quiet] + [stash_message] |
| 84 | + ) |
| 85 | + end |
| 86 | + |
| 87 | + unless result.success? |
| 88 | + # Failure to stash in this case is likely due to a configuration |
| 89 | + # issue (e.g. author/email not set or GPG signing key incorrect) |
| 90 | + raise Overcommit::Exceptions::HookSetupFailed, |
| 91 | + "Unable to setup environment for #{hook_script_name} hook run:" \ |
| 92 | + "\nSTDOUT:#{result.stdout}\nSTDERR:#{result.stderr}" |
| 93 | + end |
| 94 | + |
| 95 | + @changes_stashed = `git stash list -1`.include?(stash_message) |
| 96 | + end |
| 97 | + |
| 98 | + # Restores the file modification times for all modified files to make it |
| 99 | + # appear like they never changed. |
| 100 | + def restore_modified_times |
| 101 | + @modified_times.each do |file, time| |
| 102 | + next if Overcommit::Utils.broken_symlink?(file) |
| 103 | + next unless File.exist?(file) |
| 104 | + File.utime(time, time, file) |
| 105 | + end |
| 106 | + end |
| 107 | + |
| 108 | + # Clears the working tree so that the stash can be applied. |
| 109 | + def clear_working_tree |
| 110 | + removed_submodules = Overcommit::GitRepo.staged_submodule_removals |
| 111 | + |
| 112 | + result = Overcommit::Utils.execute(%w[git reset --hard]) |
| 113 | + unless result.success? |
| 114 | + raise Overcommit::Exceptions::HookCleanupFailed, |
| 115 | + "Unable to cleanup working tree after #{hook_script_name} hooks run:" \ |
| 116 | + "\nSTDOUT:#{result.stdout}\nSTDERR:#{result.stderr}" |
| 117 | + end |
| 118 | + |
| 119 | + # Hard-resetting a staged submodule removal results in the index being |
| 120 | + # reset but the submodule being restored as an empty directory. This empty |
| 121 | + # directory prevents us from stashing on a subsequent run if a hook fails. |
| 122 | + # |
| 123 | + # Work around this by removing these empty submodule directories as there |
| 124 | + # doesn't appear any reason to keep them around. |
| 125 | + removed_submodules.each do |submodule| |
| 126 | + FileUtils.rmdir(submodule.path) |
| 127 | + end |
| 128 | + end |
| 129 | + |
| 130 | + # Applies the stash to the working tree to restore the user's state. |
| 131 | + def restore_working_tree |
| 132 | + result = Overcommit::Utils.execute(%w[git stash pop --index --quiet]) |
| 133 | + unless result.success? |
| 134 | + raise Overcommit::Exceptions::HookCleanupFailed, |
| 135 | + "Unable to restore working tree after #{hook_script_name} hooks run:" \ |
| 136 | + "\nSTDOUT:#{result.stdout}\nSTDERR:#{result.stderr}" |
| 137 | + end |
| 138 | + end |
| 139 | + end |
| 140 | + end |
| 141 | +end |
0 commit comments