• Stars
    star
    216
  • Rank 182,392 (Top 4 %)
  • Language
    Python
  • License
    MIT License
  • Created almost 8 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Git pre-receive hook to check commits and code style

Git Pre-Receive Hook to Validate Commits

It is exhausting to edit files again and again to have a consistent style. This project provides a Git pre-receive hook to validate pushed commits on the Git server side. The hook avoids all issues by rejecting any commit not matching the rules to get in to the repository in the first place.

The pre-receive hook runs some checks on commits on its own, and searches for programming language specific syntax checkers on the PATH of the server to check changed files with them. The process is pretty fast, because only the added and modified files on the pushed commits are passed to the syntax checkers, also in parallel. It wouldn't slow you down unless your commits are touching hundreds of files.

Installation

It is possible to install the tool with pip:

pip install igcommit

Link the script to hooks/pre-receive on you Git repositories on your Git server:

ln -s igcommit-receive /home/git/repositories/myproject.git/hooks/pre-receive

Features

  • Validate received commits one by one not just the last one
  • Only validate added or modified files on the commits
  • Report all problems before failing
  • Check for duplicate commit summaries
  • Check for misleading merge commits
  • Validate committer and author timestamps
  • Validate committer and author names and email addresses
  • Check commit message best practices (80 lines, first line summary...)
  • Check commit summary formatting
  • Validate commit tags against a list [BUGFIX], [FEATURE], [WIP]...
  • Check for changed file paths
  • Accept commits tagged as [HOTFIX], [MESS], [TEMP], or [WIP] with issues
  • Check executable bits and shebangs
  • Check for allowed branch names
  • Check symlinks
  • Check CSS files with csslint
  • Check Go files with golint
  • Check HTML files with htmlhint
  • Check Puppet files with puppet parser validate and puppet-lint
  • Check Python files with flake8 or pycodestyle and pyflakes
  • Check Ruby files with rubocop
  • Check shell scripts with shellcheck
  • Check JavaScript files with eslint, jshint, jscs, or standard
  • Check CoffeeScript files with coffeelint
  • Check PHP files with phpcs
  • Run the external check commands in parallel
  • Validate JSON, XML, YAML file formats

Here is an example problem output:

=== CheckDuplicateCommitSummaries on CommitList ===
ERROR: summary "Add nagios check for early expiration of licenses" duplicated 2 times

=== CheckCommitSummary on 31d0f6b ===
WARNING: summary longer than 72 characters

=== CheckCommitSummary on 6bded65 ===
WARNING: past tense used on summary

=== CheckCommand "flake8" on src/check_multiple.py at 6bded65 ===
INFO: line 10 col 5: E225 missing whitespace around operator
INFO: line 17 col 80: E501 line too long (122 > 79 characters)
INFO: line 17 col 85: E203 whitespace before ','

=== CheckCommitMessage on 6fdbc00 ===
WARNING: line 7 is longer than 80
WARNING: line 9 is longer than 80

Configuration

The script itself is currently configuration free. Though, some of the syntax checkers called by the script uses or requires configurations. Those configuration files has to be on the top level of the Git repository.

Syntax Checker Configuration File Β 
coffeelint coffeelint.json, or package.json optional
csslint .csslintrc optional
eslint eslint.(js|yaml|yml|json), or package.json required
flake8 .flake8, setup.cfg, or tox.ini optional
htmlhint .htmlhintrc optional
jscs .jscsrc, .jscs.json, or package.json required
jshint .jshintrc, or package.json optional
phpcs phpcs.xml, or phpcs.xml.dist optional
puppet-lint .puppet-lint.rc optional
pycodestyle setup.cfg, or tox.ini optional
rubocop .rubocop.yml optional

Pros and Cons of Pre-receive Hook

Continuous Integration Server

A continuous integration server can run such checks with the many other things it is doing. Moving this job from it has many benefits:

  • Synchronous feedback
  • More efficient
  • Disallow any commit violating the rules
Pre-commit Hook

Even though, pre-receive hook gives later feedback than pre-commit hook, it has many advantages over it:

  • No client side configuration
  • Plugins has to be installed only once to the Git server
  • Everybody gets the same checks
  • Enforcement, nobody can skip the checks
  • Commit checking (pre-commit hook only gets what is changed in the commit)
IDE Integration
The same advantages compared to pre-commit hooks applies to IDE integration. Though, IDE integration gives much sooner and nicer feedback, so it is still a good idea, even with the pre-receive hook.

Dependencies

The script has no dependencies on Python 3.4 or above. The script executes the validation commands using the shell. The necessary ones for checked repositories need to be installed separately. See the complete list of commands on the config.py. The commands which are not available on the PATH is not going to be used.

Testing

I found it useful to check what the script would have complained if it had been active on different Git repositories. You can run a command like this to test this inside a Git repository against last 50 commits:

git log --reverse --oneline HEAD~50..HEAD |
    sed 's:\([^ ]*\) .*:\1 \1 refs/heads/master:' |
    python ../igcommit/igcommit-receive

Changes

Version 2.0
  • Fix line numbers on syntax errors for executables being 1 off
  • Recognize and validate symlinks
  • Validate committer and author timestamps
  • Validate contributor names and email addresses
  • Reduce commit message line length limits
  • Complain about file extensions on executables
Version 2.1
  • Add [TEMP] to recognized commit tags
  • Fix getting the changes of the initial commit (Zheng Wei)
  • Fix various file descriptor leaks
  • Check commit summaries more strictly
  • Check shebangs of non-executable files too
  • Don't check on empty file contents
  • Improve unicode support on Python 2
  • Fix checking symlink targets
