11

The Importance of Git Hooks

 4 years ago
source link: https://fuzzyblog.io/blog/git/2014/08/29/the-importance-of-git-hooks.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

The Importance of Git Hooks

Aug 29, 2014

So I was working this morning and got one of those bloody awful Rails stack traces – every error was at the system layer and I could not make any progress debugging it. So I looked at git status and there were like 25 + files all tied to an overall CSV import process (2 data models, 1 controller, system level UI changes, etc) and then it hit me – I need my .git hooks setup on this project! Every bit of source code under management by git has a .git directory at the top level and under it a hooks directory. Hooks are executable files that are run when you do an atomic level git action. For example there is one called pre-commit and this is code that is executed BEFORE a commit actually happens. What you can do with a pre-commit hook is tie in a code evaluation process (think lint if you're a C guy) to find any errors before they are committed. Now I've set these up in almost every project but not here so a quick copy of a file from another project to .git/hooks gave me this:

app/controllers/csv_imports_controller.rb:
8: syntax error, unexpected ')', expecting ']'
 @csv_import = current_user.csv_imports.find(params[:id))
 ^
24: syntax error, unexpected $end, expecting keyword_end

And here's the offending line in question:

@csv_import = current_user.csv_imports.find(params[:id))

Yep. params[:id]) was needed (second to last char was a ) instead of a ]. I looked at that line like 4 times and didn't see it.

Having used these now for several years, its hard for me to imagine working in a dynamic language without them. Kudos to Dv for setting these up once upon a time.

##.git/hooks for the win!

And here's the code for .git/hooks/pre-commit Adjust the shebang line accordingly and make sure its an executable file.

#!/usr/bin/env ruby
#
# A hook script to verify that only syntactically valid ruby code is commited.
# Called by git-commit with no arguments. The hook should
# exit with non-zero status after issuing an appropriate message if
# it wants to stop the commit.
#
# Put this code into a file called "pre-commit" inside your .git/hooks
# directory, and make sure it is executable ("chmod +x .git/hooks/pre-commit")
puts "in .git/hooks/pre-commit"
require 'open3'
include Open3
# Set this to true if you want warnings to stop your commit
stop_on_warnings = false
stop_on_errors = true
# compiler_ruby = `which rbx`.strip
# compiler_ruby = `which ruby`.strip if compiler_ruby.length == 0
# setting compiler to ruby-1.9.3 since `which ruby` was setting it to 1.8.7
compiler_ruby = "$HOME/.rvm/rubies/ruby-1.9.3-p484/bin/ruby"
changed_ruby_files = `git diff-index --name-only --cached HEAD`.split("\n").inject([]) do |files, line|
 files << line.chomp if line =~ /(.+\.(e?rb|task|rake|thor)|[Rr]akefile|[Tt]horfile)/
 files
end
problematic_files = changed_ruby_files.inject([]) do |problematic_files, file|
 if File.readable? file
 cmd = if file =~ /\.erb\z/
 # Set trim mode to "-", just as Rails does
 # Replacing all <%= tags with <% for the syntax check
 "sed 's/<%=/<%/g' #{file} | erb -xT - | #{compiler_ruby} -wc"
 else
 "#{compiler_ruby} -wc #{file}"
 end
errors = nil
 popen3(cmd) do |stdin, stdout, stderr|
 errors = stderr.read.split("\n")
 end
errors.reject!{ |line| line =~ /[0-9]+:\s+warning:/ } unless stop_on_warnings
 errors.reject!{ |line| line =~ /[0-9]+:\s+syntax error/ } unless stop_on_errors
unless errors.empty?
 errors.map!{ |line| line.sub(/#{file}:/, '') }
 problematic_files << "#{file}:\n#{errors.join("\n")}"
 end
 end
problematic_files
end
if problematic_files.size > 0
 $stderr.puts problematic_files.join("\n\n")
 exit 1
else
 puts "no syntax errors"
 # All is well
 exit 0
end

Recommend

  • 20
    • webfe.kujiale.com 4 years ago
    • Cache

    git hooks

    现在代码一般都会使用git来进行管理, 其中git hooks(git钩子)是git提供的在代码管理的生命周期中会被触发的一个阶段, 如同react里面组件的生命周期一样, 随着组件的状态的改变, 一些生命周期函数会被触发, 然后可以在触发的时候进行自定义...

  • 15
    • zwischenzugs.com 4 years ago
    • Cache

    Git Hooks the Hard Way

    Git Hooks the Hard Way This post is adapted from an advanced chapter of Learn Git the Hard Way. Each section is self-contained, and should be typed out by hand to ensure...

  • 12

    昨天开始接手开发公司前端团队的主页,在稍微修改点东西后推送到远程仓库想看下线上结果时发现并没有更改!询问一把手得知,居然还需要连接到服务器执行一下 git pull 才行……对于我这种怕麻烦的人来说,简直不能忍! 经过一番查找资料以及...

  • 15
    • nick.scialli.me 4 years ago
    • Cache

    Fun with React and Git Hooks

    One topic I have gotten more and more excited about throughout my software development career is quality! Perhaps I’ve been burned one too many times. Alas, I decided...

  • 5
    • git-scm.com 3 years ago
    • Cache

    Git - Git Hooks

    8.3 Customizing Git - Git Hooks Git Hooks Like many other Version Control Systems, Git has a way to fire off custom scripts when certain important actions occur. There are two groups of these hooks: client-sid...

  • 6

    手写 git hooks 脚本(pre-commit、commit-msg)Git 能在特定的重要动作发生时触发自定义脚本,其中比较常用的有:pre-commit、commit-msg、pre-push

  • 7
    • segmentfault.com 3 years ago
    • Cache

    前端代码库里的 git hooks

    本篇文章收获什么:git hooks 的基本知识yorkie 的原理浅析如何使用 本地 vscode 打开 github 项目故事是这样的:我:在npm run lint脚本里面加了vue-tsc --noEmit --skipLibCheck &...

  • 12
    • www.mycode.net.cn 3 years ago
    • Cache

    CMake 自动安装 git pre-commit hooks

    在日常开发中,我们经常通过各类 IDE 工具来自动修正代码风格,但由于部分 IDE 工具与 clang-format 配合不是特别完善,导致保存或者按下分号、冒号以后代码自动格式化导致错乱,或者格式化时间过长等问题。这在日常开发中是很难让人接受的。 那么我们有...

  • 4

    之前有写过一篇如何优雅写 Git Commit 的文章,但是如果真的要每次提交都注意这些规范,未免有些繁琐。作为一个程序猿,能用工具解决当然是要使用工具的,所以今天我们就来介绍一下Commitizen

  • 7

    Git hooks made easy on go

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK