Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ public String getCurrentCommitHash() throws IOException {
return headCommit.getName();
}

/**
* Returns the repository's origin URL
*
* @return the origin URL as a String, or null if not configured
*/
/**
* Returns the repository's origin URL
*
Expand All @@ -79,6 +84,29 @@ public String getOriginUrl() {
return gitRepository.getConfig().getString("remote", "origin", "url");
}

public String getRepoUrl() throws IOException {
String repoUrl;
String originUrl = getOriginUrl();
if (originUrl != null && originUrl.startsWith("git@")) {
originUrl = originUrl.replace(":", "/").replace("git@", "https://");
}

if (originUrl == null) {
return "";
}

repoUrl = originUrl.replace(".git", "");

if (repoUrl.contains("gitlab")) {
repoUrl = repoUrl + "/-/blob/" + getCurrentCommitHash() + "/";
} else if (repoUrl.contains("bitbucket")) {
repoUrl = repoUrl + "/src/" + getCurrentCommitHash() + "/";
} else {
repoUrl = repoUrl + "/blob/" + getCurrentCommitHash() + "/";
}
return repoUrl;
}

// log --follow implementation may be worth adopting in the future
// https://github.com/spearce/jgit/blob/master/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/RevWalkTextBuiltin.java

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package org.hjug.git;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

class GitLogReaderGetRepoUrlTest {

@TempDir
Path tempDir;

private Git git;
private File projectBaseDir;

@BeforeEach
void setUp() throws GitAPIException, IOException {
projectBaseDir = tempDir.toFile();
git = Git.init().setDirectory(projectBaseDir).call();
Files.write(tempDir.resolve("test.txt"), "test content".getBytes());
git.add().addFilepattern(".").call();
git.commit().setMessage("Initial commit").call();
}

@AfterEach
void tearDown() {
git.close();
}

@Test
void testGetRepoUrlWithGitHubSshOrigin() throws Exception {
git.remoteAdd()
.setName("origin")
.setUri(new org.eclipse.jgit.transport.URIish("git@github.com:user/repo.git"))
.call();

try (GitLogReader gitLogReader = new GitLogReader(projectBaseDir)) {
String repoUrl = gitLogReader.getRepoUrl();
String commitHash = git.log().call().iterator().next().getName();
assertTrue(repoUrl.startsWith("https://github.com/user/repo/blob/" + commitHash + "/"));
}
}

@Test
void testGetRepoUrlWithGitLabSshOrigin() throws Exception {
git.remoteAdd()
.setName("origin")
.setUri(new org.eclipse.jgit.transport.URIish("git@gitlab.com:user/repo.git"))
.call();

try (GitLogReader gitLogReader = new GitLogReader(projectBaseDir)) {
String repoUrl = gitLogReader.getRepoUrl();
String commitHash = git.log().call().iterator().next().getName();
assertTrue(repoUrl.startsWith("https://gitlab.com/user/repo/-/blob/" + commitHash + "/"));
}
}

@Test
void testGetRepoUrlWithBitBucketSshOrigin() throws Exception {
git.remoteAdd()
.setName("origin")
.setUri(new org.eclipse.jgit.transport.URIish("git@bitbucket.org:user/repo.git"))
.call();

try (GitLogReader gitLogReader = new GitLogReader(projectBaseDir)) {
String repoUrl = gitLogReader.getRepoUrl();
String commitHash = git.log().call().iterator().next().getName();
assertTrue(repoUrl.startsWith("https://bitbucket.org/user/repo/src/" + commitHash + "/"));
}
}

@Test
void testGetRepoUrlWithHttpsOrigin() throws Exception {
git.remoteAdd()
.setName("origin")
.setUri(new org.eclipse.jgit.transport.URIish("https://github.com/user/repo.git"))
.call();

try (GitLogReader gitLogReader = new GitLogReader(projectBaseDir)) {
String repoUrl = gitLogReader.getRepoUrl();
String commitHash = git.log().call().iterator().next().getName();
assertTrue(repoUrl.startsWith("https://github.com/user/repo/blob/" + commitHash + "/"));
}
}

@Test
void testGetRepoUrlWithNoOrigin() throws Exception {
try (GitLogReader gitLogReader = new GitLogReader(projectBaseDir)) {
String repoUrl = gitLogReader.getRepoUrl();
assertEquals("", repoUrl);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -395,11 +395,9 @@ public StringBuilder generateReport(
}

static String getRepoUrl(String projectBaseDir) throws Exception {
String repoUrl;
try (GitLogReader glr = new GitLogReader(new File(projectBaseDir))) {
repoUrl = glr.getOriginUrl().replace(".git", "") + "/blob/" + glr.getCurrentCommitHash() + "/";
return glr.getRepoUrl();
}
return repoUrl;
}

StringBuilder createMenu(
Expand Down
Loading