Horje
Extracting Commit History with JGit

JGit is an open-source and pure Java library that can implement the Git version control system. It can allow the developers to interact with the Git repositories programmatically and enable them to perform various Git operations such as cloning the repositories, creating the branches and viewing the commit history.

This article will guide you to focus on using the JGit to extract and display the repository’s commit history.

Prerequisites

  • Basic Knowledge of the Java and Git.
  • Java Development Kit installed in your local system.
  • Maven for building dependency management.

Main Concept: Opening and Interacting with the Git Repository using JGit

  1. Repository and FileRepositoryBuilder Classes: JGit can provide these classes to locate and open the Git repository. The FileRepositoryBuilder can help in constructing the Repository object by specifying the path to the .git directory of the local repository.
  2. Git Class for Git Commands: The Git class in JGit can provide various methods to execute the Git commands. It can be used to retrieve the commit history.
  3. RevCommit Class: Each RevCommit object that can be represents the commit in the repository. It contains information such as the commit hash, author, date and commit messages of the repository.

Implementing the Extracting Commit History with JGit

Step 1: Clone the Repository

git clonet remote-repo-path

Step 2: Create the Maven Project

Create the maven project using IntelliJ Idea. Once create the project then the file structure looks like the below image.

jgitcommitfile


Step 3: Add the JGit Dependency

Open the pom.xml and add the below JGit dependency into the project.

 <!-- https://mvnrepository.com/artifact/org.eclipse.jgit/org.eclipse.jgit -->
        <dependency>
            <groupId>org.eclipse.jgit</groupId>
            <artifactId>org.eclipse.jgit</artifactId>
            <version>6.9.0.202403050737-r</version>
        </dependency>


Step 4: Create the GitCommitHistory Class

Java
package org.example;


import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;

import java.io.File;
import java.io.IOException;

public class GitCommitHistory {

    public static void main(String[] args) {
        // Path to the local Git repository
        String repoPath = "C:\\Users\\Mahesh\\Desktop\\jgit-commit\\E-Commerce-Backend-Spring-Microservices\\.git";

        try {
            // Open the repository
            FileRepositoryBuilder builder = new FileRepositoryBuilder();
            Repository repository = builder.setGitDir(new File(repoPath))
                    .readEnvironment()
                    .findGitDir()
                    .build();

            // Access the Git log
            try (Git git = new Git(repository)) {
                Iterable<RevCommit> commits = git.log().call();

                // Iterate over the commits
                for (RevCommit commit : commits) {
                    System.out.println("Commit: " + commit.getName());
                    System.out.println("Author: " + commit.getAuthorIdent().getName());
                    System.out.println("Date: " + commit.getAuthorIdent().getWhen());
                    System.out.println("Message: " + commit.getFullMessage());
                    System.out.println("----------------------------------");
                }
            }
        } catch (IOException | GitAPIException e) {
            e.printStackTrace();
        }
    }
}

Pom.xml

XML
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>JGit-Commit-Demo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>


    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.eclipse.jgit/org.eclipse.jgit -->
        <dependency>
            <groupId>org.eclipse.jgit</groupId>
            <artifactId>org.eclipse.jgit</artifactId>
            <version>6.9.0.202403050737-r</version>
        </dependency>
    </dependencies>

</project>


Step 5: Run the application

It will show the below output. It will print the commit history of specified repository.

gitcommitlog-compressed


By the following these steps. we should be able to extract the commit history from any Git repository using the JGit. This article covers the setting up the environment, explaining the concept and providing the complete example to illustrate the process of extracting commit history with JGit.




Reffered: https://www.geeksforgeeks.org


Java

Related
Adding JGit to the project with Gradle Adding JGit to the project with Gradle
How to Calculate Size of Object in Java? How to Calculate Size of Object in Java?
Java Program to Search an Element in Vector Java Program to Search an Element in Vector
Java Program to Solve the Fractional Knapsack Problem Java Program to Solve the Fractional Knapsack Problem
Prim&#039;s Algorithm with a Java Implementation Prim&#039;s Algorithm with a Java Implementation

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
12