<commit_before>package br.ufmg.dcc.labsoft.apidiff.detect.diff;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.eclipse.jgit.diff.DiffEntry.ChangeType;
import org.eclipse.jgit.lib.PersonIdent;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevWalk;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import br.ufmg.dcc.labsoft.apidiff.UtilFile;
import br.ufmg.dcc.labsoft.apidiff.UtilTools;
import br.ufmg.dcc.labsoft.apidiff.detect.diff.service.git.GitFile;
import br.ufmg.dcc.labsoft.apidiff.detect.diff.service.git.GitService;
import br.ufmg.dcc.labsoft.apidiff.detect.diff.service.git.GitServiceImpl;
import br.ufmg.dcc.labsoft.apidiff.detect.parser.APIVersion;
import br.ufmg.dcc.labsoft.apidiff.enums.ClassifierAPI;

public class APIDiff {
	
	private final String nameFile = "output.csv";
	
	private String nameProject;
	private String url;
	
	private Result resultType; 
	private Result resultAnnotationType; 
	private Result resultAnnotationTypeMember; 
	private Result resultFild;
	private Result resultMethod;
	private Result resultEnum;
	private Result resultEnumConstant;
	
	private Logger logger = LoggerFactory.getLogger(APIDiff.class);

	public APIDiff(final String nameProject, final String url) {
		this.url = url;
		this.nameProject = nameProject;
	}
	
	public APIDiff(final String nameProject) {
		this.nameProject = nameProject;
	}

	public void calculateDiffCommit() {
		try {
			GitService service = new GitServiceImpl();
			Repository repository = service.openRepositoryAndCloneIfNotExists(this.nameProject, this.url);
			RevWalk revWalk = service.fetchAndCreateNewRevsWalk(repository, null);
			
			//Itera sobre os commits.
			Iterator<RevCommit> i = revWalk.iterator();
			while(i.hasNext()){
				RevCommit currentCommit = i.next();
				this.diffCommit(currentCommit, repository, this.nameProject, ClassifierAPI.API);
				this.diffCommit(currentCommit, repository, this.nameProject, ClassifierAPI.NON_API_EXPERIMENTAL);
				this.diffCommit(currentCommit, repository, this.nameProject, ClassifierAPI.NON_API_INTERNAL);
			}
		
		} catch (Exception e) {
			this.logger.error("Error in calculating commitn diff ", e);
		}
		this.logger.info("Finished processing. Check the output file <" + this.nameFile + ">");
	}
	
	
	/**
	 * Calcula a diferença entre dois projetos.
	 * @param nameProjectVersion1
	 * @param nameProjectVersion2
	 */
	public void calculateDiffProject(final String nameProjectVersion1, final String nameProjectVersion2, ClassifierAPI classifierAPI){
		APIVersion version1 = new APIVersion(nameProjectVersion1, classifierAPI);
		APIVersion version2 = new APIVersion(nameProjectVersion2, classifierAPI);
		this.diff(version1, version2);
		this.printAll(null, classifierAPI);//Escreve saída em arquivo.
	}
	
	
	/**
	 * Calcula um diff entre um commit e a versão anterior dos seus arquivos.
	 * @param currentCommit
	 * @param repository
	 * @param nameProject
	 * @throws Exception
	 */<commit_msg>Ajustes das mensagens de log.<commit_after>package br.ufmg.dcc.labsoft.apidiff.detect.diff;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.eclipse.jgit.diff.DiffEntry.ChangeType;
import org.eclipse.jgit.lib.PersonIdent;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevWalk;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import br.ufmg.dcc.labsoft.apidiff.UtilFile;
import br.ufmg.dcc.labsoft.apidiff.UtilTools;
import br.ufmg.dcc.labsoft.apidiff.detect.diff.service.git.GitFile;
import br.ufmg.dcc.labsoft.apidiff.detect.diff.service.git.GitService;
import br.ufmg.dcc.labsoft.apidiff.detect.diff.service.git.GitServiceImpl;
import br.ufmg.dcc.labsoft.apidiff.detect.parser.APIVersion;
import br.ufmg.dcc.labsoft.apidiff.enums.ClassifierAPI;

public class APIDiff {
	
	private final String nameFile = "output.csv";
	
	private String nameProject;
	private String url;
	
	private Result resultType; 
	private Result resultAnnotationType; 
	private Result resultAnnotationTypeMember; 
	private Result resultFild;
	private Result resultMethod;
	private Result resultEnum;
	private Result resultEnumConstant;
	
	private Logger logger = LoggerFactory.getLogger(APIDiff.class);

	public APIDiff(final String nameProject, final String url) {
		this.url = url;
		this.nameProject = nameProject;
	}
	
	public APIDiff(final String nameProject) {
		this.nameProject = nameProject;
	}

	public void calculateDiffCommit() {
		try {
			GitService service = new GitServiceImpl();
			Repository repository = service.openRepositoryAndCloneIfNotExists(this.nameProject, this.url);
			RevWalk revWalk = service.fetchAndCreateNewRevsWalk(repository, null);
			
			//Itera sobre os commits.
			Iterator<RevCommit> i = revWalk.iterator();
			while(i.hasNext()){
				RevCommit currentCommit = i.next();
				this.diffCommit(currentCommit, repository, this.nameProject, ClassifierAPI.API);
				this.diffCommit(currentCommit, repository, this.nameProject, ClassifierAPI.NON_API_EXPERIMENTAL);
				this.diffCommit(currentCommit, repository, this.nameProject, ClassifierAPI.NON_API_INTERNAL);
			}
		
		} catch (Exception e) {
			this.logger.error("Error in calculating commitn diff ", e);
		}
		this.logger.info("Finished processing. Check the output file <" + this.nameFile + ">");
	}
	
	
	/**
	 * Calcula a diferença entre dois projetos.
	 * @param nameProjectVersion1
	 * @param nameProjectVersion2
	 */
	public void calculateDiffProject(final String nameProjectVersion1, final String nameProjectVersion2, ClassifierAPI classifierAPI){
		APIVersion version1 = new APIVersion(nameProjectVersion1, classifierAPI);
		APIVersion version2 = new APIVersion(nameProjectVersion2, classifierAPI);
		this.diff(version1, version2);
		this.printAll(null, classifierAPI);//Escreve saída em arquivo.
		logger.info("Finished!");
	}
	
	
	/**
	 * Calcula um diff entre um commit e a versão anterior dos seus arquivos.
	 * @param currentCommit
	 * @param repository
	 * @param nameProject
	 * @throws Exception
	 */
{'fraction_non_alphanumeric': 0.10009442870632672, 'fraction_numerical': 0.003777148253068933, 'mean_word_length': 4.961538461538462, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '418258', 'n_tokens_mistral': 2145, 'n_tokens_neox': 1993, 'n_words': 431}