Exemple #1
0
        public async Task <Tuple <string, string> > FetchDiffContent(ICommitFile diffFile,
                                                                     string headCommit, string baseCommit)
        {
            var    headFileName  = BuildHeadFileName(headCommit, diffFile.Filename);
            var    headPath      = "";
            string contentOfHead = null;

            if (diffFile.Status == FileStatus.Removed)
            {
                headPath = await SaveToFile(headFileName, "");
            }
            else if (!_fileContentPersist.ExistsInCached(_pullRequestLocator, headFileName))
            {
                contentOfHead = await _fileContentRetriever.GetContent(_pullRequestLocator, diffFile.GetFilePath(headCommit));

                headPath = await SaveToFile(headFileName, contentOfHead);
            }
            else
            {
                headPath = _fileContentPersist.GetCachedFilePath(_pullRequestLocator, headFileName);
            }

            var baseFileName = BuildBaseFileName(baseCommit, diffFile.Filename);
            var basePath     = "";

            if (_fileContentPersist.ExistsInCached(_pullRequestLocator, baseFileName))
            {
                basePath = _fileContentPersist.GetCachedFilePath(_pullRequestLocator, baseFileName);
            }
            else if (diffFile.Status == FileStatus.Renamed)
            {
                if (contentOfHead == null)
                {
                    contentOfHead = await _fileContentPersist.ReadContent(headPath);
                }

                basePath = _fileContentPersist.GetCachedFilePath(_pullRequestLocator, baseFileName);

                await _patchService.RevertViaPatch(contentOfHead, diffFile.Patch, basePath);
            }
            else if (diffFile.Status == FileStatus.New)
            {
                basePath = await SaveToFile(baseFileName, "");
            }
            else
            {
                var contentOfBase = await _fileContentRetriever.GetContent(_pullRequestLocator, diffFile.GetFilePath(baseCommit));

                basePath = await SaveToFile(baseFileName, contentOfBase);
            }
            return(new Tuple <string, string>(basePath, headPath));
        }
        public async void GivenARenamedFile_ShouldCallPatchToGetBaseFile()
        {
            _patchService.RevertViaPatch(Arg.Any <string>(), Arg.Any <string>(), Arg.Any <string>())
            .Returns(Task.FromResult(""));

            var headContent = MockFile1PersistFor("headContent", _pullRequest.Head.Sha);

            const string basePath = "basePath";

            _fileContentPersist.GetCachedFilePath(_pullRequestLocator,
                                                  DiffContentFetcher.BuildBaseFileName(_pullRequest.Base.Sha, _baseFileName)).Returns(basePath);

            const string headPath = "headpath";

            _fileContentPersist.SaveContent(Arg.Any <PullRequestLocator>(),
                                            Arg.Any <string>(),
                                            headContent.Content).Returns(Task.FromResult(headPath));

            _fileContentPersist.ReadContent(headPath).Returns(Task.FromResult(headContent.Content));

            _compareResults.File1.Status   = GitFileStatus.Renamed;
            _mainWindowVm.SelectedDiffFile = new CommitFileVm(_compareResults.File1);

            await _mainWindowVm.RetrieveDiffs();

            await _mainWindowVm.PrepareDiffContent();

            _patchService.Received(1)
            .RevertViaPatch(headContent.Content, _compareResults.File1.Patch, basePath)
            .IgnoreAsyncWarning();

            _fileContentPersist.DidNotReceive().SaveContent(Arg.Any <PullRequestLocator>(),
                                                            _baseFileName,
                                                            Arg.Any <string>()).IgnoreAsyncWarning();

            _contentsClient.DidNotReceive().GetAllContents(_pullRequestLocator.Owner,
                                                           _pullRequestLocator.Repository, _baseFileName).IgnoreAsyncWarning();
            _diffTool.Received(1).Open(basePath, headPath);
        }
        public async Task <Tuple <string, string> > FetchDiffContent(GitHubCommitFile diffFile,
                                                                     string headCommit, string baseCommit)
        {
            var    headFileName  = BuildHeadFileName(headCommit, diffFile.Filename);
            var    headPath      = "";
            string contentOfHead = null;

            if (diffFile.Status == GitFileStatus.Removed)
            {
                headPath = await SaveToFile(headFileName, "");
            }
            else if (!_fileContentPersist.ExistsInCached(_pullRequestLocator, headFileName))
            {
                var collectionOfContentOfHead =
                    await
                    _client.Repository.Content.GetAllContents(_pullRequestLocator.Owner,
                                                              _pullRequestLocator.Repository,
                                                              diffFile.GetFilePath(headCommit));

                contentOfHead = collectionOfContentOfHead.First().Content;
                headPath      = await SaveToFile(headFileName, contentOfHead);
            }
            else
            {
                headPath = _fileContentPersist.GetCachedFilePath(_pullRequestLocator, headFileName);
            }

            var baseFileName = BuildBaseFileName(baseCommit, diffFile.Filename);
            var basePath     = "";

            if (_fileContentPersist.ExistsInCached(_pullRequestLocator, baseFileName))
            {
                basePath = _fileContentPersist.GetCachedFilePath(_pullRequestLocator, baseFileName);
            }
            else if (diffFile.Status == GitFileStatus.Renamed)
            {
                if (contentOfHead == null)
                {
                    contentOfHead = await _fileContentPersist.ReadContent(headPath);
                }

                basePath = _fileContentPersist.GetCachedFilePath(_pullRequestLocator, baseFileName);

                await _patchService.RevertViaPatch(contentOfHead, diffFile.Patch, basePath);
            }
            else if (diffFile.Status == GitFileStatus.New)
            {
                basePath = await SaveToFile(baseFileName, "");
            }
            else
            {
                var contentOfBase =
                    await
                    _client.Repository.Content.GetAllContents(_pullRequestLocator.Owner,
                                                              _pullRequestLocator.Repository,
                                                              diffFile.GetFilePath(baseCommit));

                basePath = await SaveToFile(baseFileName, contentOfBase.First().Content);
            }
            return(new Tuple <string, string>(basePath, headPath));
        }