<commit_before>const git = require('@cardstack/server/git');
const ngit = require('nodegit');
const temp = require('./temp-helper');
const {
  inRepo,
  commitOpts,
  makeRepo
}= require('./git-assertions');
const moment = require('moment-timezone');

describe('git', function() {
  let path;

  beforeEach(async function() {
    let root = await temp.mkdir('cardstack-server-test');
    path = `${root}/example`;
  });
  afterEach(async function() {
    await temp.cleanup();
  });

  it('can make new empty repo', async function() {

    await git.createEmptyRepo(path, commitOpts({
      message: 'First commit',
      authorDate: moment.tz('2017-01-16 12:21', 'Africa/Addis_Ababa')
    }));

    let commit = await inRepo(path).getCommit('master');
    expect(commit.authorName).to.equal('<NAME>');
    expect(commit.authorEmail).to.equal('<EMAIL>');
    expect(commit.message).to.equal('First commit');
    expect(commit.authorDate).to.equal('2017-01-16T12:21:00+03:00');
  });

  it('can include separate committer info', async function() {

    await git.createEmptyRepo(path, commitOpts({
      message: 'First commit',
      authorDate: moment.tz('2017-01-16 12:21', 'Africa/Addis_Ababa'),
      committerName: 'The Committer',
      committerEmail: '<EMAIL>'
    }));

    let commit = await inRepo(path).getCommit('master');
    expect(commit.authorName).to.equal('<NAME>');
    expect(commit.authorEmail).to.equal('<EMAIL>');
    expect(commit.committerName).to.equal('The Committer');
    expect(commit.committerEmail).to.equal('<EMAIL>');
  });

  it('can fast-forward merge some new content', async function() {
    let repo = await git.createEmptyRepo(path, commitOpts({
      message: 'First commit'
    }));

    let parentRef = await ngit.Branch.lookup(repo, 'master', ngit.Branch.BRANCH.LOCAL);

    let updatedContent = [
      { filename: 'hello-world.txt', buffer: Buffer.from('This is a file', 'utf8') }
    ];
    let id = (await git.mergeCommit(repo, parentRef.target(), 'master', updatedContent, commitOpts({ message: 'Second commit' })));

    let commit = await inRepo(path).getCommit(id);
    expect(commit.message).to.equal('Second commit');

    let head = await inRepo(path).getCommit('master');
    expect(head.message).to.equal('Second commit');

    let parentCommit = await inRepo(path).getCommit(id + '^');
    expect(parentCommit.message).to.equal('First commit');

    expect(await inRepo(path).getContents(id, 'hello-world.txt')).to.equal('This is a file');

  });

  it('automatically fast-forwards when no base version is provided', async function() {
    let repo = await git.createEmptyRepo(path, commitOpts({
      message: 'First commit'
    }));

    let updatedContent = [
      { filename: 'hello-world.txt', buffer: Buffer.from('This is a file', 'utf8') }
    ];
    let id = (await git.mergeCommit(repo, null, 'master', updatedContent, commitOpts({ message: 'Second commit' })));

    let commit = await inRepo(path).getCommit(id);
    expect(commit.message).to.equal('Second commit');

    let head = await inRepo(path).getCommit('master');
    expect(head.message).to.equal('Second commit');

    let parentCommit = await inRepo(path).getCommit(id + '^');
    expect(parentCommit.message).to.equal('First commit');

    expect(await inRepo(path).getContents(id, 'hello-world.txt')).to.equal('This is a file');

  });

  it('can detect unintended filename collision', async function() {
    let { repo, head } = await makeRepo(path, [
      {
        changes: [
          {
            filename: 'sample.txt',
            buffer: Buffer.from('sample', 'utf8'),
            createOnly: true
          }
        ]
      }
    ]);


    let updatedContent = [<commit_msg>cleaning up some tests<commit_after>const git = require('@cardstack/server/git');
const ngit = require('nodegit');
const temp = require('./temp-helper');
const {
  inRepo,
  commitOpts,
  makeRepo
}= require('./git-assertions');
const moment = require('moment-timezone');

describe('git', function() {
  let path;

  beforeEach(async function() {
    let root = await temp.mkdir('cardstack-server-test');
    path = `${root}/example`;
  });
  afterEach(async function() {
    await temp.cleanup();
  });

  it('can make new empty repo', async function() {

    await git.createEmptyRepo(path, commitOpts({
      message: 'First commit',
      authorDate: moment.tz('2017-01-16 12:21', 'Africa/Addis_Ababa')
    }));

    let commit = await inRepo(path).getCommit('master');
    expect(commit.authorName).to.equal('<NAME>');
    expect(commit.authorEmail).to.equal('<EMAIL>');
    expect(commit.message).to.equal('First commit');
    expect(commit.authorDate).to.equal('2017-01-16T12:21:00+03:00');
  });

  it('can include separate committer info', async function() {
    let { repo, head } = await makeRepo(path);

    let id = await git.mergeCommit(repo, head, 'master', [], commitOpts({
      message: 'Second commit',
      authorDate: moment.tz('2017-01-16 12:21', 'Africa/Addis_Ababa'),
      committerName: 'The Committer',
      committerEmail: '<EMAIL>'
    }));

    let commit = await inRepo(path).getCommit(id);
    expect(commit.authorName).to.equal('<NAME>');
    expect(commit.authorEmail).to.equal('<EMAIL>');
    expect(commit.committerName).to.equal('<NAME>');
    expect(commit.committerEmail).to.equal('<EMAIL>');
  });

  it('can fast-forward merge some new content', async function() {
    let { repo, head } = await makeRepo(path);

    let updatedContent = [
      { filename: 'hello-world.txt', buffer: Buffer.from('This is a file', 'utf8') }
    ];
    let id = (await git.mergeCommit(repo, head, 'master', updatedContent, commitOpts({ message: 'Second commit' })));

    let commit = await inRepo(path).getCommit(id);
    expect(commit.message).to.equal('Second commit');

    let masterCommit = await inRepo(path).getCommit('master');
    expect(masterCommit.id).to.equal(id);

    let parentCommit = await inRepo(path).getCommit(id + '^');
    expect(parentCommit.message).to.equal('First commit');

    expect(await inRepo(path).getContents(id, 'hello-world.txt')).to.equal('This is a file');
  });

  it('automatically fast-forwards when no base version is provided', async function() {
    let { repo } = await makeRepo(path);

    let updatedContent = [
      { filename: 'hello-world.txt', buffer: Buffer.from('This is a file', 'utf8') }
    ];
    let id = (await git.mergeCommit(repo, null, 'master', updatedContent, commitOpts({ message: 'Second commit' })));

    let commit = await inRepo(path).getCommit(id);
    expect(commit.message).to.equal('Second commit');

    let head = await inRepo(path).getCommit('master');
    expect(head.id).to.equal(id);

    let parentCommit = await inRepo(path).getCommit(id + '^');
    expect(parentCommit.message).to.equal('First commit');

    expect(await inRepo(path).getContents(id, 'hello-world.txt')).to.equal('This is a file');
  });

  it('can detect unintended filename collision', async function() {
    let { repo, head } = await makeRepo(path, [
      {
        changes: [
          {
            filename: 'sample.txt',
            buffer: Buffer.from('sample', 'utf8'),
            createOnly: true
          }
        ]
      }
    ]);


    let updatedContent = [
{'fraction_non_alphanumeric': 0.16959868059373281, 'fraction_numerical': 0.012369433754810335, 'mean_word_length': 3.635031847133758, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, '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': '4020101', 'n_tokens_mistral': 2297, 'n_tokens_neox': 2292, 'n_words': 480}