SlideShare a Scribd company logo
1 of 18
Download to read offline
You're Doing it Wrong:
              Commit Hooks!

                          or
           Commit Hooks: the Subtle Hammer


Ben McGraw
Sr. Software Engineer, IMVU Inc.
ben@imvu.com
@bengrue
What are commit hooks?

A way to run automated actions before your local changes get
committed to your code repository.
What are commit hooks?

A way to run automated actions before your local changes get
committed to your code repository.
A way to make the computer check all those annoying little
repetitive tasks before your code goes on your permanent
record.
Computers are good at repetition.

(People are bad at repetition...
and good about forgetting little things!)

Make the computer do the repetitive,
boring stuff so you can focus on the
interesting code.
How do I make them?

In SVN:
as the SVN user, go to /PATH_TO_REPO/hooks/ and make an
executable script file in your favorite shell scripting language.


In GIT:
Basically the same... but there's a lot more options/specific
hooks available (see link at the end).
How do I make them?

The basic principle is the same in both:

1. The script receives text about the files you're attempting to
commit.

2. You inspect those files for rules you make yourself.

3. If you want the commit to not get through to the repository,
return an error code (and print out an error message to
STDERR).

4. Otherwise, your commit continues on to the repository.
Best Practices

1. Subdivide each individual check into it's own function.

2. Give each individual check it's own passphrase to skip it.
What can you use them to do?
Enforce Coding Conventions
- Prevent Code that doesn't allow Linting (Static Analysis!)

- Enforcing Company/Project-specific coding style guidelines

  - For example, IMVU has commit hooks to prevent:
     - tabs from being committed

     - leading-underscored ('lazy private') functions from being
       used outside of a module.

     - using a module's name in a function
       (e.g., customer::get() is good,
        customer::get_customer() is bad.)

- Passphrase to skip is per type ("Skip whitespace check.",
"Skip module check.", etc... )
Running tests as part of your workflow

- One of the simplest way to integrate test-running into a coding
workflow

- If you want acceptance tests, and you don't prevent commits
upon failure, you're Doing It Wrong (but that's a seperate talk).

- Running tests-pre commit doesn't scale to a large team / large
number of tests
Continuous Deployment?

- If you're doing a Continuous Deployment System, commit
hooks are a simple but critical element of your system...

    If your build system is red, you must prevent commits.




- Passphrase to commit through this:
   "Fixing buildbot"
Locking Down Sensitive Areas

- Some areas of your architechture are incredibly sensitive (e.
g., low-level database/memcache libraries, payment
processing, )

- Try to use this sparingly (make a blanklist of files, not a
whitelist) since it breaks workflow and halts engineer velocity to
have to verify everything on every file.




- Passphrase to commit through this:
   "Reviewed by XXX" where XXX is the name of an engineer.
Enforcing Social Policies

- You can enforce a Code Freeze during weekends and
holidays to prevent committing to the main code line when
there's minimal staff around.

  - Lowers stress for ops during off-hours

  - Engineers can commit to branches during the weekend to
work unimpeded
Enforcing Social Policies

- We found this useful at IMVU for years, but refinement of our
entire Continuous Deployment system has recently made us
reconsider preventing weekend commits, so we changed policy!

- Passphrase to commit on weekends is a password that is listed on
the internal wiki for the weekend commit policy. The passphrase is
changed every time we change the policy.

- The rejection error message itself contains the link to the wiki
page with the current policy and the passphrase.
The Shibboleth of Saving Face

Problem:
Sometimes you are proving something to yourself in your
sandbox (noodling through a problem) and you don't want this
code going out.

Sometimes you forget and commit that code.

Solution:
A commit hook checking for the phrase "DO NOT COMMIT".

Now when you noodle, you start it with that phrase in a
comment and your ass is covered.

Override: "Ignore Do Not Commit"
Passive Education

Not strictly "coding conventions" in the traditional sense, but
good habits based on context.

For example:
$_REQUEST bad in PHP, $_GET/$_POST are good...

...so forbid access of $_REQUEST and link to a webpage that
explains why, and provide a short blurb of how to correct the
issue.


For these types of rules, no specific "skip this" message
Further reading (yay homework)


- http://wordaligned.org/articles/a-subversion-pre-commit-hook
  General how-to for SVN pre-commit hooks

- http://book.git-scm.com/5_git_hooks.html
  General how-to for all sorts of GIT commit hooks

- http://eng.wealthfront.com/2011/03/corrective-action-with-gits-
pre-commit.html
  Corrective Actions with GIT commit hooks

- http://engineering.imvu.com/
  We do cool things, and talk about workflow/build systems a lot
Questions?




Ben McGraw - @bengrue

More Related Content

What's hot

Fall 2011 PHP Class - Session 1
Fall 2011 PHP Class - Session 1Fall 2011 PHP Class - Session 1
Fall 2011 PHP Class - Session 1
jimbojsb
 
A quick guide to some prominent Ruby on Rails learning materials
A quick guide to some prominent Ruby on Rails learning materialsA quick guide to some prominent Ruby on Rails learning materials
A quick guide to some prominent Ruby on Rails learning materials
Thomas Elam
 

