diff --git a/src/main/java/be/seeseepuff/webgit/controller/RepoController.java b/src/main/java/be/seeseepuff/webgit/controller/RepoController.java index a2efe71..dc6977a 100644 --- a/src/main/java/be/seeseepuff/webgit/controller/RepoController.java +++ b/src/main/java/be/seeseepuff/webgit/controller/RepoController.java @@ -103,6 +103,14 @@ public class RepoController model.addAttribute("name", name); model.addAttribute("hash", hash); model.addAttribute("shortHash", hash.substring(0, Math.min(7, hash.length()))); + var info = gitService.getCommitInfo(name, hash); + model.addAttribute("commitInfo", info); + String fullMsg = info.message() != null ? info.message().trim() : ""; + int newline = fullMsg.indexOf('\n'); + String title = newline >= 0 ? fullMsg.substring(0, newline).trim() : fullMsg; + String body = newline >= 0 ? fullMsg.substring(newline).trim() : ""; + model.addAttribute("commitTitle", title); + model.addAttribute("commitBody", body); model.addAttribute("diffs", gitService.getCommitDiff(name, hash)); return "commit"; } diff --git a/src/main/java/be/seeseepuff/webgit/service/GitService.java b/src/main/java/be/seeseepuff/webgit/service/GitService.java index 695ed4e..b1f14fe 100644 --- a/src/main/java/be/seeseepuff/webgit/service/GitService.java +++ b/src/main/java/be/seeseepuff/webgit/service/GitService.java @@ -434,6 +434,33 @@ public class GitService return sb.toString().stripTrailing(); } + public CommitInfo getCommitInfo(String name, String commitHash) throws IOException + { + try (Git git = openRepository(name)) + { + Repository repo = git.getRepository(); + ObjectId commitId = repo.resolve(commitHash); + try (var walk = new org.eclipse.jgit.revwalk.RevWalk(repo)) + { + RevCommit commit = walk.parseCommit(commitId); + DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm") + .withZone(ZoneId.systemDefault()); + List parents = Arrays.stream(commit.getParents()) + .map(p -> p.getId().abbreviate(7).name()) + .toList(); + return new CommitInfo( + commit.getId().getName(), + commit.getId().abbreviate(7).name(), + commit.getFullMessage(), + commit.getAuthorIdent().getName(), + fmt.format(Instant.ofEpochSecond(commit.getCommitTime())), + parents, + "" + ); + } + } + } + public List getCommitDiff(String name, String commitHash) throws IOException { try (Git git = openRepository(name)) diff --git a/src/main/resources/templates/commit.html b/src/main/resources/templates/commit.html index 6eea09a..bf17a6c 100644 --- a/src/main/resources/templates/commit.html +++ b/src/main/resources/templates/commit.html @@ -8,6 +8,20 @@

< Back to commits

+ + + + + + + + + +
Author:
Date:
+ +

+

+
 
diff --git a/src/test/java/be/seeseepuff/webgit/controller/RepoControllerTest.java b/src/test/java/be/seeseepuff/webgit/controller/RepoControllerTest.java index 66d9b6c..e6c17b1 100644 --- a/src/test/java/be/seeseepuff/webgit/controller/RepoControllerTest.java +++ b/src/test/java/be/seeseepuff/webgit/controller/RepoControllerTest.java @@ -246,6 +246,9 @@ class RepoControllerTest @Test void commitDetailShowsDiff() throws Exception { + when(gitService.getCommitInfo("myrepo", "abc1234")).thenReturn( + new be.seeseepuff.webgit.model.CommitInfo("abc1234abc1234", "abc1234", "Fix bug\n\nDetails here", "Alice", "2024-01-01 12:00", List.of(), "") + ); when(gitService.getCommitDiff("myrepo", "abc1234")).thenReturn(List.of( new be.seeseepuff.webgit.model.DiffInfo("ADD", "/dev/null", "file.txt", "+hello") )); @@ -254,6 +257,7 @@ class RepoControllerTest .andExpect(status().isOk()) .andExpect(view().name("commit")) .andExpect(model().attribute("hash", "abc1234")) + .andExpect(content().string(org.hamcrest.Matchers.containsString("Fix bug"))) .andExpect(content().string(org.hamcrest.Matchers.containsString("file.txt"))); } diff --git a/src/test/java/be/seeseepuff/webgit/service/GitServiceTest.java b/src/test/java/be/seeseepuff/webgit/service/GitServiceTest.java index f49f5d4..ca678b5 100644 --- a/src/test/java/be/seeseepuff/webgit/service/GitServiceTest.java +++ b/src/test/java/be/seeseepuff/webgit/service/GitServiceTest.java @@ -349,6 +349,19 @@ class GitServiceTest assertNotNull(commits.getFirst().graphLine()); } + @Test + void getCommitInfoReturnsDetails() throws GitAPIException, IOException + { + gitService.cloneRepository(bareRemote.toUri().toString(), "myrepo"); + + var commits = gitService.listCommits("myrepo"); + var info = gitService.getCommitInfo("myrepo", commits.getFirst().hash()); + assertNotNull(info); + assertTrue(info.message().contains("Initial commit")); + assertNotNull(info.author()); + assertNotNull(info.date()); + } + @Test void listCommitsGraphLineShowsBranchingCorrectly() throws GitAPIException, IOException {