Skip to content

Commit

Permalink
imp(index): support for commit'ishs for diffs
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Dec 25, 2016
1 parent f89651b commit e451067
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
13 changes: 9 additions & 4 deletions src/index.rs
@@ -1,7 +1,7 @@
use std::path::Path;

use git2::build::RepoBuilder;
use git2::{Tree, Repository, Oid, ErrorClass, Error as GitError};
use git2::{ObjectType, Tree, Repository, Oid, ErrorClass, Error as GitError};

static INDEX_GIT_URL: &'static str = "https://github.com/rust-lang/crates.io-index";

Expand All @@ -10,12 +10,12 @@ pub struct Index {
}

pub enum ChangeType {
Added
Added,
}

pub struct Crate {
pub name: String,
pub state: ChangeType
pub state: ChangeType,
}

impl Index {
Expand All @@ -37,7 +37,12 @@ impl Index {
S2: AsRef<str>
{
fn into_tree<S: AsRef<str>>(repo: &Repository, rev: S) -> Result<Tree, GitError> {
repo.revparse_single(rev.as_ref()).and_then(|obj| repo.find_tree(obj.id()))
repo.revparse_single(rev.as_ref()).and_then(|obj| {
repo.find_tree(match obj.kind() {
Some(ObjectType::Commit) => obj.as_commit().expect("valid commit").tree_id(),
_ => /* let it fail later */ obj.id()
})
})
}

let (from, to) = (into_tree(&self.repo, from)?, into_tree(&self.repo, to)?);
Expand Down
3 changes: 2 additions & 1 deletion tests/index.rs
Expand Up @@ -26,7 +26,8 @@ fn traverse_changed_crates() {
assert_eq!(index.traverse_changes("foo", rev_one_added).is_err(), true);
assert_eq!(index.traverse_changes(rev_one_added, "bar").is_err(), true);

let crates: Vec<Crate> = index.traverse_changes(format!("{}~1^{{tree}}", rev_one_added), format!("{}^{{tree}}", rev_one_added))
let crates: Vec<Crate> = index.traverse_changes(format!("{}~1^{{tree}}", rev_one_added),
format!("{}", rev_one_added))
.expect("ids to be valid and diff ok");
assert_eq!(crates.len(), 1);
}

0 comments on commit e451067

Please sign in to comment.