Merge "Use display.display for executor debug messages" into feature/zuulv3
diff --git a/tests/base.py b/tests/base.py
index 76f7e03..7d33ffc 100755
--- a/tests/base.py
+++ b/tests/base.py
@@ -776,21 +776,6 @@
         repo = self._getRepo()
         return repo.references[self._getPRReference()].commit.hexsha
 
-    def setStatus(self, sha, state, url, description, context, user='zuul'):
-        # Since we're bypassing github API, which would require a user, we
-        # hard set the user as 'zuul' here.
-        # insert the status at the top of the list, to simulate that it
-        # is the most recent set status
-        self.statuses[sha].insert(0, ({
-            'state': state,
-            'url': url,
-            'description': description,
-            'context': context,
-            'creator': {
-                'login': user
-            }
-        }))
-
     def addReview(self, user, state, granted_on=None):
         gh_time_format = '%Y-%m-%dT%H:%M:%SZ'
         # convert the timestamp to a str format that would be returned
@@ -882,6 +867,7 @@
         self.connection_name = connection_name
         self.pr_number = 0
         self.pull_requests = []
+        self.statuses = {}
         self.upstream_root = upstream_root
         self.merge_failure = False
         self.merge_not_allowed_count = 0
@@ -949,7 +935,8 @@
                 'repo': {
                     'full_name': pr.project
                 }
-            }
+            },
+            'files': pr.files
         }
         return data
 
@@ -960,10 +947,6 @@
         pr = prs[0]
         return self.getPull(pr.project, pr.number)
 
-    def getPullFileNames(self, project, number):
-        pr = self.pull_requests[number - 1]
-        return pr.files
-
     def _getPullReviews(self, owner, project, number):
         pr = self.pull_requests[number - 1]
         return pr.reviews
@@ -1014,25 +997,24 @@
         pull_request.merge_message = commit_message
 
     def getCommitStatuses(self, project, sha):
-        owner, proj = project.split('/')
-        for pr in self.pull_requests:
-            pr_owner, pr_project = pr.project.split('/')
-            # This is somewhat risky, if the same commit exists in multiple
-            # PRs, we might grab the wrong one that doesn't have a status
-            # that is expected to be there. Maybe re-work this so that there
-            # is a global registry of commit statuses like with github.
-            if (pr_owner == owner and pr_project == proj and
-                sha in pr.statuses):
-                return pr.statuses[sha]
+        return self.statuses.get(project, {}).get(sha, [])
 
-    def setCommitStatus(self, project, sha, state,
-                        url='', description='', context=''):
-        owner, proj = project.split('/')
-        for pr in self.pull_requests:
-            pr_owner, pr_project = pr.project.split('/')
-            if (pr_owner == owner and pr_project == proj and
-                pr.head_sha == sha):
-                pr.setStatus(sha, state, url, description, context)
+    def setCommitStatus(self, project, sha, state, url='', description='',
+                        context='default', user='zuul'):
+        # always insert a status to the front of the list, to represent
+        # the last status provided for a commit.
+        # Since we're bypassing github API, which would require a user, we
+        # default the user as 'zuul' here.
+        self.statuses.setdefault(project, {}).setdefault(sha, [])
+        self.statuses[project][sha].insert(0, {
+            'state': state,
+            'url': url,
+            'description': description,
+            'context': context,
+            'creator': {
+                'login': user
+            }
+        })
 
     def labelPull(self, project, pr_number, label):
         pull_request = self.pull_requests[pr_number - 1]
diff --git a/tests/unit/test_github_driver.py b/tests/unit/test_github_driver.py
index 4979087..ba8e497 100644
--- a/tests/unit/test_github_driver.py
+++ b/tests/unit/test_github_driver.py
@@ -248,16 +248,18 @@
 
     @simple_layout('layouts/reporting-github.yaml', driver='github')
     def test_reporting(self):
+        project = 'org/project'
         # pipeline reports pull status both on start and success
         self.executor_server.hold_jobs_in_build = True
-        A = self.fake_github.openFakePullRequest('org/project', 'master', 'A')
+        A = self.fake_github.openFakePullRequest(project, 'master', 'A')
         self.fake_github.emitEvent(A.getPullRequestOpenedEvent())
         self.waitUntilSettled()
         # We should have a status container for the head sha
