<commit_before>/**
 * This code is free software; you can redistribute it and/or modify it under
 * the terms of the new BSD License.
 *
 * Copyright (c) 2011, <NAME>
 */

package com.github.koraktor.mavanagaiata;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.maven.plugin.MojoExecutionException;

import org.eclipse.jgit.lib.PersonIdent;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevWalk;

/**
 * This goal allows to generate a list of contributors for the currently
 * checked out branch of the Git repository. It will list all authors of the
 * commits in this branch. It can be configured to display the changelog or
 * save it to a file.
 *
 * @author <NAME>
 * @goal contributors
 * @phase compile
 * @requiresProject
 * @since 0.2.0
 */
public class GitContributorsMojo extends AbstractGitOutputMojo {

    /**
     * The header to print above the changelog
     *
     * @parameter expression="${mavanagaiata.contributors.header}"
     */
    protected String header = "Contributors\n============\n";

    /**
     * The string to prepend to every contributor name
     *
     * @parameter expression="${mavanagaiata.contributors.contributorPrefix}"
     */
    protected String contributorPrefix = " * ";

    /**
     * Whether the number of contributoions should be listed
     *
     * @parameter expression="${mavanagaiata.contributors.showCounts}"
     */
    protected boolean showCounts = true;

    /**
     * Whether the email addresses of contributors should be listed
     *
     * @parameter expression="${mavanagaiata.contributors.showEmail}"
     */
    protected boolean showEmail = false;

    /**
     * The method used to sort contributors
     *
     * @parameter expression="${mavanagaiata.contributors.sort}"
     */
    protected String sort;

    /**
     * Walks through the history of the currently checked out branch of the
     * Git repository and builds a list of contributors from the authors of the
     * commits.
     *
     * @throws MojoExecutionException if retrieving information from the Git
     *         repository fails
     */
    public void execute() throws MojoExecutionException {
        this.contributorPrefix = this.contributorPrefix.replaceAll("([^\\\\])\\\\n", "$1\n");
        this.header            = this.header.replaceAll("([^\\\\])\\\\n", "$1\n");

        try {
            this.initRepository();
            this.initOutputStream();

            RevWalk revWalk = new RevWalk(this.repository);
            revWalk.markStart(this.getHead());

            RevCommit tempCommit;
            List<RevCommit> commits = new ArrayList<RevCommit>();
            while((tempCommit = revWalk.next()) != null) {
                commits.add(tempCommit);
            }

            if(this.sort == null) {
                this.sort = "count";
            } else {
                this.sort = this.sort.toLowerCase();
            }

            final Map<String, Integer> counts = new HashMap<String, Integer>();
            if(this.showCounts ||
               (!this.sort.equals("date") && !this.sort.equals("name"))) {
                for(RevCommit commit : commits) {
                    String emailAddress = commit.getAuthorIdent().getEmailAddress();
                    if(!counts.containsKey(emailAddress)) {
                        counts.put(emailAddress, 1);
                    } else {
                        counts.put(emailAddress, counts.get(emailAddress) + 1);
                    }
                }
            }

            if(this.sort.equals("date")) {
                Collections.reverse(commits);
            } else if(this.sort.equals("name")) {
                Collections.sort(commits, new Comparator<RevCommit>() {
                    public int compare(RevCommit c1, RevCommit c2) {
                        String a1 = c1.getAuthorIdent().getName();
                        String a2 = c2.getAuthorIdent().getName();
                        return a1.compareTo(a2);
                    }
                });
            } else {
                Collections.sort(commits, new Comparator<RevCommit>() {<commit_msg>Simplified GitContributorsMojo's code a bit<commit_after>/**
 * This code is free software; you can redistribute it and/or modify it under
 * the terms of the new BSD License.
 *
 * Copyright (c) 2011, <NAME>
 */

package com.github.koraktor.mavanagaiata;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.maven.plugin.MojoExecutionException;

import org.eclipse.jgit.lib.PersonIdent;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevWalk;

/**
 * This goal allows to generate a list of contributors for the currently
 * checked out branch of the Git repository. It will list all authors of the
 * commits in this branch. It can be configured to display the changelog or
 * save it to a file.
 *
 * @author <NAME>
 * @goal contributors
 * @phase compile
 * @requiresProject
 * @since 0.2.0
 */
public class GitContributorsMojo extends AbstractGitOutputMojo {

    /**
     * The header to print above the changelog
     *
     * @parameter expression="${mavanagaiata.contributors.header}"
     */
    protected String header = "Contributors\n============\n";

    /**
     * The string to prepend to every contributor name
     *
     * @parameter expression="${mavanagaiata.contributors.contributorPrefix}"
     */
    protected String contributorPrefix = " * ";

    /**
     * Whether the number of contributoions should be listed
     *
     * @parameter expression="${mavanagaiata.contributors.showCounts}"
     */
    protected boolean showCounts = true;

    /**
     * Whether the email addresses of contributors should be listed
     *
     * @parameter expression="${mavanagaiata.contributors.showEmail}"
     */
    protected boolean showEmail = false;

    /**
     * The method used to sort contributors
     *
     * @parameter expression="${mavanagaiata.contributors.sort}"
     */
    protected String sort;

    /**
     * Walks through the history of the currently checked out branch of the
     * Git repository and builds a list of contributors from the authors of the
     * commits.
     *
     * @throws MojoExecutionException if retrieving information from the Git
     *         repository fails
     */
    public void execute() throws MojoExecutionException {
        this.contributorPrefix = this.contributorPrefix.replaceAll("([^\\\\])\\\\n", "$1\n");
        this.header            = this.header.replaceAll("([^\\\\])\\\\n", "$1\n");

        try {
            this.initRepository();
            this.initOutputStream();

            RevWalk revWalk = new RevWalk(this.repository);
            revWalk.markStart(this.getHead());

            RevCommit tempCommit;
            List<RevCommit> commits = new ArrayList<RevCommit>();
            while((tempCommit = revWalk.next()) != null) {
                commits.add(tempCommit);
            }

            if(this.sort == null) {
                this.sort = "count";
            } else {
                this.sort = this.sort.toLowerCase();
            }

            final Map<String, Integer> counts = new HashMap<String, Integer>();
            if(this.showCounts || this.sort.equals("count")) {
                for(RevCommit commit : commits) {
                    String emailAddress = commit.getAuthorIdent().getEmailAddress();
                    if(!counts.containsKey(emailAddress)) {
                        counts.put(emailAddress, 1);
                    } else {
                        counts.put(emailAddress, counts.get(emailAddress) + 1);
                    }
                }
            }

            if(this.sort.equals("date")) {
                Collections.reverse(commits);
            } else if(this.sort.equals("name")) {
                Collections.sort(commits, new Comparator<RevCommit>() {
                    public int compare(RevCommit c1, RevCommit c2) {
                        String a1 = c1.getAuthorIdent().getName();
                        String a2 = c2.getAuthorIdent().getName();
                        return a1.compareTo(a2);
                    }
                });
            } else {
                Collections.sort(commits, new Comparator<RevCommit>() {
{'fraction_non_alphanumeric': 0.11161335187760779, 'fraction_numerical': 0.00440426518312471, 'mean_word_length': 2.1264492753623188, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, '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': '534704', 'n_tokens_mistral': 2328, 'n_tokens_neox': 2245, 'n_words': 593}