Repositories

grarr

(mirrored on github)

Modified src/handler/commit.rs

@@ -7,7 +7,7 @@ impl Handler for Commit {
fn handle(&self, req: &mut Request) -> IronResult<Response> {
let context = itry!(req.extensions.get::<RepositoryContext>().ok_or(Error::MissingExtension), status::InternalServerError);
let commit = itry!(context.commit(), status::NotFound);
let root = "/".to_owned() + &context.requested_path.to_string_lossy();
let root = format!("/{}", context.path);
Html {
render: RepositoryWrapper(&context, &render::Commit(&root, &context.repository, &commit)),
etag: Some(EntityTag::weak(versioned_sha1!())),

Modified src/handler/commits.rs

@@ -24,7 +24,7 @@ impl Handler for Commits {
};
let commits = itry!(CommitTree::new(&context.repository, &initial_commit, 50), status::InternalServerError);
Html {
render: RepositoryWrapper(&context, render::Commits(&("/".to_owned() + context.requested_path.to_str().unwrap()), &referenced_commit, commits)),
render: RepositoryWrapper(&context, render::Commits(&format!("/{}", context.path), &referenced_commit, commits)),
etag: Some(EntityTag::weak(versioned_sha1!(referenced_commit.commit.id().as_bytes()))),
req: req,
}.into()

Modified src/handler/review.rs

@@ -13,7 +13,7 @@ impl Handler for Review {
let commit = itry!(router.find("ref").ok_or(Error::MissingPathComponent), status::InternalServerError);
let id = itry!(Oid::from_str(commit), status::BadRequest);
let review = itry!(context.repository.review_for(id), status::NotFound);
let root = "/".to_owned() + &context.requested_path.to_string_lossy();
let root = format!("/{}", context.path);
Html {
render: RepositoryWrapper(&context, &render::Review(&root, &review)),
etag: None,

Modified src/handler/reviews.rs

@@ -10,7 +10,7 @@ impl Handler for Reviews {
let mut reviews: Vec<_> = context.repository.all_reviews().and_then(|revs| revs.collect()).ok().unwrap_or_default();
reviews.sort_by(|a, b| a.request().timestamp().cmp(&b.request().timestamp()));
reviews.reverse();
let root = "/".to_owned() + &context.requested_path.to_string_lossy();
let root = format!("/{}", context.path);
Html {
render: RepositoryWrapper(&context, &render::Reviews(&root, &reviews)),
etag: None,

Modified src/render/repository_wrapper.rs

@@ -20,11 +20,10 @@ impl<'a, R: RenderOnce + RepositoryTab> RenderOnce for RepositoryWrapper<'a, R>
fn render_once(self, mut w: &mut fmt::Write) -> fmt::Result {
let tab = R::tab();
let RepositoryWrapper(context, content) = self;
let path = context.requested_path.to_string_lossy().into_owned();
html!(w, {
div.block {
^super::RepositoryHeader(&path, &context.repository)
^RepositoryWrapperTabs(tab, path, context.repository.head().unwrap().shorthand().unwrap().to_owned())
^super::RepositoryHeader(&context.path, &context.repository)
^RepositoryWrapperTabs(&tab, &context.path, context.repository.head().unwrap().shorthand().unwrap())
}
^content
})
@@ -32,12 +31,12 @@ impl<'a, R: RenderOnce + RepositoryTab> RenderOnce for RepositoryWrapper<'a, R>
}
renderers! {
RepositoryWrapperTabs(tab: Tab, requested_path: String, head: String) {
RepositoryWrapperTabs(tab: &'a Tab, path: &'a str, head: &'a str) {
div.tabs {
div class={ "overview" @if tab == Tab::Overview { " selected" } } { a href={ "/" ^requested_path } { "Overview" } }
div class={ "files" @if tab == Tab::Files { " selected" } } { a href={ "/" ^requested_path "/tree/" ^head } { "Files" } }
div class={ "commits" @if tab == Tab::Commits { " selected" } } { a href={ "/" ^requested_path "/commits/" ^head } { "Commits" } }
div class={ "reviews" @if tab == Tab::Reviews { " selected" } } { a href={ "/" ^requested_path "/reviews" } { "Reviews" } }
div class={ "overview" @if *tab == Tab::Overview { " selected" } } { a href={ "/" ^path } { "Overview" } }
div class={ "files" @if *tab == Tab::Files { " selected" } } { a href={ "/" ^path "/tree/" ^head } { "Files" } }
div class={ "commits" @if *tab == Tab::Commits { " selected" } } { a href={ "/" ^path "/commits/" ^head } { "Commits" } }
div class={ "reviews" @if *tab == Tab::Reviews { " selected" } } { a href={ "/" ^path "/reviews" } { "Reviews" } }
}
}
}

Modified src/repository_context.rs

@@ -16,7 +16,7 @@ use error::Error;
use referenced_commit::ReferencedCommit;
pub struct RepositoryContext {
pub requested_path: PathBuf,
pub path: String,
pub repository: git2::Repository,
reference: Option<String>,
}
@@ -65,26 +65,25 @@ pub struct RepositoryContextHandler<H: Handler> {
impl<H: Handler> Handler for RepositoryContextHandler<H> {
fn handle(&self, req: &mut Request) -> IronResult<Response> {
let (requested_path, reference) = {
let (path, reference) = {
let router = itry!(req.extensions.get::<Router>().ok_or(Error::MissingExtension), status::InternalServerError);
(router.find("repo").map(ToOwned::to_owned), router.find("ref").map(ToOwned::to_owned))
};
let requested_path = PathBuf::from(itry!(requested_path.ok_or(Error::MissingPathComponent), status::InternalServerError));
let full_path = self.canonical_root.join(&requested_path);
let path = itry!(path.ok_or(Error::MissingPathComponent), status::InternalServerError);
let full_path = self.canonical_root.join(&path);
let full_canonical_path = itry!(fs::canonicalize(&full_path), status::NotFound);
if full_path == full_canonical_path {
let repository = itry!(git2::Repository::open(full_canonical_path), status::NotFound);
req.extensions.insert::<RepositoryContext>(RepositoryContext {
requested_path: requested_path,
path: path,
repository: repository,
reference: reference,
});
self.handler.handle(req)
} else {
let canonical_path = itry!(full_canonical_path.strip_prefix(&self.canonical_root), status::InternalServerError).to_owned();
let old_path = requested_path.to_string_lossy();
let new_path = canonical_path.to_string_lossy();
let new_url = Url::parse(&*req.url.to_string().replace(&*old_path, &*new_path)).unwrap();
let new_url = Url::parse(&*req.url.to_string().replace(&*path, &*new_path)).unwrap();
Ok(Response::with((status::TemporaryRedirect, Redirect(new_url))))
}
}

Modified src/tree_entry.rs

@@ -1,4 +1,3 @@
use std::borrow::Cow;
use iron::{ status, IronResult };
use std::path::Path;
use repository_context::RepositoryContext;
@@ -7,7 +6,7 @@ use referenced_commit::ReferencedCommit;
pub struct TreeEntryContext<'a> {
pub entry: git2::Object<'a>,
pub repo_path: Cow<'a, str>,
pub repo_path: &'a str,
pub entry_path: String,
pub reff: String,
pub commit: ReferencedCommit<'a>,
@@ -48,7 +47,7 @@ pub fn get_tree_entry<'a>(context: &'a RepositoryContext, path: &'a str) -> Iron
let reff = referenced_commit.reference.as_ref().and_then(|r| r.shorthand()).unwrap_or(&*idstr).to_owned();
Ok(TreeEntryContext {
entry: entry,
repo_path: context.requested_path.to_string_lossy(),
repo_path: &context.path,
entry_path: "/".to_owned() + path,
reff: reff,
commit: referenced_commit,