-        self.assertIn(A.head_sha, A.statuses.keys())
+        statuses = self.fake_github.statuses[project][A.head_sha]
+        self.assertIn(A.head_sha, self.fake_github.statuses[project].keys())
         # We should only have one status for the head sha
-        self.assertEqual(1, len(A.statuses[A.head_sha]))
-        check_status = A.statuses[A.head_sha][0]
+        self.assertEqual(1, len(statuses))
+        check_status = statuses[0]
         check_url = ('http://zuul.example.com/status/#%s,%s' %
                      (A.number, A.head_sha))
         self.assertEqual('tenant-one/check', check_status['context'])
@@ -270,8 +272,9 @@
         self.executor_server.release()
         self.waitUntilSettled()
         # We should only have two statuses for the head sha
-        self.assertEqual(2, len(A.statuses[A.head_sha]))
-        check_status = A.statuses[A.head_sha][0]
+        statuses = self.fake_github.statuses[project][A.head_sha]
+        self.assertEqual(2, len(statuses))
+        check_status = statuses[0]
         check_url = ('http://zuul.example.com/status/#%s,%s' %
                      (A.number, A.head_sha))
         self.assertEqual('tenant-one/check', check_status['context'])
@@ -286,7 +289,8 @@
         self.fake_github.emitEvent(
             A.getCommentAddedEvent('reporting check'))
         self.waitUntilSettled()
