checkout: make a function for listing files changed

This commit is contained in:
Graham Christensen 2018-02-03 10:22:24 -05:00
parent cb42c4fa08
commit e13b64f1b8
No known key found for this signature in database
GPG key ID: ACA1C1D120C83D5C
2 changed files with 57 additions and 2 deletions

View file

@ -148,7 +148,6 @@ impl CachedProjectCo {
} }
} }
pub fn commit_messages_from_head(&self, commit: &str) -> Result<Vec<String>, Error> { pub fn commit_messages_from_head(&self, commit: &str) -> Result<Vec<String>, Error> {
let mut lock = self.lock()?; let mut lock = self.lock()?;
@ -175,6 +174,33 @@ impl CachedProjectCo {
)); ));
} }
} }
pub fn files_changed_from_head(&self, commit: &str) -> Result<Vec<String>, Error> {
let mut lock = self.lock()?;
let result = Command::new("git")
.arg("diff")
.arg("--name-only")
.arg(format!("HEAD..{}", commit))
.current_dir(self.clone_to())
.output()?;
lock.unlock();
if result.status.success() {
return Ok(
String::from_utf8_lossy(&result.stdout)
.lines()
.map(|l| l.to_owned())
.collect(),
);
} else {
return Err(Error::new(
ErrorKind::Other,
String::from_utf8_lossy(&result.stderr).to_lowercase(),
));
}
}
} }
impl clone::GitClonable for CachedProjectCo { impl clone::GitClonable for CachedProjectCo {
@ -264,7 +290,9 @@ mod tests {
let working_co = project let working_co = project
.clone_for("testing-commit-msgs".to_owned(), "123".to_owned()) .clone_for("testing-commit-msgs".to_owned(), "123".to_owned())
.expect("clone should work"); .expect("clone should work");
working_co.checkout_origin_ref(OsStr::new("master")); working_co
.checkout_origin_ref(OsStr::new("master"))
.unwrap();
let expect: Vec<String> = vec!["check out this cool PR".to_owned()]; let expect: Vec<String> = vec!["check out this cool PR".to_owned()];
@ -275,4 +303,31 @@ mod tests {
expect expect
); );
} }
#[test]
pub fn test_files_changed_list() {
let workingdir = TestScratch::new_dir("test-test-files-changed-list");
let bare = TestScratch::new_dir("bare-files-changed");
let mk_co = TestScratch::new_dir("mk-files-changed");
let hash = make_pr_repo(&bare.path(), &mk_co.path());
let cloner = cached_cloner(&workingdir.path());
let project = cloner.project("commit-files-changed-list".to_owned(), bare.string());
let working_co = project
.clone_for("testing-files-changed".to_owned(), "123".to_owned())
.expect("clone should work");
working_co
.checkout_origin_ref(OsStr::new("master"))
.unwrap();
let expect: Vec<String> = vec!["default.nix".to_owned(), "hi another file".to_owned()];
assert_eq!(
working_co.files_changed_from_head(&hash).expect(
"fetching files changed should work",
),
expect
);
}
} }