Repository: buildbot/buildbot

Issue:
Send JSON to GitHub instead of form encoded
GitHub expects a JSON request body



Discussion:

Resolving patch:
diff --git a/master/buildbot/steps/source/git.py b/master/buildbot/steps/source/git.py
--- a/master/buildbot/steps/source/git.py
+++ b/master/buildbot/steps/source/git.py
@@ -13,7 +13,7 @@
 #
 # Copyright Buildbot Team Members
 
-from twisted.python import log, failure
+from twisted.python import log
 from twisted.internet import defer
 
 from buildbot.process import buildstep
@@ -25,7 +25,7 @@ class Git(Source):
     name='git'
     renderables = [ "repourl"]
 
-    def __init__(self, repourl=None, branch='master', mode='incremental',
+    def __init__(self, repourl=None, branch='HEAD', mode='incremental',
                  method=None, submodules=False, shallow=False, progress=False,
                  retryFetch=False, clobberOnFailure=False, **kwargs):
         """
@@ -88,7 +88,7 @@ def __init__(self, repourl=None, branch='master', mode='incremental',
             assert self.method in ['clean', 'fresh', 'clobber', 'copy', None]
 
     def startVC(self, branch, revision, patch):
-        self.branch = branch or 'master'
+        self.branch = branch or 'HEAD'
         self.revision = revision
         self.method = self._getMethod()
         self.stdio_log = self.addLog("stdio")
@@ -104,6 +104,8 @@ def checkInstall(gitInstalled):
             d.addCallback(lambda _: self.incremental())
         elif self.mode == 'full':
             d.addCallback(lambda _: self.full())
+        if patch:
+            d.addCallback(self.patch, patch)
         d.addCallback(self.parseGotRevision)
         d.addCallback(self.finish)
         d.addErrback(self.failed)
@@ -169,6 +171,12 @@ def incremental(self):
                     self._dovccmd(['reset', '--hard', self.revision]))
             yield wfd
             wfd.getResult()
+
+            if self.branch != 'HEAD':
+                wfd = defer.waitForDeferred(
+                    self._dovccmd(['branch', '-M', self.branch], abandonOnFailure=False))
+                yield wfd
+                wfd.getResult()
         else:
             wfd = defer.waitForDeferred(
                     self._doFetch(None))
@@ -256,11 +264,12 @@ def setrev(stdout):
         d.addCallback(setrev)
         return d
 
-    def _dovccmd(self, command, abandonOnFailure=True, collectStdout=False):
+    def _dovccmd(self, command, abandonOnFailure=True, collectStdout=False, extra_args={}):
         cmd = buildstep.RemoteShellCommand(self.workdir, ['git'] + command,
                                            env=self.env,
                                            logEnviron=self.logEnviron,
-                                           collectStdout=collectStdout)
+                                           collectStdout=collectStdout,
+                                           **extra_args)
         cmd.useLog(self.stdio_log, False)
         log.msg("Starting git command : git %s" % (" ".join(command), ))
         d = self.runCommand(cmd)
@@ -294,6 +303,20 @@ def checkout(_):
             abandonOnFailure = not self.retryFetch and not self.clobberOnFailure
             return self._dovccmd(command, abandonOnFailure)
         d.addCallback(checkout)
+        def renameBranch(res):
+            if res != 0:
+                return res
+            d = self._dovccmd(['branch', '-M', self.branch], abandonOnFailure=False)
+            # Ignore errors
+            d.addCallback(lambda _: res)
+            return d
+
+        if self.branch != 'HEAD':
+            d.addCallback(renameBranch)
+        return d
+
+    def patch(self, _, patch):
+        d = self._dovccmd(['apply', '--index'], extra_args={'initial_stdin': patch})
         return d
 
     @defer.deferredGenerator
@@ -321,9 +344,9 @@ def _doFetch(self, _):
 
     def _full(self):
         if self.shallow:
-            command = ['clone', '--depth', '1', self.repourl, '.']
+            command = ['clone', '--depth', '1', '--branch', self.branch, self.repourl, '.']
         else:
-            command = ['clone', self.repourl, '.']
+            command = ['clone', '--branch', self.branch, self.repourl, '.']
         #Fix references
         if self.prog:
             command.append('--progress')
@@ -349,7 +372,7 @@ def clobber(res):
                 if self.clobberOnFailure:
                     return self.clobber()
                 else:
-                    raise failure.Failure(res)
+                    raise buildstep.BuildStepFailed()
             else:
                 return res
         d.addCallback(clobber)


Test patch:
diff --git a/master/buildbot/test/fake/remotecommand.py b/master/buildbot/test/fake/remotecommand.py
--- a/master/buildbot/test/fake/remotecommand.py
+++ b/master/buildbot/test/fake/remotecommand.py
@@ -56,9 +56,11 @@ class FakeRemoteShellCommand(FakeLoggedRemoteCommand):
     def __init__(self, workdir, command, env=None,
                  want_stdout=1, want_stderr=1,
                  timeout=DEFAULT_TIMEOUT, maxTime=DEFAULT_MAXTIME, logfiles={},
+                 initial_stdin=None,
                  usePTY=DEFAULT_USEPTY, logEnviron=True, collectStdout=False):
         args = dict(workdir=workdir, command=command, env=env or {},
                 want_stdout=want_stdout, want_stderr=want_stderr,
+                initial_stdin=initial_stdin,
                 timeout=timeout, maxTime=maxTime, logfiles=logfiles,
                 usePTY=usePTY, logEnviron=logEnviron)
         FakeLoggedRemoteCommand.__init__(self, "shell", args,
@@ -255,11 +257,12 @@ class ExpectShell(ExpectLogged):
     non-default arguments must be specified explicitly (e.g., usePTY).
     """
     def __init__(self, workdir, command, env={},
-                 want_stdout=1, want_stderr=1,
+                 want_stdout=1, want_stderr=1, initial_stdin=None,
                  timeout=DEFAULT_TIMEOUT, maxTime=DEFAULT_MAXTIME, logfiles={},
                  usePTY=DEFAULT_USEPTY, logEnviron=True):
         args = dict(workdir=workdir, command=command, env=env,
                 want_stdout=want_stdout, want_stderr=want_stderr,
+                initial_stdin=initial_stdin,
                 timeout=timeout, maxTime=maxTime, logfiles=logfiles,
                 usePTY=usePTY, logEnviron=logEnviron)
         ExpectLogged.__init__(self, "shell", args)
diff --git a/master/buildbot/test/unit/test_steps_source_git.py b/master/buildbot/test/unit/test_steps_source_git.py
--- a/master/buildbot/test/unit/test_steps_source_git.py
+++ b/master/buildbot/test/unit/test_steps_source_git.py
@@ -44,7 +44,7 @@ def test_mode_full_clean(self):
             ExpectShell(workdir='wkdir',
                         command=['git', 'fetch', '-t',
                                  'http://github.com/buildbot/buildbot.git',
-                                 'master'])
+                                 'HEAD'])
             + 0,
             ExpectShell(workdir='wkdir',
                         command=['git', 'reset', '--hard', 'FETCH_HEAD'])
