SlideShare a Scribd company logo
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

What's hot

Puppet Camp Atlanta 2014: r10k Puppet Workflow
Puppet Camp Atlanta 2014: r10k Puppet WorkflowPuppet Camp Atlanta 2014: r10k Puppet Workflow
Puppet Camp Atlanta 2014: r10k Puppet Workflow
Puppet
 
Open Source Collaboration With Git And Git Hub
Open Source Collaboration With Git And Git HubOpen Source Collaboration With Git And Git Hub
Open Source Collaboration With Git And Git Hub
Nick Quaranto
 

What's hot (20)

Deployment made easy with Git
Deployment made easy with GitDeployment made easy with Git
Deployment made easy with Git
 
Using GitLab CI
Using GitLab CIUsing GitLab CI
Using GitLab CI
 
Workflows using Git GitHub | Edureka
Workflows using Git GitHub | EdurekaWorkflows using Git GitHub | Edureka
Workflows using Git GitHub | Edureka
 
Puppet Camp Atlanta 2014: r10k Puppet Workflow
Puppet Camp Atlanta 2014: r10k Puppet WorkflowPuppet Camp Atlanta 2014: r10k Puppet Workflow
Puppet Camp Atlanta 2014: r10k Puppet Workflow
 
Jenkins vs GitLab CI
Jenkins vs GitLab CIJenkins vs GitLab CI
Jenkins vs GitLab CI
 
Open Source Collaboration With Git And Git Hub
Open Source Collaboration With Git And Git HubOpen Source Collaboration With Git And Git Hub
Open Source Collaboration With Git And Git Hub
 
Introduction to GIT
Introduction to GITIntroduction to GIT
Introduction to GIT
 
SCM Gitlab Advanced
SCM Gitlab AdvancedSCM Gitlab Advanced
SCM Gitlab Advanced
 
Webinar - Automated Puppet Code Deployment with R10K
Webinar - Automated Puppet Code Deployment with R10KWebinar - Automated Puppet Code Deployment with R10K
Webinar - Automated Puppet Code Deployment with R10K
 
CI/CD with Rancher CLI + Jenkins
CI/CD with Rancher CLI + JenkinsCI/CD with Rancher CLI + Jenkins
CI/CD with Rancher CLI + Jenkins
 
Git advanced
Git advancedGit advanced
Git advanced
 
Webinar - Continuous Integration with GitLab
Webinar - Continuous Integration with GitLabWebinar - Continuous Integration with GitLab
Webinar - Continuous Integration with GitLab
 
Devops Porto - CI/CD at Gitlab
Devops Porto - CI/CD at GitlabDevops Porto - CI/CD at Gitlab
Devops Porto - CI/CD at Gitlab
 
Git and git workflow best practice
Git and git workflow best practiceGit and git workflow best practice
Git and git workflow best practice
 
Enjoy privacy on Gitlab
Enjoy privacy on GitlabEnjoy privacy on Gitlab
Enjoy privacy on Gitlab
 
Brief tutorial on Git
Brief tutorial on GitBrief tutorial on Git
Brief tutorial on Git
 
Gorush: A push notification server written in Go
Gorush: A push notification server written in GoGorush: A push notification server written in Go
Gorush: A push notification server written in Go
 
Git & Github
Git & GithubGit & Github
Git & Github
 
Swift Argument Parser
Swift Argument ParserSwift Argument Parser
Swift Argument Parser
 
Git
GitGit
Git
 

Similar to Git hooks

Server Development Workflow For PicCollage
Server Development Workflow For PicCollageServer Development Workflow For PicCollage
Server Development Workflow For PicCollage
Lin Jen-Shin
 
SECON'2017, Цаль-Цалко Иван, Go на практике
SECON'2017, Цаль-Цалко Иван, Go на практикеSECON'2017, Цаль-Цалко Иван, Go на практике
SECON'2017, Цаль-Цалко Иван, Go на практике
SECON
 
