通过换行符拆分 git diff --name-status

发布于 2024-12-24 18:53:26 字数 662 浏览 1 评论 0原文

我正在尝试使用 git 使用以下命令导出两次提交中已更改文件的列表:

read -ra ADDR <<< `git diff --name-only HEAD..HEAD~1 | sed -e "s/ /\\\ /g"`
for i in "${ADDR[@]}"; do 
    echo "$i"
done

这工作正常。我得到了一个很好的列表,打印到终端,每个文件都在新行上。

file1.txt
file2.txt
file3.txt

但是,我想使用 --name-status 而不是 --name-only。原因是这样我以后可以根据所做的更改类型(例如 M 或 D)做不同的事情。

但这不起作用。我得到以下格式的列表:

M
file1.txt
M
file2.txt
M
file3.txt

我尝试将 IFS 变量更改为 $'\n' (返回一行上的所有内容),$' \t'(返回第一行,例如 M file1.txt)和 '(与 \t 相同)没有成功。

I'm trying to export a list of changed files from two commits with git, with this command:

read -ra ADDR <<< `git diff --name-only HEAD..HEAD~1 | sed -e "s/ /\\\ /g"`
for i in "${ADDR[@]}"; do 
    echo "$i"
done

This works fine. I get a nice list printed out to the terminal with each file on a new row.

file1.txt
file2.txt
file3.txt

However, I want to use --name-status instead of --name-only. The reason is so I can later do different things depending on what kind of change was made (e.g. M or D).

This doesn't work though. I get a list in the following format:

M
file1.txt
M
file2.txt
M
file3.txt

I have tried to changed the IFS variable to $'\n' (returns everything on one row), $'\t' (returns first row, e.g. M file1.txt) and ' ' (same as \t) without success.

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

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

发布评论

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

评论(1

固执像三岁 2024-12-31 18:53:26
while read STATUS ADDR
do
    echo "$ADDR ($STATUS)"
done  < <(git diff --name-status HEAD..HEAD~1)

例如,对于 git 输出,

M       INSTALL
M       debian/changelog
M       src/lib/libnvpair/SConscript
M       src/lib/libzfscommon/SConscript
M       src/lib/libzpool/SConscript
M       src/zfs-fuse/main.c

由 while 循环打印如下所示:

INSTALL (M)
debian/changelog (M)
src/lib/libnvpair/SConscript (M)
src/lib/libzfscommon/SConscript (M)
src/lib/libzpool/SConscript (M)
src/zfs-fuse/main.c (M)
while read STATUS ADDR
do
    echo "$ADDR ($STATUS)"
done  < <(git diff --name-status HEAD..HEAD~1)

E.g. for git output like

M       INSTALL
M       debian/changelog
M       src/lib/libnvpair/SConscript
M       src/lib/libzfscommon/SConscript
M       src/lib/libzpool/SConscript
M       src/zfs-fuse/main.c

the following is printed by the while loop:

INSTALL (M)
debian/changelog (M)
src/lib/libnvpair/SConscript (M)
src/lib/libzfscommon/SConscript (M)
src/lib/libzpool/SConscript (M)
src/zfs-fuse/main.c (M)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文