<commit_before>import {Refs} from "./refs";

export interface CommitOptions {
  author: string;
  subject: string;
  body?: string;
  notes?: string;
  refs?: Refs;
  tree?: string;
  commit?: string;
  parent?: string;
}

/**
 * Generate a random hash.
 *
 * @return {string} hex string with 40 chars
 */
const getRandomHash = () => (
  (Math.random()).toString(16).substring(3) +
  (Math.random()).toString(16).substring(3) +
  (Math.random()).toString(16).substring(3) +
  (Math.random()).toString(16).substring(3)
).substring(0, 40);

export class Commit {
  /**
   * Ref names
   */
  public refs: string[];
  /**
   * Commit hash
   */
  public commit: string;
  /**
   * Abbreviated commit hash
   */
  public commitAbbrev: string;
  /**
   * Tree hash
   */
  public tree: string;
  /**
   * Abbreviated tree hash
   */
  public treeAbbrev: string;
  /**
   * Parent hashes
   */
  public parents: string[];
  /**
   * Abbreviated parent hashed
   */
  public parentsAbbrev: string[];
  /**
   * Author
   */
  public author: {
    /**
     * Author name
     */
    name: string;
    /**
     * Author email
     */
    email: string;
    /**
     * Author date
     */
    timestamp: number;
  };
  /**
   * Committer
   */
  public committer: {
    /**
     * Commiter name
     */
    name: string;
    /**
     * Commiter email
     */
    email: string;
    /**
     * Commiter date
     */
    timestamp: number;
  };
  /**
   * Subject
   */
  public subject: string;
  /**
   * Body
   */
  public body: string;
  /**
   * Notes
   */
  public notes: string;

  public constructor(options: CommitOptions) {
    // Set author & committer
    const [, name, email] = options.author.match(/(.*) <(.*)>/) as RegExpExecArray;
    this.author = { name, email, timestamp: Date.now() };
    this.committer = { name, email, timestamp: Date.now() };

    // Set commit message
    this.subject = options.subject;
    this.body = options.body || "";
    this.notes = options.notes || "";

    // Set commit hash
    this.commit = options.commit || getRandomHash();
    this.commitAbbrev = this.commit.substring(0, 7);

    // Set tree hash
    this.tree = options.tree || getRandomHash();
    this.treeAbbrev = this.tree.substring(0, 7);

    // Set parent hash
    this.parents = options.parent ? [options.parent] : [];
    this.parentsAbbrev = this.parents.map((commit) => commit.substring(0, 7));

    // Set ref
    this.refs = options.refs || new Refs();
  }

  getRefs(): string[] {
    return this.refs.getFromCommit(this);
  }
}
<commit_msg>Fallback if author don't have the correct format<commit_after>import Refs from "./refs";

export interface CommitOptions {
  author: string;
  subject: string;
  body?: string;
  notes?: string;
  refs?: Refs;
  tree?: string;
  commit?: string;
  parent?: string;
}

/**
 * Generate a random hash.
 *
 * @return {string} hex string with 40 chars
 */
const getRandomHash = () => (
  (Math.random()).toString(16).substring(3) +
  (Math.random()).toString(16).substring(3) +
  (Math.random()).toString(16).substring(3) +
  (Math.random()).toString(16).substring(3)
).substring(0, 40);

export class Commit {
  /**
   * Ref names
   */
  public refs: string[];
  /**
   * Commit hash
   */
  public commit: string;
  /**
   * Abbreviated commit hash
   */
  public commitAbbrev: string;
  /**
   * Tree hash
   */
  public tree: string;
  /**
   * Abbreviated tree hash
   */
  public treeAbbrev: string;
  /**
   * Parent hashes
   */
  public parents: string[];
  /**
   * Abbreviated parent hashed
   */
  public parentsAbbrev: string[];
  /**
   * Author
   */
  public author: {
    /**
     * Author name
     */
    name: string;
    /**
     * Author email
     */
    email: string;
    /**
     * Author date
     */
    timestamp: number;
  };
  /**
   * Committer
   */
  public committer: {
    /**
     * Commiter name
     */
    name: string;
    /**
     * Commiter email
     */
    email: string;
    /**
     * Commiter date
     */
    timestamp: number;
  };
  /**
   * Subject
   */
  public subject: string;
  /**
   * Body
   */
  public body: string;
  /**
   * Notes
   */
  public notes: string;

  public constructor(options: CommitOptions) {
    // Set author & committer
    let name, email;
    try {
      [, name, email] = options.author.match(/(.*) <(.*)>/) as RegExpExecArray;
    } catch (e) {
      [name, email] = [options.author, ""];
    }
    this.author = { name, email, timestamp: Date.now() };
    this.committer = { name, email, timestamp: Date.now() };

    // Set commit message
    this.subject = options.subject;
    this.body = options.body || "";
    this.notes = options.notes || "";

    // Set commit hash
    this.commit = options.commit || getRandomHash();
    this.commitAbbrev = this.commit.substring(0, 7);

    // Set tree hash
    this.tree = options.tree || getRandomHash();
    this.treeAbbrev = this.tree.substring(0, 7);

    // Set parent hash
    this.parents = options.parent ? [options.parent] : [];
    this.parentsAbbrev = this.parents.map((commit) => commit.substring(0, 7));

    // Set ref
    this.refs = options.refs || new Refs();
  }

  getRefs(): string[] {
    return this.refs.getFromCommit(this);
  }
}

{'fraction_non_alphanumeric': 0.16418480336006108, 'fraction_numerical': 0.008781977854142803, 'mean_word_length': 2.30119722747322, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, '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': '2539449', 'n_tokens_mistral': 1763, 'n_tokens_neox': 1695, 'n_words': 403}