Mercurial hooks — pass information between hooks?

こ雲淡風輕ζ 提交于 2019-12-08 01:30:21

问题


I currently have a pre-commit hook in my mercurial project that gives the user the option to update the version number of the project if they wish (e.g. 1.0 to 1.0.1 or 1.1 or 2.0). They select one of these options, and the hook updates the version number in one of the project's files prior to the commit taking place.

When I run hg commit, this hook runs and updates the relevant file with the new version number, and then performs the commit.

I'd like to add to the hook such that it calls hg tag <new_verson_number> as well.

However, I can't add this to the pre-commit hook, because then the tag will be added before the commit is called, causing the tag to be one revision out of date.

I'd like to add the hg tag command to a commit hook (run after the commit), so that the sequence of events is like so:

  • hg commit -m "my commit message"
  • user says yes, I'd like to change the version number
  • version number is updated in the relevant file
  • commit occurs [everything up to here is fine]
  • if the user changed the version number, run a commit hook: hg tag <new_version_number>.

Now, I could add a commit hook that read off the new version number from the file it's stored in and run hg tag <new_version_number>, but what if the user decided not to change the version number? In that case, I don't want a tag added, and if I blindly run hg tag <new_version_number> I'll end up with tags I don't want.

So - is there some way I can tell the pre-commit hook to leave some information for the commit hook (a yes/no of whether to add the tag), and the commit hook can use this to determine whether to add the tag or not?

cheers.


回答1:


How about a commit hook that checks if the last commit modified the file in which you store the version? Something like:

[hooks]
commit.tagversion = if hg log -r $HG_NODE --template '{files}' | grep -q VERSION_FILE ; then hg tag $(hg cat -r $HG_NODE VERSION_FILE) ; fi

I haven't tested it, but that or something like it should work.




回答2:


You could extract the tag name that you would add and then check to see if it already exists in hg tags before you add it. That would also catch the case where the developer amends the version number manually.



来源:https://stackoverflow.com/questions/9611045/mercurial-hooks-pass-information-between-hooks

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