I've got a simple script for GIT:
cp --parents $(git diff --name-only $1 $2) $3
git diff ... - get modified files list between commits ($1 and $2).
cp --parents ... - copies them into desired directory ($3).
But the only problem exists: white spaces in directory names. Script doesn't work properly in that case. Obviously each line of git diff output should be wrapped into quotes, but how?
Or is there another solution? Cannot find it yet.
git diff supports a -z option that will NUL-separate the names. You can then pipe it through xargs. Example:
$ git diff -z --name-only HEAD^ HEAD | xargs -I% -0 cp --parents "%" /tmp/
Options to xargs:
-0 causes it to expect input data to be NUL-separated;-I% makes it run the command once per input, and replace % in the command line with the input (apparently -i% on Windows?). % was arbitrarily chosen because it's short, you can use -IFOO / "FOO" as well;cp ... the command to execute.I used /tmp/ as a safe example.
So this should effectively run
$ cp --parents "file1" /tmp/
and so on. Alternatively, you can read the output of git diff line-by-line within Bash.
$ git diff --name-only HEAD^ HEAD | ( while read fn ; do cp "${fn}" /tmp/ ; done )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With