From 8784dfc391d0dd7837ff16fef108e3723ae44c92 Mon Sep 17 00:00:00 2001 From: Sebastiaan de Schaetzen Date: Fri, 27 Feb 2026 11:24:40 +0100 Subject: [PATCH] Show commit title, description, author and date on commit detail page Added GitService.getCommitInfo() using getFullMessage() to preserve the full commit body. Controller splits the message into title (first line) and body (remainder) for separate display in the template. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../webgit/controller/RepoController.java | 8 ++++++ .../seeseepuff/webgit/service/GitService.java | 27 +++++++++++++++++++ src/main/resources/templates/commit.html | 14 ++++++++++ .../webgit/controller/RepoControllerTest.java | 4 +++ .../webgit/service/GitServiceTest.java | 13 +++++++++ 5 files changed, 66 insertions(+) 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 {