How to embed an updated git-hash into Version.hpp?

亡梦爱人 提交于 2019-12-03 20:07:15

To address my original problem: How to make git ignore my file regardless of branching?

  1. git rm cpp/inc/core/util/VERSION.hpp
  2. add cpp/inc/core/util/VERSION.hpp to .gitignore

To address: How to embed an updated git-hash into Version.hpp?

Part of the issue is a chicken-egg problem where you cannot know your hash until all files are checked in, but you cannot check in your updated VERSION.hpp until you know your git-hash. So my solution was to leave VERSION.hpp out of repository and in the .gitignore file.

Keep in mind that the purpose of embedding VERSION.hpp into the executable: if a nasty bug shows up in production, with the git-hash in the binary, we will know what code to checkout of git so that we can debug the code properly.

Here are the steps I took in addition to the two steps above:

  1. Write a short ruby script which does two things, depending on whether the local area is dirty or not (if git diff --shortstat shows more than 0 lines, you have a dirty area). git rev-list HEAD | sed -n '1p' | cut -c1-10 will give you the first 10 characters as the git-hash. If your local area is dirty, this is the LAST HEAD's git-hash (I embed the string LAST inside VERSION.hpp). If this is clean, it is your true current git-hash.
  2. The ruby script will check the result in 1. vs the actual VERSION.hpp file. If the result has changed, then write the new result into VERSION.hpp.
  3. Modify my .bashrc, adding two small aliases - cmakerel and cmakedbg for calling cmake in my local and release directories, but before I do I cmake and make in the alias, I call the ruby script in 1. to update my VERSION.hpp if necessary.

If I call cmakerel and cmakedbg after git checkout and if I don't modify the code before the build (which I shouldn't be doing for an actual release anyways), I will have the VERSION.hpp I need with all the proper information embedded.

NOTE An alternative solution to modifying .bashrc with the aliases is to use git's post-checkout hook (in theory). However, I couldn't get this to work and I had spent too much time on this issue already, but you may have better luck.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!