SlideShare a Scribd company logo
2
Most read
3
Most read
4
Most read
Git hooks
hebinbin.herokuapp.com
2013/05/10
Why ?
Problem: Some guys forgot to log their working time.
Therefore, for teamleaders, they can not know
how this ticket going.
Solution: After you git commit, open browser to
log your working time.
How ?
Git has one function named as
hooks. ( http://git-scm.com/book )
In my words, Hooks like event trigger
After you did git operation, it will trigger some events.
Examples
Git checkout ----------> you want to check whether all
gems have been installed.
Git push ----------> you want to trigger jenkins server to
run rspec to check you code.
Place
• Git hooks is in .git/hooks
• You can use shell script or ruby or python
• But remember to use chmod +x filename
Types
There has two types:
Client side (In your local PC)
Server side (In remote server like Stash)
• applypatch-msg
• post-update
• pre-rebase
• commit-msg
• pre-applypatch
• update
• post-commit
• pre-commit
• post-receive
• prepare-commit-msg
Git Commit
When you use git commit :
pre-commit
↓
prepare-commit-msg
↓
commit-msg
↓
post-commit
No Ticket, No Commit
In the future, when you look back to the code,
you want to know why this guy changed this code ?
Such as: “Fixed rspec error”
“Refactored the code”
---- > “GI-12345: Fixed rspec error”
“GI-54321: Refactored the code”
Pre-commit -1
Before commit, this hook can check something for you.
For example, in my pc, I will check where the code has “binding.pry” or not.
Place: .git/hooks/pre-commit
Code:
#!/usr/bin/env ruby
require 'colorize'
# check for rails debugger
rails_result = `grep -rlsn "binding.pry" --exclude-dir=".git" .`
if rails_result != ""
puts "[Error] you put binding.pry in your filesn".red
puts rails_result
exit(1)
end
Pre-commit -2
Prepare-commit-msg -1
• Means giving commit-msg a template
For example:
Now you are working at pr-GI-12345-2.5-id
Every time, when you commit, you want to add ticket
number to your commit message.
commit message here
↓
Ticket #GI-12345 commit message here.
Prepare-commit-msg -2
• #!/usr/bin/env ruby
•
• TICKET_REGEXP = /[A-Z]+-d+/ # WEB-12345
•
• TICKET_REF_FORMAT = 'Ticket #%s # ' # JIRA Ticket reference syntax.
•
• branch_name = `git branch --no-color 2> /dev/null`[/^* (.+)/, 1]
• msg_filename = ARGV[0]
•
• if ticket_in_branch = branch_name.match(TICKET_REGEXP)
• ticket_ref = TICKET_REF_FORMAT % ticket_in_branch.to_s
• old_msg = File.read msg_filename
•
• unless old_msg.match ticket_ref
• File.open msg_filename, 'w' do |new_msg|
• new_msg.write ticket_ref + old_msg
• end
• end
• end
•
• exit 0
Prepare-commit-msg -3
Commit-msg -1
• We can this hook to check whether your commit message has ticket number or not.
• #!/usr/bin/env ruby
•
• require 'colorize'
•
• TICKET_REGEXP = /[A-Z]+-d+/ # WEB-12345
•
• commit_message = File.open ARGV[0], 'r' { |f| f.read }
•
• unless commit_message.match(TICKET_REGEXP)
• puts "[ERROR] commit-message-format: JIRA ticket number is not existed.".red
• exit(1)
• end
Commit-msg -2
Post-commit -1
If everything goes well, then we can open firefox to log your time.
• #!/usr/bin/env ruby
•
• require 'colorize'
•
• TICKET_REGEXP = /[A-Z]+-d+/ # WEB-12345
•
• LINK = "https://*****.atlassian.net/browse/"
•
• puts "[Warning] log time: Please do not forget to log your time".red
•
• commit_message = %x{git log -1 --abbrev-commit}.split("n").last.strip
•
• jira_ticket = commit_message.match(TICKET_REGEXP)
• puts "please visit #{LINK}#{jira_ticket.to_s} to log time".green
•
• exec("firefox #{LINK}#{jira_ticket.to_s}")
Post-commit -2
Server side (git push)
• pre-receive
• update
• post-receive
Thanks
https://github.com/hebinbin/git-hooks-jira

More Related Content

PDF
Git hooks for front end developers
PDF
Git hooks
PDF
Breaking Bad Habits with GitLab CI
PDF
Breaking bad habits with GitLab CI
PDF
Using GitLab CI
PDF
CI with Gitlab & Docker
PDF
FOSDEM 2017: GitLab CI
PDF
沒有 GUI 的 Git
Git hooks for front end developers
Git hooks
Breaking Bad Habits with GitLab CI
Breaking bad habits with GitLab CI
Using GitLab CI
CI with Gitlab & Docker
FOSDEM 2017: GitLab CI
沒有 GUI 的 Git

What's hot (20)

PDF
Deployment made easy with Git
PDF
Using GitLab CI
PPTX
Workflows using Git GitHub | Edureka
PDF
Puppet Camp Atlanta 2014: r10k Puppet Workflow
PDF
Jenkins vs GitLab CI
PPT
Open Source Collaboration With Git And Git Hub
PDF
Introduction to GIT
PPTX
SCM Gitlab Advanced
PDF
Webinar - Automated Puppet Code Deployment with R10K
PPTX
CI/CD with Rancher CLI + Jenkins
PDF
Git advanced
PDF
Webinar - Continuous Integration with GitLab
PDF
Devops Porto - CI/CD at Gitlab
PPTX
Git and git workflow best practice
PDF
Enjoy privacy on Gitlab
PDF
Brief tutorial on Git
PPTX
Gorush: A push notification server written in Go
PPTX
Git & Github
PPTX
Swift Argument Parser
PPTX
Deployment made easy with Git
Using GitLab CI
Workflows using Git GitHub | Edureka
Puppet Camp Atlanta 2014: r10k Puppet Workflow
Jenkins vs GitLab CI
Open Source Collaboration With Git And Git Hub
Introduction to GIT
SCM Gitlab Advanced
Webinar - Automated Puppet Code Deployment with R10K
CI/CD with Rancher CLI + Jenkins
Git advanced
Webinar - Continuous Integration with GitLab
Devops Porto - CI/CD at Gitlab
Git and git workflow best practice
Enjoy privacy on Gitlab
Brief tutorial on Git
Gorush: A push notification server written in Go
Git & Github
Swift Argument Parser
Ad

Similar to Git hooks (20)

PPTX
Git and jira
DOCX
PrizeExample.DS_Store__MACOSXPrizeExample._.DS_StoreP.docx
PDF
Git Started With Git
PDF
Jedi Mind Tricks in Git
PPTX
2018 the conf put git to work - increase the quality of your rails project...
DOCX
Testing Gas Mileage Claims Assume that you are working for the C.docx
PPTX
Jedi Mind Tricks for Git
PPTX
2018 RubyHACK: put git to work - increase the quality of your rails project...
PPT
version_control_lectures_for _git_all.ppt
PDF
Advanced Git Tutorial
PDF
git and github
PDF
Git Developer Cheatsheet
PPTX
Git beyond basics
KEY
Working with Git
PPTX
Introduction to git, a version control system
PPT
Effective Git with Eclipse
PPT
Rails Rookies Bootcamp - Blogger
PDF
Ninja Git: Save Your Master
KEY
Git Magic: Versioning Files like a Boss
PPTX
MakingGitWorkForYou
Git and jira
PrizeExample.DS_Store__MACOSXPrizeExample._.DS_StoreP.docx
Git Started With Git
Jedi Mind Tricks in Git
2018 the conf put git to work - increase the quality of your rails project...
Testing Gas Mileage Claims Assume that you are working for the C.docx
Jedi Mind Tricks for Git
2018 RubyHACK: put git to work - increase the quality of your rails project...
version_control_lectures_for _git_all.ppt
Advanced Git Tutorial
git and github
Git Developer Cheatsheet
Git beyond basics
Working with Git
Introduction to git, a version control system
Effective Git with Eclipse
Rails Rookies Bootcamp - Blogger
Ninja Git: Save Your Master
Git Magic: Versioning Files like a Boss
MakingGitWorkForYou
Ad

Recently uploaded (20)

PDF
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PPTX
Cloud computing and distributed systems.
PDF
GDG Cloud Iasi [PUBLIC] Florian Blaga - Unveiling the Evolution of Cybersecur...
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Modernizing your data center with Dell and AMD
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
20250228 LYD VKU AI Blended-Learning.pptx
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Cloud computing and distributed systems.
GDG Cloud Iasi [PUBLIC] Florian Blaga - Unveiling the Evolution of Cybersecur...
MYSQL Presentation for SQL database connectivity
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Spectral efficient network and resource selection model in 5G networks
Modernizing your data center with Dell and AMD
Reach Out and Touch Someone: Haptics and Empathic Computing
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
The AUB Centre for AI in Media Proposal.docx
Diabetes mellitus diagnosis method based random forest with bat algorithm
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
NewMind AI Weekly Chronicles - August'25 Week I
CIFDAQ's Market Insight: SEC Turns Pro Crypto
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf

Git hooks

  • 2. Why ? Problem: Some guys forgot to log their working time. Therefore, for teamleaders, they can not know how this ticket going. Solution: After you git commit, open browser to log your working time.
  • 3. How ? Git has one function named as hooks. ( http://git-scm.com/book ) In my words, Hooks like event trigger After you did git operation, it will trigger some events.
  • 4. Examples Git checkout ----------> you want to check whether all gems have been installed. Git push ----------> you want to trigger jenkins server to run rspec to check you code.
  • 5. Place • Git hooks is in .git/hooks • You can use shell script or ruby or python • But remember to use chmod +x filename
  • 6. Types There has two types: Client side (In your local PC) Server side (In remote server like Stash) • applypatch-msg • post-update • pre-rebase • commit-msg • pre-applypatch • update • post-commit • pre-commit • post-receive • prepare-commit-msg
  • 7. Git Commit When you use git commit : pre-commit ↓ prepare-commit-msg ↓ commit-msg ↓ post-commit
  • 8. No Ticket, No Commit In the future, when you look back to the code, you want to know why this guy changed this code ? Such as: “Fixed rspec error” “Refactored the code” ---- > “GI-12345: Fixed rspec error” “GI-54321: Refactored the code”
  • 9. Pre-commit -1 Before commit, this hook can check something for you. For example, in my pc, I will check where the code has “binding.pry” or not. Place: .git/hooks/pre-commit Code: #!/usr/bin/env ruby require 'colorize' # check for rails debugger rails_result = `grep -rlsn "binding.pry" --exclude-dir=".git" .` if rails_result != "" puts "[Error] you put binding.pry in your filesn".red puts rails_result exit(1) end
  • 11. Prepare-commit-msg -1 • Means giving commit-msg a template For example: Now you are working at pr-GI-12345-2.5-id Every time, when you commit, you want to add ticket number to your commit message. commit message here ↓ Ticket #GI-12345 commit message here.
  • 12. Prepare-commit-msg -2 • #!/usr/bin/env ruby • • TICKET_REGEXP = /[A-Z]+-d+/ # WEB-12345 • • TICKET_REF_FORMAT = 'Ticket #%s # ' # JIRA Ticket reference syntax. • • branch_name = `git branch --no-color 2> /dev/null`[/^* (.+)/, 1] • msg_filename = ARGV[0] • • if ticket_in_branch = branch_name.match(TICKET_REGEXP) • ticket_ref = TICKET_REF_FORMAT % ticket_in_branch.to_s • old_msg = File.read msg_filename • • unless old_msg.match ticket_ref • File.open msg_filename, 'w' do |new_msg| • new_msg.write ticket_ref + old_msg • end • end • end • • exit 0
  • 14. Commit-msg -1 • We can this hook to check whether your commit message has ticket number or not. • #!/usr/bin/env ruby • • require 'colorize' • • TICKET_REGEXP = /[A-Z]+-d+/ # WEB-12345 • • commit_message = File.open ARGV[0], 'r' { |f| f.read } • • unless commit_message.match(TICKET_REGEXP) • puts "[ERROR] commit-message-format: JIRA ticket number is not existed.".red • exit(1) • end
  • 16. Post-commit -1 If everything goes well, then we can open firefox to log your time. • #!/usr/bin/env ruby • • require 'colorize' • • TICKET_REGEXP = /[A-Z]+-d+/ # WEB-12345 • • LINK = "https://*****.atlassian.net/browse/" • • puts "[Warning] log time: Please do not forget to log your time".red • • commit_message = %x{git log -1 --abbrev-commit}.split("n").last.strip • • jira_ticket = commit_message.match(TICKET_REGEXP) • puts "please visit #{LINK}#{jira_ticket.to_s} to log time".green • • exec("firefox #{LINK}#{jira_ticket.to_s}")
  • 18. Server side (git push) • pre-receive • update • post-receive