How to automatically call a script before adding git?

Here is my use case: I take PNG and other things in my Git repository. I would like to apply a specific process for every PNG that I would like to commit, and this is the result of the process that I finally want to complete (a potentially modified PNG).

In the beginning, I thought of a hook (like pre-commit ), but this is a bit strange because the process will change the file, so I will need to add it again! And according to what I read , there is no pre-add hook (or something like that).

Maybe the solution is to create a Git alias? But I do not want to change - too much - how people work, I am looking for a smooth and transparent way.

If you have a key ... even if the key should change my idea of ​​the process.

+8
git gitattributes hook
source share
3 answers

You might want to consider blurring and cleaning the filter, with clean applied when adding git and blurring during validation. They can be distinguished by file type. Take a look at the β€œgit attributes (5)” man and Git SCM Book: git Attributes .

+4
source share

There may be a good reason to use clean instead, but nothing really prevents you from re-adding files during the pre-commit hook, which is, in my opinion, a little intuitive.

Your pre-commit will look something like this:

*run your PNG processing here* git add *.png

Then the fixation will continue, as usual. If you want a fantasy, you can throw exit 1 where something goes wrong with compression and it stops fixing.

+2
source share

I did something like disso using gulp and gulp-git .

 var git = require('gulp-git') // ... other tasks gulp.task('add', function(){ return gulp.src('cdn/**') .pipe(git.add()) }) 

This add task is then called at the end of everything else. Then I have gulp with a pre-commit hook. It works like a charm.

So, in your case, the full file might look something like this:

 var gulp = require('gulp') var $ = require('gulp-load-plugins')() var runSequence = require('run-sequence') gulp.task('default', function () { return runSequence( 'images', 'add' ) }) gulp.task('images', function() { return gulp.src('app/images/**/*') .pipe($.cache($.imagemin({ progressive: true, interlaced: true }))) .pipe(gulp.dest('dist/images')) }) gulp.task('add', function(){ return gulp.src('dist/**') .pipe($.git.add()) }) 

(Please note that I have not tested it ... I received the image task code from Google.)

0
source share

All Articles