Friday, May 27, 2022

[SOLVED] Git Diff and Copy

Issue

Good day,

** Updated Description

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/

Any guidance or assistance would be appreciated!

Peter


Solution

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.



Answered By - ThomasN
Answer Checked By - Katrina (WPSolving Volunteer)