@@ -58,6 +58,107 @@ def test_mode_full_clean(self):
         self.expectOutcome(result=SUCCESS, status_text=["update"])
         return self.runStep()
 
+    def test_mode_full_clean_patch(self):
+        self.setupStep(
+                git.Git(repourl='http://github.com/buildbot/buildbot.git',
+                                    mode='full', method='clean'),
+                patch = 'patch')
+        self.expectCommands(
+            ExpectShell(workdir='wkdir',
+                        command=['git', '--version'])
+            + 0,
+            ExpectLogged('stat', dict(file='wkdir/.git',
+                                      logEnviron=True))
+            + 0,
+            ExpectShell(workdir='wkdir',
+                        command=['git', 'clean', '-f', '-d'])
+            + 0,
+            ExpectShell(workdir='wkdir',
+                        command=['git', 'fetch', '-t',
+                                 'http://github.com/buildbot/buildbot.git',
+                                 'HEAD'])
+            + 0,
+            ExpectShell(workdir='wkdir',
+                        command=['git', 'reset', '--hard', 'FETCH_HEAD'])
+            + 0,
+            ExpectShell(workdir='wkdir',
+                        command=['git', 'apply', '--index'],
+                        initial_stdin='patch')
+            + 0,
+            ExpectShell(workdir='wkdir',
+                        command=['git', 'rev-parse', 'HEAD'])
+            + ExpectShell.log('stdio',
+                stdout='f6ad368298bd941e934a41f3babc827b2aa95a1d')
+            + 0,
+        )
+        self.expectOutcome(result=SUCCESS, status_text=["update"])
+        return self.runStep()
+
+    def test_mode_full_clean_patch_fail(self):
+        self.setupStep(
+                git.Git(repourl='http://github.com/buildbot/buildbot.git',
+                                    mode='full', method='clean'),
+                patch = 'patch')
+        self.expectCommands(
+            ExpectShell(workdir='wkdir',
+                        command=['git', '--version'])
+            + 0,
+            ExpectLogged('stat', dict(file='wkdir/.git',
+                                      logEnviron=True))
+            + 0,
+            ExpectShell(workdir='wkdir',
+                        command=['git', 'clean', '-f', '-d'])
+            + 0,
+            ExpectShell(workdir='wkdir',
+                        command=['git', 'fetch', '-t',
+                                 'http://github.com/buildbot/buildbot.git',
+                                 'HEAD'])
+            + 0,
+            ExpectShell(workdir='wkdir',
+                        command=['git', 'reset', '--hard', 'FETCH_HEAD'])
+            + 0,
+            ExpectShell(workdir='wkdir',
+                        command=['git', 'apply', '--index'],
+                        initial_stdin='patch')
+            + 1,
+        )
+        self.expectOutcome(result=FAILURE, status_text=["updating"])
+        return self.runStep()
+
+    def test_mode_full_clean_branch(self):
+        self.setupStep(
+                git.Git(repourl='http://github.com/buildbot/buildbot.git',
+                                    mode='full', method='clean', branch='test-branch'))
+        self.expectCommands(
+            ExpectShell(workdir='wkdir',
+                        command=['git', '--version'])
+            + 0,
+            ExpectLogged('stat', dict(file='wkdir/.git',
+                                      logEnviron=True))
+            + 0,
+            ExpectShell(workdir='wkdir',
+                        command=['git', 'clean', '-f', '-d'])
+            + 0,
+            ExpectShell(workdir='wkdir',
+                        command=['git', 'fetch', '-t',
+                                 'http://github.com/buildbot/buildbot.git',
+                                 'test-branch'])
+            + 0,
+            ExpectShell(workdir='wkdir',
+                        command=['git', 'reset', '--hard', 'FETCH_HEAD'])
+            + 0,
+            ExpectShell(workdir='wkdir',
+                        command=['git', 'branch', '-M', 'test-branch'])
+            + 0,
+            ExpectShell(workdir='wkdir',
+                        command=['git', 'rev-parse', 'HEAD'])
+            + ExpectShell.log('stdio',
+                stdout='f6ad368298bd941e934a41f3babc827b2aa95a1d')
+            + 0,
+        )
+        self.expectOutcome(result=SUCCESS, status_text=["update"])
+        return self.runStep()
+
     def test_mode_full_clean_parsefail(self):
         self.setupStep(
                 git.Git(repourl='http://github.com/buildbot/buildbot.git',
@@ -75,7 +176,7 @@ def test_mode_full_clean_parsefail(self):
             ExpectShell(workdir='wkdir',
                         command=['git', 'fetch', '-t',
                                  'http://github.com/buildbot/buildbot.git',
-                                 'master'])
+                                 'HEAD'])
             + 0,
             ExpectShell(workdir='wkdir',
                         command=['git', 'reset', '--hard', 'FETCH_HEAD'])
