Class: Overcommit::HookContext::PreCommit

Inherits:
Base
  • Object
show all
Defined in:
lib/overcommit/hook_context/pre_commit.rb

Overview

Contains helpers related to contextual information used by pre-commit hooks.

This includes staged files, which lines of those files have been modified, etc. It is also responsible for saving/restoring the state of the repo so hooks only inspect staged changes.

Instance Method Summary collapse

Methods inherited from Base

#execute_hook, #hook_class_name, #hook_script_name, #hook_type_name, #initialize, #input_lines, #input_string

Constructor Details

This class inherits a constructor from Overcommit::HookContext::Base

Instance Method Details

#amendment?Boolean

Returns whether this hook run was triggered by ‘git commit –amend`

Returns:

  • (Boolean)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/overcommit/hook_context/pre_commit.rb', line 12

def amendment?
  return @amendment unless @amendment.nil?

  cmd = Overcommit::Utils.parent_command
  amend_pattern = 'commit(\s.*)?\s--amend(\s|$)'

  return @amendment if
    # True if the command is a commit with the --amend flag
    @amendment = !(/\s#{amend_pattern}/ =~ cmd).nil?

  # Check for git aliases that call `commit --amend`
  `git config --get-regexp "^alias\\." "#{amend_pattern}"`.
    scan(/alias\.([-\w]+)/). # Extract the alias
    each do |match|
      return @amendment if
        # True if the command uses a git alias for `commit --amend`
        @amendment = !(/git(\.exe)?\s+#{match[0]}/ =~ cmd).nil?
    end

  @amendment
end

#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.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/overcommit/hook_context/pre_commit.rb', line 71

def cleanup_environment
  unless initial_commit? || (@stash_attempted && !@changes_stashed)
    clear_working_tree # Ensure working tree is clean before restoring it
    restore_modified_times
  end

  if @changes_stashed
    restore_working_tree
    restore_modified_times
  end

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

#modified_filesObject

Get a list of added, copied, or modified files that have been staged. Renames and deletions are ignored, since there should be nothing to check.



89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/overcommit/hook_context/pre_commit.rb', line 89

def modified_files
  unless @modified_files
    currently_staged = Overcommit::GitRepo.modified_files(staged: true)
    @modified_files = currently_staged

    # Include files modified in last commit if amending
    if amendment?
      subcmd = 'show --format=%n'
      previously_modified = Overcommit::GitRepo.modified_files(subcmd: subcmd)
      @modified_files |= filter_modified_files(previously_modified)
    end
  end
  @modified_files
end

#modified_lines_in_file(file) ⇒ Object

Returns the set of line numbers corresponding to the lines that were changed in a specified file.



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/overcommit/hook_context/pre_commit.rb', line 106

def modified_lines_in_file(file)
  @modified_lines ||= {}
  unless @modified_lines[file]
    @modified_lines[file] =
      Overcommit::GitRepo.extract_modified_lines(file, staged: true)

    # Include lines modified in last commit if amending
    if amendment?
      subcmd = 'show --format=%n'
      @modified_lines[file] +=
        Overcommit::GitRepo.extract_modified_lines(file, subcmd: subcmd)
    end
  end
  @modified_lines[file]
end

#setup_environmentObject

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



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/overcommit/hook_context/pre_commit.rb', line 36

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

  if !initial_commit? && any_changes?
    @stash_attempted = true

    stash_message = "Overcommit: Stash of repo state before hook run at #{Time.now}"
    result = Overcommit::Utils.execute(
      %w[git stash save --keep-index --quiet] + [stash_message]
    )

    unless result.success?
      # Failure to stash in this case is likely due to a configuration
      # issue (e.g. author/email not set or GPG signing key incorrect)
      raise Overcommit::Exceptions::HookSetupFailed,
            "Unable to setup environment for #{hook_script_name} hook run:" \
            "\nSTDOUT:#{result.stdout}\nSTDERR:#{result.stderr}"
    end

    @changes_stashed = `git stash list -1`.include?(stash_message)
  end

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