{"id":649,"date":"2015-07-24T14:31:36","date_gmt":"2015-07-24T12:31:36","guid":{"rendered":"https:\/\/www.yelloworb.com\/orbblog\/?p=649"},"modified":"2015-07-24T14:34:55","modified_gmt":"2015-07-24T12:34:55","slug":"automatic-versioning-information-in-git-projects","status":"publish","type":"post","link":"https:\/\/www.yelloworb.com\/orbblog\/automatic-versioning-information-in-git-projects\/","title":{"rendered":"Automatic versioning information in Git projects"},"content":{"rendered":"<p>When developing software using several different libraries etc it is always good to know which version of which library is used in a certain installation etc. Though there is no really easy way in Git to solve this as in CVS for instance with its <a href=\"http:\/\/cvsman.com\/cvs-1.12.12\/cvs_100.php\" target=\"_blank\">keywords<\/a>.<\/p>\n<p>Though the following trick creates a nice way of keeping track.<\/p>\n<p>The concept is to utilise <a href=\"https:\/\/git-scm.com\/book\/en\/v2\/Customizing-Git-Git-Hooks\" target=\"_blank\">GIT Hooks<\/a> and that we write a script that generates a file with the versioning information in it and then our build system integrates this with the code. The following example is made using C but should be easy to adopt to other languages.<\/p>\n<p>Create and edit the file <samp>.git\/hooks\/post-commit<\/samp> to look like this:<\/p>\n<pre><code class=\"\" data-line=\"\">\n#!\/bin\/sh\n#\nversion=$(git describe --tags --long)\necho &quot;#define LIBRARY_NAME_VERSION \\&quot;&quot;$version&quot;\\&quot;&quot; &gt; .\/version.h\n<\/code><\/pre>\n<p>Make the script executable:<\/p>\n<pre>chmod a+x .git\/hooks\/post-commit<\/pre>\n<p>For this to work though you must have made a tag, see <a href=\"https:\/\/git-scm.com\/book\/en\/v2\/Git-Basics-Tagging\" target=\"_blank\">GIT Tagging<\/a>. Once your repository is tagged you can test the script:<\/p>\n<pre>\r\n.git\/hooks\/post-commit\r\nmore version.h<\/pre>\n<p>You should have got something like this:<\/p>\n<pre><code class=\"\" data-line=\"\">\n#define LIBRARY_NAME_VERSION &quot;v0.4-0-g76ed9ef&quot;\n<\/code><\/pre>\n<p>Now you can use this in your code for instance to output versioning information at startup.<\/p>\n<p>And finally add version.h to your GIT ignore file or you will have a continuous flow of commits to include the latest change of this file, since it will change after each commit \ud83d\ude42<\/p>\n<pre>\r\necho version.h >> .gitignore\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>When developing software using several different libraries etc it is always good to know which version of which library is used in a certain installation etc. Though there is no really easy way in Git to solve this as in CVS for instance with its keywords. Though the following trick creates a nice way of [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","footnotes":""},"categories":[28],"tags":[],"class_list":["post-649","post","type-post","status-publish","format-standard","hentry","category-software-development"],"_links":{"self":[{"href":"https:\/\/www.yelloworb.com\/orbblog\/wp-json\/wp\/v2\/posts\/649","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yelloworb.com\/orbblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yelloworb.com\/orbblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yelloworb.com\/orbblog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yelloworb.com\/orbblog\/wp-json\/wp\/v2\/comments?post=649"}],"version-history":[{"count":10,"href":"https:\/\/www.yelloworb.com\/orbblog\/wp-json\/wp\/v2\/posts\/649\/revisions"}],"predecessor-version":[{"id":659,"href":"https:\/\/www.yelloworb.com\/orbblog\/wp-json\/wp\/v2\/posts\/649\/revisions\/659"}],"wp:attachment":[{"href":"https:\/\/www.yelloworb.com\/orbblog\/wp-json\/wp\/v2\/media?parent=649"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yelloworb.com\/orbblog\/wp-json\/wp\/v2\/categories?post=649"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yelloworb.com\/orbblog\/wp-json\/wp\/v2\/tags?post=649"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}