Skip to content
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
45 changes: 44 additions & 1 deletion tool/bundle/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::{HashMap, HashSet};
use std::fs;
use std::path::Path;
use std::path::{Path, PathBuf};
use std::process::Command;

use anyhow::{Context, Result};
Expand Down Expand Up @@ -112,6 +112,7 @@ fn download_remote_repository() -> Result<std::path::PathBuf> {
}

struct CrateInfo {
source_path: PathBuf,
content: String,
dependencies: Vec<String>,
external_dependencies: Vec<String>,
Expand All @@ -130,6 +131,10 @@ fn list_available_crates(crates: &HashMap<String, CrateInfo>) -> String {
}

fn bundle_crate(crate_name: &str, workspace_path: &Path) -> Result<String> {
let repository_root = PathBuf::from(git_rev_parse(workspace_path, "--show-toplevel")?)
.canonicalize()
.context("Failed to resolve repository root")?;
let sha = git_rev_parse(workspace_path, "HEAD^{commit}")?;
Comment on lines +134 to +137

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Keep bundling available for workspaces without Git metadata

When --workspace points to an extracted source archive or a copied workspace without .git, these unconditional Git lookups now abort bundling before any source is processed. Previously, such workspaces worked because bundling only required libs/*/Cargo.toml and the source files. Adding a provenance comment should not prevent generating otherwise valid bundled code; make the URL optional when repository metadata is unavailable.

Useful? React with 👍 / 👎.

let libs_path = workspace_path.join("libs");

let mut crates = HashMap::new();
Expand All @@ -152,6 +157,10 @@ fn bundle_crate(crate_name: &str, workspace_path: &Path) -> Result<String> {
let mut bundled_code = String::new();
bundled_code.push_str("// Bundled\n");

bundled_code.push_str(&format!(
"// {}\n",
source_url(&target_crate_info.source_path, &repository_root, &sha)?
));
bundled_code.push_str("#[rustfmt::skip]\n");
bundled_code.push_str("#[allow(unused)]\n");
bundled_code.push_str(&format!("mod {} {{\n", crate_name));
Expand Down Expand Up @@ -190,6 +199,37 @@ fn bundle_crate(crate_name: &str, workspace_path: &Path) -> Result<String> {
Ok(bundled_code)
}

fn git_rev_parse(workspace_path: &Path, revision: &str) -> Result<String> {
let output = Command::new("git")
.current_dir(workspace_path)
.args(["rev-parse", revision])
.output()
.context("Failed to run git rev-parse")?;
if !output.status.success() {
anyhow::bail!(
"Git rev-parse {} failed: {}",
revision,
String::from_utf8_lossy(&output.stderr)
);
}
Ok(String::from_utf8(output.stdout)?.trim().to_string())
}

fn source_url(source_path: &Path, repository_root: &Path, sha: &str) -> Result<String> {
let source_path = source_path.canonicalize()?;
let relative_path = source_path
.strip_prefix(repository_root)
.context("Source file is outside the repository")?;
let relative_path = relative_path
.to_str()
.context("Source path is not valid UTF-8")?
.replace(std::path::MAIN_SEPARATOR, "/");
Ok(format!(
"https://github.com/ia7ck/rust-competitive-programming/blob/{}/{}",
sha, relative_path
))
}

fn collect_crates(libs_path: &Path, crates: &mut HashMap<String, CrateInfo>) -> Result<()> {
if !libs_path.exists() {
anyhow::bail!(
Expand Down Expand Up @@ -240,6 +280,7 @@ fn collect_crates(libs_path: &Path, crates: &mut HashMap<String, CrateInfo>) ->
crates.insert(
package.name.clone(),
CrateInfo {
source_path: lib_path,
content: lib_content,
dependencies,
external_dependencies,
Expand Down Expand Up @@ -498,6 +539,7 @@ pub fn world() {}"#;
crates.insert(
"crate_a".to_string(),
CrateInfo {
source_path: PathBuf::from("libs/crate_a/src/lib.rs"),
content: "pub fn a() {}".to_string(),
dependencies: vec![],
external_dependencies: vec![],
Expand All @@ -506,6 +548,7 @@ pub fn world() {}"#;
crates.insert(
"crate_b".to_string(),
CrateInfo {
source_path: PathBuf::from("libs/crate_b/src/lib.rs"),
content: "pub fn b() {}".to_string(),
dependencies: vec![],
external_dependencies: vec![],
Expand Down
Loading