From 63b75f09e401a9bf2cee7d16f173686b3813bbd9 Mon Sep 17 00:00:00 2001 From: Johannes Schnatterer Date: Wed, 27 May 2020 18:30:59 +0200 Subject: [PATCH] Implements git.pull() --- README.md | 4 ++ src/com/cloudogu/ces/cesbuildlib/Git.groovy | 22 +++++++-- .../cloudogu/ces/cesbuildlib/GitTest.groovy | 47 +++++++++++++++---- .../ces/cesbuildlib/ScriptMock.groovy | 2 +- 4 files changed, 62 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 469b20e6..b7e7a553 100644 --- a/README.md +++ b/README.md @@ -484,6 +484,10 @@ gitWithCreds 'https://your.repo' // Implicitly passed credentials * `git.setTag('tag', 'message', 'Author', 'Author@mail.server)` * `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', 'Author@mail.server)` * `git.merge('develop', 'Author', 'Author@mail.server)` * `git.merge('develop')` - uses the name and email of the last committer as author and committer. * `git.mergeFastForwardOnly('master')` diff --git a/src/com/cloudogu/ces/cesbuildlib/Git.groovy b/src/com/cloudogu/ces/cesbuildlib/Git.groovy index 5c69d32a..ea474ef0 100644 --- a/src/com/cloudogu/ces/cesbuildlib/Git.groovy +++ b/src/com/cloudogu/ces/cesbuildlib/Git.groovy @@ -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\"" } } @@ -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() } } @@ -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 gh-pages branch of the current repo. * Can be used to conveniently deliver websites. See https://pages.github.com/ diff --git a/test/com/cloudogu/ces/cesbuildlib/GitTest.groovy b/test/com/cloudogu/ces/cesbuildlib/GitTest.groovy index c1ea21ea..6faf6385 100644 --- a/test/com/cloudogu/ces/cesbuildlib/GitTest.groovy +++ b/test/com/cloudogu/ces/cesbuildlib/GitTest.groovy @@ -190,9 +190,7 @@ class GitTest { @Test void commit() { - ScriptMock scriptMock = new ScriptMock() scriptMock.expectedDefaultShRetValue = "User Name " - Git git = new Git(scriptMock) git.commit 'msg' def actualWithEnv = scriptMock.actualWithEnvAsMap() assert actualWithEnv['GIT_AUTHOR_NAME'] == 'User Name' @@ -203,9 +201,7 @@ class GitTest { @Test void setTag() { - ScriptMock scriptMock = new ScriptMock() scriptMock.expectedDefaultShRetValue = "User Name " - Git git = new Git(scriptMock) git.setTag("someTag", "someMessage") def actualWithEnv = scriptMock.actualWithEnvAsMap() @@ -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 ') + 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'] == 'user.name@doma.in' + assert actualWithEnv['GIT_COMMITTER_EMAIL'] == 'user.name@doma.in' + + 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 ') + 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'] == 'user.name@doma.in' + assert actualWithEnv['GIT_COMMITTER_EMAIL'] == 'user.name@doma.in' + + assert scriptMock.actualShMapArgs.size() == 3 + assert scriptMock.actualShMapArgs.get(2).trim() == expectedGitCommandWithCredentials + } + @Test void checkout() { git.checkout("master") @@ -264,8 +298,6 @@ 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" @@ -273,7 +305,6 @@ class GitTest { @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') @@ -281,7 +312,6 @@ class GitTest { @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') @@ -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 diff --git a/test/com/cloudogu/ces/cesbuildlib/ScriptMock.groovy b/test/com/cloudogu/ces/cesbuildlib/ScriptMock.groovy index 3e540ccd..c108ef13 100644 --- a/test/com/cloudogu/ces/cesbuildlib/ScriptMock.groovy +++ b/test/com/cloudogu/ces/cesbuildlib/ScriptMock.groovy @@ -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 {