public ChangesetDiffViewModel( string username, string repository, string node, string filename, IApplicationService applicationService = null, IDiffService diffService = null) { Username = username; Repository = repository; Node = node; Filename = filename; _applicationService = applicationService = applicationService ?? Locator.Current.GetService <IApplicationService>(); diffService = diffService ?? Locator.Current.GetService <IDiffService>(); var actualFilename = Path.GetFileName(filename); if (actualFilename == null) { actualFilename = filename.Substring(filename.LastIndexOf('/') + 1); } Title = actualFilename; LoadCommand = ReactiveCommand.CreateFromTask(async t => { Patch = null; BinaryFilePath = null; var currentFilePath = Path.Combine(Path.GetTempPath(), actualFilename); var hasCurrent = _commitFileModel.Type != "removed"; var hasPast = _commitFileModel.Type != "added"; var isBinary = false; if (hasCurrent) { using (var stream = new FileStream(currentFilePath, FileMode.Create, FileAccess.Write)) { await applicationService.Client.Repositories.GetRawFile(username, repository, node, filename, stream); } using (var stream = new FileStream(currentFilePath, FileMode.Open, FileAccess.Read)) { var buffer = new byte[1024]; var read = stream.Read(buffer, 0, 1024); isBinary = buffer.Take(read).Any(x => x == 0); } } if (isBinary) { BinaryFilePath = currentFilePath; return; } var parentFilePath = actualFilename + ".parent"; var pastFilePath = Path.Combine(Path.GetTempPath(), parentFilePath); if (hasPast) { var changeset = await applicationService.Client.Commits.GetChangeset(username, repository, node); var parent = changeset.Parents?.FirstOrDefault(); if (parent == null) { throw new Exception("Diff has no parent. Unable to generate view."); } using (var stream = new FileStream(pastFilePath, FileMode.Create, FileAccess.Write)) { await applicationService.Client.Repositories.GetRawFile(username, repository, parent, filename, stream); } using (var stream = new FileStream(pastFilePath, FileMode.Open, FileAccess.Read)) { var buffer = new byte[1024]; var read = stream.Read(buffer, 0, 1024); isBinary = buffer.Take(read).Any(x => x == 0); } } if (isBinary) { BinaryFilePath = currentFilePath; return; } string newText = null, oldText = null; if (hasCurrent) { newText = await Task.Run(() => File.ReadAllText(currentFilePath)); } if (hasPast) { oldText = await Task.Run(() => File.ReadAllText(pastFilePath)); } Patch = diffService.CreateDiff(oldText, newText, 5).ToList(); var items = await applicationService.Client.AllItems(x => x.Commits.GetComments(username, repository, node)); var comments = items.Where(x => x.Inline?.Path == filename).ToList(); var commentsMap = comments.ToDictionary(x => x.Id); foreach (var comment in comments.Where(x => x.Parent != null)) { var parentComment = commentsMap[comment.Parent.Id]; while (parentComment?.Parent != null) { parentComment = commentsMap[parentComment.Parent.Id]; } comment.Inline = parentComment.Inline; } Comments = comments; }); }