2

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.

Zsolt Botykai
  • 50,406
  • 14
  • 85
  • 110
Magnus
  • 417
  • 4
  • 18
  • can you just add ';/^M$/d' to your sed script to delete the lines that are just M. Good luck. – shellter Jan 05 '12 at 14:39
  • When I run `git diff --name-status ..` I get a list of file names, with the status in the first column. I'm not sure what your `sed` command is offering you. My bash is not the best, so I can't give an example, but I think you can get what you want with `awk`, which will let you grab fields by position, and output them. http://www.thegeekstuff.com/2010/01/awk-introduction-tutorial-7-awk-print-examples/ – lhagemann Jan 05 '12 at 15:29

1 Answers1

2
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)
sehe
  • 374,641
  • 47
  • 450
  • 633