使用gruntjs作为precommit钩子

时间:2013-01-08 10:30:25

标签: git gruntjs pre-commit-hook

有没有办法以预先挂起的方式运行gruntjs任务。例如,我想防止提交失败的测试或jshint问题。我还考虑为每次提交运行代码美化器。

3 个答案:

答案 0 :(得分:9)

Git钩子只是在执行commit等操作时执行的脚本。它们可以包含您想要的任何编程语言。

触发grunt的示例:

#!/bin/sh
grunt

将其保存在文件中:.git/hooks/pre-commit

如果grunt以高于0的错误代码退出,如果任何任务失败,它会阻止提交:

  

从此挂钩退出非零会中止提交


阅读材料:http://codeinthehole.com/writing/tips-for-using-a-git-pre-commit-hook/

git docs:http://git-scm.com/book/en/Customizing-Git-Git-Hooks

  

与许多其他版本控制系统一样,Git可以在发生某些重要操作时触发自定义脚本。

答案 1 :(得分:5)

我最近遇到了同样的问题,并在http://viget.com/extend/grunt-getting-started-with-git-hooks

上详细介绍了更全面的Grunt解决方案

答案 2 :(得分:2)

我认为你应该查看grunt-githooks,这是一个很好的Grunt插件,可以为你完成所有魔法,并让你轻松地为你Gruntfile.js中的某些钩子注册Grunt任务,例如:

grunt.initConfig({
  githooks: {
    all: {
      // Will run the jshint and test:unit tasks at every commit
      'pre-commit': 'jshint test:unit',
    }
  }
});

(摘自https://github.com/wecodemore/grunt-githooks#defining-a-few-hooks的文件)

在npm here上找到该模块。

相关问题