Skip to content

Stash changes before running pre-commit #12

Closed

Description

@finnhodgkin

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.

Activity

  1. DenisGorbachev commented on Jun 9, 2018

    @DenisGorbachev

    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
    
  2. jdav-dev commented on Jul 18, 2018

    @jdav-dev

    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)
    
  3. ZooeyMiller commented on Jul 19, 2018

    @ZooeyMiller
    Collaborator

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

  4. jdav-dev commented on Jul 19, 2018

    @jdav-dev

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

  5. iteles commented on Jul 20, 2018

    @iteles
    Member

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

  6. secondspass commented on Jul 26, 2018

    @secondspass

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

  7. ZooeyMiller commented on Jul 27, 2018

    @ZooeyMiller
    Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions