Exemplo n.º 1
0
        public async Task TestDiffDeletedFiletAsync()
        {
            GitDiffParser diffParser = new GitDiffParser();

            await git.InitRepoAsync();

            io.WriteFile("file1.txt", "text1");
            GitCommit commit1 = await git.CommitAllChangesAsync("Message1");

            io.DeleteFile("file1.txt");
            GitCommit commit2 = await git.CommitAllChangesAsync("Message2");

            R <string> result1 = await cmd.GetFileDiffAsync(commit1.Sha.Sha, "file1.txt", ct);

            Assert.AreEqual(true, result1.IsOk);

            CommitDiff diff1 = await diffParser.ParseAsync(commit1.Sha, result1.Value, false, false);

            Assert.IsNullOrEmpty(File.ReadAllText(diff1.LeftPath));
            Assert.AreEqual("text1\r\n", File.ReadAllText(diff1.RightPath));

            R <string> result2 = await cmd.GetFileDiffAsync(commit2.Sha.Sha, "file1.txt", ct);

            Assert.AreEqual(true, result2.IsOk);

            CommitDiff diff2 = await diffParser.ParseAsync(commit2.Sha, result2.Value, false, false);

            Assert.AreEqual("text1\r", File.ReadAllText(diff2.LeftPath));
            Assert.IsNullOrEmpty(File.ReadAllText(diff2.RightPath));
        }
