1

I need to output the result of this shell script "git diff --name-only commit2 commit1" to an array in groovy. How can I do it?

Already tried to create a variable

def diff = sh(script: "git diff --name-only commit2 commit1", returnStdout: true)

and then work it with Pattern and Matcher, but probably due to its formatting it is always returning an empty array.

The output of the shell script is something like:

directory/file1.java
directory/file2.java

Found a way to do it with bash but can't figure out how to adapt it to groovy (mapfile -t my_array < <( my_command ))

1 Answer 1

5

If you want to get the output as a list, where each line is a separate entry in that list, you could call split('\n') on the output of the sh step:

def diff = sh(script: "git diff --name-only commit2 commit1", returnStdout: true).trim().split('\n')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.