1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +02:00

Merge pull request #150 from marcusholl/pr/insideWorkingTree

git utils: is inside working tree
This commit is contained in:
Marcus Holl 2018-06-11 09:46:51 +02:00 committed by GitHub
commit 3ae2d0ae47
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 1 deletions

View File

@ -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

View File

@ -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)