git差异并复制
我想针对Bitbucket上的拉请请求的一部分,对已添加或修改的文件进行静态代码分析(PMD)报告。已修改的文件等在管道图像中本地可用,但是我需要进行git差异以识别仅源分支(从)和目标分支(将合并到)之间的更改。然后,我将针对仅包含“更改文件”的目录执行PMD CLI(带有规则集等),以突出显示这些文件的任何问题,专门作为更改的一部分。
我基本上想复制git diff结果中指示的文件。我希望这能提供更多的背景。
我已经尝试找到一些示例并完成了测试,但是由于我对这些疯狂的Linux命令的了解不足,我只是没有正确处理:)
到目前为止,我有以下命令,但会导致一个空文件夹。
git diff - name-lly -pretty $ bitbucket_pr_destination_branch $ bitbucket_branch | xargs -i {} cp {} -t〜/branch -diff/
I am wanting to run a Static Code Analysis (PMD) report against the files that have been added or modified as part of a pull request on bitbucket. The files that have been modified etc are available locally within the pipeline image, however I need to do a git diff to identify the changes ONLY between the source branch (pulling from) and the target branch (to be merged into). I will then be executing the PMD CLI (with rulesets etc) against a directory that will contain only the "changed files" to highlight any issues with those files specifically as part of the change.
I basically want to copy out the files indicated in the git diff result. I hope this provides some more context.
I have tried finding some examples and done testing however I am just not getting it right due to my lack of understanding on these crazy linux commands :)
So far I have the below command, but results in an empty folder.
git diff --name-only --pretty $BITBUCKET_PR_DESTINATION_BRANCH $BITBUCKET_BRANCH | xargs -i {} cp {} -t ~/branch-diff/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Xargs可能会遇到一些问题 - 参数太大。 结果,我建议
您将所有这些文件放在一个目录中(无目录树)。另一个问题是,这确实是您需要的。
xargs might have problems will a number of files - argument would be too big. I Propose something like
As a result you will have all these files in one directory (without directory tree). Other question is that is it really what you need.
首先,您当前的解决方案的问题:
Xargs
与有空格的文件名无法很好地发挥作用。您现在可能没有这个问题,您可以解决它,但是最好避免如果可能的话。cp
没有构建目录树 - 您可以在该目录中进行琐碎的验证 - 因此它不会做您问的事情。git
不是相对于当前路径而是与工作树基群产生路径名。git diff $ bitbucket_pr_destination_branch $ bitbucket_branch
生成的文件名甚至不必存在于您的工作树中,而仅在(至少一个)分支中。使用标准文件工具的功能脚本看起来像:
May 是纯粹在git中进行此操作的更好方法,但我不知道。
Firstly, the issues with your current solution:
xargs
doesn't play nicely with filenames which have spaces in. You may not have that problem now, and you can work around it, but it's better to just avoid this if possible.cp
does not build a directory tree - which you can trivially verify - so it wouldn't do what you asked anyway.git
does not produce pathnames relative to the current path, but to the working tree base.git diff $BITBUCKET_PR_DESTINATION_BRANCH $BITBUCKET_BRANCH
don't even have to exist in your working tree, but only in (at least one of) the branches.A functional script using standard file tools would look something like:
There may be a better way to do this purely in git, but I don't know it.
如果您具有
cp
和xargs
的GNU变体,则可以执行此操作:这不会在每个文件中生成
cp
,而是复制了很多用一个cp
进程进行文件。通过指定-Target-directory
,目标可以在cp
命令上首先出现,XARGS
可以粘贴到在该源文件名称中的许多源文件名称和cp
命令的命令。- 父母
保留源文件的目录名称。-z
ingit diff
将文件名通过nul字符而不是线路分开,而的
代码>知道如何将NUL分离的路径列表分开而不绊倒文件名中的Whitespace字符。-0
xargs
If you have the GNU variant of
cp
andxargs
, you can do this:This does not spawn a
cp
per file, but copies as many files as possible with onecp
process. By specifying--target-directory
, the destination can come first on thecp
command, andxargs
can paste as many source file names at the and of thecp
command as it likes.--parents
keeps the directory names of the source files.The
-z
ingit diff
separates file names by a NUL character instead of line breaks, and the-0
ofxargs
knows how to take the NUL separated path list apart without stumbling over whitespace characters in file names.