Repository: paritytech/substrate
File: scripts/ci/node-template-release/src/main.rs (Rust)

Code before:
		.unwrap_or_else(|e| panic!("Failed to read `{}`: {}", file.display(), e))
		.parse()
		.unwrap_or_else(|e| panic!("Failed to parse `{}`: {}", file.display(), e))
}

/// Write the given `Cargo.toml` to the given path.
fn write_cargo_toml(path: &Path, cargo_toml: CargoToml) {
	fs::write(path, cargo_toml.to_string())
		.unwrap_or_else(|e| panic!("Failed to write `{}`: {}", path.display(), e));
}

/// Gets the latest commit id of the repository given by `path`.
fn get_git_commit_id(mut path: &Path) -> String {
	while !path.join(".git").exists() {
		if let Some(parent) = path.parent() {
			path = parent;
		} else {
			panic!("Node template ({}) should be in a git repository.", path.display());
		}
	}

	let git = path.join(".git");
	let head = git.join("HEAD");
	let head_contents = std::fs::read_to_string(head).expect("Repository should have a HEAD");
	let branch = head_contents.strip_prefix("ref: ").expect(".git/HEAD to start 'ref: '");
	std::fs::read_to_string(git.join(branch)).expect("Head references a commit")
}

/// Rewrites git dependencies:
/// - inserts `workspace = true`;
/// - removes `path`;
/// - removes `version`;
/// - removes `default-features`
/// - and returns the dependencies that were rewritten.
fn update_git_dependencies<F: Copy + Fn(&str) -> bool>(
	cargo_toml: &mut CargoToml,
	path_filter: F,
) -> Dependencies {
	let process_dep = |dep: (toml_edit::KeyMut, &mut Item)| -> Option<Dependency> {
		let (key, value) = dep;
		value
			.as_table_like_mut()
			.filter(|dep| {
				dep.get("path").and_then(|path| path.as_str()).map(path_filter).unwrap_or(false)
			})
			.map(|dep| {
				dep.insert("workspace", toml_edit::value(true));
				dep.remove("path");

				Dependency {
					name: key.get().to_string(),

Review comment (suggestion):
```suggestion
	let head_contents = fs::read_to_string(head).expect("Repository should have a HEAD");
	let branch = head_contents.strip_prefix("ref: ").expect(".git/HEAD to start 'ref: '");
	fs::read_to_string(git.join(branch)).expect("Head references a commit")
```

Code after:
		.unwrap_or_else(|e| panic!("Failed to read `{}`: {}", file.display(), e))
		.parse()
		.unwrap_or_else(|e| panic!("Failed to parse `{}`: {}", file.display(), e))
}

/// Write the given `Cargo.toml` to the given path.
fn write_cargo_toml(path: &Path, cargo_toml: CargoToml) {
	fs::write(path, cargo_toml.to_string())
		.unwrap_or_else(|e| panic!("Failed to write `{}`: {}", path.display(), e));
}

/// Gets the latest commit id of the repository given by `path`.
fn get_git_commit_id(path: &Path) -> String {
	let mut dir = path;
	while !dir.join(".git").exists() {
		dir = dir
			.parent()
			.expect(&format!("Node template ({}) should be in a git repository.", path.display()));
	}

	let git = dir.join(".git");
	let head = git.join("HEAD");
	let head_contents = fs::read_to_string(head).expect("Repository should have a HEAD");
	let branch = head_contents.strip_prefix("ref: ").expect(".git/HEAD to start 'ref: '").trim();
	let mut commit = fs::read_to_string(git.join(branch)).expect("Head references a commit");
	commit.truncate(commit.trim_end().len());
	commit
}

/// Rewrites git dependencies:
/// - inserts `workspace = true`;
/// - removes `path`;
/// - removes `version`;
/// - removes `default-features`
/// - and returns the dependencies that were rewritten.
fn update_git_dependencies<F: Copy + Fn(&str) -> bool>(
	cargo_toml: &mut CargoToml,
	path_filter: F,
) -> Dependencies {
	let process_dep = |dep: (toml_edit::KeyMut, &mut Item)| -> Option<Dependency> {
		let (key, value) = dep;
		value
			.as_table_like_mut()
			.filter(|dep| {
				dep.get("path").and_then(|path| path.as_str()).map(path_filter).unwrap_or(false)
			})
			.map(|dep| {
				dep.insert("workspace", toml_edit::value(true));
				dep.remove("path");

				Dependency {