Git Distributed Version Control System
Git   Distributed Version Control SystemGit   Distributed Version Control System
Git Distributed Version Control System
Victor Wong
 
Using Git as your VCS with Bioconductor
Using Git as your VCS with BioconductorUsing Git as your VCS with Bioconductor
Using Git as your VCS with Bioconductor
timyates
 

Similar to Git hooks (20)

Developing with the Go client for Apache Kafka
Developing with the Go client for Apache KafkaDeveloping with the Go client for Apache Kafka
Developing with the Go client for Apache Kafka
 
Server Development Workflow For PicCollage
Server Development Workflow For PicCollageServer Development Workflow For PicCollage
Server Development Workflow For PicCollage
 
SECON'2017, Цаль-Цалко Иван, Go на практике
SECON'2017, Цаль-Цалко Иван, Go на практикеSECON'2017, Цаль-Цалко Иван, Go на практике
SECON'2017, Цаль-Цалко Иван, Go на практике
 
Git Distributed Version Control System
Git   Distributed Version Control SystemGit   Distributed Version Control System
Git Distributed Version Control System
 
Using Git as your VCS with Bioconductor
Using Git as your VCS with BioconductorUsing Git as your VCS with Bioconductor
Using Git as your VCS with Bioconductor
 
Wokshop de Git
Wokshop de Git Wokshop de Git
Wokshop de Git
 
Jedi Mind Tricks in Git
Jedi Mind Tricks in GitJedi Mind Tricks in Git
Jedi Mind Tricks in Git
 
Jedi Mind Tricks for Git
Jedi Mind Tricks for GitJedi Mind Tricks for Git
Jedi Mind Tricks for Git
 
How to use git without rage
How to use git without rageHow to use git without rage
How to use git without rage
 
Working in Team using Git in Unity
Working in Team using Git in UnityWorking in Team using Git in Unity
Working in Team using Git in Unity
 
Continuous Delivery Workshop with Ansible x GitLab CI (2nd+)
Continuous Delivery Workshop with Ansible x GitLab CI (2nd+)Continuous Delivery Workshop with Ansible x GitLab CI (2nd+)
Continuous Delivery Workshop with Ansible x GitLab CI (2nd+)
 
Git basics 2
Git basics 2Git basics 2
Git basics 2
 
Год в Github bugbounty, опыт участия
Год в Github bugbounty, опыт участияГод в Github bugbounty, опыт участия
Год в Github bugbounty, опыт участия
 
Oracle to Postgres Migration - part 2
Oracle to Postgres Migration - part 2Oracle to Postgres Migration - part 2
Oracle to Postgres Migration - part 2
 
Introduction To Git Workshop
Introduction To Git WorkshopIntroduction To Git Workshop
Introduction To Git Workshop
 
Pro git - grasping it conceptually
Pro git - grasping it conceptuallyPro git - grasping it conceptually
Pro git - grasping it conceptually
 
git fail --force (make it up with your pull requests)
git fail --force (make it up with your pull requests)git fail --force (make it up with your pull requests)
git fail --force (make it up with your pull requests)
 
Git
GitGit
Git
 
Osol Pgsql
Osol PgsqlOsol Pgsql
Osol Pgsql
 
Switching to Git
Switching to GitSwitching to Git
Switching to Git
 

Recently uploaded

Recently uploaded (20)

PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
 
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptxUnpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
 
Demystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John StaveleyDemystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John Staveley
 
In-Depth Performance Testing Guide for IT Professionals
In-Depth Performance Testing Guide for IT ProfessionalsIn-Depth Performance Testing Guide for IT Professionals
In-Depth Performance Testing Guide for IT Professionals
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
Exploring UiPath Orchestrator API: updates and limits in 2024 🚀
Exploring UiPath Orchestrator API: updates and limits in 2024 🚀Exploring UiPath Orchestrator API: updates and limits in 2024 🚀
Exploring UiPath Orchestrator API: updates and limits in 2024 🚀
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.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