Skip to content

Commit

Permalink
Merge pull request #41 from cloudogu/feature/git_pull
Browse files Browse the repository at this point in the history
Implements git.pull()
  • Loading branch information
pmarkiewka authored May 27, 2020
2 parents a117dcf + 63b75f0 commit 4882fa1
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 13 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,10 @@ gitWithCreds 'https://your.repo' // Implicitly passed credentials
* `git.setTag('tag', 'message', 'Author', '[email protected])`
* `git.setTag('tag', 'message')` - uses the name and email of the last committer as author and committer.
* `git.fetch()`
* `git.pull()` - pulls, and in case of merge, uses the name and email of the last committer as author and committer.
* `git.pull('refspec')` - pulls specific refspec (e.g. `origin master`), and in case of merge, uses the name and email
of the last committer as author and committer.
* `git.pull('refspec', 'Author', '[email protected])`
* `git.merge('develop', 'Author', '[email protected])`
* `git.merge('develop')` - uses the name and email of the last committer as author and committer.
* `git.mergeFastForwardOnly('master')`
Expand Down
22 changes: 19 additions & 3 deletions src/com/cloudogu/ces/cesbuildlib/Git.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,7 @@ class Git implements Serializable {
* @param message
*/
void commit(String message, String authorName, String authorEmail) {
script.withEnv(["GIT_AUTHOR_NAME=$authorName", "GIT_AUTHOR_EMAIL=$authorEmail",
"GIT_COMMITTER_NAME=$authorName", "GIT_COMMITTER_EMAIL=$authorEmail"]) {
withAuthorAndEmail(authorName, authorEmail) {
script.sh "git commit -m \"$message\""
}
}
Expand All @@ -198,9 +197,15 @@ class Git implements Serializable {
* @param authorEmail
*/
void setTag(String tag, String message, String authorName, String authorEmail) {
withAuthorAndEmail(authorName, authorEmail) {
script.sh "git tag -m \"${message}\" ${tag}"
}
}

private void withAuthorAndEmail(String authorName, String authorEmail, Closure closure) {
script.withEnv(["GIT_AUTHOR_NAME=$authorName", "GIT_AUTHOR_EMAIL=$authorEmail",
"GIT_COMMITTER_NAME=$authorName", "GIT_COMMITTER_EMAIL=$authorEmail"]) {
script.sh "git tag -m \"${message}\" ${tag}"
closure.call()
}
}

Expand Down Expand Up @@ -290,6 +295,17 @@ class Git implements Serializable {
executeGitWithCredentials "push origin ${refSpec}"
}

/**
* Pulls to local from remote repo.
*
* @param refSpec branch or tag name
*/
void pull(String refSpec = '', String authorName = commitAuthorName, String authorEmail = commitAuthorEmail) {
withAuthorAndEmail(authorName, authorEmail) {
executeGitWithCredentials "pull ${refSpec}"
}
}

/**
* Commits and pushes a folder to the <code>gh-pages</code> branch of the current repo.
* Can be used to conveniently deliver websites. See https://pages.github.com/
Expand Down
47 changes: 38 additions & 9 deletions test/com/cloudogu/ces/cesbuildlib/GitTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,7 @@ class GitTest {

@Test
void commit() {
ScriptMock scriptMock = new ScriptMock()
scriptMock.expectedDefaultShRetValue = "User Name <[email protected]>"
Git git = new Git(scriptMock)
git.commit 'msg'
def actualWithEnv = scriptMock.actualWithEnvAsMap()
assert actualWithEnv['GIT_AUTHOR_NAME'] == 'User Name'
Expand All @@ -203,9 +201,7 @@ class GitTest {

@Test
void setTag() {
ScriptMock scriptMock = new ScriptMock()
scriptMock.expectedDefaultShRetValue = "User Name <[email protected]>"
Git git = new Git(scriptMock)
git.setTag("someTag", "someMessage")
def actualWithEnv = scriptMock.actualWithEnvAsMap()

Expand All @@ -224,6 +220,44 @@ class GitTest {
assert scriptMock.actualShStringArgs[1] == "git fetch --all"
}

@Test
void pull() {
def expectedGitCommandWithCredentials = 'git -c credential.helper="!f() { echo username=\'$GIT_AUTH_USR\'; echo password=\'$GIT_AUTH_PSW\'; }; f" pull'
scriptMock.expectedShRetValueForScript.put(expectedGitCommandWithCredentials, 0)
scriptMock.expectedShRetValueForScript.put('git --no-pager show -s --format=\'%an <%ae>\' HEAD', 'User Name <[email protected]>')
git = new Git(scriptMock, 'creds')

git.pull()

def actualWithEnv = scriptMock.actualWithEnvAsMap()
assert actualWithEnv['GIT_AUTHOR_NAME'] == 'User Name'
assert actualWithEnv['GIT_COMMITTER_NAME'] == 'User Name'
assert actualWithEnv['GIT_AUTHOR_EMAIL'] == '[email protected]'
assert actualWithEnv['GIT_COMMITTER_EMAIL'] == '[email protected]'

assert scriptMock.actualShMapArgs.size() == 3
assert scriptMock.actualShMapArgs.get(2).trim() == expectedGitCommandWithCredentials
}

@Test
void 'pull with refspec'() {
def expectedGitCommandWithCredentials = 'git -c credential.helper="!f() { echo username=\'$GIT_AUTH_USR\'; echo password=\'$GIT_AUTH_PSW\'; }; f" pull origin master'
scriptMock.expectedShRetValueForScript.put(expectedGitCommandWithCredentials, 0)
scriptMock.expectedShRetValueForScript.put('git --no-pager show -s --format=\'%an <%ae>\' HEAD', 'User Name <[email protected]>')
git = new Git(scriptMock, 'creds')

git.pull 'origin master'

def actualWithEnv = scriptMock.actualWithEnvAsMap()
assert actualWithEnv['GIT_AUTHOR_NAME'] == 'User Name'
assert actualWithEnv['GIT_COMMITTER_NAME'] == 'User Name'
assert actualWithEnv['GIT_AUTHOR_EMAIL'] == '[email protected]'
assert actualWithEnv['GIT_COMMITTER_EMAIL'] == '[email protected]'

assert scriptMock.actualShMapArgs.size() == 3
assert scriptMock.actualShMapArgs.get(2).trim() == expectedGitCommandWithCredentials
}

@Test
void checkout() {
git.checkout("master")
Expand Down Expand Up @@ -264,24 +298,20 @@ class GitTest {

@Test
void mergeFastForwardOnly() {
ScriptMock scriptMock = new ScriptMock()
Git git = new Git(scriptMock)
git.mergeFastForwardOnly("master")

assert scriptMock.actualShStringArgs[0] == "git merge --ff-only master"
}

@Test
void push() {
ScriptMock scriptMock = new ScriptMock()
scriptMock.expectedShRetValueForScript.put('git -c credential.helper="!f() { echo username=\'$GIT_AUTH_USR\'; echo password=\'$GIT_AUTH_PSW\'; }; f" push origin master', 0)
git = new Git(scriptMock, 'creds')
git.push('master')
}

@Test
void pushNonHttps() {
ScriptMock scriptMock = new ScriptMock()
scriptMock.expectedShRetValueForScript.put('git -c credential.helper="!f() { echo username=\'$GIT_AUTH_USR\'; echo password=\'$GIT_AUTH_PSW\'; }; f" push origin master', 0)
git = new Git(scriptMock, 'creds')
git.push('master')
Expand All @@ -292,7 +322,6 @@ class GitTest {

@Test
void pushWithRetry() {
ScriptMock scriptMock = new ScriptMock()
scriptMock.expectedShRetValueForScript.put('git -c credential.helper="!f() { echo username=\'$GIT_AUTH_USR\'; echo password=\'$GIT_AUTH_PSW\'; }; f" push origin master', [128, 128, 0])
git = new Git(scriptMock, 'creds')
git.retryTimeout = 1
Expand Down
2 changes: 1 addition & 1 deletion test/com/cloudogu/ces/cesbuildlib/ScriptMock.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class ScriptMock {
private Object getReturnValueFor(Object arg) {
if (expectedDefaultShRetValue == null) {
// toString() to make Map also match GStrings
def value = expectedShRetValueForScript.get(arg.toString())
def value = expectedShRetValueForScript.get(arg.toString().trim())
if (value instanceof List) {
return ((List) value).removeAt(0)
} else {
Expand Down

0 comments on commit 4882fa1

Please sign in to comment.