共計 1436 個字符,預計需要花費 4 分鐘才能閱讀完成。
Java 可以使用 JGit 庫來獲取 Git 提交記錄。JGit 是一個用于訪問 Git 版本控制系統的 Java 庫。
首先,你需要在 Java 項目中引入 JGit 庫的依賴。你可以使用 Maven 或 Gradle 等構建工具來管理依賴。
Maven 依賴:
<dependency>
<groupId>org.eclipse.jgit</groupId>
<artifactId>org.eclipse.jgit</artifactId>
<version>5.13.1.202109080827-r</version>
</dependency>
Gradle 依賴:
implementation 'org.eclipse.jgit:org.eclipse.jgit:5.13.1.202109080827-r'
接下來,你可以使用以下代碼獲取 Git 提交記錄:
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.revwalk.RevCommit;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Iterator;
public class GitCommitHistory {public static void main(String[] args) {// 設置本地倉庫路徑
Path repositoryPath = Paths.get("/path/to/your/repository");
try (Git git = Git.open(repositoryPath.toFile())) {Iterable<RevCommit> commits = git.log().all().call();
Iterator<RevCommit> iterator = commits.iterator();
while (iterator.hasNext()) {RevCommit commit = iterator.next();
System.out.println(commit.getFullMessage());
System.out.println(commit.getAuthorIdent().getName());
System.out.println(commit.getAuthorIdent().getEmailAddress());
System.out.println(commit.getAuthorIdent().getWhen());
System.out.println("----------------------");
}
} catch (IOException | GitAPIException e) {e.printStackTrace();
}
}
}
請確保將 /path/to/your/repository 替換為你的本地 Git 倉庫路徑。
以上代碼將打印出每個提交記錄的完整消息、作者姓名、作者郵箱和提交時間。
注意,你需要在代碼中捕獲可能拋出的異常,例如 IOException 和GitAPIException。
這是一種獲取 Git 提交記錄的基本方法。你還可以根據需要使用 JGit 提供的其他方法來獲取更多相關信息。
丸趣 TV 網 – 提供最優質的資源集合!
正文完