[关闭]
@weixin 2015-07-13T20:57:22.000000Z 字数 3113 阅读 2127

write a git pre-push hook

active-active git maven

write a pre-push hook

  1. #!/bin/bash
  2. # before push, do unit test
  3. cd app/webapp
  4. mvn clean test

pretty simple.
if you go to .git/hooks, you would find a file pre-push.sample, take it as a reference. then name the above script pre-push, no suffix, put it to .git/hooks folder.

then give permissin:
chmod +x pre-push.

then each time you do push, the unit test would be triggered.

how to distribute this script and enforce it

you can not directly push the .git/hooks. so you need to find some other ways to do it. Here is my step:
- create a folder, such as buildtool in your project, then put the pre-push script there.
- write a Makefile, it would copy the script to .git/hooks, and assign it appropriate permission.
- let maven to call make to build the makefile.

for step 2, here is my Makefile

  1. SHELL = /bin/bash
  2. build : copyfile assignpermission
  3. copyfile :
  4. if [ -d "../../../.git/hooks" ]; then \
  5. cp -f pre-push ../../../.git/hooks; \
  6. fi
  7. assignpermission :
  8. if [ -e "../../../.git/hooks/pre-push" ]; then \
  9. chmod +x ../../../.git/hooks/pre-push; \
  10. fi

notice I use the relative path because the project I'm working on is a module of a bigger project.

for step 3, here is the pom

for the parent pom.xml, I did something like this:

  1. <pluginManagement>
  2. <plugins>
  3. ......
  4. <plugin>
  5. <artifactId>maven-antrun-plugin</artifactId>
  6. <version>1.8</version>
  7. </plugin>

then in the project pom.xml, the change is :

  1. <plugin>
  2. <artifactId>maven-antrun-plugin</artifactId>
  3. <executions>
  4. <execution>
  5. <id>make</id>
  6. <phase>generate-sources</phase>
  7. <goals>
  8. <goal>run</goal>
  9. </goals>
  10. <configuration>
  11. <target name="make">
  12. <exec executable="make" failonerror="true"
  13. dir="${project.basedir}/buildtool"/>
  14. </target>
  15. </configuration>
  16. </execution>
  17. </executions>
  18. </plugin>

use maven-antrun-plugin to trigger the make command.

then it is done.

results

then each time when other developer who run mvn clean install, he would see something like this :

  1. [INFO] --- maven-antrun-plugin:1.8:run (make) @ webapp ---
  2. [INFO] Executing tasks
  3. make:
  4. [exec] if [ -d "../../../.git/hooks" ]; then \
  5. [exec] cp -f pre-push ../../../.git/hooks; \
  6. [exec] fi
  7. [exec] if [ -e "../../../.git/hooks/pre-push" ]; then \
  8. [exec] chmod +x ../../../.git/hooks/pre-push; \
  9. [exec] fi
  10. [INFO] Executed tasks

then when they do push, unit test would be triggered:

  1. 842 [main] DEBUG - ==> Parameters: 98765432(Long), qbo(String)
  2. 842 [main] DEBUG - <== Total: 1
  3. 843 [main] DEBUG - Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@5d2a4eed]
  4. Tests run: 6, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.363 sec
  5. Jul 13, 2015 1:29:30 PM org.springframework.context.support.GenericApplicationContext doClose
  6. INFO: Closing org.springframework.context.support.GenericApplicationContext@cb644e: startup date [Mon Jul 13 13:29:29 PDT 2015]; root of context hierarchy
  7. Results :
  8. Tests run: 6, Failures: 0, Errors: 0, Skipped: 0
  9. [INFO] ------------------------------------------------------------------------
  10. [INFO] BUILD SUCCESS
  11. [INFO] ------------------------------------------------------------------------
  12. [INFO] Total time: 4.082 s
  13. [INFO] Finished at: 2015-07-13T13:29:30-07:00
  14. [INFO] Final Memory: 29M/491M
  15. [INFO] ------------------------------------------------------------------------
  16. Counting objects: 4, done.
  17. Delta compression using up to 8 threads.
  18. Compressing objects: 100% (4/4), done.
  19. Writing objects: 100% (4/4), 390 bytes | 0 bytes/s, done.
  20. Total 4 (delta 2), reused 0 (delta 0)
  21. To https://github.intuit.com/SBG/dispatcher-service/
  22. 044f122..31dbf12 master -> master
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注