Repositories

grarr

(mirrored on github)

Wim Looman <wim@nemo157.com>
e6f7fb Add start on compare page
Wim Looman committed at 2016-03-11 23:35:27

Added src/handler/compare.rs

@@ -0,0 +1,65 @@
use super::base::*;
use referenced_commit::ReferencedCommit;
#[derive(Clone)]
pub struct Compare;
impl Handler for Compare {
fn handle(&self, req: &mut Request) -> IronResult<Response> {
let context = itry!(req.extensions.get::<RepositoryContext>().ok_or(Error::MissingExtension), status::InternalServerError);
let new_commit = itry!(context.referenced_commit(), status::NotFound);
// TODO: Read which old commit from url
let old_commit = itry!(
context.repository
.head().map_err(Error::from)
.and_then(|h|
h.resolve().map_err(Error::from)
.and_then(|h| h.target().ok_or(Error::from("<.< >.> <.<")))
.and_then(|id| context.repository.find_commit(id).map_err(Error::from))
.map(|commit| ReferencedCommit { commit: commit, reference: Some(h) })),
status::InternalServerError);
let base = itry!(
context.repository
.merge_base(old_commit.commit.id(), new_commit.commit.id())
.and_then(|id| context.repository.find_commit(id)),
status::InternalServerError);
let commits = itry!(
context.repository
.revwalk()
.and_then(|mut walker| walker.push(new_commit.commit.id()).map(|_| walker))
.and_then(|mut walker| walker.hide(base.id()).map(|_| walker))
.and_then(|walker|
walker.map(|id| id.and_then(|id| context.repository.find_commit(id)))
.collect()),
status::InternalServerError);
Html {
render: RepositoryWrapper(&context, &render::Compare {
context: &context,
new: new_commit,
old: old_commit,
base: base,
commits: commits,
}),
etag: Some(EntityTag::weak(versioned_sha1!())),
req: req,
}.into()
}
}
impl Route for Compare {
fn method() -> Method {
Method::Get
}
fn routes() -> Vec<Cow<'static, str>> {
vec![
"/compare/:ref".into(),
// "/compare/:new_ref...:old_ref".into(),
]
}
}

Modified src/handler/mod.rs

@@ -20,6 +20,7 @@ mod about;
mod tree;
mod blob;
mod pages;
mod compare;
pub use self::avatar::Avatars;
pub use self::review::Review;
@@ -31,6 +32,7 @@ pub use self::repositories::Repositories;
pub use self::tree::Tree;
pub use self::blob::Blob;
pub use self::pages::Pages;
pub use self::compare::Compare;
pub use self::register::Register;
pub use self::statics::Static;

Modified src/main.rs

@@ -89,6 +89,7 @@ fn main() {
.register(inject_repository_context(&config.repos.root, handler::Tree))
.register(inject_repository_context(&config.repos.root, handler::Blob))
.register(inject_repository_context(&config.repos.root, handler::Pages))
.register(inject_repository_context(&config.repos.root, handler::Compare))
.register(statics![
prefix: "./static/";
"./static/js/highlight.js",

Modified src/render/commit.rs

@@ -183,9 +183,9 @@ impl<'repo, 'a> RenderOnce for CommitTree<'repo, 'a> {
}
impl<'a> super::repository_wrapper::RepositoryTab for &'a Commit<'a> {
fn tab() -> super::repository_wrapper::Tab { super::repository_wrapper::Tab::Commits }
fn tab() -> Option<super::repository_wrapper::Tab> { Some(super::repository_wrapper::Tab::Commits) }
}
impl<'a, 'b> super::repository_wrapper::RepositoryTab for Commits<'a, 'b> {
fn tab() -> super::repository_wrapper::Tab { super::repository_wrapper::Tab::Commits }
fn tab() -> Option<super::repository_wrapper::Tab> { Some(super::repository_wrapper::Tab::Commits) }
}

Added src/render/compare.rs

@@ -0,0 +1,39 @@
use std::fmt;
use maud::Render;
use git2;
use repository_context::RepositoryContext;
use referenced_commit::ReferencedCommit;
pub struct Compare<'r> {
pub context: &'r RepositoryContext,
pub new: ReferencedCommit<'r>,
pub old: ReferencedCommit<'r>,
pub base: git2::Commit<'r>,
pub commits: Vec<git2::Commit<'r>>,
}
impl<'r> Render for Compare<'r> {
fn render(&self, mut w: &mut fmt::Write) -> fmt::Result {
html!(w, {
div.block div.block-header h2 { "Comparing base " ^super::Reference(&self.old) " to " ^super::Reference(&self.new) }
div.block {
div.block-header h3 { "Commits" }
div.block-details {
@for commit in &self.commits {
^super::CommitStub(&("/".to_owned() + &self.context.path), commit)
}
}
}
div.block {
div.block-header h3 { "File changes" }
div.block-details {
^super::DiffCommits(&self.context.repository, &Some(&self.base), &self.new.commit)
}
}
})
}
}
impl<'a> super::repository_wrapper::RepositoryTab for &'a Compare<'a> {
fn tab() -> Option<super::repository_wrapper::Tab> { None }
}

