Add withMerge flag for gitSSHCheckout step

Rather than merge the changes to be built into a branch which
tracks the remote I want to push to, it checks out one rev and
merges the others on, resulting in a detached HEAD.

Some tools expects to be run on a specific branch, and it
fails because the repo isn't in that state.

[1]. https://issues.jenkins-ci.org/browse/JENKINS-6856

Change-Id: I9d6e2caf639d1aba651ae5f709c6369ea2848546
diff --git a/vars/gitSSHCheckout.groovy b/vars/gitSSHCheckout.groovy
index 0bb696f..46b76bc 100644
--- a/vars/gitSSHCheckout.groovy
+++ b/vars/gitSSHCheckout.groovy
@@ -5,17 +5,26 @@
   body.delegate = config
   body()
 
+  def merge = config.withMerge ?: false
   def targetDir = config.targetDir ?: "./"
   def port = config.port ?: "29418"
 
+  // default parameters
+  def scmExtensions = [
+    [$class: 'CleanCheckout'],
+    [$class: 'RelativeTargetDirectory', relativeTargetDir: "${targetDir}"]
+  ]
+
+  // https://issues.jenkins-ci.org/browse/JENKINS-6856
+  if (merge) {
+    scmExtensions.add([$class: 'LocalBranch', localBranch: "${config.branch}"])
+  }
+
   checkout(
     scm: [
       $class: 'GitSCM',
       branches: [[name: "${config.branch}"]],
-      extensions: [
-        [$class: 'CleanCheckout'],
-        [$class: 'RelativeTargetDirectory', relativeTargetDir: "${targetDir}"]
-      ],
+      extensions: scmExtensions,
       userRemoteConfigs: [[
         credentialsId: "${config.credentialsId}",
         name: 'origin',
diff --git a/vars/gitSSHCheckout.txt b/vars/gitSSHCheckout.txt
index 47bf702..b01939e 100644
--- a/vars/gitSSHCheckout.txt
+++ b/vars/gitSSHCheckout.txt
@@ -25,3 +25,46 @@
   }
 }
 
+// by default if you checkout even to branch you will be in detached mode
+// but some tools (e.g. pip) requires "merged" code
+// https://issues.jenkins-ci.org/browse/JENKINS-6856
+// so you need to use withMerge = true
+
+node {
+  gitSSHCheckout {
+    credentialsId = "mcp-ci-gerrit"
+    branch = "mcp"
+    host = "ci.mcp-ci.local"
+    project = "projectcalico/calico-containers"
+    targetDir = "some_directory"
+    withMerge = true
+  }
+}
+
+Please see example below:
+1. withMerge = false (default)
+  $ git status
+  HEAD detached at 25fd9c2
+  nothing to commit, working directory clean
+  $ git branch -av
+  * (HEAD detached at 25fd9c2)       25fd9c2 Fix .gitreview
+    remotes/origin/HEAD              7e2b36e Merge pull request #160 from tomdee/tiny-build-image-size-tweaks
+    remotes/origin/develop           013614f Merge pull request #144 from djosborne/comment-etcd-pin
+    remotes/origin/feature/utreports 3c52c2a disable usage reports while running unit tests
+    remotes/origin/go                350e251 Merge pull request #128 from caseydavenport/ipam-code-review
+    remotes/origin/master            7e2b36e Merge pull request #160 from tomdee/tiny-build-image-size-tweaks
+    remotes/origin/mcp               25fd9c2 Fix .gitreview
+
+
+2. withMerge = true
+  $ git status
+  On branch mcp
+  nothing to commit, working directory clean
+  $ git branch -av
+  * mcp                              25fd9c2 Fix .gitreview
+    remotes/origin/HEAD              7e2b36e Merge pull request #160 from tomdee/tiny-build-image-size-tweaks
+    remotes/origin/develop           013614f Merge pull request #144 from djosborne/comment-etcd-pin
+    remotes/origin/feature/utreports 3c52c2a disable usage reports while running unit tests
+    remotes/origin/go                350e251 Merge pull request #128 from caseydavenport/ipam-code-review
+    remotes/origin/master            7e2b36e Merge pull request #160 from tomdee/tiny-build-image-size-tweaks
+    remotes/origin/mcp               25fd9c2 Fix .gitreview