{"id":1260,"date":"2017-07-05T19:04:29","date_gmt":"2017-07-05T18:04:29","guid":{"rendered":"http:\/\/martinjoiner.co.uk\/blog\/?p=1260"},"modified":"2017-07-13T21:15:20","modified_gmt":"2017-07-13T20:15:20","slug":"a-pre-commit-hook-to-block-tab-indented-code","status":"publish","type":"post","link":"https:\/\/martinjoiner.co.uk\/blog\/2017\/07\/05\/a-pre-commit-hook-to-block-tab-indented-code\/","title":{"rendered":"A pre-commit hook to block tab-indented code"},"content":{"rendered":"<p><a href=\"http:\/\/martinjoiner.co.uk\/blog\/wp-content\/uploads\/2017\/07\/Terminal-Screen-Shot.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1261\" src=\"http:\/\/martinjoiner.co.uk\/blog\/wp-content\/uploads\/2017\/07\/Terminal-Screen-Shot.png\" alt=\"\" width=\"724\" height=\"549\" srcset=\"https:\/\/martinjoiner.co.uk\/blog\/wp-content\/uploads\/2017\/07\/Terminal-Screen-Shot.png 724w, https:\/\/martinjoiner.co.uk\/blog\/wp-content\/uploads\/2017\/07\/Terminal-Screen-Shot-300x227.png 300w, https:\/\/martinjoiner.co.uk\/blog\/wp-content\/uploads\/2017\/07\/Terminal-Screen-Shot-400x303.png 400w\" sizes=\"auto, (max-width: 724px) 100vw, 724px\" \/><\/a><\/p>\n<p>I work in a team who use a mix of Windows and Mac machines to review and edit code. In fact I myself use a mix, coding on\u00a0an iMac in the office but a Windows machine at home. This scenario seems to be becoming more and more common these days and with the distributed nature of open source projects it's almost guaranteed code will travel across platforms during it's life.<\/p>\n<p>Avoiding tab-indentation is the best way to ensure code looks\u00a0the same on all code editors\/viewers including Mac, Windows, Vim and the GitHub website. It's just sensible and courteous to try and ensure code is visually formatted in a consistent manner for all contributors. Tabs are not consistent. Use spaces.<\/p>\n<p>So now that's settled lets move on to how to enforce that policy on a day-to-day basis...<\/p>\n<p>Switching your code editor to indent using spaces by default goes a long way. But if you are managing\u00a0legacy code - written before you decided to set a standard - you may be opening files with hundreds of lines of tab-indented code but wanting your <em>new<\/em> lines to be indented with spaces. If you hit \"Return\" at the end of an indented line, most code editors will try and be friendly by auto-completing to whatever the previous line uses so even with your code editor defaulting to use spaces you can easily end up accidentally generating some tab-indented lines. It's clear we need a guardian to watch over us and catch these mistakes!<\/p>\n<p>So I made a Git pre-commit hook that lints (checks) your newly added lines\u00a0(<strong>only<\/strong> the <em>new<\/em>\u00a0lines) for tab indentation and stops the commit if it finds any, printing a handy report to the command line when it does. Download the hook at\u00a0<a href=\"https:\/\/github.com\/martinjoiner\/portable-code-pre-commit-hook\">https:\/\/github.com\/martinjoiner\/portable-code-pre-commit-hook<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Avoiding tab-indentation is the best way to ensure code looks the same on all code editors\/viewers including Mac, Windows, Vim and the GitHub website. I made a Git pre-commit hook that lints your newly added lines for tab indentation and stops the commit if it finds any.<\/p>\n","protected":false},"author":1,"featured_media":1261,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[75],"tags":[131],"class_list":["post-1260","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-software","tag-git"],"_links":{"self":[{"href":"https:\/\/martinjoiner.co.uk\/blog\/wp-json\/wp\/v2\/posts\/1260","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/martinjoiner.co.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/martinjoiner.co.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/martinjoiner.co.uk\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/martinjoiner.co.uk\/blog\/wp-json\/wp\/v2\/comments?post=1260"}],"version-history":[{"count":3,"href":"https:\/\/martinjoiner.co.uk\/blog\/wp-json\/wp\/v2\/posts\/1260\/revisions"}],"predecessor-version":[{"id":1266,"href":"https:\/\/martinjoiner.co.uk\/blog\/wp-json\/wp\/v2\/posts\/1260\/revisions\/1266"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/martinjoiner.co.uk\/blog\/wp-json\/wp\/v2\/media\/1261"}],"wp:attachment":[{"href":"https:\/\/martinjoiner.co.uk\/blog\/wp-json\/wp\/v2\/media?parent=1260"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/martinjoiner.co.uk\/blog\/wp-json\/wp\/v2\/categories?post=1260"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/martinjoiner.co.uk\/blog\/wp-json\/wp\/v2\/tags?post=1260"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}