@@ -101,6 +202,33 @@ def test_mode_full_clean_no_existing_repo(self):
             + 1,
             ExpectShell(workdir='wkdir',
                         command=['git', 'clone',
+                                 '--branch', 'HEAD',
+                                 'http://github.com/buildbot/buildbot.git', '.'])
+            + 0,
+            ExpectShell(workdir='wkdir',
+                        command=['git', 'rev-parse', 'HEAD'])
+            + ExpectShell.log('stdio',
+                stdout='f6ad368298bd941e934a41f3babc827b2aa95a1d')
+            + 0,
+        )
+        self.expectOutcome(result=SUCCESS, status_text=["update"])
+        return self.runStep()
+
+    def test_mode_full_clean_no_existing_repo_branch(self):
+        self.setupStep(
+                git.Git(repourl='http://github.com/buildbot/buildbot.git',
+                                    mode='full', method='clean', branch='test-branch'))
+        self.expectCommands(
+            ExpectShell(workdir='wkdir',
+                        command=['git', '--version'])
+            + 0,
+
+            ExpectLogged('stat', dict(file='wkdir/.git',
+                                      logEnviron=True))
+            + 1,
+            ExpectShell(workdir='wkdir',
+                        command=['git', 'clone',
+                                 '--branch', 'test-branch',
                                  'http://github.com/buildbot/buildbot.git', '.'])
             + 0,
             ExpectShell(workdir='wkdir',
@@ -126,6 +254,34 @@ def test_mode_full_clobber(self):
             + 0,
             ExpectShell(workdir='wkdir',
                         command=['git', 'clone',
+                                 '--branch', 'HEAD',
+                                 'http://github.com/buildbot/buildbot.git',
+                                 '.', '--progress'])
+            + 0,
+            ExpectShell(workdir='wkdir',
+                        command=['git', 'rev-parse', 'HEAD'])
+            + ExpectShell.log('stdio',
+                stdout='f6ad368298bd941e934a41f3babc827b2aa95a1d')
+            + 0,
+        )
+        self.expectOutcome(result=SUCCESS, status_text=["update"])
+        return self.runStep()
+
+    def test_mode_full_clobber_branch(self):
+        self.setupStep(
+                git.Git(repourl='http://github.com/buildbot/buildbot.git',
+                        mode='full', method='clobber', progress=True, branch='test-branch'))
+
+        self.expectCommands(
+            ExpectShell(workdir='wkdir',
+                        command=['git', '--version'])
+            + 0,
+            ExpectLogged('rmdir', dict(dir='wkdir',
+                                       logEnviron=True))
+            + 0,
+            ExpectShell(workdir='wkdir',
+                        command=['git', 'clone',
+                                 '--branch', 'test-branch',
                                  'http://github.com/buildbot/buildbot.git',
                                  '.', '--progress'])
             + 0,
@@ -152,7 +308,7 @@ def test_mode_incremental(self):
             ExpectShell(workdir='wkdir',
                         command=['git', 'fetch', '-t',
                                  'http://github.com/buildbot/buildbot.git',
-                                 'master'])
+                                 'HEAD'])
             + 0,
             ExpectShell(workdir='wkdir',
                         command=['git', 'reset', '--hard', 'FETCH_HEAD'])
