dwyl / elixir-pre-commit

✅ Pre-commit hooks for Elixir projects
GNU General Public License v2.0
46 stars 10 forks source link

Stash changes before running pre-commit #12

Closed finnhodgkin closed 6 years ago

finnhodgkin commented 6 years ago

It's possible to sneak broken code through the pre-commit hook by making changes that are saved but not staged with git. Ideally the shell script should stash changes before running pre-commit and then pop the changes back after.

DenisGorbachev commented 6 years ago

Doesn't work with git version 1.9.1: stash command doesn't have push subcommand.

starfall@nx:~/workspace/leverex$ git commit
Pre-commit running...
usage: git stash list [<options>]
   or: git stash show [<stash>]
   or: git stash drop [-q|--quiet] [<stash>]
   or: git stash ( pop | apply ) [--index] [-q|--quiet] [<stash>]
   or: git stash branch <branchname> [<stash>]
   or: git stash [save [--patch] [-k|--[no-]keep-index] [-q|--quiet]
               [-u|--include-untracked] [-a|--all] [<message>]]
   or: git stash clear
** (MatchError) no match of right hand side value: {"", 1}
    lib/mix/tasks/pre_commit.ex:18: Mix.Tasks.PreCommit.run/1
    (mix) lib/mix/task.ex:314: Mix.Task.run_task/3
    (mix) lib/mix/cli.ex:80: Mix.CLI.run_task/2
    (elixir) lib/code.ex:677: Code.require_file/2
starfall@nx:~/workspace/leverex$ git --version
git version 1.9.1
jdav-dev commented 6 years ago

Using git stash push --keep-index as in #30 restores deleted files to the working tree:

$ git init
Initialized empty Git repository in /tmp/git-stash-test/.git/
$ touch test
$ git add -A
$ git commit -m 'initial commit'
[master (root-commit) 5e0577e] initial commit
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 test
$ rm test
$ git add -A
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        deleted:    test

$ git stash push --keep-index --message pre_commit
Saved working directory and index state On master: pre_commit
$ git stash pop
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        deleted:    test

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        test

Dropped refs/stash@{0} (b6658734f4ae35a43bcff3f43b306ea6ae238902)
ZooeyMiller commented 6 years ago

Can we close this issue now that #30 is merged? @finnhodgkin

jdav-dev commented 6 years ago

I've opened #32 with my comment so it can be tracked on its own.

iteles commented 6 years ago

@ZooeyMiller I'm with @finnhodgkin now and will nudge 😊

secondspass commented 6 years ago

Hey, just wanted to let you know that git stash push does not exist in git versions older than 2.13. git stash save exists which covers the same ground (but it doesn't have the --message flag).

So if you want it to be backwards compatible, you might want to take another look at it. (Or if you're intending this only for newer versions, disregard this comment).

ZooeyMiller commented 6 years ago

@secondspass Thanks for letting us know! I'll add required git version to the docs as well.