diff --git a/ofborg/src/checkout.rs b/ofborg/src/checkout.rs index b670c78..877339c 100644 --- a/ofborg/src/checkout.rs +++ b/ofborg/src/checkout.rs @@ -148,7 +148,6 @@ impl CachedProjectCo { } } - pub fn commit_messages_from_head(&self, commit: &str) -> Result, Error> { let mut lock = self.lock()?; @@ -175,6 +174,33 @@ impl CachedProjectCo { )); } } + + pub fn files_changed_from_head(&self, commit: &str) -> Result, 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 { @@ -264,7 +290,9 @@ mod tests { let working_co = project .clone_for("testing-commit-msgs".to_owned(), "123".to_owned()) .expect("clone should work"); - working_co.checkout_origin_ref(OsStr::new("master")); + working_co + .checkout_origin_ref(OsStr::new("master")) + .unwrap(); let expect: Vec = vec!["check out this cool PR".to_owned()]; @@ -275,4 +303,31 @@ mod tests { 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 = 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 + ); + } } diff --git a/ofborg/test-srcs/build-pr/hi another file b/ofborg/test-srcs/build-pr/hi another file new file mode 100644 index 0000000..e69de29