@@ -167,6 +323,38 @@ def test_mode_incremental(self):
         self.expectOutcome(result=SUCCESS, status_text=["update"])
         return self.runStep()
 
+    def test_mode_incremental_branch(self):
+        self.setupStep(
+                git.Git(repourl='http://github.com/buildbot/buildbot.git',
+                                    mode='incremental', branch='test-branch'))
+        self.expectCommands(
+            ExpectShell(workdir='wkdir',
+                        command=['git', '--version'])
+            + 0,
+            ExpectLogged('stat', dict(file='wkdir/.git',
+                                      logEnviron=True))
+            + 0,
+            ExpectShell(workdir='wkdir',
+                        command=['git', 'fetch', '-t',
+                                 'http://github.com/buildbot/buildbot.git',
+                                 'test-branch'])
+            + 0,
+            ExpectShell(workdir='wkdir',
+                        command=['git', 'reset', '--hard', 'FETCH_HEAD'])
+            + 0,
+            ExpectShell(workdir='wkdir',
+                        command=['git', 'branch', '-M', 'test-branch'])
+            + 0,
+            ExpectShell(workdir='wkdir',
+                        command=['git', 'rev-parse', 'HEAD'])
+            + ExpectShell.log('stdio',
+                stdout='f6ad368298bd941e934a41f3babc827b2aa95a1d')
+            + 0,
+
+        )
+        self.expectOutcome(result=SUCCESS, status_text=["update"])
+        return self.runStep()
+
     def test_mode_full_fresh(self):
         self.setupStep(
                 git.Git(repourl='http://github.com/buildbot/buildbot.git',
@@ -184,7 +372,7 @@ def test_mode_full_fresh(self):
             ExpectShell(workdir='wkdir',
                         command=['git', 'fetch', '-t',
                                  'http://github.com/buildbot/buildbot.git',
-                                 'master'])
+                                 'HEAD'])
             + 0,
             ExpectShell(workdir='wkdir',
                         command=['git', 'reset', '--hard', 'FETCH_HEAD'])
