Domanda

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.

È stato utile?

Soluzione

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)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top