Fix branch/commit view bugs

- Filter out non-branch refs (like HEAD) from branch list
- Walk all branch tips in commit list so older checkouts still
  show newer commits
- Show '(current)' instead of Checkout button for HEAD commit
- Swap nav link order in blob view (commits first, then tree)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
2026-02-27 09:52:59 +01:00
parent fdc520cfaf
commit fbfffac73f
4 changed files with 24 additions and 7 deletions

View File

@@ -63,6 +63,7 @@ public class RepoController
{
model.addAttribute("name", name);
model.addAttribute("commits", gitService.listCommits(name));
model.addAttribute("headHash", gitService.getHeadCommitHash(name));
return "commits";
}

View File

@@ -133,7 +133,8 @@ public class GitService
.call()
.stream()
.map(Ref::getName)
.map(ref -> ref.startsWith("refs/heads/") ? ref.substring("refs/heads/".length()) : ref)
.filter(ref -> ref.startsWith("refs/heads/"))
.map(ref -> ref.substring("refs/heads/".length()))
.toList();
}
}
@@ -146,6 +147,15 @@ public class GitService
}
}
public String getHeadCommitHash(String name) throws IOException
{
try (Git git = openRepository(name))
{
ObjectId head = git.getRepository().resolve("HEAD");
return head != null ? head.getName() : null;
}
}
public void checkoutBranch(String name, String branch) throws IOException, GitAPIException
{
try (Git git = openRepository(name))
@@ -273,10 +283,15 @@ public class GitService
Repository repo = git.getRepository();
try (PlotWalk plotWalk = new PlotWalk(repo))
{
// Walk from all branch tips so we see all commits
for (Ref ref : repo.getRefDatabase().getRefsByPrefix("refs/heads/"))
{
plotWalk.markStart(plotWalk.parseCommit(ref.getObjectId()));
}
// Also include HEAD in case of detached HEAD
ObjectId head = repo.resolve("HEAD");
if (head == null)
return List.of();
plotWalk.markStart(plotWalk.parseCommit(head));
if (head != null)
plotWalk.markStart(plotWalk.parseCommit(head));
PlotCommitList<PlotLane> plotCommitList = new PlotCommitList<>();
plotCommitList.source(plotWalk);

View File

@@ -7,9 +7,9 @@
<h2 th:text="${filePath}">file.txt</h2>
<p>
<a th:href="@{/repo/{name}/tree/{hash}(name=${name}, hash=${hash})}">&lt; Back to tree</a>
|
<a th:href="@{/repo/{name}/commits(name=${name})}">&lt; Back to commits</a>
|
<a th:href="@{/repo/{name}/tree/{hash}(name=${name}, hash=${hash})}">&lt; Back to tree</a>
</p>
<table border="0" cellpadding="4" cellspacing="0">

View File

@@ -25,7 +25,8 @@
<td th:text="${c.date}"></td>
<td><a th:href="@{/repo/{name}/tree/{hash}(name=${name}, hash=${c.hash})}">Browse</a></td>
<td>
<form method="post" th:action="@{/repo/{name}/checkout-commit(name=${name})}">
<span th:if="${c.hash == headHash}">(current)</span>
<form th:unless="${c.hash == headHash}" method="post" th:action="@{/repo/{name}/checkout-commit(name=${name})}">
<input type="hidden" name="hash" th:value="${c.hash}">
<input type="submit" value="Checkout">
</form>