A modern Java library for navigating GitHub repository commits programmatically with support for file filtering, authentication, and automatic synchronization.
- Commit Navigation: Navigate forward and backward through commit history with metadata
- Auto-Sync: Automatically pull latest changes when opening existing repositories
- File Filtering: Filter commits based on file changes (exact paths or glob patterns)
- Multiple Authentication: Support for Personal Access Token, OAuth, Username/Password, and SSH keys
- Flexible Configuration: Builder pattern for easy configuration
- Checkout Support: Optional checkout during navigation
<dependency>
<groupId>edu.stanford.protege.commitnavigator</groupId>
<artifactId>github-commit-navigator</artifactId>
<version>1.0.0</version>
</dependency>
import edu.stanford.protege.commitnavigator.GitHubRepository;
import edu.stanford.protege.commitnavigator.GitHubRepositoryBuilder;
import edu.stanford.protege.commitnavigator.services.CommitNavigator;
import edu.stanford.protege.commitnavigator.model.CommitMetadata;
// Create navigator with Personal Access Token
var repository = GitHubRepositoryBuilder
.forRepository("https://github.com/example/repo.git")
.withPersonalAccessToken("your-token-here")
.build();
// Initialize the navigator
repository.initialize();
// Get commit navigator
var commitNavigator = navigator.getCommitNavigator();
// Navigate through commits
while (commitNavigator.hasNext()) {
var commit = commitNavigator.next();
System.out.println("Commit: " + commit.getCommitHash());
System.out.println("Author: " + commit.getCommitterUsername());
System.out.println("Date: " + commit.getCommitDate());
System.out.println("Message: " + commit.getCommitMessage());
System.out.println("---");
}
// Navigate with checkout
while (commitNavigator.hasPrevious()) {
var commit = commitNavigator.previousAndCheckout();
System.out.println("Checked out commit: " + commit.getCommitHash());
}
// Clean up
navigator.close();
var navigator = GitHubRepoNavigatorBuilder
.forRepository("https://github.com/example/repo.git")
.withPersonalAccessToken("your-token-here")
.localCloneDirectory("/path/to/local/repo")
.fileFilters("*.java", "*.md", "pom.xml")
.branch("develop")
.startingCommit("abc123def")
.shallowClone(true)
.build();
The library supports multiple authentication methods for accessing private repositories:
.withPersonalAccessToken("your-pat-token")
.withOAuthToken("your-oauth-token")
.withUsernamePassword("username", "password")
.withSshKey("/path/to/ssh/key")
For public repositories, authentication is optional:
var navigator = GitHubRepositoryBuilder
.forRepository("https://github.com/public/repo.git")
.build(); // No authentication needed
next()
- Move to next commit (returnsCommitMetadata
)previous()
- Move to previous commit (returnsCommitMetadata
)hasNext()
- Check if next commit existshasPrevious()
- Check if previous commit existsgetCurrentCommit()
- Get current commit metadatareset()
- Reset navigator to initial state
nextAndCheckout()
- Move to next commit and checkout working directorypreviousAndCheckout()
- Move to previous commit and checkout working directory
The CommitMetadata
record provides comprehensive commit information:
var commit = commitNavigator.next();
var hash = commit.getCommitHash(); // Full commit SHA
var author = commit.getCommitterUsername(); // Committer username
var date = commit.getCommitDate(); // LocalDateTime of commit
var message = commit.getCommitMessage(); // Full commit message
Filter commits to only include those that modified specific files:
// Exact file paths
.fileFilters("src/main/java/Main.java", "README.md")
// Glob patterns
.fileFilters("*.java", "**/*.md", "src/**/*.xml")
// Mixed patterns
.fileFilters("pom.xml", "*.java", "docs/**/*.md")
When opening an existing local repository, the library automatically:
- Fetches latest changes from remote
- Compares local vs remote commits
- Pulls new commits if available
- Logs all sync operations
DEBUG - Checking for remote changes...
INFO - New changes detected on remote branch 'main', pulling changes...
INFO - Successfully pulled 3 new commits from remote
The library includes a CLI for quick repository analysis:
# Basic usage
java -jar github-commit-navigator-1.0.0.jar https://github.com/user/repo.git
# With authentication and filters
java -jar github-commit-navigator-1.0.0.jar \
--token your-token \
--file-filter "*.java,*.md" \
--branch develop \
--clone-directory /tmp/repo \
https://github.com/user/repo.git
-t, --token
: GitHub personal access token-b, --branch
: Branch to analyze (default: main)-d, --clone-directory
: Local clone directory-f, --file-filter
: Comma-separated file filters
- Java 17+ (uses modern Java features)
- Maven 3.6+ for building
- Git installed on system (for JGit operations)
# Clone the repository
git clone https://github.com/your-org/github-commit-navigator-library.git
# Build with Maven
mvn clean compile
# Run tests
mvn test
# Create JAR
mvn package