<commit_before>﻿using System.Linq;
using NUnit.Framework;
using System.IO;

namespace GitSharp.Tests.API
{
    [TestFixture]
    public class CommitTests : ApiTestCase
    {
        [Test]
        public void Commit_into_empty_repository()
        {
            var workingDirectory = Path.Combine(trash.FullName, "test");
            using (Repository repo = Repository.Init(workingDirectory))
            {
                string filepath = Path.Combine(workingDirectory, "for henon.txt");
                File.WriteAllText(filepath, "Weißbier");
                repo.Index.Add(filepath);
                string filepath1 = Path.Combine(workingDirectory, "for nulltoken.txt");
                File.WriteAllText(filepath1, "Rotwein");
                repo.Index.Add(filepath1);
                var commit = repo.Commit("Hello World!", new Author("<NAME>", "<EMAIL>"));
                Assert.NotNull(commit);
                Assert.IsTrue(commit.IsCommit);
                Assert.IsNull(commit.Parent);
                Assert.AreEqual("<NAME>", commit.Author.Name);
                Assert.AreEqual("<EMAIL>", commit.Author.EmailAddress);
                Assert.AreEqual("Hello World!", commit.Message);
                // TODO: check if tree contains for henon and for nulltoken, get the blobs and check  the content.
                Assert.AreEqual(commit, repo.Head.CurrentCommit);
                var changes = commit.Changes.ToDictionary(change => change.Name);
                Assert.AreEqual(ChangeType.Added, changes["for henon.txt"].ChangeType);
                Assert.AreEqual(ChangeType.Added, changes["for nulltoken.txt"].ChangeType);
                Assert.AreEqual("Weißbier", (changes["for henon.txt"].ComparedObject as Blob).Data);
                Assert.AreEqual("Rotwein", (changes["for nulltoken.txt"].ComparedObject as Blob).Data);
                Assert.AreEqual(2, changes.Count);
            }
        }

        [Test]
        public void Commit_into_empty_repository_forShiftJist1()
        {
            var workingDirectory = Path.Combine(trash.FullName, "test1");
            using (Repository repo = Repository.Init(workingDirectory))
            {
                //GitSharp.Core.Constants.setCHARSET("Shift_JIS");
                string filepath = Path.Combine(workingDirectory, "for henon.txt");
                File.WriteAllText(filepath, "Weißbier");
                repo.Index.Add(filepath);
                string filepath1 = Path.Combine(workingDirectory, "for nulltoken.txt");
                File.WriteAllText(filepath1, "Rotwein");
                repo.Index.Add(filepath1);
                string filepath2 = Path.Combine(workingDirectory, "俺のためだ.txt");
                File.WriteAllText(filepath2, "西東京市");
                repo.Index.Add(filepath2);
                var commit = repo.Commit("Hello World!日本からShift_JISのためをコミットしました", new Author("ポウルス", "<EMAIL>"));
                Assert.NotNull(commit);
                Assert.IsTrue(commit.IsCommit);
                Assert.IsNull(commit.Parent);
                Assert.AreEqual("ポウルス", commit.Author.Name);
                Assert.AreEqual("<EMAIL>", commit.Author.EmailAddress);
                Assert.AreEqual("Hello World!日本からShift_JISのためをコミットしました", commit.Message);
                // TODO: check if tree contains for henon and for nulltoken, get the blobs and check  the content.
                Assert.AreEqual(commit, repo.Head.CurrentCommit);
                var changes = commit.Changes.ToDictionary(change => change.Name);
                Assert.AreEqual(ChangeType.Added, changes["for henon.txt"].ChangeType);
                Assert.AreEqual(ChangeType.Added, changes["for nulltoken.txt"].ChangeType);
                Assert.AreEqual(ChangeType.Added, changes["俺のためだ.txt"].ChangeType);
                Assert.AreEqual("Weißbier", (changes["for henon.txt"].ComparedObject as Blob).Data);
                Assert.AreEqual("Rotwein", (changes["for nulltoken.txt"].ComparedObject as Blob).Data);
                Assert.AreEqual("西東京市", (changes["俺のためだ.txt"].ComparedObject as Blob).Data);
                Assert.AreEqual(3, changes.Count);
            }
        }

        [Test]
        public void Commit_into_empty_repository_forShiftJist2()
        {
            var workingDirectory = Path.Combine(trash.FullName, "test2");
            using (Repository repo = Repository.Init(workingDirectory))
            {
                //GitSharp.Core.Constants.setCHARSET("Shift_JIS");
                
                string filepath = Path.Combine(workingDirectory, @"Resources\encodingTestData\Shift_JIS\ウサギちゃん\Rabbitはウサギです.txt");
                System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(filepath));
                System.IO.File.Copy(@"Resources\encodingTestData\Shift_JIS\ウサギちゃん\Rabbitはウサギです.txt",filepath);
                repo.Index.Add(filepath); //Add using UTF-8 params, but the file itself is Shift_JIS.. heh!?

                string filepath1 = Path.Combine(workingDirectory, @"Resources\encodingTestData\Shift_JIS\東京都\新宿三丁目.txt");
                System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(filepath1));
                System.IO.File.Copy(@"Resources\encodingTestData\Shift_JIS\東京都\新宿三丁目.txt", filepath1);
                repo.Index.Add(filepath1); //Add using UTF-8 params, but the file itself is Shift_JIS.. heh!?