-        self.assertEqual(2, len(A.statuses[A.head_sha]))
+        statuses = self.fake_github.statuses[project][A.head_sha]
+        self.assertEqual(2, len(statuses))
         # comments increased by one for the start message
         self.assertEqual(2, len(A.comments))
         self.assertThat(A.comments[1],
@@ -295,8 +299,9 @@
         self.executor_server.release()
         self.waitUntilSettled()
         # pipeline reports success status
-        self.assertEqual(3, len(A.statuses[A.head_sha]))
-        report_status = A.statuses[A.head_sha][0]
+        statuses = self.fake_github.statuses[project][A.head_sha]
+        self.assertEqual(3, len(statuses))
+        report_status = statuses[0]
         self.assertEqual('tenant-one/reporting', report_status['context'])
         self.assertEqual('success', report_status['state'])
         self.assertEqual(2, len(A.comments))
diff --git a/tests/unit/test_github_requirements.py b/tests/unit/test_github_requirements.py
index 5dd6e80..301ea2f 100644
--- a/tests/unit/test_github_requirements.py
+++ b/tests/unit/test_github_requirements.py
@@ -25,7 +25,8 @@
     @simple_layout('layouts/requirements-github.yaml', driver='github')
     def test_pipeline_require_status(self):
         "Test pipeline requirement: status"
-        A = self.fake_github.openFakePullRequest('org/project1', 'master', 'A')
+        project = 'org/project1'
+        A = self.fake_github.openFakePullRequest(project, 'master', 'A')
         # A comment event that we will keep submitting to trigger
         comment = A.getCommentAddedEvent('test me')
         self.fake_github.emitEvent(comment)
@@ -34,13 +35,15 @@
         self.assertEqual(len(self.history), 0)
 
         # An error status should not cause it to be enqueued
-        A.setStatus(A.head_sha, 'error', 'null', 'null', 'check')
+        self.fake_github.setCommitStatus(project, A.head_sha, 'error',
+                                         context='check')
         self.fake_github.emitEvent(comment)
         self.waitUntilSettled()
         self.assertEqual(len(self.history), 0)
 
         # A success status goes in
-        A.setStatus(A.head_sha, 'success', 'null', 'null', 'check')
+        self.fake_github.setCommitStatus(project, A.head_sha, 'success',
+                                         context='check')
         self.fake_github.emitEvent(comment)
         self.waitUntilSettled()
         self.assertEqual(len(self.history), 1)
@@ -49,7 +52,8 @@
     @simple_layout('layouts/requirements-github.yaml', driver='github')
     def test_trigger_require_status(self):
         "Test trigger requirement: status"
-        A = self.fake_github.openFakePullRequest('org/project1', 'master', 'A')
+        project = 'org/project1'
+        A = self.fake_github.openFakePullRequest(project, 'master', 'A')
         # A comment event that we will keep submitting to trigger
         comment = A.getCommentAddedEvent('trigger me')
         self.fake_github.emitEvent(comment)
@@ -58,13 +62,15 @@
         self.assertEqual(len(self.history), 0)
 
         # An error status should not cause it to be enqueued
-        A.setStatus(A.head_sha, 'error', 'null', 'null', 'check')
+        self.fake_github.setCommitStatus(project, A.head_sha, 'error',
+                                         context='check')
         self.fake_github.emitEvent(comment)
         self.waitUntilSettled()
         self.assertEqual(len(self.history), 0)
 
         # A success status goes in
-        A.setStatus(A.head_sha, 'success', 'null', 'null', 'check')
+        self.fake_github.setCommitStatus(project, A.head_sha, 'success',
+                                         context='check')
         self.fake_github.emitEvent(comment)
         self.waitUntilSettled()
         self.assertEqual(len(self.history), 1)
@@ -73,10 +79,12 @@
     @simple_layout('layouts/requirements-github.yaml', driver='github')
     def test_trigger_on_status(self):
         "Test trigger on: status"
-        A = self.fake_github.openFakePullRequest('org/project2', 'master', 'A')
+        project = 'org/project2'
+        A = self.fake_github.openFakePullRequest(project, 'master', 'A')
 
         # An error status should not cause it to be enqueued
-        A.setStatus(A.head_sha, 'error', 'null', 'null', 'check')
+        self.fake_github.setCommitStatus(project, A.head_sha, 'error',
+                                         context='check')
         self.fake_github.emitEvent(A.getCommitStatusEvent('check',
                                                           state='error'))
         self.waitUntilSettled()
@@ -84,7 +92,8 @@
 
         # A success status from unknown user should not cause it to be
         # enqueued
-        A.setStatus(A.head_sha, 'success', 'null', 'null', 'check', user='foo')
+        self.fake_github.setCommitStatus(project, A.head_sha, 'success',
+                                         context='check', user='foo')
         self.fake_github.emitEvent(A.getCommitStatusEvent('check',
                                                           state='success',
                                                           user='foo'))
@@ -92,7 +101,8 @@
         self.assertEqual(len(self.history), 0)
 
         # A success status from zuul goes in
-        A.setStatus(A.head_sha, 'success', 'null', 'null', 'check')
+        self.fake_github.setCommitStatus(project, A.head_sha, 'success',
+                                         context='check')
         self.fake_github.emitEvent(A.getCommitStatusEvent('check'))
         self.waitUntilSettled()
         self.assertEqual(len(self.history), 1)
@@ -100,7 +110,8 @@
 
         # An error status for a different context should not cause it to be
         # enqueued
-        A.setStatus(A.head_sha, 'error', 'null', 'null', 'gate')
+        self.fake_github.setCommitStatus(project, A.head_sha, 'error',
+                                         context='gate')
         self.fake_github.emitEvent(A.getCommitStatusEvent('gate',
                                                           state='error'))
         self.waitUntilSettled()
diff --git a/tests/unit/test_scheduler.py b/tests/unit/test_scheduler.py
index d9e20be..839007d 100755
--- a/tests/unit/test_scheduler.py
+++ b/tests/unit/test_scheduler.py
@@ -1516,7 +1516,7 @@
         trusted, project = tenant.getProject('org/project')
         url = self.fake_gerrit.getGitUrl(project)
         self.executor_server.merger._addProject('review.example.com',
-                                                'org/project', url)
+                                                'org/project', url, None)
         A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
         A.addPatchset(large=True)
         # TODOv3(jeblair): add hostname to upstream root
diff --git a/zuul/ansible/library/zuul_console.py b/zuul/ansible/library/zuul_console.py
index 7f8a1b6..42f41f0 100644
--- a/zuul/ansible/library/zuul_console.py
+++ b/zuul/ansible/library/zuul_console.py
@@ -15,10 +15,12 @@
 # You should have received a copy of the GNU General Public License
 # along with this software.  If not, see <http://www.gnu.org/licenses/>.
 
+import glob
 import os
 import sys
 import select
 import socket
