Do not try to format removed files

This commit is contained in:
clubby789 2024-03-05 15:07:41 +00:00
parent 41d97c8a5d
commit 39887d3ccd
2 changed files with 13 additions and 6 deletions

View File

@ -81,7 +81,7 @@ fn update_rustfmt_version(build: &Builder<'_>) {
} }
/// Returns the Rust files modified between the `merge-base` of HEAD and /// Returns the Rust files modified between the `merge-base` of HEAD and
/// rust-lang/master and what is now on the disk. /// rust-lang/master and what is now on the disk. Does not include removed files.
/// ///
/// Returns `None` if all files should be formatted. /// Returns `None` if all files should be formatted.
fn get_modified_rs_files(build: &Builder<'_>) -> Result<Option<Vec<String>>, String> { fn get_modified_rs_files(build: &Builder<'_>) -> Result<Option<Vec<String>>, String> {

View File

@ -113,6 +113,7 @@ pub fn get_git_merge_base(
/// Returns the files that have been modified in the current branch compared to the master branch. /// Returns the files that have been modified in the current branch compared to the master branch.
/// The `extensions` parameter can be used to filter the files by their extension. /// The `extensions` parameter can be used to filter the files by their extension.
/// Does not include removed files.
/// If `extensions` is empty, all files will be returned. /// If `extensions` is empty, all files will be returned.
pub fn get_git_modified_files( pub fn get_git_modified_files(
config: &GitConfig<'_>, config: &GitConfig<'_>,
@ -125,13 +126,19 @@ pub fn get_git_modified_files(
if let Some(git_dir) = git_dir { if let Some(git_dir) = git_dir {
git.current_dir(git_dir); git.current_dir(git_dir);
} }
let files = output_result(git.args(["diff-index", "--name-only", merge_base.trim()]))? let files = output_result(git.args(["diff-index", "--name-status", merge_base.trim()]))?
.lines() .lines()
.map(|s| s.trim().to_owned()) .filter_map(|f| {
.filter(|f| { let (status, name) = f.trim().split_once(char::is_whitespace).unwrap();
Path::new(f).extension().map_or(false, |ext| { if status == "D" {
None
} else if Path::new(name).extension().map_or(false, |ext| {
extensions.is_empty() || extensions.contains(&ext.to_str().unwrap()) extensions.is_empty() || extensions.contains(&ext.to_str().unwrap())
}) }) {
Some(name.to_owned())
} else {
None
}
}) })
.collect(); .collect();
Ok(Some(files)) Ok(Some(files))