如何嵌入一个更新的git-hash转换Version.hpp? [英] How to embed an updated git-hash into Version.hpp?

查看:172
本文介绍了如何嵌入一个更新的git-hash转换Version.hpp?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

原标题:如何使混帐忽略我的文件,而不管分支

我有以下几点结账后的文件作为其预期工作:

I have the following post-checkout file which works as expected:

#!/usr/bin/ruby

cmd = ENV["HOME"] + "/dev/pitbull/cpp/bin/gen_version.rb --write"
`#{cmd}`

该gen_version.rb脚本计算出一个时间戳,上次主标签,头git的哈希值,并写入到这也是一个混帐 VERSION.hpp 文件。

然后我用使用git更新索引--assume-不变VERSION.hpp 使混帐忽略我的变化。

I then use use git update-index --assume-unchanged VERSION.hpp to make git ignore my change.

现在,这个如果我留在我的开发分支的伟大工程。但是,当我尝试了 GET结帐主,我拧:

Now, this works great if I stay on my development branch. But when I try a get checkout master, I'm screwed:

git checkout master
error: Your local changes to the following files would be overwritten by checkout:
    cpp/inc/core/util/VERSION.hpp
Please, commit your changes or stash them before you can switch branches.
Aborting

什么是正确的git的设置,这样我就可以更新VERSION.hpp当我检查出来,但有混帐忽略这个文件进行任何更改,不管我的分支?

修改
我改变了话题,因为最终的解决方案实际上是解决这可能是这么多的用户更实用更广泛的话题。所以,你可以阅读本主题两个方面:与下面的整个解决方案与原来的标题和下面的答案,或者与上面的更广泛的问题,再次

EDIT I changed the topic as the final solution actually addresses a broader topic which may be more useful to more users of SO. So you can read this topic two ways: with the original title and the answer below, or with the broader problem above, again with the entire solution below.

推荐答案

为了解决我原来的问题:如何使混帐忽略我的文件,而不管分支


  1. git的RM CPP / INC /核心/ UTIL / VERSION.hpp

  2. 添加CPP / INC /核心/ UTIL / VERSION.hpp到的.gitignore

为了解决?如何嵌入一个更新的git-hash转换Version.hpp

问题的部分原因是鸡有蛋的问题,你无法知道你的哈希值,直到所有的文件都签入,但你不能在你的更新VERSION.hpp检查,直到你知道你的混帐哈希值。所以我的解决办法是离开VERSION.hpp出库,并在.gitignore文件以

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.

在心中的记住,嵌入VERSION.hpp到可执行文件的目的:如果一个讨厌的错误在生产中出现,与二元git的哈希,我们会知道什么是C $ C $到结账混帐使我们能够正确地调试code。

下面是我参加了除两个步骤上述步骤:

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


  1. 写一个简短的Ruby脚本,做两件事情,这取决于当地是否脏了或不(如 git的差异--shortstat 显示超过0线,你有一个肮脏的区域)。 git的转速名单HEAD | SED -n1P|切-C 1-10 会给你前10个字符作为混帐哈希值。如果您的本地区域脏了,这是最后一个头的混帐哈希(我嵌入里面的字符串VERSION.hpp LAST)。如果这是干净的,它是你真正的电流混帐哈希值。

  2. 红宝石脚本将检查结果在1 VS实际VERSION.hpp文件。如果结果发生了变化,然后写入新的结果为VERSION.hpp。

  3. 修改我的.bashrc,加入两个小别名 - cmakerel cmakedbg​​ 作为调用我的本​​地cmake并发布目录,但我做之前,我cmake并做了别名,我叫红宝石脚本1.如果需要更新我的VERSION.hpp。

  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.

如果我叫混帐结账后cmakerel和cmakedbg​​,如果我不生成(我不应该做的,反正实际释放)之前修改code,我将有VERSION.hpp我需要嵌入所有正确的信息。

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.

注意的替代解决方案的别名修改的.bashrc是使用git的结账后挂机(理论上)。但是,我不能得到这个工作,我花了太多的时间在这个问题上了,但你可能有更好的运气。

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.

这篇关于如何嵌入一个更新的git-hash转换Version.hpp?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