{"id":5701,"date":"2019-11-06T11:51:19","date_gmt":"2019-11-06T16:51:19","guid":{"rendered":"https:\/\/www.rushworth.us\/lisa\/?p=5701"},"modified":"2019-11-06T11:51:19","modified_gmt":"2019-11-06T16:51:19","slug":"preventing-erronious-use-of-the-master-branch-on-development-servers","status":"publish","type":"post","link":"https:\/\/www.rushworth.us\/lisa\/?p=5701","title":{"rendered":"Preventing erronious use of the master branch on development servers"},"content":{"rendered":"<p>One of the web servers at work uses a refspec in the &#8220;git pull&#8221; command to map the remote development branch to the local remote-tracking master branch. This is fairly confusing (and it <em>looks<\/em> like the dev server is <em>using<\/em> the master branch unless you dig into how the pull is performed), but I can see how this prevents someone from accidentally typing something like &#8220;git checkout master&#8221; and really messing up the development environment. I can also see a dozen ways someone can issue what is a completely reasonable git command 99% of the time and really mess up the development environment.<\/p>\n<p>While it is simple enough to just checkout the development branch, doing so <em>does<\/em> open us up to the possibility that someone will erroneously\u00a0 deliver the production code to the development server and halt all testing. While you cannot create shell aliases for multi-word commands (or, more accurately, alias expansion is performed for the <a href=\"https:\/\/ss64.com\/bash\/alias.html\" target=\"_blank\" rel=\"noopener noreferrer\">first word of a simple command<\/a> is checked to see if it has an alias &#8230; so you&#8217;ll never <em>get<\/em> the multi-word command), you can define a function to intercept git commands and avoid running unwanted commands:<\/p>\n<pre>function git() { \r\n     case $* in \r\n         \"checkout master\" ) command echo \"This is a dev server, do not checkout the master branch!\" ;; \r\n         \"pull origin master\" ) command echo \"This is a dev server, do not pull the master branch\" ;; \r\n         * ) command git \"$@\" ;; \r\n     esac\r\n}<\/pre>\n<p>Or define the desired commands and avoid running any others:<\/p>\n<pre>function git(){\r\n     if echo \"$@\" | grep -Eq '^checkout uat$'; then\r\n          command git $@\r\n     elif echo \"$@\" | grep -Eq '^pull .+ uat$'; then\r\n          command git $@\r\n     else\r\n          echo \"The command $@ needs to be whitelisted before it can be run\"\r\n     fi\r\n}<\/pre>\n<p>Either approach mitigates the risk of someone incorrectly using the master branch on the development server.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the web servers at work uses a refspec in the &#8220;git pull&#8221; command to map the remote development branch to the local remote-tracking master branch. This is fairly confusing (and it looks like the dev server is using the master branch unless you dig into how the pull is performed), but I can &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[33,30],"tags":[632,233],"class_list":["post-5701","post","type-post","status-publish","format-standard","hentry","category-coding","category-system-administration","tag-bash","tag-git"],"_links":{"self":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/5701","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5701"}],"version-history":[{"count":1,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/5701\/revisions"}],"predecessor-version":[{"id":5702,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/5701\/revisions\/5702"}],"wp:attachment":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5701"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5701"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5701"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}