Shell Script – Read all file names in Git Pull Request

Shell script to read all files part of Pull Request & iterate through them

In your CI / CD process, it could be very common scenario that you need to know name of all files thats part of any pull request. Example- in Salesforce you want to perform delta deployment with only components that are part of user story.

Below shell script demonstrates how we can read all file names and iterate through it. For Demo purpose, I’m just adding white space at end of each file however you can do anything as per you continuous integration pipeline requirement.

# File name - AddWhitespace.sh 
# Read list of all unique file and store as array - 231327
echo "Provide Pull Request Number"
read prNumber
echo "Your entered $prNumber"
#Read all files that are part of Git
fileNames=$(  git log origin/remoteBranchName --grep "$prNumber"  --pretty=format: --name-only | grep -v -e "^$" | sort | uniq )
#convert variable to array
IFS=$'\n' array=($fileNames) 
echo "------ Printing file names"
for element in "${array[@]}"
do   
    echo "Trying to add White space in $element"
    printf " " >> $element  
done

To run above file just use command

bash AddWhitespace.sh

Related posts

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.