11

My ideal situation is to automatically minify CSS files and add them to the git commit. I'm not sure if #4 below can be done, but I would like the following flow to be performed:

  1. Modify CSS file
  2. Add to staging area
  3. Commit
  4. Run script that updates the minified files and adds them to the commit
  5. Commit completes

If there is another way, I'd be interested in that as well.

6
  • 5
    I don't think this is possible, as git hooks are not meant to modify a commit. Further, I believe that the process of minification should be part of deployment, and minified files should never be added to the repository. Commented Mar 17, 2011 at 3:57
  • 2
    +1 for not adding minified files to repository. Commented Mar 17, 2011 at 4:00
  • @Andrew Marshall - care to explain the reasoning behind not having the files in git? My reasoning for having them is to make sure they get minified when they are changed, so we don't have to worry about it in the deploy process. Which makes the deploy process simpler, no need to worry about having the tools in that environment, when you are deploying. Commented Mar 17, 2011 at 4:52
  • 2
    @emostar: The rule of thumb is not to track things which can be generated from what is tracked. Obviously it's not necessary, and can increase repo size, but even if that's not a factor, it provides a potential way for things to get out of sync. What if the hook doesn't run properly one time, and you don't notice? What if one dev uses it and another doesn't? And are you prepared to deal with merge conflicts in the minified files too? Besides, you're making your deploy process (maybe) simpler at the expense of your development process. Commented Mar 17, 2011 at 5:08
  • @emostar I agree with Jefromi and want to clarify that in order for such a git hook to work, every developer will have to make sure to manually add the git hook to every machine they ever work on. How is that simpler than minifying in deployment? Further, does minifying your CSS really take that long that you can't afford to do it on every deploy even if the files haven't changed? You could always add logic to your deploy script to compare timestamps if you feel necessary. Finally, you may be saving time in deployment, but every single commit will require minifying, slowing your commit time. Commented Mar 17, 2011 at 6:46

3 Answers 3

9

Whether you should is another matter, but you can.

in .git/hooks/, write a script in your language of choice (make sure it's executable) named pre-commit in that script, run your minifier command, and do 'git add '

here's an example of someone who minifies javascript this way: https://gist.github.com/786460

a test hook I wrote:

#/bin/sh

tr "aeiou" "AEIOU" < test1.css > test1_diff.css
git add test1_diff.css

after running the commit, test1_diff.css was in the working directory, and in git, tracked.

Sign up to request clarification or add additional context in comments.

Comments

2

You would use a "pre-commit hook" which gets called before/as your actual commit is made. Google it -- it basically just involved putting a pre-commit script file in your .git folder.

2 Comments

Do you think I didn't read the manpage, search Google, and search SO beforehand? I am asking for what kind of pre-commit hook I can use to get what I want done.
I don't think the downvote or comment is warranted. You did not once use the term pre-commit hook in your question so I was giving you exactly the right term that would help you move in the right direction.
2

Write a smudge/clean script and mark your css files with the filter attribute. The trick is to do the work on a branch that does not have the attribute and deploy from the one that does. This is easy to set up if you back merge from the deploy branch initially with an ours merge strategy. This ensures that subsequent merges don't propagate the attribute.

That should do what you want.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.