Problems using git diff to create file list for de

2019-04-02 08:01发布

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?

6条回答
聊天终结者
2楼-- · 2019-04-02 08:02

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.

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-04-02 08:09

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.

查看更多
走好不送
4楼-- · 2019-04-02 08:13

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.

查看更多
做自己的国王
5楼-- · 2019-04-02 08:16

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.

查看更多
叼着烟拽天下
6楼-- · 2019-04-02 08:19

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.

查看更多
贼婆χ
7楼-- · 2019-04-02 08:20

I settled on the following solution with sed.

tar cjf ~/deploy.tar.bz2 \
`git diff --name-only 0abc 1def|sed -e "s/ /\\\ /g"`
查看更多
登录 后发表回答