Modified src/render/diff.rs

@@ -208,7 +208,6 @@ impl DiffDelta {
impl<'a> From<git2::DiffDelta<'a>> for DiffDelta {
fn from(delta: git2::DiffDelta<'a>) -> DiffDelta {
println!("old: {}, new: {}", delta.old_file().id(), delta.new_file().id());
DiffDelta {
status: Delta(delta.status()),
old_file: delta.old_file().path().map(|p| p.to_owned()),

Modified src/render/mod.rs

@@ -23,6 +23,7 @@ mod highlight;
mod settings;
mod about;
mod reference;
mod compare;
pub use self::style::Style;
pub use self::event::{ Event, Events };
@@ -32,16 +33,17 @@ pub use self::comment::{ Comment };
pub use self::ci_status::{ CIStatus };
pub use self::analysis::{ Analysis };
pub use self::avatar::{ Avatar };
pub use self::commit::{ Commit, Commits };
pub use self::commit::{ Commit, CommitStub, Commits };
pub use self::repository::{ Repository, Repositories, RepositoryHeader };
pub use self::repository_wrapper::{ RepositoryWrapper };
pub use self::error::{ Error };
pub use self::tree::{ TreeEntry, Tree, Blob };
pub use self::wrapper::Wrapper;
pub use self::signature::Signature;
pub use self::diff::DiffCommit;
pub use self::diff::{ DiffCommit, DiffCommits };
pub use self::utils::MaybeLink;
pub use self::highlight::HighlightJS;
pub use self::settings::Settings;
pub use self::about::About;
pub use self::reference::Reference;
pub use self::compare::Compare;

Modified src/render/repository.rs

@@ -116,5 +116,5 @@ renderers! {
}
impl<'a> super::repository_wrapper::RepositoryTab for &'a Repository<'a> {
fn tab() -> super::repository_wrapper::Tab { super::repository_wrapper::Tab::Overview }
fn tab() -> Option<super::repository_wrapper::Tab> { Some(super::repository_wrapper::Tab::Overview) }
}

Modified src/render/repository_wrapper.rs

@@ -11,7 +11,7 @@ pub enum Tab {
}
pub trait RepositoryTab {
fn tab() -> Tab;
fn tab() -> Option<Tab>;
}
pub struct RepositoryWrapper<'a, R: RepositoryTab>(pub &'a RepositoryContext, pub R);
@@ -31,12 +31,12 @@ impl<'a, R: RenderOnce + RepositoryTab> RenderOnce for RepositoryWrapper<'a, R>
}
renderers! {
RepositoryWrapperTabs(tab: &'a Tab, path: &'a str, head: &'a str) {
RepositoryWrapperTabs(tab: &'a Option<Tab>, path: &'a str, head: &'a str) {
div.tabs {
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" } }
div class={ "overview" @if *tab == Some(Tab::Overview) { " selected" } } { a href={ "/" ^path } { "Overview" } }
div class={ "files" @if *tab == Some(Tab::Files) { " selected" } } { a href={ "/" ^path "/tree/" ^head } { "Files" } }
div class={ "commits" @if *tab == Some(Tab::Commits) { " selected" } } { a href={ "/" ^path "/commits/" ^head } { "Commits" } }
div class={ "reviews" @if *tab == Some(Tab::Reviews) { " selected" } } { a href={ "/" ^path "/reviews" } { "Reviews" } }
}
}
}

Modified src/render/review.rs

@@ -19,9 +19,9 @@ renderers! {
}
impl<'a> super::repository_wrapper::RepositoryTab for &'a Review<'a> {
fn tab() -> super::repository_wrapper::Tab { super::repository_wrapper::Tab::Reviews }
fn tab() -> Option<super::repository_wrapper::Tab> { Some(super::repository_wrapper::Tab::Reviews) }
}
impl<'a> super::repository_wrapper::RepositoryTab for &'a Reviews<'a> {
fn tab() -> super::repository_wrapper::Tab { super::repository_wrapper::Tab::Reviews }
fn tab() -> Option<super::repository_wrapper::Tab> { Some(super::repository_wrapper::Tab::Reviews) }
}

Modified src/render/tree.rs

@@ -157,13 +157,13 @@ impl PartialOrd for Sorter {
}
impl<'a> super::repository_wrapper::RepositoryTab for &'a TreeEntry<'a> {
fn tab() -> super::repository_wrapper::Tab { super::repository_wrapper::Tab::Files }
fn tab() -> Option<super::repository_wrapper::Tab> { Some(super::repository_wrapper::Tab::Files) }
}
impl<'a> super::repository_wrapper::RepositoryTab for &'a Tree<'a> {
fn tab() -> super::repository_wrapper::Tab { super::repository_wrapper::Tab::Files }
fn tab() -> Option<super::repository_wrapper::Tab> { Some(super::repository_wrapper::Tab::Files) }
}
impl<'a> super::repository_wrapper::RepositoryTab for &'a Blob<'a> {
fn tab() -> super::repository_wrapper::Tab { super::repository_wrapper::Tab::Files }
fn tab() -> Option<super::repository_wrapper::Tab> { Some(super::repository_wrapper::Tab::Files) }
}