+import subprocess
 import threading
 import time
 
@@ -196,6 +198,53 @@
                 pass
 
 
+def get_inode(port_number=19885):
+    for netfile in ('/proc/net/tcp6', '/proc/net/tcp'):
+        if not os.path.exists(netfile):
+            continue
+        with open(netfile) as f:
+            # discard header line
+            f.readline()
+            for line in f:
+                # sl local_address rem_address st tx_queue:rx_queue tr:tm->when
+                # retrnsmt   uid  timeout inode
+                fields = line.split()
+                # Format is localaddr:localport in hex
+                port = int(fields[1].split(':')[1], base=16)
+                if port == port_number:
+                    return fields[9]
+
+
+def get_pid_from_inode(inode):
+    my_euid = os.geteuid()
+    exceptions = []
+    for d in os.listdir('/proc'):
+        try:
+            try:
+                int(d)
+            except Exception as e:
+                continue
+            d_abs_path = os.path.join('/proc', d)
+            if os.stat(d_abs_path).st_uid != my_euid:
+                continue
+            fd_dir = os.path.join(d_abs_path, 'fd')
+            if os.path.exists(fd_dir):
+                if os.stat(fd_dir).st_uid != my_euid:
+                    continue
+                for fd in os.listdir(fd_dir):
+                    try:
+                        fd_path = os.path.join(fd_dir, fd)
+                        if os.path.islink(fd_path):
+                            target = os.readlink(fd_path)
+                            if '[' + inode + ']' in target:
+                                return d, exceptions
+                    except Exception as e:
+                        exceptions.append(e)
+        except Exception as e:
+            exceptions.append(e)
+    return None, exceptions
+
+
 def test():
     s = Server(LOG_STREAM_FILE, LOG_STREAM_PORT)
     s.run()
@@ -206,19 +255,54 @@
         argument_spec=dict(
             path=dict(default=LOG_STREAM_FILE),
             port=dict(default=LOG_STREAM_PORT, type='int'),
+            state=dict(default='present', choices=['absent', 'present']),
         )
     )
 
     p = module.params
     path = p['path']
     port = p['port']
+    state = p['state']
 
-    if daemonize():
+    if state == 'present':
+        if daemonize():
+            module.exit_json()
+
+        s = Server(path, port)
+        s.run()
+    else:
+        pid = None
+        exceptions = []
+        inode = get_inode()
+        if not inode:
+            module.fail_json(
+                "Could not find inode for port",
+                exceptions=[])
+
+        pid, exceptions = get_pid_from_inode(inode)
+        if not pid:
+            except_strings = [str(e) for e in exceptions]
+            module.fail_json(
+                msg="Could not find zuul_console process for inode",
+                exceptions=except_strings)
+
+        try:
+            subprocess.check_output(['kill', pid])
+        except subprocess.CalledProcessError as e:
+            module.fail_json(
+                msg="Could not kill zuul_console pid",
+                exceptions=[str(e)])
+
+        for fn in glob.glob(LOG_STREAM_FILE.format(log_uuid='*')):
+            try:
+                os.unlink(fn)
+            except Exception as e:
+                module.fail_json(
+                    msg="Could not remove logfile {fn}".format(fn=fn),
+                    exceptions=[str(e)])
+
         module.exit_json()
 
-    s = Server(path, port)
-    s.run()
-
 from ansible.module_utils.basic import *  # noqa
 from ansible.module_utils.basic import AnsibleModule
 
diff --git a/zuul/configloader.py b/zuul/configloader.py
index 029b840..5e0fe65 100644
--- a/zuul/configloader.py
+++ b/zuul/configloader.py
@@ -327,6 +327,7 @@
                'dependencies': to_list(str),
                'allowed-projects': to_list(str),
                'override-branch': str,
+               'description': str,
                }
 
         return vs.Schema(job)
diff --git a/zuul/driver/github/githubconnection.py b/zuul/driver/github/githubconnection.py
index 659d88b..4910e51 100644
--- a/zuul/driver/github/githubconnection.py
+++ b/zuul/driver/github/githubconnection.py
@@ -118,6 +118,12 @@
             event = None
 
         if event:
