mygit

[UNMAINTAINED] A cgit/webgit alternative, written in Rust
Log | Files | Refs | README | LICENSE

commit a24c87629debfc0395a9c0c7d4e69f382c0bb26d
parent bb3bf0e79138e99743860ab80eae8a1f10a958dd
Author: Johann150 <johann@qwertqwefsday.eu>
Date:   Mon, 15 Mar 2021 18:57:43 +0100

handle shallow clones

Although unusual for a git server, encountering a shallow clone should
be handled correctly.

Diffstat:
Msrc/main.rs | 29++++++++++++++++++++---------
1 file changed, 20 insertions(+), 9 deletions(-)

diff --git a/src/main.rs b/src/main.rs @@ -111,7 +111,7 @@ async fn repo_home(req: Request<()>) -> tide::Result { let repo = repo_from_request(&req.param("repo_name")?)?; if repo.is_empty().unwrap() { // we would not be able to find HEAD - return Ok(RepoEmptyTemplate { repo }.into()) + return Ok(RepoEmptyTemplate { repo }.into()); } let readme_text = { @@ -172,15 +172,26 @@ async fn repo_log(req: Request<()>) -> tide::Result { return Ok(tide::Redirect::temporary(url.to_string()).into()); } - let mut revwalk = repo.revwalk()?; - match req.param("ref") { - Ok(r) => revwalk.push_ref(&format!("refs/heads/{}", r))?, - _ => revwalk.push_head()?, + let commits = if repo.is_shallow() { + tide::log::warn!("repository {:?} is only a shallow clone", repo.path()); + vec![repo.head()?.peel_to_commit().unwrap()] + } else { + let mut revwalk = repo.revwalk()?; + match req.param("ref") { + Ok(r) => revwalk.push_ref(&format!("refs/heads/{}", r))?, + _ => revwalk.push_head()?, + }; + + // show newest commits first + revwalk + .set_sorting(git2::Sort::TIME | git2::Sort::REVERSE) + .unwrap(); + + revwalk + .filter_map(|oid| repo.find_commit(oid.unwrap()).ok().clone()) // TODO error handling + .take(100) // Only get first 100 commits + .collect() }; - let commits = revwalk - .filter_map(|oid| repo.find_commit(oid.unwrap()).ok().clone()) // TODO error handling - .take(100) // Only get first 100 commits - .collect(); let tmpl = RepoLogTemplate { repo: &repo, commits,