@@ -243,7 +431,7 @@ def test_mode_full_fresh_submodule(self):
             ExpectShell(workdir='wkdir',
                         command=['git', 'fetch', '-t',
                                  'http://github.com/buildbot/buildbot.git',
-                                 'master'])
+                                 'HEAD'])
             + 0,
             ExpectShell(workdir='wkdir',
                         command=['git', 'reset', '--hard', 'FETCH_HEAD'])
@@ -278,6 +466,7 @@ def test_mode_full_clobber_shallow(self):
             + 0,
             ExpectShell(workdir='wkdir',
                         command=['git', 'clone', '--depth', '1',
+                                 '--branch', 'HEAD',
                                  'http://github.com/buildbot/buildbot.git',
                                  '.'])
             + 0,
@@ -305,7 +494,44 @@ def test_mode_incremental_retryFetch(self):
             ExpectShell(workdir='wkdir',
                         command=['git', 'fetch', '-t',
                                  'http://github.com/buildbot/buildbot.git',
-                                 'master'])
+                                 'HEAD'])
+            + 0,
+            ExpectShell(workdir='wkdir',
+                        command=['git', 'reset', '--hard', 'FETCH_HEAD'])
+            + 1,
+            ExpectShell(workdir='wkdir',
+                        command=['git', 'fetch', '-t',
+                                 'http://github.com/buildbot/buildbot.git',
+                                 'HEAD'])
+            + 0,
+            ExpectShell(workdir='wkdir',
+                        command=['git', 'reset', '--hard', 'FETCH_HEAD'])
+            + 0,
+            ExpectShell(workdir='wkdir',
+                        command=['git', 'rev-parse', 'HEAD'])
+            + ExpectShell.log('stdio',
+                stdout='f6ad368298bd941e934a41f3babc827b2aa95a1d')
+            + 0,
+        )
+        self.expectOutcome(result=SUCCESS, status_text=["update"])
+        return self.runStep()
+
+    def test_mode_incremental_retryFetch_branch(self):
+        self.setupStep(
+                git.Git(repourl='http://github.com/buildbot/buildbot.git',
+                        mode='incremental', retryFetch=True, branch='test-branch'))
+
+        self.expectCommands(
+            ExpectShell(workdir='wkdir',
+                        command=['git', '--version'])
+            + 0,
+            ExpectLogged('stat', dict(file='wkdir/.git',
+                                      logEnviron=True))
+            + 0,
+            ExpectShell(workdir='wkdir',
+                        command=['git', 'fetch', '-t',
+                                 'http://github.com/buildbot/buildbot.git',
+                                 'test-branch'])
             + 0,
             ExpectShell(workdir='wkdir',
                         command=['git', 'reset', '--hard', 'FETCH_HEAD'])
@@ -313,11 +539,14 @@ def test_mode_incremental_retryFetch(self):
             ExpectShell(workdir='wkdir',
                         command=['git', 'fetch', '-t',
                                  'http://github.com/buildbot/buildbot.git',
-                                 'master'])
+                                 'test-branch'])
             + 0,
             ExpectShell(workdir='wkdir',
                         command=['git', 'reset', '--hard', 'FETCH_HEAD'])
             + 0,