+            if event.change_number:
+                project = self.connection.source.getProject(event.project_name)
+                self.connection._getChange(project,
+                                           event.change_number,
+                                           event.patch_number,
+                                           refresh=True)
             event.project_hostname = self.connection.canonical_hostname
             self.connection.logEvent(event)
             self.connection.sched.addEvent(event)
@@ -463,28 +469,20 @@
             if change not in relevant:
                 del self._change_cache[key]
 
-    def getChange(self, event):
+    def getChange(self, event, refresh=False):
         """Get the change representing an event."""
 
         project = self.source.getProject(event.project_name)
         if event.change_number:
-            change = PullRequest(event.project_name)
-            change.project = project
-            change.number = event.change_number
+            change = self._getChange(project, event.change_number,
+                                     event.patch_number, refresh=refresh)
             change.refspec = event.refspec
             change.branch = event.branch
             change.url = event.change_url
             change.updated_at = self._ghTimestampToDate(event.updated_at)
-            change.patchset = event.patch_number
-            change.files = self.getPullFileNames(project, change.number)
-            change.title = event.title
-            change.status = self._get_statuses(project, event.patch_number)
-            change.reviews = self.getPullReviews(project, change.number)
             change.source_event = event
-            change.open = self.getPullOpen(event.project_name, change.number)
-            change.is_current_patchset = self.getIsCurrent(event.project_name,
-                                                           change.number,
-                                                           event.patch_number)
+            change.is_current_patchset = (change.pr.get('head').get('sha') ==
+                                          event.patch_number)
         elif event.ref:
             change = Ref(project)
             change.ref = event.ref
@@ -497,6 +495,38 @@
             change = Ref(project)
         return change
 
+    def _getChange(self, project, number, patchset, refresh=False):
+        key = '%s/%s/%s' % (project.name, number, patchset)
+        change = self._change_cache.get(key)
+        if change and not refresh:
+            return change
+        if not change:
+            change = PullRequest(project.name)
+            change.project = project
+            change.number = number
+            change.patchset = patchset
+        self._change_cache[key] = change
+        try:
+            self._updateChange(change)
+        except Exception:
+            if key in self._change_cache:
+                del self._change_cache[key]
+            raise
+        return change
+
+    def _updateChange(self, change):
+        self.log.info("Updating %s" % (change,))
+        change.pr = self.getPull(change.project.name, change.number)
+        change.files = change.pr.get('files')
+        change.title = change.pr.get('title')
+        change.open = change.pr.get('state') == 'open'
+        change.status = self._get_statuses(change.project,
+                                           change.patchset)
+        change.reviews = self.getPullReviews(change.project,
+                                             change.number)
+
+        return change
+
     def getGitUrl(self, project):
         if self.git_ssh_key:
             return 'ssh://git@%s/%s.git' % (self.git_host, project)
@@ -535,7 +565,9 @@
     def getPull(self, project_name, number):
         github = self.getGithubClient(project_name)
         owner, proj = project_name.split('/')
-        pr = github.pull_request(owner, proj, number).as_dict()
+        probj = github.pull_request(owner, proj, number)
+        pr = probj.as_dict()
+        pr['files'] = [f.filename for f in probj.files()]
         log_rate_limit(self.log, github)
         return pr
 
@@ -578,14 +610,6 @@
             return None
         return pulls.pop()
 
-    def getPullFileNames(self, project, number):
-        github = self.getGithubClient(project)
-        owner, proj = project.name.split('/')
-        filenames = [f.filename for f in
-                     github.pull_request(owner, proj, number).files()]
-        log_rate_limit(self.log, github)
-        return filenames
-
     def getPullReviews(self, project, number):
         owner, proj = project.name.split('/')
 
@@ -723,14 +747,6 @@
         pull_request.remove_label(label)
         log_rate_limit(self.log, github)
 
-    def getPullOpen(self, project, number):
-        pr = self.getPull(project, number)
-        return pr.get('state') == 'open'
-
-    def getIsCurrent(self, project, number, sha):
-        pr = self.getPull(project, number)
-        return pr.get('head').get('sha') == sha
-
     def getPushedFileNames(self, event):
         files = set()
         for c in event.commits:
