[英]How to format "git describe" output in simple file with .bat script?
I want to get output in simple txt file inside git repo folder:我想在 git repo 文件夹中的简单 txt 文件中获取输出:
About:
date = (2022_06_04)
version = (1d34a1b1)
And I stuck with formatting data in ( ) brackets.我坚持在 ( ) 括号中格式化数据。
Here is my attempt:这是我的尝试:
@echo off
set dt=%DATE:~6,4%_%DATE:~3,2%_%DATE:~0,2%%
set dt=%dt: =0%
set wrap=About:
set file_name="vers.txt"
set git_cmd=git describe --abbrev=8 --always
echo %wrap% >%file_name%
echo.date = (%dt%)>>%file_name%
echo|set /p ="version = (" >>%file_name%
%git_cmd% >>%file_name%
echo.) >>%file_name%
%NL%>>%file_name%
Problem is with ')' for git output formatting.问题在于 ')' 用于 git 输出格式。 Here is what I'm getting:
这是我得到的:
About:
date = (2022_06_04)
version = (1d34a1b1
)
I've tried to play with echo -n
, but it doesn't work on Windows.我试过玩
echo -n
,但它在 Windows 上不起作用。
UPDATE更新
NL is just new line: NL 只是新行:
set NL=echo.
Here is example of command line with whole git command output:这是带有整个 git 命令输出的命令行示例:
USR@DESKTOP-N1 MINGW64 /c/project/prj (v1.1.2)<CR><LF>
$ git describe --abbrev=8 --always<CR><LF>
1d34a1b1<CR><LF>
<CR><LF>
USR@DESKTOP-N1 MINGW64 /c/project/prj (v1.1.2)<CR><LF>
And here is how my output text file looks like:这是我的输出文本文件的样子:
About:<CR><LF>
date = (2022_06_06)<CR><LF>
version = (1d34a1b1<LF>
) <CR><LF>
Seems <LF>
appeared at some stage.似乎
<LF>
出现在某个阶段。 For me it looks like <CR>
was cutted but <LF>
stayed because of some Windows standard formatting or something like that, I'm no sure.对我来说,似乎
<CR>
被剪掉了,但<LF>
由于某些 Windows 标准格式或类似的东西而留下来,我不确定。
Two possibilities leap to mind两种可能性跃入脑海
%git_cmd%|set /p ="version = (" >>%file_name%
echo.) >>%file_name%
and和
for /f "delims=" %%e in ('%git_cmd%') do echo version = (%%e)>>%file_name%
but I've no git
applications, so this is a guess...但我没有
git
应用程序,所以这是一个猜测......
This worked for me to give the output desired:这对我来说可以提供所需的输出:
@ECHO Off
SETLOCAL
set dt=%date%
set wrap=About:
set file_name="u:\vers.txt"
set git_cmd=q72496859s describe --abbrev=8 --always
echo %wrap% >%file_name%
echo.date = (%dt%)>>%file_name%
for /f "delims=" %%e in ('%git_cmd%') do echo version = (%%e)>>%file_name%
TYPE %file_name%
GOTO :EOF
Noting:注意:
My %date%
format is YYYYMMDD我的
%date%
格式是 YYYYMMDD
I've removed the %NL%...
line.我删除了
%NL%...
行。 NL
is not defined in the original post. NL
在原帖中没有定义。 I'd guess it's supposed to add a new line.我猜它应该添加一个新行。
q72496859s
is a batch file that simply responds 1d34a1b1
on a single line. q72496859s
是一个批处理文件,它仅在一行上响应1d34a1b1
。
I've no idea what git
actually generates - does it contain empty lines or perhaps <CR><LF><CR>
sequences?我不知道
git
实际生成什么 - 它是否包含空行或<CR><LF><CR>
序列?
It's SOP for me to use set "var=value"
for setting string values - this avoids problems caused by trailing spaces and not assign a terminal \
, Space or "
- but build pathnames from the elements - counterintuitively, it is likely to make the process easier.对我来说,使用
set "var=value"
来设置字符串值是 SOP - 这避免了由尾随空格引起的问题,而不是分配终端\
、空格或"
- 但从元素构建路径名 - 违反直觉,它可能会使过程更容易。
Using these principles, this batch becomes使用这些原则,这批成为
@ECHO Off
SETLOCAL
set "dt=%date%"
set "wrap=About:"
set "file_name=u:\vers.txt"
set "git_cmd=q72496859s describe --abbrev=8 --always"
echo %wrap% >"%file_name%"
ECHO date = (%dt%)>>"%file_name%"
for /f "delims=" %%e in ('%git_cmd%') do echo version = (%%e)>>"%file_name%"
TYPE "%file_name%"
GOTO :EOF
Edit to second version of above.编辑到上面的第二个版本。
@ECHO Off
SETLOCAL
set "dt=%date%"
set "wrap=About:"
set "file_name=u:\vers.txt"
set "git_cmd=q72496859s describe --abbrev=8 --always"
echo %wrap% >"%file_name%"
ECHO date = (%dt%)>>"%file_name%"
for /f "delims=" %%e in ('%git_cmd% ^|findstr /v "/ -"') do echo version = (%%e)>>"%file_name%" for /f "delims=" %%e in ('%git_cmd% ^|findstr /v "/ -"') do echo version = (%%e)>>"%file_name%"
TYPE "%file_name%"
GOTO :EOF
I set q72496859s
to deliver the git
response described.我设置
q72496859s
来传递描述的git
响应。
The pipe needs to be escaped (by a caret) to tell cmd
that it's part of the command-to-be-executed, not of the actual for
command.管道需要转义(通过插入符号)以告诉
cmd
它是要执行的命令的一部分,而不是实际for
命令。
The findstr
ignores empty lines and reports those lines that do not (/v) contain either /
or -
- the /
needs to be escaped (using backslash this time), which should isolate the required data. findstr
忽略空行并报告那些不 (/v) 包含/
或-
的行 - 需要转义/
(这次使用反斜杠),这应该隔离所需的数据。
The result I obtained was我得到的结果是
About:
date = (06/06/2022)
version = (1d34a1b1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.