Note that there are some explanatory texts on larger screens.

plurals
  1. POpre-commit hook doesn't check if saved changes are staged before running
    primarykey
    data
    text
    <p>I followed <a href="http://codeinthehole.com/writing/tips-for-using-a-git-pre-commit-hook/" rel="nofollow">this guide on creating git pre-commit hooks</a>, and so far I really like the benefit it's given me.</p> <p>However, I have a problem when I use it:</p> <ol> <li>I write some code, perhaps something that doesn't pass a rubocop inspection.</li> <li>I stage it, then attempt to commit it. The pre-commit hook works as intended.</li> <li>I go and fix the issues that rubocop reported.</li> <li>I save the changes, <em>but forget to <code>add</code> it to the index</em>.</li> </ol> <p>When I commit, my script just takes the list of files changed that are in <code>git diff --cached --name-only --diff-filter=AM</code>, runs rubocop on each of them, and quits if there's any issues.</p> <p>Here's my script:</p> <pre><code>#!/bin/sh # Set the ruby environment from local/rvm, depending on your machine. # http://stackoverflow.com/questions/17515769 if [ -d "$HOME/.rvm/bin" ]; then PATH="$HOME/.rvm/bin:$PATH" [[ -s "$HOME/.rvm/scripts/rvm" ]] &amp;&amp; source "$HOME/.rvm/scripts/rvm" if [ -f ".ruby-version" ]; then rvm use "$(cat .ruby-version)" fi if [ -f ".ruby-gemset" ]; then rvm gemset use "$(cat .ruby-gemset)" fi fi # http://codeinthehole.com/writing/tips-for-using-a-git-pre-commit-hook/ FILES_PATTERN='\.rb(\..+)?$' FORBIDDEN='debug' # Quit if no ruby files are being checked in. RB_FILES=$(git df --cached --name-only --diff-filter=AM | grep -Ec $FILES_PATTERN) if [ "$RB_FILES" = "0" ]; then exit 0 fi git diff --cached --name-only --diff-filter=AM | \ grep -E $FILES_PATTERN | \ GREP_COLOR='37;41' xargs grep --color --with-filename -n $FORBIDDEN &amp;&amp; \ echo 'Please remove debugging statements before commiting.' &amp;&amp; exit 1 # Pull in altered files, check with rubocop. git diff --cached --name-only --diff-filter=AM | \ grep -E $FILES_PATTERN | xargs rubocop -f simple | \ grep 'no offences detected' &amp;&amp; exit 0 # If it didn't exit 0 above, warn of issues, output results. echo 'Rubocop has detected issues with your commit.' &amp;&amp; \ git diff --cached --name-only --diff-filter=AM | \ grep -E $FILES_PATTERN | xargs rubocop -f simple &amp;&amp; exit 1 </code></pre> <p>I don't want to use <code>sed</code> to parse through the results of <code>git diff</code>. Is there an easier way to ensure that I'm checking the <em>index</em>, opposed to the file as it appears on disk?</p> <p>My instinct tells me that there may be a way to check if there are any files that are both in <code>git diff --name-only --cached --diff-filter=M</code> <em>and</em> <code>git diff --name-only --diff-filter=M</code>, and just quit if that's the case.</p> <p>Other suggestions are welcome.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload