<commit_before>package org.openlca.git.util;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;

import org.eclipse.jgit.internal.storage.file.FileRepository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.treewalk.TreeWalk;
import org.eclipse.jgit.treewalk.filter.PathFilter;
import org.openlca.jsonld.PackageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.Gson;
import com.google.gson.JsonElement;

public final class Repositories {

	private static final Logger log = LoggerFactory.getLogger(Repositories.class);

	private Repositories() {
	}

	public static FileRepository open(File dir) {
		return open(dir.toPath());
	}

	public static FileRepository open(Path dir) {
		try {
			var repo = new FileRepository(dir.toFile());
			if (!Files.exists(dir)) {
				// create a new bare repository
				repo.create(true);
			}
			return repo;
		} catch (IOException e) {
			throw new RuntimeException(
					"could not get repository from folder: " + dir, e);
		}
	}

	public static PackageInfo infoOf(FileRepository repo) {
		var commit = headCommitOf(repo);
		if (commit == null)
			return null;
		try (var walk = new TreeWalk(repo);
				var reader = repo.getObjectDatabase().newReader()) {
			walk.addTree(commit.getTree().getId());
			walk.setRecursive(false);
			walk.setFilter(PathFilter.create(PackageInfo.FILE_NAME));
			if (!walk.next())
				return null;
			var blobId = walk.getObjectId(0);
			var bytes = reader.open(blobId).getBytes();
			var json = new Gson().fromJson(new String(bytes, StandardCharsets.UTF_8), JsonElement.class);
			return PackageInfo.of(json);
		} catch (IOException e) {
			log.error("failed to read schema version", e);
			return null;
		}
	}

	public static RevCommit headCommitOf(FileRepository repo) {
		try (var walk = new RevWalk(repo)) {
			var head = repo.resolve("HEAD");
			if (head == null) {
				head = repo.resolve("refs/heads/master");
			}
			if (head == null)
				return null;
			var commit = walk.parseCommit(head);
			if (commit == null)
				return null;
			return commit;
		} catch (IOException e) {
			log.error("Error reading commit tree", e);
			return null;
		}
	}
<commit_msg>Added option to pass commit for retrieving package info<commit_after>package org.openlca.git.util;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;

import org.eclipse.jgit.internal.storage.file.FileRepository;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.treewalk.TreeWalk;
import org.eclipse.jgit.treewalk.filter.PathFilter;
import org.openlca.git.model.Commit;
import org.openlca.jsonld.PackageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.Gson;
import com.google.gson.JsonElement;

public final class Repositories {

	private static final Logger log = LoggerFactory.getLogger(Repositories.class);

	private Repositories() {
	}

	public static FileRepository open(File dir) {
		return open(dir.toPath());
	}

	public static FileRepository open(Path dir) {
		try {
			var repo = new FileRepository(dir.toFile());
			if (!Files.exists(dir)) {
				// create a new bare repository
				repo.create(true);
			}
			return repo;
		} catch (IOException e) {
			throw new RuntimeException(
					"could not get repository from folder: " + dir, e);
		}
	}

	public static PackageInfo infoOf(FileRepository repo) {
		return infoOf(repo, null);
	}

	public static PackageInfo infoOf(FileRepository repo, Commit commit) {
		try (var walk = new TreeWalk(repo);
				var reader = repo.getObjectDatabase().newReader()) {
			var revCommit = commit != null
					? repo.parseCommit(ObjectId.fromString(commit.id))
					: headCommitOf(repo);
			walk.addTree(revCommit.getTree().getId());
			walk.setRecursive(false);
			walk.setFilter(PathFilter.create(PackageInfo.FILE_NAME));
			if (!walk.next())
				return null;
			var blobId = walk.getObjectId(0);
			var bytes = reader.open(blobId).getBytes();
			var json = new Gson().fromJson(new String(bytes, StandardCharsets.UTF_8), JsonElement.class);
			return PackageInfo.of(json);
		} catch (IOException e) {
			log.error("failed to read schema version", e);
			return null;
		}
	}

	public static RevCommit headCommitOf(FileRepository repo) {
		try (var walk = new RevWalk(repo)) {
			var head = repo.resolve("HEAD");
			if (head == null) {
				head = repo.resolve("refs/heads/master");
			}
			if (head == null)
				return null;
			var commit = walk.parseCommit(head);
			if (commit == null)
				return null;
			return commit;
		} catch (IOException e) {
			log.error("Error getting head commit", e);
			return null;
		}
	}

{'fraction_non_alphanumeric': 0.12380565155519414, 'fraction_numerical': 0.0016263468184590363, 'mean_word_length': 4.03582395087001, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '415999', 'n_tokens_mistral': 1775, 'n_tokens_neox': 1591, 'n_words': 384}