From 2803f4cd335701abfe478ec6504229fc66d4040c Mon Sep 17 00:00:00 2001 From: Szymon Martynuska Date: Mon, 30 Apr 2012 17:16:58 +0200 Subject: [PATCH] Deleting automatically created workspace after an exception. --- GitTfs.VsCommon/TfsHelper.Common.cs | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/GitTfs.VsCommon/TfsHelper.Common.cs b/GitTfs.VsCommon/TfsHelper.Common.cs index 75f2d92e0..380a59381 100644 --- a/GitTfs.VsCommon/TfsHelper.Common.cs +++ b/GitTfs.VsCommon/TfsHelper.Common.cs @@ -217,19 +217,26 @@ public void WithWorkspace(string localDirectory, IGitTfsRemote remote, TfsChange private Workspace GetWorkspace(string localDirectory, string repositoryPath) { + Workspace workspace = null; try { - var workspace = VersionControl.CreateWorkspace(GenerateWorkspaceName()); - workspace.CreateMapping(new WorkingFolder(repositoryPath, localDirectory)); - return workspace; + workspace = VersionControl.CreateWorkspace(GenerateWorkspaceName()); + workspace.CreateMapping(new WorkingFolder(repositoryPath, localDirectory)); + return workspace; } catch (MappingConflictException e) { - throw new GitTfsException(e.Message, new[] {"Run 'git tfs cleanup-workspaces' to remove the workspace."}, e); + if (workspace != null) workspace.Delete(); + throw new GitTfsException(e.Message, new[] { "Run 'git tfs cleanup-workspaces' to remove the workspace." }, e); + } + catch + { + if (workspace != null) workspace.Delete(); + throw; } } - private string GenerateWorkspaceName() + private string GenerateWorkspaceName() { return "git-tfs-" + Guid.NewGuid(); }