Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Minify CSS files via git hook

Tags:

git

githooks

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.

like image 245
staackuser2 Avatar asked Mar 17 '11 03:03

staackuser2


2 Answers

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.

like image 177
YenTheFirst Avatar answered Nov 02 '22 22:11

YenTheFirst


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.

like image 24
philfreo Avatar answered Nov 02 '22 21:11

philfreo