Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mention pre-commit hook in readme #2156

Closed
matkoniecz opened this issue Jun 8, 2016 · 5 comments
Closed

mention pre-commit hook in readme #2156

matkoniecz opened this issue Jun 8, 2016 · 5 comments

Comments

@matkoniecz
Copy link
Contributor

matkoniecz commented Jun 8, 2016

I just discovered that it is possible to autoregenerate and autocommit .mml file on every commit including change of .yaml.

Solution is quite simple - create .git/hooks/pre-commit file with following content.

# Redirect output to stderr.
exec 1>&2

#Generate .mml file from staged version - hide unstaged changes
git stash -q --keep-index

#Run .mml generation script
./scripts/yaml2mml.py < project.yaml > project.mml 

#Commit project.mml file
git add project.mml

#Restore unstaged changes
git stash pop -q

Before every commit it runs ./scripts/yaml2mml.py script and commits result what should ensure correct content in .mml file without manual regeneration.

If there are no unforeseen consequences here it may be a good idea to mention it in contributing file.

Unfortunately there is no clean method to make this file part of repo... There is http://pre-commit.com/#install that requires anyway installation. Maybe add script that would create this file?

@matkoniecz
Copy link
Contributor Author

Unfortunately it is not so simple... To keep amending possible change described in http://codeinthehole.com/writing/tips-for-using-a-Git-pre-commit-hook/#comment-645959940 was necessary. To handle also Windows it should be written in something more portable.

#!/bin/bash 

function regenerate_mml {
    #Run .mml generation script
    ./scripts/yaml2mml.py < project.yaml > project.mml;
    #Commit project.mml file
    git add project.mml;
}

# Redirect output to stderr.
exec 1>&2

if git diff-index --quiet HEAD --; 
then
    # no changes between index and working copy; just run tests
    regenerate_mml
else
    # Test the version that's about to be committed,
    # stashing all unindexed changes
    git stash -q --keep-index

    regenerate_mml

    git stash pop -q
fi

@pnorman
Copy link
Collaborator

pnorman commented Jun 8, 2016

Does this work with git add -p? That's what I've not been sure of in the past when I've looked at this

@matkoniecz
Copy link
Contributor Author

Yes, it works. On git add -p nothing special happens as pre-commit hook is triggered only before commit.

On commit "stashing all unindexed changes" branch is used - so unindexed changes (not selected in git add -p) are stashed, regenerate_mml runs and after that changes are unstashed.

Now normal commit happens, with only staged changes commited.

The only potential problem is with unstashed changes not applying cleanly, but for now I am unable to answer whatever it is purely theoretical.

@matkoniecz
Copy link
Contributor Author

And thanks for git add -p, I was using git add -i and jumping through an additional menu.

@kocio-pl kocio-pl added the code label Nov 15, 2016
@kocio-pl kocio-pl added this to the Bugs and improvements milestone Nov 15, 2016
@kocio-pl
Copy link
Collaborator

Made obsolete by #2473.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants