Problems using git diff to create file list for de

2019-04-02 08:05发布

问题:

I want to use something like the following command to create a tarball to deploy:

tar cjvf ~/deploy.tar.bz2 `git diff --name-only 0abc 1def`

The inner git diff command produces a list of files with relative including the relative path when i run it separately.

I'm running into two problems though, I need to be able to auto escape spaces in the output, so tar doesn't complain about files containing spaces and when the tar does get created, all the files have a duplicate 'hidden file' preceded by a '.' that don't show up with ls -al. These are OSX specific metafiles as noted by kch.

Anyway, does anyone know of the solution to these problems, or is there just a plain easier way to script this?

回答1:

I settled on the following solution with sed.

tar cjf ~/deploy.tar.bz2 \
`git diff --name-only 0abc 1def|sed -e "s/ /\\\ /g"`


回答2:

The hidden dot-files, are they dot-underscore-files?

If for file foo you have another ._foo, and you're on a Mac, the dot-underscore file is where the file resource fork / metadata is kept.

As for the git output, might try piping it through sed or perl for quoting. I believe xargs could help here too.



回答3:

Why do not use --files-from=FILE or -T FILE option of tar, where FILE can be '-' to signify standard input?

 git diff --name-only 0abc 1def | tar -T - cjf ~/deploy.tar.bz2

You shouldn't have problem with spaces or tabs in filenames, or with single quotes, or backquotes, or backslashes (I think your solution would have problems with single quote "'" in filename). You might have problem with newlines in filenames, just like in IFS solution.



回答4:

If size doesn't matter you could use git archive.

I'm not sure if it's possible to create a tarball containing only the differences between two commits.



回答5:

You can try

eval tar cjvf ~/deploy.tar.bz2 `git diff --name-only 0abc 1def | while read x; do echo "'""$x""'"; done`

or try the same thing using sed, or

IFS='
' tar cjvf ~/deploy.tar.bz2 `git diff --name-only 0abc 1def`

Note that there is only a single newline in IFS.

However, if the name of your files contain newlines, you're doomed.



回答6:

Here's another, more elegant solution that works around escaping spaces in the filenames

git diff --name-only 0abc 1def | \
    tr '\n' '\0' | \
    xargs -0 tar -rjvf ~/deploy.tar.bz2

As jpalecek pointed out, one wold not want to run "tar -c..." multiple times, it's better to use -r.