diff --git a/zuul/driver/github/githubsource.py b/zuul/driver/github/githubsource.py
index 1350b10..1c2f727 100644
--- a/zuul/driver/github/githubsource.py
+++ b/zuul/driver/github/githubsource.py
@@ -58,8 +58,8 @@
         """Called after configuration has been processed."""
         pass
 
-    def getChange(self, event):
-        return self.connection.getChange(event)
+    def getChange(self, event, refresh=False):
+        return self.connection.getChange(event, refresh)
 
     def getProject(self, name):
         p = self.connection.getProject(name)
@@ -87,10 +87,6 @@
         """Get the git-web url for a project."""
         return self.connection.getGitwebUrl(project, sha)
 
-    def getPullFiles(self, project, number):
-        """Get filenames of the pull request"""
-        return self.connection.getPullFileNames(project, number)
-
     def _ghTimestampToDate(self, timestamp):
         return time.strptime(timestamp, '%Y-%m-%dT%H:%M:%SZ')
 
diff --git a/zuul/executor/server.py b/zuul/executor/server.py
index f44fd50..c498fa4 100644
--- a/zuul/executor/server.py
+++ b/zuul/executor/server.py
@@ -363,6 +363,7 @@
         self.hostname = socket.gethostname()
         self.zuul_url = config.get('merger', 'zuul_url')
         self.merger_lock = threading.Lock()
