Question

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?

Was it helpful?

Solution

I settled on the following solution with sed.

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

OTHER TIPS

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.

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.

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.

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top