diff --git a/src/com/sap/piper/GitUtils.groovy b/src/com/sap/piper/GitUtils.groovy index 2b781820e..d3ff39a86 100644 --- a/src/com/sap/piper/GitUtils.groovy +++ b/src/com/sap/piper/GitUtils.groovy @@ -1,7 +1,11 @@ package com.sap.piper +boolean insideWorkTree() { + return sh(returnStatus: true, script: 'git rev-parse --is-inside-work-tree 1>/dev/null 2>&1') == 0 +} + String getGitCommitIdOrNull() { - if (sh(returnStatus: true, script: 'git rev-parse --is-inside-work-tree 1>/dev/null 2>&1') == 0) { + if ( insideWorkTree() ) { return getGitCommitId() } else { return null diff --git a/test/groovy/com/sap/piper/GitUtilsTest.groovy b/test/groovy/com/sap/piper/GitUtilsTest.groovy index 306c36787..35e612c7c 100644 --- a/test/groovy/com/sap/piper/GitUtilsTest.groovy +++ b/test/groovy/com/sap/piper/GitUtilsTest.groovy @@ -10,6 +10,8 @@ import util.JenkinsShellCallRule import util.Rules import static org.junit.Assert.assertEquals +import static org.junit.Assert.assertFalse +import static org.junit.Assert.assertTrue import static org.junit.Assert.assertNull class GitUtilsTest extends BasePiperTest { @@ -25,6 +27,19 @@ class GitUtilsTest extends BasePiperTest { jscr.setReturnValue('git rev-parse HEAD', 'testCommitId') } + @Test + void TestIsInsideWorkTree() { + jscr.setReturnValue('git rev-parse --is-inside-work-tree 1>/dev/null 2>&1', 0) + assertTrue(gitUtils.insideWorkTree()) + } + + @Test + void TestIsNotInsideWorkTree() { + jscr.setReturnValue('git rev-parse --is-inside-work-tree 1>/dev/null 2>&1', 1) + assertFalse(gitUtils.insideWorkTree()) + } + + @Test void testGetGitCommitId() { jscr.setReturnValue('git rev-parse --is-inside-work-tree 1>/dev/null 2>&1', 0)