如何获取工作目录哈希确认所有未提交的更改?

发布于 2024-10-19 04:20:15 字数 1338 浏览 2 评论 0原文

我的软件构建过程的一部分包括将工作目录父级的哈希值转换为 C++ 字符串,以便包含在“版本”输出中。这是通过使用 hg recognize -i 获取全局修订 ID,并将此输出复制到 .h 文件中以包含在内来完成的。我在 Windows 批处理文件中执行此操作:

setlocal enabledelayedexpansion
for /f "tokens=1 delims=;=" %%a in ('hg identify -i') do (
  echo const std::string revision^(_T^("%%a"^)^); > rev.h
)

它将在文件中输出类似这样的内容:

const std::string revision(_T("3b746fd492c6"));

如果工作目录有任何未提交的更改,则哈希会附加一个 +,使字符串 "3b746fd492c6+ “。这使我可以轻松检查我构建的软件版本是否受到控制 - 如果字符串包含 + 则该软件无法从存储库中重现。

但是,hg recognize 添加了 + 来表示未提交的更改,但它无法识别未跟踪的文件。如果我提交所有更改但忘记添加最重要的“do stuff”类,则 hg recognize 将不会指示这一点。

所以我的问题是:如何获得所需的功能?
如何模拟 hg recognize 识别新文件和已删除文件?

理想情况下,我希望不必使用扩展,但会将它们视为一种选择。


更新

根据 Oben Sonne 结合使用 hg sthg id -r 的建议,我提出了以下批处理文件,它产生了相当好的结果:

@echo off
set REPOMODS=
for /F %%a IN ('hg st -n') DO set REPOMODS=+

for /f "tokens=1 delims=;=" %%a in ('hg identify -i -r .') do (
  echo const std::string revision^(_T^("%%a%REPOMODS%"^)^); > rev.h
)

%REPOMODS% 为空,除非 hg st 的输出中有任何内容,在这种情况下它是 +。我做了一些测试,似乎有效。

是否有另一种解决方案需要更少的批处理文件处理?或者这是我能得到的最好的?

Part of my software build process includes getting the hash of the working directory parent into a C++ string for inclusion in the "version" output. This is done by using hg identify -i to get the global revision id, and copying this output into a .h file for inclusion. I do this in a Windows batch file:

setlocal enabledelayedexpansion
for /f "tokens=1 delims=;=" %%a in ('hg identify -i') do (
  echo const std::string revision^(_T^("%%a"^)^); > rev.h
)

Which will output something like this into the file:

const std::string revision(_T("3b746fd492c6"));

If the working directory has any uncommitted changes, the hash has a + appended, making the string "3b746fd492c6+". This allows me to easily check whether the version of software I have built is controlled or not - if the string includes a + then the software is not reproducible from the repository.

However, hg identify adds a + to denote uncommitted changes, but it does not recognised untracked files. If I commit all changes but forget to add that all-important "do stuff" class, hg identify will not indicate this.

So my question is: how can I get the required functionality?
How can I simulate hg identify recognising new and removed files?

Ideally I would like to not have to use extensions, but will consider them as an option.


Update

Following on from Oben Sonne's suggestion of using a combination of hg st and hg id -r . I have come up with the following batch file which produces quite a nice result:

@echo off
set REPOMODS=
for /F %%a IN ('hg st -n') DO set REPOMODS=+

for /f "tokens=1 delims=;=" %%a in ('hg identify -i -r .') do (
  echo const std::string revision^(_T^("%%a%REPOMODS%"^)^); > rev.h
)

%REPOMODS% is empty unless there is anything in the output of the hg st, in which case it is +. I've done a few tests and it seems to work.

Is there another solution which requires less faffing in batch files? Or is this the best I'll get?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

虫児飞 2024-10-26 04:20:15

简单地检查 hg st 的输出是否为空怎么样?如果没有,请自行添加 + 到版本中(如果 hg id 尚未给出)。

更新:为了防止双重+问题,您可以运行hg id -r .,它永远不会给您一个尾随的+< /代码>。

How about simply checking if the output of hg st is empty? If not, add a + yourself to the version (if not already given by hg id).

UPDATE: To prevent the double + issue, you could run hg id -r ., which never gives you a trailing +.

长亭外,古道边 2024-10-26 04:20:15

我自己的问题的答案是执行类似于以下内容的操作,这将执行测试 addremove 并解析任何输出。我不太喜欢它,但将其作为可能的解决方案提供,希望其他人有更好的想法:

set ADDREMOVERESULT=
for /F %%a IN ('hg addremove -n') DO set ADDREMOVERESULT=+
if "%ADDREMOVERESULT%"=="+" echo New or Removed Files

此解决方案的问题是,如果同时存在修改的文件和新的/删除的文件,版本字符串可能最终会出现与++。问题不大,但就这样吧。

An answer to my own question is to do something similar to the following, which will perform a test addremove and parse any output. I don't really like it, but offer it as a possible solution in the hope that someone else has a better idea:

set ADDREMOVERESULT=
for /F %%a IN ('hg addremove -n') DO set ADDREMOVERESULT=+
if "%ADDREMOVERESULT%"=="+" echo New or Removed Files

The issue with this solution is that if there are both modified and new/removed files, the version string could end up with ++. Not a big problem, but there you go.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文