git差异并复制

发布于 2025-02-01 03:10:06 字数 458 浏览 3 评论 0原文

我想针对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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

久隐师 2025-02-08 03:10:06

Xargs可能会遇到一些问题 - 参数太大。 结果,我建议

for name in $(git diff --name-only --pretty $BITBUCKET_PR_DESTINATION_BRANCH $BITBUCKET_BRANCH); do cp $name ~/branch-diff/; done

您将所有这些文件放在一个目录中(无目录树)。另一个问题是,这确实是您需要的。

xargs might have problems will a number of files - argument would be too big. I Propose something like

for name in $(git diff --name-only --pretty $BITBUCKET_PR_DESTINATION_BRANCH $BITBUCKET_BRANCH); do cp $name ~/branch-diff/; done

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.

久光 2025-02-08 03:10:06

首先,您当前的解决方案的问题:

  1. Xargs与有空格的文件名无法很好地发挥作用。您现在可能没有这个问题,您可以解决它,但是最好避免如果可能的话。
  2. cp没有构建目录树 - 您可以在该目录中进行琐碎的验证 - 因此它不会做您问的事情。
  3. git不是相对于当前路径而是与工作树基群产生路径名。
  4. git diff $ bitbucket_pr_destination_branch $ bitbucket_branch生成的文件名甚至不必存在于您的工作树中,而仅在(至少一个)分支中。
  5. 如果两个版本的文件之间存在差异,则您没有说要复制哪个文件!

使用标准文件工具的功能脚本看起来像:

#!/bin/bash
# diffcopy.sh
#
DESTDIR="$1"
BRANCH1="$2"
BRANCH2="$3"

# so relative paths match git output
SRCDIR="$(git rev-parse --show-toplevel)"

# choose the branch whose files we want to copy
git checkout "$BRANCH2"

# make the output directory
mkdir -p "$DESTDIR"

# sync the changed files
rsync -a --files-from=<(git diff --name-only "${BRANCH1}".."${BRANCH2}") "$SRCDIR" "$DESTDIR"

# restore working copy
git checkout -

May 是纯粹在git中进行此操作的更好方法,但我不知道。

Firstly, the issues with your current solution:

  1. 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.
  2. cp does not build a directory tree - which you can trivially verify - so it wouldn't do what you asked anyway.
  3. git does not produce pathnames relative to the current path, but to the working tree base.
  4. The filenames produced by 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.
  5. If there's a diff between the two versions of a file, you haven't said which one you want copied!

A functional script using standard file tools would look something like:

#!/bin/bash
# diffcopy.sh
#
DESTDIR="$1"
BRANCH1="$2"
BRANCH2="$3"

# so relative paths match git output
SRCDIR="$(git rev-parse --show-toplevel)"

# choose the branch whose files we want to copy
git checkout "$BRANCH2"

# make the output directory
mkdir -p "$DESTDIR"

# sync the changed files
rsync -a --files-from=<(git diff --name-only "${BRANCH1}".."${BRANCH2}") "$SRCDIR" "$DESTDIR"

# restore working copy
git checkout -

There may be a better way to do this purely in git, but I don't know it.

煞人兵器 2025-02-08 03:10:06

如果您具有cpxargs的GNU变体,则可以执行此操作:

git diff --name-only -z $BITBUCKET_PR_DESTINATION_BRANCH $BITBUCKET_BRANCH |
   xargs -0 cp --target-directory="$HOME/branch-diff/" --parents

这不会在每个文件中生成cp,而是复制了很多用一个cp进程进行文件。通过指定-Target-directory,目标可以在cp命令上首先出现,XARGS可以粘贴到在该源文件名称中的许多源文件名称和cp命令的命令。 - 父母保留源文件的目录名称。

-z in git diff将文件名通过nul字符而不是线路分开,而-0 xargs代码>知道如何将NUL分离的路径列表分开而不绊倒文件名中的Whitespace字符。

If you have the GNU variant of cp and xargs, you can do this:

git diff --name-only -z $BITBUCKET_PR_DESTINATION_BRANCH $BITBUCKET_BRANCH |
   xargs -0 cp --target-directory="$HOME/branch-diff/" --parents

This does not spawn a cp per file, but copies as many files as possible with one cp process. By specifying --target-directory, the destination can come first on the cp command, and xargs can paste as many source file names at the and of the cp command as it likes. --parents keeps the directory names of the source files.

The -z in git diff separates file names by a NUL character instead of line breaks, and the -0 of xargs knows how to take the NUL separated path list apart without stumbling over whitespace characters in file names.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文