forked from git-tfs/git-tfs
/
TfsHelper.VsFake.cs
501 lines (411 loc) · 17.4 KB
/
TfsHelper.VsFake.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Sep.Git.Tfs.Commands;
using Sep.Git.Tfs.Core;
using Sep.Git.Tfs.Core.TfsInterop;
using Sep.Git.Tfs.Util;
using StructureMap;
namespace Sep.Git.Tfs.VsFake
{
public class MockBranchObject : IBranchObject
{
public string Path { get; set; }
public string ParentPath { get; set; }
public bool IsRoot { get; set; }
}
public class TfsHelper : ITfsHelper
{
#region misc/null
IContainer _container;
TextWriter _stdout;
Script _script;
FakeVersionControlServer _versionControlServer;
public TfsHelper(IContainer container, TextWriter stdout, Script script)
{
_container = container;
_stdout = stdout;
_script = script;
_versionControlServer = new FakeVersionControlServer(_script);
}
public string TfsClientLibraryVersion { get { return "(FAKE)"; } }
public string Url { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public void EnsureAuthenticated() {}
public bool CanShowCheckinDialog { get { return false; } }
public long ShowCheckinDialog(IWorkspace workspace, IPendingChange[] pendingChanges, IEnumerable<IWorkItemCheckedInfo> checkedInfos, string checkinComment)
{
throw new NotImplementedException();
}
public IIdentity GetIdentity(string username)
{
return new NullIdentity();
}
#endregion
#region read changesets
public ITfsChangeset GetLatestChangeset(IGitTfsRemote remote)
{
return _script.Changesets.LastOrDefault().Try(x => BuildTfsChangeset(x, remote));
}
public IEnumerable<ITfsChangeset> GetChangesets(string path, long startVersion, IGitTfsRemote remote)
{
if (!_script.Changesets.Any(c => c.IsBranchChangeset) && _script.Changesets.Any(c => c.IsMergeChangeset))
return _script.Changesets.Where(x => x.Id >= startVersion).Select(x => BuildTfsChangeset(x, remote));
return _script.Changesets
.Where(x => x.Id >= startVersion && x.Changes.Any(c => c.RepositoryPath.ToLower().IndexOf(path.ToLower()) == 0 || path.ToLower().IndexOf(c.RepositoryPath.ToLower()) == 0))
.Select(x => BuildTfsChangeset(x, remote));
}
public int FindMergeChangesetParent(string path, long firstChangeset, GitTfsRemote remote)
{
var firstChangesetOfBranch = _script.Changesets.FirstOrDefault(c => c.IsMergeChangeset && c.MergeChangesetDatas.MergeIntoBranch == path && c.MergeChangesetDatas.BeforeMergeChangesetId < firstChangeset);
if (firstChangesetOfBranch != null)
return firstChangesetOfBranch.MergeChangesetDatas.BeforeMergeChangesetId;
return -1;
}
private ITfsChangeset BuildTfsChangeset(ScriptedChangeset changeset, IGitTfsRemote remote)
{
var tfsChangeset = _container.With<ITfsHelper>(this).With<IChangeset>(new Changeset(_versionControlServer, changeset)).GetInstance<TfsChangeset>();
tfsChangeset.Summary = new TfsChangesetInfo { ChangesetId = changeset.Id, Remote = remote };
return tfsChangeset;
}
class Changeset : IChangeset
{
private IVersionControlServer _versionControlServer;
private ScriptedChangeset _changeset;
public Changeset(IVersionControlServer versionControlServer, ScriptedChangeset changeset)
{
_versionControlServer = versionControlServer;
_changeset = changeset;
}
public IChange[] Changes
{
get { return _changeset.Changes.Select(x => new Change(_versionControlServer, _changeset, x)).ToArray(); }
}
public string Committer
{
get { return "todo"; }
}
public DateTime CreationDate
{
get { return _changeset.CheckinDate; }
}
public string Comment
{
get { return _changeset.Comment; }
}
public int ChangesetId
{
get { return _changeset.Id; }
}
public IVersionControlServer VersionControlServer
{
get { throw new NotImplementedException(); }
}
public void Get(IWorkspace workspace)
{
workspace.GetSpecificVersion(this);
}
}
class Change : IChange, IItem
{
IVersionControlServer _versionControlServer;
ScriptedChangeset _changeset;
ScriptedChange _change;
public Change(IVersionControlServer versionControlServer, ScriptedChangeset changeset, ScriptedChange change)
{
_versionControlServer = versionControlServer;
_changeset = changeset;
_change = change;
}
TfsChangeType IChange.ChangeType
{
get { return _change.ChangeType; }
}
IItem IChange.Item
{
get { return this; }
}
IVersionControlServer IItem.VersionControlServer
{
get { return _versionControlServer; }
}
int IItem.ChangesetId
{
get { return _changeset.Id; }
}
string IItem.ServerItem
{
get { return _change.RepositoryPath; }
}
int IItem.DeletionId
{
get { return 0; }
}
TfsItemType IItem.ItemType
{
get { return _change.ItemType; }
}
int IItem.ItemId
{
get { return _change.ItemId.Value; }
}
long IItem.ContentLength
{
get
{
using (var temp = ((IItem)this).DownloadFile())
return new FileInfo(temp).Length;
}
}
TemporaryFile IItem.DownloadFile()
{
var temp = new TemporaryFile();
using(var writer = new StreamWriter(temp))
writer.Write(_change.Content);
return temp;
}
}
#endregion
#region workspaces
public void WithWorkspace(string localDirectory, IGitTfsRemote remote, IEnumerable<Tuple<string, string>> mappings, TfsChangesetInfo versionToFetch, Action<ITfsWorkspace> action)
{
Trace.WriteLine("Setting up a TFS workspace at " + localDirectory);
var fakeWorkspace = new FakeWorkspace(localDirectory, remote.TfsRepositoryPath);
var workspace = _container.With("localDirectory").EqualTo(localDirectory)
.With("remote").EqualTo(remote)
.With("contextVersion").EqualTo(versionToFetch)
.With("workspace").EqualTo(fakeWorkspace)
.With("tfsHelper").EqualTo(this)
.GetInstance<TfsWorkspace>();
action(workspace);
}
public void WithWorkspace(string directory, IGitTfsRemote remote, TfsChangesetInfo versionToFetch, Action<ITfsWorkspace> action)
{
Trace.WriteLine("Setting up a TFS workspace at " + directory);
var fakeWorkspace = new FakeWorkspace(directory, remote.TfsRepositoryPath);
var workspace = _container.With("localDirectory").EqualTo(directory)
.With("remote").EqualTo(remote)
.With("contextVersion").EqualTo(versionToFetch)
.With("workspace").EqualTo(fakeWorkspace)
.With("tfsHelper").EqualTo(this)
.GetInstance<TfsWorkspace>();
action(workspace);
}
class FakeWorkspace : IWorkspace
{
string _directory;
string _repositoryRoot;
public FakeWorkspace(string directory, string repositoryRoot)
{
_directory = directory;
_repositoryRoot = repositoryRoot;
}
public void GetSpecificVersion(IChangeset changeset)
{
GetSpecificVersion(changeset.ChangesetId, changeset.Changes);
}
public void GetSpecificVersion(int changeset, IEnumerable<IChange> changes)
{
var repositoryRoot = _repositoryRoot.ToLower();
if(!repositoryRoot.EndsWith("/")) repositoryRoot += "/";
foreach (var change in changes)
{
if (change.Item.ItemType == TfsItemType.File)
{
var outPath = Path.Combine(_directory, change.Item.ServerItem.ToLower().Replace(repositoryRoot, ""));
var outDir = Path.GetDirectoryName(outPath);
if (!Directory.Exists(outDir)) Directory.CreateDirectory(outDir);
using (var download = change.Item.DownloadFile())
File.WriteAllText(outPath, File.ReadAllText(download.Path));
}
}
}
#region unimplemented
public void Merge(string sourceTfsPath, string tfsRepositoryPath)
{
throw new NotImplementedException();
}
public IPendingChange[] GetPendingChanges()
{
throw new NotImplementedException();
}
public ICheckinEvaluationResult EvaluateCheckin(TfsCheckinEvaluationOptions options, IPendingChange[] allChanges, IPendingChange[] changes, string comment, ICheckinNote checkinNote, IEnumerable<IWorkItemCheckinInfo> workItemChanges)
{
throw new NotImplementedException();
}
public ICheckinEvaluationResult EvaluateCheckin(TfsCheckinEvaluationOptions options, IPendingChange[] allChanges, IPendingChange[] changes, string comment, string authors, ICheckinNote checkinNote, IEnumerable<IWorkItemCheckinInfo> workItemChanges)
{
throw new NotImplementedException();
}
public void Shelve(IShelveset shelveset, IPendingChange[] changes, TfsShelvingOptions options)
{
throw new NotImplementedException();
}
public int Checkin(IPendingChange[] changes, string comment, string author, ICheckinNote checkinNote, IEnumerable<IWorkItemCheckinInfo> workItemChanges, TfsPolicyOverrideInfo policyOverrideInfo, bool overrideGatedCheckIn)
{
throw new NotImplementedException();
}
public int PendAdd(string path)
{
throw new NotImplementedException();
}
public int PendEdit(string path)
{
throw new NotImplementedException();
}
public int PendDelete(string path)
{
throw new NotImplementedException();
}
public int PendRename(string pathFrom, string pathTo)
{
throw new NotImplementedException();
}
public void ForceGetFile(string path, int changeset)
{
throw new NotImplementedException();
}
public void GetSpecificVersion(int changeset)
{
throw new NotImplementedException();
}
public string GetLocalItemForServerItem(string serverItem)
{
throw new NotImplementedException();
}
public string GetServerItemForLocalItem(string localItem)
{
throw new NotImplementedException();
}
public string OwnerName
{
get { throw new NotImplementedException(); }
}
#endregion
}
public void CleanupWorkspaces(string workingDirectory)
{
}
public bool IsExistingInTfs(string path)
{
var exists = false;
foreach (var changeset in _script.Changesets)
{
foreach (var change in changeset.Changes)
{
if (change.RepositoryPath == path)
{
exists = !change.ChangeType.IncludesOneOf(TfsChangeType.Delete);
}
}
}
return exists;
}
public bool CanGetBranchInformation { get { return false; } }
public IChangeset GetChangeset(int changesetId)
{
return new Changeset(_versionControlServer, _script.Changesets.First(c => c.Id == changesetId));
}
public int GetRootChangesetForBranch(string tfsPathBranchToCreate, string tfsPathParentBranch = null)
{
var firstChangesetOfBranch = _script.Changesets.FirstOrDefault(c => c.IsBranchChangeset && c.BranchChangesetDatas.BranchPath == tfsPathBranchToCreate);
if (firstChangesetOfBranch != null)
return firstChangesetOfBranch.BranchChangesetDatas.RootChangesetId;
return -1;
}
public IEnumerable<IBranchObject> GetBranches()
{
var branches = new List<IBranchObject>();
branches.AddRange(_script.RootBranches.Select(b => new MockBranchObject { IsRoot = true, Path = b.BranchPath, ParentPath = null }));
branches.AddRange(_script.Changesets.Where(c=>c.IsBranchChangeset).Select(c => new MockBranchObject
{
IsRoot = false,
Path = c.BranchChangesetDatas.BranchPath,
ParentPath = c.BranchChangesetDatas.ParentBranch
}));
return branches;
}
#endregion
#region unimplemented
public IShelveset CreateShelveset(IWorkspace workspace, string shelvesetName)
{
throw new NotImplementedException();
}
public IEnumerable<IWorkItemCheckinInfo> GetWorkItemInfos(IEnumerable<string> workItems, TfsWorkItemCheckinAction checkinAction)
{
throw new NotImplementedException();
}
public IEnumerable<IWorkItemCheckedInfo> GetWorkItemCheckedInfos(IEnumerable<string> workItems, TfsWorkItemCheckinAction checkinAction)
{
throw new NotImplementedException();
}
public ICheckinNote CreateCheckinNote(Dictionary<string, string> checkinNotes)
{
throw new NotImplementedException();
}
public ITfsChangeset GetChangeset(int changesetId, IGitTfsRemote remote)
{
throw new NotImplementedException();
}
public bool HasShelveset(string shelvesetName)
{
throw new NotImplementedException();
}
public ITfsChangeset GetShelvesetData(IGitTfsRemote remote, string shelvesetOwner, string shelvesetName)
{
throw new NotImplementedException();
}
public int ListShelvesets(ShelveList shelveList, IGitTfsRemote remote)
{
throw new NotImplementedException();
}
public IEnumerable<string> GetAllTfsRootBranchesOrderedByCreation()
{
throw new NotImplementedException();
}
public IEnumerable<TfsLabel> GetLabels(string tfsPathBranch, string nameFilter = null)
{
throw new NotImplementedException();
}
public void CreateBranch(string sourcePath, string targetPath, int changesetId, string comment = null)
{
throw new NotImplementedException();
}
public void CreateTfsRootBranch(string projectName, string mainBranch, string gitRepositoryPath, bool createTeamProjectFolder)
{
throw new NotImplementedException();
}
#endregion
private class FakeVersionControlServer : IVersionControlServer
{
Script _script;
public FakeVersionControlServer(Script script)
{
_script = script;
}
public IItem GetItem(int itemId, int changesetNumber)
{
var match = _script.Changesets.AsEnumerable().Reverse()
.SkipWhile(cs => cs.Id > changesetNumber)
.Select(cs => new { Changeset = cs, Change = cs.Changes.SingleOrDefault(change => change.ItemId == itemId) })
.First(x => x.Change != null);
return new Change(this, match.Changeset, match.Change);
}
public IItem GetItem(string itemPath, int changesetNumber)
{
throw new NotImplementedException();
}
public IItem[] GetItems(string itemPath, int changesetNumber, TfsRecursionType recursionType)
{
throw new NotImplementedException();
}
public IEnumerable<IChangeset> QueryHistory(string path, int version, int deletionId, TfsRecursionType recursion, string user, int versionFrom, int versionTo, int maxCount, bool includeChanges, bool slotMode, bool includeDownloadInfo)
{
throw new NotImplementedException();
}
}
}
}