Split git diff --name-status by line break

此生再无相见时 提交于 2019-12-11 00:54:05

问题


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.


回答1:


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)


来源:https://stackoverflow.com/questions/8743741/split-git-diff-name-status-by-line-break

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