Version 2.2
  • Fix eslint configuration (Jerevia)
  • Accepts commits with [TEMP] with issues
  • Stop skipping empty files
  • Make sure not to get unknown file contents
  • Move file extensions to config
  • Increase timestamp comparison tolerance for 1 more minute
  • Handle spaces on shebangs
Version 2.3
  • Handle check command failing immediately
  • Support pushed tags
  • Fix failing on file check with bogus return code
  • Include list of commit tags on warning
  • Fix recognising commit tags [REVIEW] and [SECURITY]
Version 2.4
  • Fix recognising removed configuration files
  • Support coffeelint
Version 2.5
  • Fix unicode issue on Python 2 for XML, YAML, and JSON (jcoetsie)
Version 3.0
  • Drop Python 2 support
  • Fix handling filenames with spaces (Friz-zy)
Version 3.1
  • Stop complaining about the same commit for Git tags
  • Fix checking contributor names and email addresses
  • Stop complaining about file extensions we don't know about
  • Filter out checking format of files under templates/ directories
  • Improve code quality and style
Version 3.2
  • Reduce severity of length for merge commit summary to warning
Version 3.3
  • Add optional branch name check via regular expressions

License

The script is released under the MIT License. The MIT License is registered with and approved by the Open Source Initiative [1].

[1]https://opensource.org/licenses/MIT

More Repositories

1

ltc

Online web application-dashboard for report analyzing,running and online monitoring of load tests started with JMeter
Python
191
star
2

slack-bot

Ready to use Slack bot for lazy developers: start Jenkins jobs, watch Jira tickets, watch pull requests with AI support...
Go
171
star
3

asset-relations-viewer

Plugin to display dependencies between assets, files, assetgroups, etc. in a tree based view within the Unity editor.
C#
136
star
4

polysh

Polysh, the remote shell multiplexer
Python
63
star
5

ProjectWindowDetails

Unity Editor extension to add columns into the project window for showing additional information about assets
C#
62
star
6

serveradmin

Configuration management database system of InnoGames
Python
43
star
7

ax3

AS3 to Haxe converter
Haxe
40
star
8

igmonplugins

Nagios plugins from InnoGames
Python
26
star
9

graphite-ch-optimizer

Daemon to optimize GraphiteMergeTree automatically
Go
24
star
10

asset-relations-viewer-addressables

C#
20
star
11

igvm

InnoGames VM provisioning tool
Python
15
star
12

tcpause

TCPause is a zero-downtime proxy for TCP and UNIX sockets written in Go.
Go
13
star
13

igcollect

Graphite data collector scripts from InnoGames
Python
13
star
14

pirate

Realtime metrics server written in Go
Go
12
star
15

springfox-protobuf

Glue for SpringFox to support Protobuf Messages
Java
12
star
16

flink-real-time-crm

Demo Flink and Kafka project to show how to react on tracking events in real-time and trigger offer for customer engagement based on campaign configurations. The project also utilizes the Broadcast State Pattern in order to update the rules (campaigns) at runtime without restarting the project, using a dedicated, low-frequency, Kafka topic.
Java
12
star
17

drac_config

Collection of shell scripts for managing iDRACs.
Shell
8
star
18

testtool

Load balancing service of InnoGames
C++
8
star
19

android-ndk

C
7
star
20

puppet-adminapi

Ruby
6
star
21

slaxy

Sentry webhooks to slack message converter proxy
Go
6
star
22

deb-drop

Http fastCGI web server for provide simple and secure access for managing Debian packages in repositories
Go
5
star
23

unity-meetup

Hamburg Unity Meetup repository with slides from Meetup events
5
star
24

AS3Communicator

AS3Communicator lets you connect your AS3 application's DisplayList to the outside world like JavaScript
ActionScript
5
star
25

openfl-mark

Benchmark scenes for OpenFL
Haxe
4
star
26

junit5-scenario-builder

Junit5 extension that introduces a Scenario Builder to your tests
Java
4
star
27

AS3-DomainMemoryManager

A manager for domain memory access in Adobe AIR/Flash.
ActionScript
4
star
28

StarCrash

An InnoDay project. First person SCIFI dungeon crawler.
JavaScript
4
star
29

kafka-client

This library offers an easy way to consume messages from Kafka using more then a single thread by creating a container with consumer.
Java
4
star
30

jobs

Open Positions at InnoGames
Python
3
star
31

dr-json

Manage content of JSON files based on JSON Schemas
TypeScript
3
star
32

puppet-clickhouse

Install and manage ClickHouse DBMS Requires for xml-simple ruby gem to be installed
Ruby
3
star
33

jenkins-build-metrics

A simple wrapper to report failed jenkins jobs to graphite
Go
2
star
34

canirunit

CanYouRunIt Tool is a simple application built in PHP that will allow you to validate your environment for a specific set of constraints set by you such as PHP Modules, PHP Configurations, Databases, Work Queues and more.
PHP
2
star
35

igcolors

Provide a simple module to colourize string for console output
Python
2
star
36

jenkins-lib

This library is used as adminLibs in InnoGames GmbH Jenkins instances
Groovy
2
star
37

flink-real-time-crm-ui

Demo UI for the flink-real-time-crm project using Spring Boot with Thymeleaf. The Demo UI is able to send messages to and consume from the related Kafka topics.
Java
2
star
38

stdin-rotate

Rotates logs reading content from stdin
Go
1
star
39

traffic_dots

A DOTS experiment on traffic simulation
C#
1
star
40

CollectionIconTool

A Unity tool to generate icon variations
C#
1
star
41

jmeter-test-plans

1
star
42

homebrew-core-ig

Ruby
1
star
43

xhprofui

A new gui for xhprof
PHP
1
star
44

hashpipe

Python
1
star
45

hosts-editor

A Gradle plugin that creates a new hosts file with defined entries based on your local hosts file.
Groovy
1
star