Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Version Control - Tips, tricks & good citizenship

Tess Barnes
February 18, 2016

Version Control - Tips, tricks & good citizenship

Are you nervous of version control even if you don't admit it? Have things got harder since your team expanded? Don’t panic! you’re not on your own. I've put together a run down of some of my tricks, tips and ‘good citizen’ techniques to avoid the worst kinds of conflicts and deal with the remaining ones as efficiently as possible. Join me on a journey through some of the causes of version control pain after which we will visit some social and technical solutions. I'll lead you via branching policies, basic conflict resolution, to more tricky conflict management. We will spend time with some of the lesser known features of version control and command line hacks to stay in charge of multiple branches. As a bonus we will discover how version control can be integrated with other tools having many positive knock on effects for your team and wider business: How forcing commit message formats and automating your change log can help your release process or apps to streamline code reviews can avoid some unnecessary context switching. Triggered notification of interested parties helps keep everyone in the loop especially for breaking changes.

Tess Barnes

February 18, 2016
Tweet

More Decks by Tess Barnes

Other Decks in Programming

Transcript

  1. Tess Barnes CC BY-ND 4.0 February 2016 Tess Barnes •

    first website in html3 • multi language developer • agile advocate • works for Fasthosts • newbie speaker
 https://joind.in/talk/ac52d • @crataegustess
  2. Tess Barnes CC BY-ND 4.0 February 2016 Basic levels •

    text conflicts • tree conflicts • avoiding conflicts • more complex codebases
  3. Tess Barnes CC BY-ND 4.0 February 2016 Bonus levels •

    keep it secret, keep it safe • collaboration and review • clean code and standards • change logs for the lazy • keeping everyone in the loop • triggering other automatons
  4. Tess Barnes CC BY-ND 4.0 February 2016 Minor conflict resolution

    • know your tools ✦ command line ✦ gui, shortcut menus, visual diff • know your code • talk to your colleagues
  5. Tess Barnes CC BY-ND 4.0 February 2016 Interleaved causes •

    two developers work on the same file at the same time • both replace the same old method • “badly” scheduled work • “no” design • “lack of” communication
  6. Tess Barnes CC BY-ND 4.0 February 2016 Add conflicts trying

    to add a file that already exists Add
  7. Tess Barnes CC BY-ND 4.0 February 2016 these are cleavers

    [2] Cleaver.php Properties: isSharp weightInGram bladeLengthInInch Methods: sharpen($on){} cut($something){}
  8. Tess Barnes February 2016 CC BY-ND 4.0 Mac:wcAdd2 tess$ vi

    addThis.php Mac:wcAdd2 tess$ svn add * A Cleavers.php Mac:wcAdd2 tess$ svn update Updating '.': Conflict discovered in '/Repos/svnTest/Add2/Cleavers.php'. Select: (p) postpone, (df) diff-full, (e) edit, (mc) mine-conflict, (tc) theirs-conflict, (s) show all options: Add conflicts
  9. Tess Barnes CC BY-ND 4.0 February 2016 Fixing add conflicts

    • pre-empt; check the svn log before you update • postpone; many tools treat this as a text conflict • check the meaning; rename your file • collaborate; this could be a duplicate
  10. Tess Barnes February 2016 CC BY-ND 4.0 Should you fix?

    just because you can doesn’t always mean you should (on your own)
  11. Tess Barnes CC BY-ND 4.0 February 2016 these are cleavers…

    … meat cleavers these are also cleavers… … following a cause
  12. Tess Barnes CC BY-ND 4.0 February 2016 Delete conflicts trying

    to edit a deleted file or delete an edited file Delete
  13. Tess Barnes February 2016 CC BY-ND 4.0 Mac:wcDel1 tess$ vi

    deletedFile.py Mac:wcDel1 tess$ svn update {…} Mac:wcDel1 tess$ svn status A + C deletedFile.py > local edit, incoming delete upon update Summary of conflicts: Tree conflicts: 1 ——————————————————————— D C editedFile.py > local delete, incoming edit upon update delete conflicts
  14. Tess Barnes CC BY-ND 4.0 February 2016 Fixing delete conflicts

    • someone has deleted that nice file you fixed so beautifully… ✦ so find them and talk - it could be just a rename! ✦ renames can sometimes be solved by patch file ✦ massive refactors? talk, collaborate, hand copy ✦ feature removal? don't get frustrated, get coffee
  15. Tess Barnes CC BY-ND 4.0 February 2016 Missing files trying

    to apply change to non existent file Missing
  16. Tess Barnes CC BY-ND 4.0 February 2016 Avoiding missing files

    • depends on branching / merging strategy • always merge up to clean working copy • useful commit messages please • merging back down becomes so much easier • … and so do code reviews!
  17. Tess Barnes CC BY-ND 4.0 February 2016 here’s why •

    lots of developers • lots of overlapping work • context swapping • impossible to talk 
 all the time • we refactor a lot that’s me in the dotty scarf [4]
  18. Tess Barnes CC BY-ND 4.0 February 2016 Get out of

    jail free • patch files • multiple checkouts • stash your work in progress • cherry pick commits • use a standard e.g. PSR2, PSR3
  19. Tess Barnes CC BY-ND 4.0 February 2016 Why just fix?

    • clean up your code base: 
 be a good code scout • be prepared • solve root causes • have a strategy • get the most out of your
 chosen tools
  20. Tess Barnes CC BY-ND 4.0 February 2016 VC tree bonsai

    • principles • branching strategies • complexity keep it looking beautiful [5]
  21. Tess Barnes CC BY-ND 4.0 February 2016 Principles • it’s

    a tree! • check out 
 only what you need • meaningful names • keep it clean • keep it current
  22. Tess Barnes CC BY-ND 4.0 February 2016 Branching strategy •

    how do components interact? what is common? • how do we code review? • what do we test? where and when? • how do we release? • apocalypse plan (hot fix management)
  23. Tess Barnes CC BY-ND 4.0 February 2016 Coping with complexity

    One script to rule them all • quick, efficient, lazy - what’s not to like? • handle complex project checkouts • fewer missed changes on check in • look ahead _before_ updating
  24. Tess Barnes February 2016 CC BY-ND 4.0 Checkout (parameterise for

    branches) + use for update: svn co http://your.repository/component/trunk C:\WCopy \Component svn co http://your.repository/common/dependent1/trunk \ C:\WCopy\lib\dependent1 Check Modifications: svn status C:\WCopy\Component svn status C:\WCopy\lib\dependent1 View commit history: svn log --limit 10 C:\WCopy\Component svn log -l 10 C:\WCopy\lib\dependent1 Script Examples
  25. Tess Barnes February 2016 CC BY-ND 4.0 Getting more out

    of your version control bonus levels…
  26. Tess Barnes CC BY-ND 4.0 February 2016 Bonus levels •

    keep it secret, keep it safe • collaboration and review • clean code and standards • change logs for the lazy • keeping everyone in the loop • triggering other automatons
  27. Tess Barnes CC BY-ND 4.0 February 2016 What is a

    hook? • insertion point for custom code ✦ before the commit happens ✦ after the commit • server side or client side • centralised or pseudo-distributed systems
  28. Tess Barnes CC BY-ND 4.0 February 2016 Get hooked •

    enforce authorisation • enforce code review • enforce standards • create change logs easily
  29. Tess Barnes CC BY-ND 4.0 February 2016 Why enforce authorisation

    • do you want the world to read everything? • are there more sensitive components? • do you want only certain people to commit? • how often or urgently will this change?
  30. Tess Barnes CC BY-ND 4.0 February 2016 Authorisation solutions ★

    apache • mod_authz_svn + rules file ★ svnserve • svnserve.conf entry -> rules file both support LDAP
  31. Tess Barnes CC BY-ND 4.0 February 2016 What if you

    use git? ★ git lab • Cloud or EE or CE on linux ★ bit bucket • EE or Cloud or Academic licence self serve is compatible with LDAP
  32. Tess Barnes CC BY-ND 4.0 February 2016 Why use tools

    with this? • get a window into the code ✦ no more switching context • visibility of reviews to everyone ✦ multiple reviewers • protect your master repository
  33. Tess Barnes CC BY-ND 4.0 February 2016 How do we

    automate? • roll your own • enforce via repository access • automate the basics: compile or run php -f -l • use an IDE or at least get good plugins
  34. Tess Barnes CC BY-ND 4.0 February 2016 Getting outside help

    • handle merge requests ✦ gitlab, bitbucket, git flow, gitlab flow • gateway software ✦ gerrit
  35. Tess Barnes CC BY-ND 4.0 February 2016 Enforcing standards •

    php code sniffer / stylecop / python compile • execute as client side pre commit hook • use built in phpcs-svn-pre-commit for server side hook • refuse commit if changes don’t pass • give grace with a threshold
  36. Tess Barnes CC BY-ND 4.0 February 2016 TortoiseSVN example •

    php code sniffer
 on working copy • check current directory or check changed list to be more efficient • Not ideal: client side hooks rely on the developer environment
  37. Tess Barnes February 2016 CC BY-ND 4.0 Why reinvent the

    wheel? Use php code sniffer’s built in pre commit hook script in your own pre commit hook file: /path/to/PHP_CodeSniffer/scripts/phpcs-svn-pre-commit \ --standard=PSR2 "$REPOS" -t "$TXN" >&2 || exit 1 This builds on svnlook changed and svnlook cat to check the latest version of the files, ignoring files being deleted To add a tolerance, amend the phpcs-svn-pre-commit file: $tolerance = 5; $numErrors = $phpcs->process(); if ($numErrors > $tolerance) { exit(1); } More info: PHP_CodeSniffer - wiki - Using-the-SVN-pre-commit-Hook server hook example
  38. Tess Barnes CC BY-ND 4.0 February 2016 Lazy change logs

    • Pre commit: Enforce commit message patterns ✦ makes things clearer for code review ✦ encourages user facing comments • Post commit: Scrape the log for publishable data ✦ allow hot fix & merge to be treated differently ✦ combine logs from multiple sources
  39. Tess Barnes February 2016 CC BY-ND 4.0 Check min length

    Pattern match Validate commit messages Allow for special tags: 3rd party, merge, hotfix
  40. Tess Barnes CC BY-ND 4.0 February 2016 Publish those logs

    • only add the latest e.g. by date or last 100 • on post commit hook or regular cron job • ftp upload with rss / atom feed? • rabbitMQ / zeroMQ pub sub? • email notification?
  41. Tess Barnes CC BY-ND 4.0 February 2016 Keep everyone in

    the loop • can you script it?
 you can post commit it • keep it specific • avoid duplication • keep it relevant • beware of performance
  42. Tess Barnes CC BY-ND 4.0 February 2016 Start a revolution

    • continuous build • deployment 
 to test servers • automated testing
  43. Tess Barnes CC BY-ND 4.0 February 2016 In summary: do…

    • get comfortable with the basics • talk to each other • care about your code • consider how version control fits in • get the most out of it
  44. Tess Barnes CC BY-ND 4.0 February 2016 Still hungry? If

    you want to look deeper check out these references for further info… ✦ git book: http://git-scm.com/book/en/v2 ✦ svn book: http://svnbook.red-bean.com/en/1.7/index.html ✦ php code scouts: http://phpboyscout.uk/php-scout/ ✦ php sniffer: https://github.com/squizlabs/PHP_CodeSniffer ✦ PSR: http://www.php-fig.org/psr/psr-2/ (the PSR-2 rundown),
 http://code.tutsplus.com/tutorials/psr-huh--net-29314 (older but friendly) ✦ useful commit hooks (svn):
 http://blog.grimsy.net/2008/07/a-few-svn-pre-commit-hooks/
  45. Tess Barnes CC BY-ND 4.0 February 2016 Popular links For

    more about tools to do things the easy way… ✦ TortoiseSVN: https://tortoisesvn.net ✦ GitHub: https://github.com ✦ GitLabFlow: http://doc.gitlab.com/ee/workflow/gitlab_flow.html ✦ Bitbucket: https://bitbucket.org/product/features ✦ Gerrit: https://www.gerritcodereview.com ✦ Top ten Sublime plugins (blog): http://neverstopbuilding.com/sublime-plugins-for-php ✦ Fossil: http://fossil-scm.org/index.html/doc/trunk/www/index.wiki
  46. Tess Barnes CC BY-ND 4.0 February 2016 Credit where it’s

    due The media in these slides are my own or credited where references can be found. [1] photo credit (CC BY 2.0) Evil Erin https://www.flickr.com/photos/evilerin/3078856253 [2] image from Wikipedia https://en.wikipedia.org/wiki/Cleaver [3] image from Old English Hexateuch https://en.wikipedia.org/wiki/Old_English_Hexateuch [4] photo credit (CC BY 2.0) JD Hancock https://www.flickr.com/photos/jdhancock/8755184025 [5] photo credit (CC BY-ND 2.0) Beverly Vealach https://www.flickr.com/photos/cowtools/293244244 happy kitten - first seen on Dave Paquettes fun article zephod (Adam Pope as Zephod) (CC BY 3.0) https://commons.wikimedia.org/wiki/ File:Adam_Pope_Zaphod_Beeblebrox.JPG don’t panic - photo credit (CC BY-SA 2.0) Sarabbit https://www.flickr.com/photos/sarabbit/4549185468 Please bear this in mind when referencing or reusing any material. CC BY-ND 4.0 licence details
  47. Tess Barnes February 2016 CC BY-ND 4.0 Thanks for listening

    Any Questions? Feedback: https://joind.in/talk/ac52d