                var commit = repo.Commit("Hello World!日本からShift_JISのためをコミットしました", new Author("ポウルス", "<EMAIL>"));
                Assert.NotNull(commit);
                Assert.IsTrue(commit.IsCommit);
                Assert.IsNull(commit.Parent);
                Assert.AreEqual("ポウルス", commit.Author.Name);
                Assert.AreEqual("<EMAIL>", commit.Author.EmailAddress);
                Assert.AreEqual("Hello World!日本からShift_JISのためをコミットしました", commit.Message);
                // TODO: check if tree contains for henon and for nulltoken, get the blobs and check  the content.
                Assert.AreEqual(commit, repo.Head.CurrentCommit);
                var changes = commit.Changes.ToDictionary(change => change.Name);
                Assert.AreEqual(ChangeType.Added, changes["Rabbitはウサギです.txt"].ChangeType);
                Assert.AreEqual(ChangeType.Added, changes["新宿三丁目.txt"].ChangeType);
                Assert.AreEqual("ラビットis usagi desu.", (changes["Rabbitはウサギです.txt"].ComparedObject as Blob).Data);
                Assert.AreEqual("電車で行きます。", (changes["新宿三丁目.txt"].ComparedObject as Blob).Data);
                Assert.AreEqual(2, changes.Count);
            }
        }

        [Test]
        public void Commit_changes_to_existing_commit()
        {
            using (var repo = GetTrashRepository())<commit_msg>Updated testcase for Japanese to check the raw data instead.<commit_after>﻿using System.Linq;
using NUnit.Framework;
using System.IO;

namespace GitSharp.Tests.API
{
    [TestFixture]
    public class CommitTests : ApiTestCase
    {
        [Test]
        public void Commit_into_empty_repository()
        {
            var workingDirectory = Path.Combine(trash.FullName, "test");
            using (Repository repo = Repository.Init(workingDirectory))
            {
                string filepath = Path.Combine(workingDirectory, "for henon.txt");
                File.WriteAllText(filepath, "Weißbier");
                repo.Index.Add(filepath);
                string filepath1 = Path.Combine(workingDirectory, "for nulltoken.txt");
                File.WriteAllText(filepath1, "Rotwein");
                repo.Index.Add(filepath1);
                var commit = repo.Commit("Hello World!", new Author("<NAME>", "<EMAIL>"));
                Assert.NotNull(commit);
                Assert.IsTrue(commit.IsCommit);
                Assert.IsNull(commit.Parent);
                Assert.AreEqual("<NAME>", commit.Author.Name);
                Assert.AreEqual("<EMAIL>", commit.Author.EmailAddress);
                Assert.AreEqual("Hello World!", commit.Message);
                // TODO: check if tree contains for henon and for nulltoken, get the blobs and check  the content.
                Assert.AreEqual(commit, repo.Head.CurrentCommit);
                var changes = commit.Changes.ToDictionary(change => change.Name);
                Assert.AreEqual(ChangeType.Added, changes["for henon.txt"].ChangeType);
                Assert.AreEqual(ChangeType.Added, changes["for nulltoken.txt"].ChangeType);
                Assert.AreEqual("Weißbier", (changes["for henon.txt"].ComparedObject as Blob).Data);
                Assert.AreEqual("Rotwein", (changes["for nulltoken.txt"].ComparedObject as Blob).Data);
                Assert.AreEqual(2, changes.Count);
            }
        }

        [Test]
        public void Commit_into_empty_repository_forShiftJist1()
        {
            var workingDirectory = Path.Combine(trash.FullName, "test1");
            using (Repository repo = Repository.Init(workingDirectory))
            {
                //GitSharp.Core.Constants.setCHARSET("Shift_JIS");
                string filepath = Path.Combine(workingDirectory, "for henon.txt");
                File.WriteAllText(filepath, "Weißbier");
                repo.Index.Add(filepath);
                string filepath1 = Path.Combine(workingDirectory, "for nulltoken.txt");
                File.WriteAllText(filepath1, "Rotwein");
                repo.Index.Add(filepath1);
                string filepath2 = Path.Combine(workingDirectory, "俺のためだ.txt");
                File.WriteAllText(filepath2, "西東京市");
                repo.Index.Add(filepath2);
                var commit = repo.Commit("Hello World!日本からShift_JISのためをコミットしました", new Author("ポウルス", "<EMAIL>"));
                Assert.NotNull(commit);
                Assert.IsTrue(commit.IsCommit);
                Assert.IsNull(commit.Parent);
                Assert.AreEqual("ポウルス", commit.Author.Name);
                Assert.AreEqual("<EMAIL>", commit.Author.EmailAddress);
                Assert.AreEqual("Hello World!日本からShift_JISのためをコミットしました", commit.Message);
                // TODO: check if tree contains for henon and for nulltoken, get the blobs and check  the content.
                Assert.AreEqual(commit, repo.Head.CurrentCommit);
                var changes = commit.Changes.ToDictionary(change => change.Name);
                Assert.AreEqual(ChangeType.Added, changes["for henon.txt"].ChangeType);
                Assert.AreEqual(ChangeType.Added, changes["for nulltoken.txt"].ChangeType);
                Assert.AreEqual(ChangeType.Added, changes["俺のためだ.txt"].ChangeType);
                Assert.AreEqual("Weißbier", (changes["for henon.txt"].ComparedObject as Blob).Data);
                Assert.AreEqual("Rotwein", (changes["for nulltoken.txt"].ComparedObject as Blob).Data);
                Assert.AreEqual("西東京市", (changes["俺のためだ.txt"].ComparedObject as Blob).Data);
                Assert.AreEqual(3, changes.Count);
            }
        }

        [Test]
        public void Commit_into_empty_repository_forShiftJist2()
        {
            var workingDirectory = Path.Combine(trash.FullName, "test2");
            using (Repository repo = Repository.Init(workingDirectory))
            {
                //GitSharp.Core.Constants.setCHARSET("Shift_JIS");
                
                string filepath = Path.Combine(workingDirectory, @"Resources\encodingTestData\Shift_JIS\ウサギちゃん\Rabbitはウサギです.txt");
                System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(filepath));
                System.IO.File.Copy(@"Resources\encodingTestData\Shift_JIS\ウサギちゃん\Rabbitはウサギです.txt",filepath);
                repo.Index.Add(filepath); //Add using UTF-8 params, but the file itself is Shift_JIS.. heh!?

                string filepath1 = Path.Combine(workingDirectory, @"Resources\encodingTestData\Shift_JIS\東京都\新宿三丁目.txt");
                System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(filepath1));
                System.IO.File.Copy(@"Resources\encodingTestData\Shift_JIS\東京都\新宿三丁目.txt", filepath1);
                repo.Index.Add(filepath1); //Add using UTF-8 params, but the file itself is Shift_JIS.. heh!?

                var commit = repo.Commit("Hello World!日本からShift_JISのためをコミットしました", new Author("ポウルス", "<EMAIL>"));
                Assert.NotNull(commit);
                Assert.IsTrue(commit.IsCommit);
                Assert.IsNull(commit.Parent);
                Assert.AreEqual("ポウルス", commit.Author.Name);
                Assert.AreEqual("<EMAIL>", commit.Author.EmailAddress);
                Assert.AreEqual("Hello World!日本からShift_JISのためをコミットしました", commit.Message);
                // TODO: check if tree contains for henon and for nulltoken, get the blobs and check  the content.
                Assert.AreEqual(commit, repo.Head.CurrentCommit);
                var changes = commit.Changes.ToDictionary(change => change.Name);
                Assert.AreEqual(ChangeType.Added, changes["Rabbitはウサギです.txt"].ChangeType);
                Assert.AreEqual(ChangeType.Added, changes["新宿三丁目.txt"].ChangeType);
                Assert.AreEqual(System.Text.Encoding.GetEncoding("Shift_JIS").GetBytes("ラビットis usagi desu."), (changes["Rabbitはウサギです.txt"].ComparedObject as Blob).RawData);
                Assert.AreEqual(System.Text.Encoding.GetEncoding("Shift_JIS").GetBytes("電車で行きます。"), (changes["新宿三丁目.txt"].ComparedObject as Blob).RawData);
                Assert.AreNotEqual(System.Text.Encoding.GetEncoding("UTF-8").GetBytes("ラビットis usagi desu."), (changes["Rabbitはウサギです.txt"].ComparedObject as Blob).RawData);
                Assert.AreNotEqual(System.Text.Encoding.GetEncoding("UTF-8").GetBytes("電車で行きます。"), (changes["新宿三丁目.txt"].ComparedObject as Blob).RawData);

                Assert.AreEqual(2, changes.Count);
            }
        }

        [Test]
        public void Commit_changes_to_existing_commit()
        {
            using (var repo = GetTrashRepository())
{'fraction_non_alphanumeric': 0.11937405785657886, 'fraction_numerical': 0.003301988371258345, 'mean_word_length': 2.3242662848962063, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5607891', 'n_tokens_mistral': 4123, 'n_tokens_neox': 3960, 'n_words': 644}