What's hot (8)

Comments: Why not What
Comments: Why not WhatComments: Why not What
Comments: Why not What
 
Fall 2011 PHP Class - Session 1
Fall 2011 PHP Class - Session 1Fall 2011 PHP Class - Session 1
Fall 2011 PHP Class - Session 1
 
Best Practices For Writing Super Readable Code
Best Practices For Writing Super Readable CodeBest Practices For Writing Super Readable Code
Best Practices For Writing Super Readable Code
 
A quick guide to some prominent Ruby on Rails learning materials
A quick guide to some prominent Ruby on Rails learning materialsA quick guide to some prominent Ruby on Rails learning materials
A quick guide to some prominent Ruby on Rails learning materials
 
2019 PHP Serbia - Boosting your performance with Blackfire
2019 PHP Serbia - Boosting your performance with Blackfire2019 PHP Serbia - Boosting your performance with Blackfire
2019 PHP Serbia - Boosting your performance with Blackfire
 
Developing The Web
Developing The WebDeveloping The Web
Developing The Web
 
OOP paradigm, principles of good design and architecture of Java applications
OOP paradigm, principles of good design and architecture of Java applicationsOOP paradigm, principles of good design and architecture of Java applications
OOP paradigm, principles of good design and architecture of Java applications
 
Java 8, the Good, the Bad and the Ugly
Java 8, the Good, the Bad and the UglyJava 8, the Good, the Bad and the Ugly
Java 8, the Good, the Bad and the Ugly
 

Similar to Commit Hooks: the Subtle Hammer

Chelberg ptcuser 2010
Chelberg ptcuser 2010Chelberg ptcuser 2010
Chelberg ptcuser 2010
Clay Helberg
 
An Introduction to unit testing
An Introduction to unit testingAn Introduction to unit testing
An Introduction to unit testing
Steven Casey
 
DevOps Days Vancouver 2014 Slides
DevOps Days Vancouver 2014 SlidesDevOps Days Vancouver 2014 Slides
DevOps Days Vancouver 2014 Slides
Alex Cruise
 
What Are We Still Doing Wrong
What Are We Still Doing WrongWhat Are We Still Doing Wrong
What Are We Still Doing Wrong
afa reg
 
Dev buchan 30 proven tips
Dev buchan 30 proven tipsDev buchan 30 proven tips
Dev buchan 30 proven tips
Bill Buchan
 

Similar to Commit Hooks: the Subtle Hammer (20)

Chelberg ptcuser 2010
Chelberg ptcuser 2010Chelberg ptcuser 2010
Chelberg ptcuser 2010
 
Writing code for people
Writing code for peopleWriting code for people
Writing code for people
 
debugging (1).ppt
debugging (1).pptdebugging (1).ppt
debugging (1).ppt
 
An important characteristic of a test suite that is computed by a dynamic ana...
An important characteristic of a test suite that is computed by a dynamic ana...An important characteristic of a test suite that is computed by a dynamic ana...
An important characteristic of a test suite that is computed by a dynamic ana...
 
Testing 101
Testing 101Testing 101
Testing 101
 
Workshop - The Little Pattern That Could.pdf
Workshop - The Little Pattern That Could.pdfWorkshop - The Little Pattern That Could.pdf
Workshop - The Little Pattern That Could.pdf
 
Guide to Destroying Codebases The Demise of Clever Code
Guide to Destroying Codebases   The Demise of Clever CodeGuide to Destroying Codebases   The Demise of Clever Code
Guide to Destroying Codebases The Demise of Clever Code
 
ITARC15 Workshop - Architecting a Large Software Project - Lessons Learned
ITARC15 Workshop - Architecting a Large Software Project - Lessons LearnedITARC15 Workshop - Architecting a Large Software Project - Lessons Learned
ITARC15 Workshop - Architecting a Large Software Project - Lessons Learned
 
Software Security : From school to reality and back!
Software Security : From school to reality and back!Software Security : From school to reality and back!
Software Security : From school to reality and back!
 
An Introduction to unit testing
An Introduction to unit testingAn Introduction to unit testing
An Introduction to unit testing
 
Topic production code
Topic production codeTopic production code
Topic production code
 
Performant Django - Ara Anjargolian
Performant Django - Ara AnjargolianPerformant Django - Ara Anjargolian
Performant Django - Ara Anjargolian
 
C++ Restrictions for Game Programming.
C++ Restrictions for Game Programming.C++ Restrictions for Game Programming.
C++ Restrictions for Game Programming.
 
DevOps Days Vancouver 2014 Slides
DevOps Days Vancouver 2014 SlidesDevOps Days Vancouver 2014 Slides
DevOps Days Vancouver 2014 Slides
 
Git Makes Me Angry Inside
Git Makes Me Angry InsideGit Makes Me Angry Inside
Git Makes Me Angry Inside
 
Architecting a Large Software Project - Lessons Learned
Architecting a Large Software Project - Lessons LearnedArchitecting a Large Software Project - Lessons Learned
Architecting a Large Software Project - Lessons Learned
 