Exemplo n.º 2
0
        public async Task TestDiffUncommittedAsync()
        {
            GitDiffParser diffParser = new GitDiffParser();

            await git.InitRepoAsync();

            io.WriteFile("file1.txt", "line1\nline2\nline3\n");
            io.WriteFile("file2.txt", "line1\nline2\nline3\n");
            await git.CommitAllChangesAsync("Message1");

            io.WriteFile("file1.txt", "line1\nline22\nline3\n");
            io.DeleteFile("file2.txt");
            io.WriteFile("file3.txt", "line1\nline2\nline3\n");

            R <string> result = await cmd.GetUncommittedDiffAsync(ct);

            CommitDiff diff = await diffParser.ParseAsync(null, result.Value, true, false);

            string d1 = File.ReadAllText(diff.LeftPath);
            string d2 = File.ReadAllText(diff.RightPath);

            Assert.IsNotNullOrEmpty(result.Value);

            result = await cmd.GetUncommittedFileDiffAsync("file1.txt", ct);

            Assert.IsNotNullOrEmpty(result.Value);



            //io.WriteFile("file1.txt", "line1\nline22\nline3\n");
            //io.DeleteFile("file2.txt");
            //io.WriteFile("file3.txt", "line1\nline2\nline3\n");

            //result = await cmd.GetUncommittedDiffAsync(ct);
            //Assert.IsNotNullOrEmpty(result.Value);

            result = await cmd.GetUncommittedFileDiffAsync("file1.txt", ct);

            Assert.IsNotNullOrEmpty(result.Value);

            result = await cmd.GetUncommittedFileDiffAsync("file2.txt", ct);

            Assert.IsNotNullOrEmpty(result.Value);

            result = await cmd.GetUncommittedFileDiffAsync("file3.txt", ct);

            Assert.IsNotNullOrEmpty(result.Value);
        }
        public async Task TestConflictsResolveAsync()
        {
            GitDiffParser diffParser = new GitDiffParser();

            await git.InitRepoAsync();

            // Add some files as initial add on master
            io.WriteFile("file1.txt", "Text 1");
            io.WriteFile("file2.txt", "Text 2");
            io.WriteFile("file3.txt", "Text 3");
            io.WriteFile("file4.txt", "Text 4");
            io.WriteFile("file5.txt", "Text 5");
            GitCommit masterCommit1 = await git.CommitAllChangesAsync("Initial add on master");

            // Create branch1
            await git.BranchAsync("branch1");

            io.WriteFile("file1.txt", "Text 12 on branch\r\n\n");
            io.DeleteFile("file2.txt");                                       // Deleted 2 on branch
            io.WriteFile("file3.txt", "Text 32 on branch");
            io.WriteFile("file4.txt", "Text 42 on branch");
            io.DeleteFile("file5.txt");                                       // Delete 5 on branch
            io.WriteFile("file6.txt", "Text 62 on branch");                   // Added 6 on branch
            GitCommit branchCommit1 = await git.CommitAllChangesAsync("Message 1 on branch1");

            // Switch to master and make some changes and commit
            await git.CheckoutAsync("master");

            io.WriteFile("file1.txt", "Text 12 on master\n");
            io.WriteFile("file2.txt", "Text 22 on master");
            io.DeleteFile("file3.txt");                                       // Delete 3 on master
            // No change on file 4
            io.DeleteFile("file5.txt");                                       // Delete 5 om master
            io.WriteFile("file6.txt", "Text 62 on master");                   // added on master
            GitCommit masterCommit2 = await git.CommitAllChangesAsync("Message 2 on master");

            // Merge branch to master, expecting 1CMM, 2CMD, 3CDM, 4M, (no 5), 6CAA
            R result = await cmd.MergeAsync("branch1", ct);

            status = await git.GetStatusAsync();

            Assert.AreEqual(1, status.Modified);
            Assert.AreEqual(4, status.Conflicted);
            Assert.AreEqual(true, status.IsMerging);

            GitConflicts conflicts = await git.GetConflictsAsync();

            Assert.AreEqual(true, conflicts.HasConflicts);
            Assert.AreEqual(4, conflicts.Count);


            io.WriteFile("file1.txt", "Text 13 merged");
            status = await git.GetStatusAsync();

            await git.Service <IGitStatusService>().Call(m => m.AddAsync("file1.txt", ct));

            status = await git.GetStatusAsync();

            Assert.AreEqual(2, status.Modified);
            Assert.AreEqual(3, status.Conflicted);

            io.DeleteFile("file2.txt");
            await git.Service <IGitStatusService>().Call(m => m.RemoveAsync("file2.txt", ct));

            status = await git.GetStatusAsync();

            Assert.AreEqual(2, status.Modified);
            Assert.AreEqual(1, status.Deleted);
            Assert.AreEqual(2, status.Conflicted);


            string branchSide = await git.GetConflictFileAsync(conflicts.Files[2].RemoteId);

            io.WriteFile("file3.txt", branchSide);
            await git.Service <IGitStatusService>().Call(m => m.AddAsync("file3.txt", ct));

            status = await git.GetStatusAsync();

            Assert.AreEqual(3, status.Modified);
            Assert.AreEqual(1, status.Deleted);
            Assert.AreEqual(1, status.Conflicted);

            io.WriteFile("file6.txt", "Text 63 merged");
            await git.Service <IGitStatusService>().Call(m => m.AddAsync("file6.txt", ct));

            status = await git.GetStatusAsync();

            Assert.AreEqual(4, status.Modified);
            Assert.AreEqual(1, status.Deleted);
            Assert.AreEqual(0, status.Conflicted);


            GitCommit mergeCommit = await git.CommitAllChangesAsync(status.MergeMessage);

            string mergePatch = await git.Service <IGitDiffService>().Call(m => m.GetCommitDiffAsync(mergeCommit.Sha.Sha, ct));

            string mergePatch2 = await git.Service <IGitDiffService>().Call(
                m => m.GetCommitDiffAsync(mergeCommit.Sha.Sha, ct));

            CommitDiff diff = await diffParser.ParseAsync(mergeCommit.Sha, mergePatch, true, false);

            CommitDiff diff2 = await diffParser.ParseAsync(mergeCommit.Sha, mergePatch2, true, false);

            string left  = File.ReadAllText(diff.LeftPath);
            string right = File.ReadAllText(diff.RightPath);

            string left2  = File.ReadAllText(diff2.LeftPath);
            string right2 = File.ReadAllText(diff2.RightPath);
        }