+            ExpectShell(workdir='wkdir',
+                        command=['git', 'branch', '-M', 'test-branch'])
+            + 0,
             ExpectShell(workdir='wkdir',
                         command=['git', 'rev-parse', 'HEAD'])
             + ExpectShell.log('stdio',
@@ -342,7 +571,45 @@ def test_mode_incremental_clobberOnFailure(self):
             ExpectShell(workdir='wkdir',
                         command=['git', 'fetch', '-t',
                                  'http://github.com/buildbot/buildbot.git',
-                                 'master'])
+                                 'HEAD'])
+            + 0,
+            ExpectShell(workdir='wkdir',
+                        command=['git', 'reset', '--hard', 'FETCH_HEAD'])
+            + 1,
+            ExpectLogged('rmdir', dict(dir='wkdir',
+                                       logEnviron=True))
+            + 0,
+            ExpectShell(workdir='wkdir',
+                        command=['git', 'clone',
+                                 '--branch', 'HEAD',
+                                 'http://github.com/buildbot/buildbot.git',
+                                 '.'])
+            + 0,
+            ExpectShell(workdir='wkdir',
+                        command=['git', 'rev-parse', 'HEAD'])
+            + ExpectShell.log('stdio',
+                stdout='f6ad368298bd941e934a41f3babc827b2aa95a1d')
+            + 0,
+        )
+        self.expectOutcome(result=SUCCESS, status_text=["update"])
+        return self.runStep()
+
+    def test_mode_incremental_clobberOnFailure_branch(self):
+        self.setupStep(
+                git.Git(repourl='http://github.com/buildbot/buildbot.git',
+                        mode='incremental', clobberOnFailure=True, branch = 'test-branch'))
+
+        self.expectCommands(
+            ExpectShell(workdir='wkdir',
+                        command=['git', '--version'])
+            + 0,
+            ExpectLogged('stat', dict(file='wkdir/.git',
+                                      logEnviron=True))
+            + 0,
+            ExpectShell(workdir='wkdir',
+                        command=['git', 'fetch', '-t',
+                                 'http://github.com/buildbot/buildbot.git',
+                                 'test-branch'])
             + 0,
             ExpectShell(workdir='wkdir',
                         command=['git', 'reset', '--hard', 'FETCH_HEAD'])
@@ -352,6 +619,7 @@ def test_mode_incremental_clobberOnFailure(self):
             + 0,
             ExpectShell(workdir='wkdir',
                         command=['git', 'clone',
+                                 '--branch', 'test-branch',
                                  'http://github.com/buildbot/buildbot.git',
                                  '.'])
             + 0,
@@ -381,7 +649,7 @@ def test_mode_full_copy(self):
             ExpectShell(workdir='source',
                         command=['git', 'fetch', '-t',
                                  'http://github.com/buildbot/buildbot.git',
-                                 'master'])
+                                 'HEAD'])
             + 0,
             ExpectShell(workdir='source',
                         command=['git', 'reset', '--hard', 'FETCH_HEAD'])
