Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

xtask: fix copyright house rule #855

Merged
merged 2 commits into from
Feb 14, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions xtask/src/tasks/fmt/house_rules/copyright.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use anyhow::anyhow;
use fs_err::File;
use std::io::BufRead;
use std::io::BufReader;
use std::io::Read;
use std::io::Write;
use std::path::Path;

Expand All @@ -19,7 +20,7 @@ pub fn check_copyright(path: &Path, fix: bool) -> anyhow::Result<()> {

if !matches!(
ext,
"rs" | "c" | "proto" | "toml" | "ts" | "js" | "py" | "ps1" | "config"
"rs" | "c" | "proto" | "toml" | "ts" | "js" | "py" | "ps1"
) {
return Ok(());
}
Expand Down Expand Up @@ -57,18 +58,20 @@ pub fn check_copyright(path: &Path, fix: bool) -> anyhow::Result<()> {
let mut missing_banner = !first_content_line.contains(HEADER_MIT_FIRST)
|| !second_content_line.contains(HEADER_MIT_SECOND);
let mut missing_blank_line = !third_content_line.is_empty();
let mut header_lines = 2;

// TEMP: until we have more robust infrastructure for distinct
// microsoft-internal checks, include this "escape hatch" for preserving
// non-MIT licensed files when running `xtask fmt` in the msft internal
// repo. This uses a job-specific env var, instead of being properly plumbed
// through via `clap`, to make it easier to remove in the future.
let is_msft_internal = std::env::var("XTASK_FMT_COPYRIGHT_ALLOW_MISSING_MIT").is_ok();
if is_msft_internal {
if is_msft_internal && missing_banner {
// support both new and existing copyright banner styles
missing_banner =
!(first_content_line.contains("Copyright") && first_content_line.contains("Microsoft"));
missing_blank_line = !second_content_line.is_empty();
header_lines = 1;
}

if fix {
Expand All @@ -87,8 +90,9 @@ pub fn check_copyright(path: &Path, fix: bool) -> anyhow::Result<()> {
let mut f = BufReader::new(File::open(path)?);
let mut f_fixed = File::create(path_fix)?;

if let Some(script_interpreter_line) = script_interpreter_line {
if let Some(script_interpreter_line) = &script_interpreter_line {
writeln!(f_fixed, "{script_interpreter_line}")?;
f.read_line(&mut String::new())?;
}
if let Some(blank_after_script_interpreter_line) = blank_after_script_interpreter_line {
if !blank_after_script_interpreter_line {
Expand All @@ -103,13 +107,21 @@ pub fn check_copyright(path: &Path, fix: bool) -> anyhow::Result<()> {
_ => unreachable!(),
};

// Preserve the UTF-8 BOM if it exists.
if script_interpreter_line.is_none() && first_content_line.starts_with('\u{feff}') {
write!(f_fixed, "\u{feff}")?;
// Skip the BOM.
f.read_exact(&mut [0; 3])?;
}

writeln!(f_fixed, "{} {}", prefix, HEADER_MIT_FIRST)?;
writeln!(f_fixed, "{} {}", prefix, HEADER_MIT_SECOND)?;
if !is_msft_internal {
writeln!(f_fixed, "{} {}", prefix, HEADER_MIT_SECOND)?;
}

writeln!(f_fixed)?; // also add that missing blank line
} else if missing_blank_line {
// copy the valid header from the current file
let header_lines = if is_msft_internal { 1 } else { 2 };
for _ in 0..header_lines {
let mut s = String::new();
f.read_line(&mut s)?;
Expand Down