Debugging
DebuggingDebugging
Debugging
 
Medium Trust for Umbraco
Medium Trust for UmbracoMedium Trust for Umbraco
Medium Trust for Umbraco
 
What Are We Still Doing Wrong
What Are We Still Doing WrongWhat Are We Still Doing Wrong
What Are We Still Doing Wrong
 
Dev buchan 30 proven tips
Dev buchan 30 proven tipsDev buchan 30 proven tips
Dev buchan 30 proven tips
 

Recently uploaded

Recently uploaded (20)

MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 

Commit Hooks: the Subtle Hammer

  • 1. You're Doing it Wrong: Commit Hooks! or Commit Hooks: the Subtle Hammer Ben McGraw Sr. Software Engineer, IMVU Inc. ben@imvu.com @bengrue
  • 2. What are commit hooks? A way to run automated actions before your local changes get committed to your code repository.
  • 3. What are commit hooks? A way to run automated actions before your local changes get committed to your code repository. A way to make the computer check all those annoying little repetitive tasks before your code goes on your permanent record.
  • 4. Computers are good at repetition. (People are bad at repetition... and good about forgetting little things!) Make the computer do the repetitive, boring stuff so you can focus on the interesting code.
  • 5. How do I make them? In SVN: as the SVN user, go to /PATH_TO_REPO/hooks/ and make an executable script file in your favorite shell scripting language. In GIT: Basically the same... but there's a lot more options/specific hooks available (see link at the end).
  • 6. How do I make them? The basic principle is the same in both: 1. The script receives text about the files you're attempting to commit. 2. You inspect those files for rules you make yourself. 3. If you want the commit to not get through to the repository, return an error code (and print out an error message to STDERR). 4. Otherwise, your commit continues on to the repository.
  • 7. Best Practices 1. Subdivide each individual check into it's own function. 2. Give each individual check it's own passphrase to skip it.
  • 8. What can you use them to do?
  • 9. Enforce Coding Conventions - Prevent Code that doesn't allow Linting (Static Analysis!) - Enforcing Company/Project-specific coding style guidelines - For example, IMVU has commit hooks to prevent: - tabs from being committed - leading-underscored ('lazy private') functions from being used outside of a module. - using a module's name in a function (e.g., customer::get() is good, customer::get_customer() is bad.) - Passphrase to skip is per type ("Skip whitespace check.", "Skip module check.", etc... )
  • 10. Running tests as part of your workflow - One of the simplest way to integrate test-running into a coding workflow - If you want acceptance tests, and you don't prevent commits upon failure, you're Doing It Wrong (but that's a seperate talk). - Running tests-pre commit doesn't scale to a large team / large number of tests
  • 11. Continuous Deployment? - If you're doing a Continuous Deployment System, commit hooks are a simple but critical element of your system... If your build system is red, you must prevent commits. - Passphrase to commit through this: "Fixing buildbot"
  • 12. Locking Down Sensitive Areas - Some areas of your architechture are incredibly sensitive (e. g., low-level database/memcache libraries, payment processing, ) - Try to use this sparingly (make a blanklist of files, not a whitelist) since it breaks workflow and halts engineer velocity to have to verify everything on every file. - Passphrase to commit through this: "Reviewed by XXX" where XXX is the name of an engineer.
  • 13. Enforcing Social Policies - You can enforce a Code Freeze during weekends and holidays to prevent committing to the main code line when there's minimal staff around. - Lowers stress for ops during off-hours - Engineers can commit to branches during the weekend to work unimpeded
  • 14. Enforcing Social Policies - We found this useful at IMVU for years, but refinement of our entire Continuous Deployment system has recently made us reconsider preventing weekend commits, so we changed policy! - Passphrase to commit on weekends is a password that is listed on the internal wiki for the weekend commit policy. The passphrase is changed every time we change the policy. - The rejection error message itself contains the link to the wiki page with the current policy and the passphrase.
  • 15. The Shibboleth of Saving Face Problem: Sometimes you are proving something to yourself in your sandbox (noodling through a problem) and you don't want this code going out. Sometimes you forget and commit that code. Solution: A commit hook checking for the phrase "DO NOT COMMIT". Now when you noodle, you start it with that phrase in a comment and your ass is covered. Override: "Ignore Do Not Commit"
  • 16. Passive Education Not strictly "coding conventions" in the traditional sense, but good habits based on context. For example: $_REQUEST bad in PHP, $_GET/$_POST are good... ...so forbid access of $_REQUEST and link to a webpage that explains why, and provide a short blurb of how to correct the issue. For these types of rules, no specific "skip this" message
  • 17. Further reading (yay homework) - http://wordaligned.org/articles/a-subversion-pre-commit-hook General how-to for SVN pre-commit hooks - http://book.git-scm.com/5_git_hooks.html General how-to for all sorts of GIT commit hooks - http://eng.wealthfront.com/2011/03/corrective-action-with-gits- pre-commit.html Corrective Actions with GIT commit hooks - http://engineering.imvu.com/ We do cool things, and talk about workflow/build systems a lot