@@ -412,6 +680,7 @@ def test_mode_incremental_no_existing_repo(self):
             + 1,
             ExpectShell(workdir='wkdir',
                         command=['git', 'clone',
+                                 '--branch', 'HEAD',
                                  'http://github.com/buildbot/buildbot.git', '.'])
             + 0,
             ExpectShell(workdir='wkdir',
@@ -439,6 +708,7 @@ def test_mode_full_clobber_given_revision(self):
             + 0,
             ExpectShell(workdir='wkdir',
                         command=['git', 'clone',
+                                 '--branch', 'HEAD',
                                  'http://github.com/buildbot/buildbot.git',
                                  '.', '--progress'])
             + 0,
@@ -469,6 +739,7 @@ def test_revparse_failure(self):
             + 0,
             ExpectShell(workdir='wkdir',
                         command=['git', 'clone',
+                                 '--branch', 'HEAD',
                                  'http://github.com/buildbot/buildbot.git',
                                  '.', '--progress'])
             + 0,
@@ -498,6 +769,7 @@ def test_mode_full_clobber_submodule(self):
             + 0,
             ExpectShell(workdir='wkdir',
                         command=['git', 'clone',
+                                 '--branch', 'HEAD',
                                  'http://github.com/buildbot/buildbot.git', '.'])
             + 0,
             ExpectShell(workdir='wkdir',
@@ -532,6 +804,7 @@ def test_mode_full_fresh_revision(self):
             + 1,
             ExpectShell(workdir='wkdir',
                         command=['git', 'clone',
+                                 '--branch', 'HEAD',
                                  'http://github.com/buildbot/buildbot.git',
                                  '.', '--progress'])
             + 0,
@@ -561,6 +834,7 @@ def test_mode_full_fresh_clobberOnFailure(self):
             + 1,
             ExpectShell(workdir='wkdir',
                         command=['git', 'clone',
+                                 '--branch', 'HEAD',
                                  'http://github.com/buildbot/buildbot.git', '.'])
             + 1,
             ExpectLogged('rmdir', dict(dir='wkdir',
@@ -568,6 +842,7 @@ def test_mode_full_fresh_clobberOnFailure(self):
             + 0,
             ExpectShell(workdir='wkdir',
                         command=['git', 'clone', 
+                                 '--branch', 'HEAD',
                                  'http://github.com/buildbot/buildbot.git',
                                  '.'])
             + 0,
@@ -597,7 +872,7 @@ def test_mode_full_no_method(self):
             ExpectShell(workdir='wkdir',
                         command=['git', 'fetch', '-t',
                                  'http://github.com/buildbot/buildbot.git',
-                                 'master'])
+                                 'HEAD'])
             + 0,
             ExpectShell(workdir='wkdir',
                         command=['git', 'reset', '--hard', 'FETCH_HEAD'])
@@ -630,7 +905,7 @@ def test_mode_full_with_env(self):
             ExpectShell(workdir='wkdir',
                         command=['git', 'fetch', '-t',
                                  'http://github.com/buildbot/buildbot.git',
-                                 'master'],
+                                 'HEAD'],
                         env={'abc': '123'})
             + 0,
             ExpectShell(workdir='wkdir',
@@ -666,7 +941,7 @@ def test_mode_full_logEnviron(self):
             ExpectShell(workdir='wkdir',
                         command=['git', 'fetch', '-t',
                                  'http://github.com/buildbot/buildbot.git',
-                                 'master'],
+                                 'HEAD'],
                         logEnviron=False)
             + 0,
             ExpectShell(workdir='wkdir',
diff --git a/master/buildbot/test/util/sourcesteps.py b/master/buildbot/test/util/sourcesteps.py
--- a/master/buildbot/test/util/sourcesteps.py
+++ b/master/buildbot/test/util/sourcesteps.py
@@ -38,7 +38,7 @@ def tearDownSourceStep(self):
 
     # utilities
 
-    def setupStep(self, step, args={}, **kwargs):
+    def setupStep(self, step, args={}, patch=None, **kwargs):
         """
         Set up C{step} for testing.  This calls L{BuildStepMixin}'s C{setupStep}
         and then does setup specific to a Source step.
@@ -51,7 +51,7 @@ def setupStep(self, step, args={}, **kwargs):
         ss.revision = args.get('revision', None)
         ss.project = ''
         ss.repository = ''
-        ss.patch = None
+        ss.patch = patch
         ss.patch_info = None
         ss.changes = []