views: branch/paginate: fix incorrectly enabled "next" link

When the number of commits on a branch page is less than
'commits_per_page', the pagination "next" link is disabled, indicating
correctly that this is the final page. However, if the number of commits
on the branch page is exactly 'commits_per_page', then the "next" link
is incorrectly enabled, even on the final page. When clicked, the user
is taken to a "404 Page not found" error page, which makes for a poor
user experience.

Fix this problem by reliably detecting when the branch page is the final
one. Do so by asking for (but not displaying) one commit more than
actually needed by the page. If the additional commit is successfully
retrieved, then another page definitely follows this one. If not
retrieved, then this is definitely the final page.

(Unfortunately, the seemingly more expedient approach of checking if the
final commit on the current page is a root commit -- has no parents --
is not a reliable indicator that this the final page since a branch may
have multiple root commits due to merging.)

Signed-off-by: Eric Sunshine <sunshine@sunshineco.com>
Signed-off-by: Alberto Bertogli <albertito@blitiri.com.ar>
This commit is contained in:
Eric Sunshine 2015-01-01 16:41:08 -05:00 committed by Alberto Bertogli
parent bebc7fa3f0
commit ac105c8383
2 changed files with 8 additions and 4 deletions

@ -18,7 +18,7 @@
</p> </p>
% commits = repo.commits("refs/heads/" + repo.branch, % commits = repo.commits("refs/heads/" + repo.branch,
% limit = repo.info.commits_per_page, % limit = repo.info.commits_per_page + 1,
% offset = repo.info.commits_per_page * offset) % offset = repo.info.commits_per_page * offset)
% commits = list(commits) % commits = list(commits)
@ -26,8 +26,12 @@
% abort(404, "No more commits") % abort(404, "No more commits")
% end % end
% more = len(commits) > repo.info.commits_per_page
% if more:
% commits = commits[:-1]
% end
% include paginate nelem = len(commits), max_per_page = repo.info.commits_per_page, offset = offset % include paginate more = more, offset = offset
% kwargs = dict(repo=repo, commits=commits, % kwargs = dict(repo=repo, commits=commits,
% shorten=shorten, repo_root="../..") % shorten=shorten, repo_root="../..")
@ -35,7 +39,7 @@
<p/> <p/>
% include paginate nelem = len(commits), max_per_page = repo.info.commits_per_page, offset = offset % include paginate more = more, offset = offset
</body> </body>
</html> </html>

@ -6,7 +6,7 @@
<span class="inactive">&larr; prev</span> <span class="inactive">&larr; prev</span>
% end % end
<span class="sep">|</span> <span class="sep">|</span>
% if nelem >= max_per_page: % if more:
<a href="{{offset + 1}}.html">next &rarr;</a> <a href="{{offset + 1}}.html">next &rarr;</a>
% else: % else:
<span class="inactive">next &rarr;</span> <span class="inactive">next &rarr;</span>