Skip to content

Commit cacad7d

Browse files
authored
Rollup merge of rust-lang#138706 - Kobzol:bootstrap-git-refactor-1, r=onur-ozkan
Improve bootstrap git modified path handling Drive-by improvements extracted out of rust-lang#138591. r? `@onur-ozkan`
2 parents 3c03c99 + b24dc75 commit cacad7d

File tree

5 files changed

+58
-56
lines changed

5 files changed

+58
-56
lines changed

src/bootstrap/src/core/build_steps/format.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ fn get_modified_rs_files(build: &Builder<'_>) -> Result<Option<Vec<String>>, Str
9494
return Ok(None);
9595
}
9696

97-
get_git_modified_files(&build.config.git_config(), Some(&build.config.src), &["rs"])
97+
get_git_modified_files(&build.config.git_config(), Some(&build.config.src), &["rs"]).map(Some)
9898
}
9999

100100
#[derive(serde_derive::Deserialize)]

src/bootstrap/src/core/config/config.rs

+49-45
Original file line numberDiff line numberDiff line change
@@ -1425,52 +1425,56 @@ impl Config {
14251425

14261426
// Infer the rest of the configuration.
14271427

1428-
// Infer the source directory. This is non-trivial because we want to support a downloaded bootstrap binary,
1429-
// running on a completely different machine from where it was compiled.
1430-
let mut cmd = helpers::git(None);
1431-
// NOTE: we cannot support running from outside the repository because the only other path we have available
1432-
// is set at compile time, which can be wrong if bootstrap was downloaded rather than compiled locally.
1433-
// We still support running outside the repository if we find we aren't in a git directory.
1434-
1435-
// NOTE: We get a relative path from git to work around an issue on MSYS/mingw. If we used an absolute path,
1436-
// and end up using MSYS's git rather than git-for-windows, we would get a unix-y MSYS path. But as bootstrap
1437-
// has already been (kinda-cross-)compiled to Windows land, we require a normal Windows path.
1438-
cmd.arg("rev-parse").arg("--show-cdup");
1439-
// Discard stderr because we expect this to fail when building from a tarball.
1440-
let output = cmd
1441-
.as_command_mut()
1442-
.stderr(std::process::Stdio::null())
1443-
.output()
1444-
.ok()
1445-
.and_then(|output| if output.status.success() { Some(output) } else { None });
1446-
if let Some(output) = output {
1447-
let git_root_relative = String::from_utf8(output.stdout).unwrap();
1448-
// We need to canonicalize this path to make sure it uses backslashes instead of forward slashes,
1449-
// and to resolve any relative components.
1450-
let git_root = env::current_dir()
1451-
.unwrap()
1452-
.join(PathBuf::from(git_root_relative.trim()))
1453-
.canonicalize()
1454-
.unwrap();
1455-
let s = git_root.to_str().unwrap();
1456-
1457-
// Bootstrap is quite bad at handling /? in front of paths
1458-
let git_root = match s.strip_prefix("\\\\?\\") {
1459-
Some(p) => PathBuf::from(p),
1460-
None => git_root,
1461-
};
1462-
// If this doesn't have at least `stage0`, we guessed wrong. This can happen when,
1463-
// for example, the build directory is inside of another unrelated git directory.
1464-
// In that case keep the original `CARGO_MANIFEST_DIR` handling.
1465-
//
1466-
// NOTE: this implies that downloadable bootstrap isn't supported when the build directory is outside
1467-
// the source directory. We could fix that by setting a variable from all three of python, ./x, and x.ps1.
1468-
if git_root.join("src").join("stage0").exists() {
1469-
config.src = git_root;
1470-
}
1428+
if let Some(src) = flags.src {
1429+
config.src = src
14711430
} else {
1472-
// We're building from a tarball, not git sources.
1473-
// We don't support pre-downloaded bootstrap in this case.
1431+
// Infer the source directory. This is non-trivial because we want to support a downloaded bootstrap binary,
1432+
// running on a completely different machine from where it was compiled.
1433+
let mut cmd = helpers::git(None);
1434+
// NOTE: we cannot support running from outside the repository because the only other path we have available
1435+
// is set at compile time, which can be wrong if bootstrap was downloaded rather than compiled locally.
1436+
// We still support running outside the repository if we find we aren't in a git directory.
1437+
1438+
// NOTE: We get a relative path from git to work around an issue on MSYS/mingw. If we used an absolute path,
1439+
// and end up using MSYS's git rather than git-for-windows, we would get a unix-y MSYS path. But as bootstrap
1440+
// has already been (kinda-cross-)compiled to Windows land, we require a normal Windows path.
1441+
cmd.arg("rev-parse").arg("--show-cdup");
1442+
// Discard stderr because we expect this to fail when building from a tarball.
1443+
let output = cmd
1444+
.as_command_mut()
1445+
.stderr(std::process::Stdio::null())
1446+
.output()
1447+
.ok()
1448+
.and_then(|output| if output.status.success() { Some(output) } else { None });
1449+
if let Some(output) = output {
1450+
let git_root_relative = String::from_utf8(output.stdout).unwrap();
1451+
// We need to canonicalize this path to make sure it uses backslashes instead of forward slashes,
1452+
// and to resolve any relative components.
1453+
let git_root = env::current_dir()
1454+
.unwrap()
1455+
.join(PathBuf::from(git_root_relative.trim()))
1456+
.canonicalize()
1457+
.unwrap();
1458+
let s = git_root.to_str().unwrap();
1459+
1460+
// Bootstrap is quite bad at handling /? in front of paths
1461+
let git_root = match s.strip_prefix("\\\\?\\") {
1462+
Some(p) => PathBuf::from(p),
1463+
None => git_root,
1464+
};
1465+
// If this doesn't have at least `stage0`, we guessed wrong. This can happen when,
1466+
// for example, the build directory is inside of another unrelated git directory.
1467+
// In that case keep the original `CARGO_MANIFEST_DIR` handling.
1468+
//
1469+
// NOTE: this implies that downloadable bootstrap isn't supported when the build directory is outside
1470+
// the source directory. We could fix that by setting a variable from all three of python, ./x, and x.ps1.
1471+
if git_root.join("src").join("stage0").exists() {
1472+
config.src = git_root;
1473+
}
1474+
} else {
1475+
// We're building from a tarball, not git sources.
1476+
// We don't support pre-downloaded bootstrap in this case.
1477+
}
14741478
}
14751479

14761480
if cfg!(test) {

src/build_helper/src/git.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ pub fn get_git_modified_files(
173173
config: &GitConfig<'_>,
174174
git_dir: Option<&Path>,
175175
extensions: &[&str],
176-
) -> Result<Option<Vec<String>>, String> {
176+
) -> Result<Vec<String>, String> {
177177
let merge_base = get_closest_merge_commit(git_dir, config, &[])?;
178178

179179
let mut git = Command::new("git");
@@ -186,7 +186,10 @@ pub fn get_git_modified_files(
186186
let (status, name) = f.trim().split_once(char::is_whitespace).unwrap();
187187
if status == "D" {
188188
None
189-
} else if Path::new(name).extension().map_or(false, |ext| {
189+
} else if Path::new(name).extension().map_or(extensions.is_empty(), |ext| {
190+
// If there is no extension, we allow the path if `extensions` is empty
191+
// If there is an extension, we allow it if `extension` is empty or it contains the
192+
// extension.
190193
extensions.is_empty() || extensions.contains(&ext.to_str().unwrap())
191194
}) {
192195
Some(name.to_owned())
@@ -195,7 +198,7 @@ pub fn get_git_modified_files(
195198
}
196199
})
197200
.collect();
198-
Ok(Some(files))
201+
Ok(files)
199202
}
200203

201204
/// Returns the files that haven't been added to git yet.

src/tools/compiletest/src/lib.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -747,8 +747,7 @@ fn modified_tests(config: &Config, dir: &Path) -> Result<Vec<PathBuf>, String> {
747747
}
748748

749749
let files =
750-
get_git_modified_files(&config.git_config(), Some(dir), &vec!["rs", "stderr", "fixed"])?
751-
.unwrap_or(vec![]);
750+
get_git_modified_files(&config.git_config(), Some(dir), &vec!["rs", "stderr", "fixed"])?;
752751
// Add new test cases to the list, it will be convenient in daily development.
753752
let untracked_files = get_git_untracked_files(&config.git_config(), None)?.unwrap_or(vec![]);
754753

src/tools/suggest-tests/src/main.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,7 @@ fn main() -> ExitCode {
1414
&Vec::new(),
1515
);
1616
let modified_files = match modified_files {
17-
Ok(Some(files)) => files,
18-
Ok(None) => {
19-
eprintln!("git error");
20-
return ExitCode::FAILURE;
21-
}
17+
Ok(files) => files,
2218
Err(err) => {
2319
eprintln!("Could not get modified files from git: \"{err}\"");
2420
return ExitCode::FAILURE;

0 commit comments

Comments
 (0)