+        self.verbose = False
         self.command_map = dict(
             stop=self.stop,
             pause=self.pause,
@@ -522,12 +523,10 @@
         pass
 
     def verboseOn(self):
-        # TODOv3: implement
-        pass
+        self.verbose = True
 
     def verboseOff(self):
-        # TODOv3: implement
-        pass
+        self.verbose = False
 
     def join(self):
         self.update_thread.join()
@@ -1308,7 +1307,7 @@
         env_copy = os.environ.copy()
         env_copy['LOGNAME'] = 'zuul'
 
-        if False:  # TODOv3: self.options['verbose']:
+        if self.executor_server.verbose:
             verbose = '-vvv'
         else:
             verbose = '-v'
diff --git a/zuul/lib/log_streamer.py b/zuul/lib/log_streamer.py
index 59d5240..6695723 100644
--- a/zuul/lib/log_streamer.py
+++ b/zuul/lib/log_streamer.py
@@ -144,6 +144,11 @@
                 else:
                     break
 
+            # See if the file has been removed, meaning we should stop
+            # streaming it.
+            if not os.path.exists(log.path):
+                return False
+
             # At this point, we are waiting for more data to be written
             time.sleep(0.5)
 
@@ -159,16 +164,6 @@
                 if not ret:
                     return False
 
-            # See if the file has been truncated
-            try:
-                st = os.stat(log.path)
-                if (st.st_ino != log.stat.st_ino or
-                    st.st_size < log.size):
-                    return True
-            except Exception:
-                return True
-            log.size = st.st_size
-
 
 class CustomForkingTCPServer(ss.ForkingTCPServer):
     '''
diff --git a/zuul/merger/merger.py b/zuul/merger/merger.py
index 6cfd904..2ac0de8 100644
--- a/zuul/merger/merger.py
+++ b/zuul/merger/merger.py
@@ -42,12 +42,16 @@
 
 
 class Repo(object):
-    def __init__(self, remote, local, email, username,
+    def __init__(self, remote, local, email, username, sshkey=None,
                  cache_path=None, logger=None):
         if logger is None:
             self.log = logging.getLogger("zuul.Repo")
         else:
             self.log = logger
+        if sshkey:
+            self.env = {'GIT_SSH_COMMAND': 'ssh -i %s' % (sshkey,)}
+        else:
+            self.env = {}
         self.remote_url = remote
         self.local_path = local
         self.email = email
@@ -69,11 +73,14 @@
             self.log.debug("Cloning from %s to %s" % (self.remote_url,
                                                       self.local_path))
             if self.cache_path:
-                git.Repo.clone_from(self.cache_path, self.local_path)
+                git.Repo.clone_from(self.cache_path, self.local_path,
+                                    env=self.env)
                 rewrite_url = True
             else:
-                git.Repo.clone_from(self.remote_url, self.local_path)
+                git.Repo.clone_from(self.remote_url, self.local_path,
+                                    env=self.env)
         repo = git.Repo(self.local_path)
+        repo.git.update_environment(**self.env)
         # Create local branches corresponding to all the remote branches
         if not repo_is_cloned:
             origin = repo.remotes.origin
@@ -98,6 +105,7 @@
     def createRepoObject(self):
         self._ensure_cloned()
         repo = git.Repo(self.local_path)
+        repo.git.update_environment(**self.env)
         return repo
 
     def reset(self):
@@ -282,23 +290,7 @@
         self.username = username
         self.cache_root = cache_root
 
-    def _get_ssh_cmd(self, connection_name):
-        sshkey = self.connections.connections.get(connection_name).\
-            connection_config.get('sshkey')
-        if sshkey:
-            return 'ssh -i %s' % sshkey
-        else:
-            return None
-
-    def _setGitSsh(self, connection_name):
-        wrapper_name = '.ssh_wrapper_%s' % connection_name
-        name = os.path.join(self.working_root, wrapper_name)
-        if os.path.isfile(name):
-            os.environ['GIT_SSH'] = name
-        elif 'GIT_SSH' in os.environ:
-            del os.environ['GIT_SSH']
-
-    def _addProject(self, hostname, project_name, url):
+    def _addProject(self, hostname, project_name, url, sshkey):
         repo = None
         key = '/'.join([hostname, project_name])
         try:
@@ -308,8 +300,8 @@
                                           project_name)
             else:
                 cache_path = None
-            repo = Repo(url, path, self.email, self.username, cache_path,
-                        self.logger)
+            repo = Repo(url, path, self.email, self.username,
+                        sshkey, cache_path, self.logger)
 
             self.repos[key] = repo
         except Exception:
@@ -325,11 +317,13 @@
         key = '/'.join([hostname, project_name])
         if key in self.repos:
             return self.repos[key]
+        sshkey = self.connections.connections.get(connection_name).\
+            connection_config.get('sshkey')
         if not url:
             raise Exception("Unable to set up repo for project %s/%s"
                             " without a url" %
                             (connection_name, project_name,))
-        return self._addProject(hostname, project_name, url)
+        return self._addProject(hostname, project_name, url, sshkey)
 
     def updateRepo(self, connection_name, project_name):
         # TODOv3(jhesketh): Reimplement
@@ -437,28 +431,26 @@
         else:
             self.log.debug("Found base commit %s for %s" % (base, key,))
         # Merge the change
-        with repo.createRepoObject().git.custom_environment(
-            GIT_SSH_COMMAND=self._get_ssh_cmd(item['connection'])):
-            commit = self._mergeChange(item, base)
-            if not commit:
+        commit = self._mergeChange(item, base)
+        if not commit:
+            return None
+        # Store this commit as the most recent for this project-branch
+        recent[key] = commit
+        # Set the Zuul ref for this item to point to the most recent
+        # commits of each project-branch
+        for key, mrc in recent.items():
+            connection, project, branch = key
+            zuul_ref = None
+            try:
+                repo = self.getRepo(connection, project)
+                zuul_ref = branch + '/' + item['ref']
+                if not repo.getCommitFromRef(zuul_ref):
+                    repo.createZuulRef(zuul_ref, mrc)
+            except Exception:
+                self.log.exception("Unable to set zuul ref %s for "
+                                   "item %s" % (zuul_ref, item))
                 return None
-            # Store this commit as the most recent for this project-branch
-            recent[key] = commit
-            # Set the Zuul ref for this item to point to the most recent
-            # commits of each project-branch
-            for key, mrc in recent.items():
-                connection, project, branch = key
-                zuul_ref = None
-                try:
-                    repo = self.getRepo(connection, project)
-                    zuul_ref = branch + '/' + item['ref']
-                    if not repo.getCommitFromRef(zuul_ref):
-                        repo.createZuulRef(zuul_ref, mrc)
-                except Exception:
-                    self.log.exception("Unable to set zuul ref %s for "
-                                       "item %s" % (zuul_ref, item))
-                    return None
-            return commit
+        return commit
 
     def mergeChanges(self, items, files=None, repo_state=None):
         # connection+project+branch -> commit