How to automatically invoke a script before a git add?

孤者浪人 提交于 2019-12-03 11:48:50
Philip Oakley

You may want to consider a smudge & clean filter, with the clean being applied during the git add, and the smudge during checkout. These can be allocated by file type. Look at the 'git attributes(5)' man page and Git SCM book : Git Attributes.

There may be a good reason to use clean instead, but there's actually nothing preventing you from re-adding the files during the pre-commit hook, which is a bit more intuitive in my opinion.

Your pre-commit would look something like:

*run your PNG processing here* (after processing completes) git add *.png

The commit will then continue as usual. If you want to get fancy you can throw an exit 1 in there when something goes wrong with the compression, and it will stop the commit.

I did something similar to 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 set up with a pre-commit hook. Works like a charm.

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

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())
})

(Note that I haven't tested it... I got the images task code from Google.)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!