Skip to content

Commit

Permalink
Stop prefix-stripping file in Index::add() and always pass relati…
Browse files Browse the repository at this point in the history
…ve paths to it

This fixes being unable to use relative paths when running the server and also handles
some corner cases like having the index root dir named as `config.json`.
  • Loading branch information
twistedfall committed Feb 11, 2025
1 parent 2ac5880 commit e79fc79
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 39 deletions.
38 changes: 22 additions & 16 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,27 +180,16 @@ impl Index {
inner(root, addr)
}

/// Add a file to the index. Note that this operation only stages the
/// file. A commit will still be necessary to make it accessible.
pub fn add(&mut self, file: &Path) -> Result<()> {
let relative_path = if !file.is_relative() {
file.strip_prefix(&self.root).with_context(|| {
format!(
"failed to make {} relative to {}",
file.display(),
self.root.display()
)
})?
} else {
file
};

/// Add a file to the index. The path must be relative to the index root.
/// Note that this operation only stages the file. A commit will still be
/// necessary to make it accessible.
pub fn add(&mut self, relative_file_path: &Path) -> Result<()> {
let mut index = self
.repository
.index()
.context("failed to retrieve git repository index")?;
index
.add_path(relative_path)
.add_path(relative_file_path)
.context("failed to add file to git index")?;
index
.write()
Expand Down Expand Up @@ -497,4 +486,21 @@ mod tests {
let statuses = index.repository.statuses(Some(&mut options)).unwrap();
assert_eq!(statuses.len(), 0);
}

/// Ensure we can use some special names as relative index root.
#[test]
fn index_root_relative_path() {
let base = tempdir().unwrap();
env::set_current_dir(&base).unwrap();
let addr = "127.0.0.1:0".parse().unwrap();

for special_name in ["config.json", "index"] {
let relative_index_root = Path::new(special_name);
create_dir_all(relative_index_root).unwrap();

let index = Index::new(relative_index_root, &addr).unwrap();
// The repository should be clean.
assert_eq!(index.repository.state(), RepositoryState::Clean);
}
}
}
17 changes: 9 additions & 8 deletions src/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,18 +254,19 @@ pub fn publish_crate(mut body: Bytes, index: &mut Index) -> Result<()> {
"crate name contains non-ASCII characters"
);

let crate_meta_dir = index.root().join(crate_path(&crate_name));
let crate_meta_relative_dir = crate_path(&crate_name);
let crate_meta_dir = index.root().join(&crate_meta_relative_dir);
create_dir_all(&crate_meta_dir)
.with_context(|| format!("failed to create directory {}", crate_meta_dir.display()))?;

let crate_length = parse_u32(&mut body)
.context("failed to read crate length")?
.try_into()
.unwrap();
.try_into()?;

// TODO: We may want to sanitize `metadata.vers` somewhat.
let data = read_crate(&mut body, crate_length).context("failed to read crate data")?;
let crate_meta_path = crate_meta_dir.join(&crate_name);
let crate_meta_relative_path = crate_meta_relative_dir.join(&crate_name);
let crate_meta_path = index.root().join(&crate_meta_relative_path);

let mut file = OpenOptions::new()
.write(true)
Expand All @@ -283,8 +284,8 @@ pub fn publish_crate(mut body: Bytes, index: &mut Index) -> Result<()> {
to_writer(&mut file, &entry).context("failed to write crate index meta data")?;
writeln!(file).context("failed to append new line to crate index meta data file")?;

let crate_file_name = crate_file_name(&crate_name, &crate_vers);
let crate_path = index.root().join(crate_file_name);
let crate_relative_path = PathBuf::from(crate_file_name(&crate_name, &crate_vers));
let crate_path = index.root().join(&crate_relative_path);
let mut file = OpenOptions::new()
.write(true)
.create(true)
Expand All @@ -296,14 +297,14 @@ pub fn publish_crate(mut body: Bytes, index: &mut Index) -> Result<()> {
.write(&data)
.with_context(|| format!("failed to write to crate file {}", crate_path.display()))?;

index.add(&crate_meta_path).with_context(|| {
index.add(&crate_meta_relative_path).with_context(|| {
format!(
"failed to add {} to git repository",
crate_meta_path.display()
)
})?;
index
.add(&crate_path)
.add(&crate_relative_path)
.with_context(|| format!("failed to add {} to git repository", crate_path.display()))?;
index
.commit(&format!("Add {} in version {}", crate_name, crate_vers))
Expand Down
62 changes: 47 additions & 15 deletions tests/end-to-end.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#![allow(clippy::ineffective_open_options)]

use std::env;
use std::fs::create_dir;
use std::fs::OpenOptions;
use std::io::Write as _;
Expand Down Expand Up @@ -51,6 +52,11 @@ enum Locator {
}


enum RegistryRootPath {
Absolute,
Relative,
}

/// Append data to a file.
fn append<B>(file: &Path, data: B) -> Result<()>
where
Expand Down Expand Up @@ -161,28 +167,54 @@ where


/// Serve our registry.
fn serve_registry() -> (JoinHandle<()>, PathBuf, SocketAddr) {
let root = tempdir().unwrap();
let path = root.path().to_owned();
fn serve_registry(root_path: RegistryRootPath) -> (JoinHandle<()>, PathBuf, SocketAddr) {
let path = match root_path {
RegistryRootPath::Absolute => tempdir().unwrap().path().to_owned(),
RegistryRootPath::Relative => {
let root = tempdir().unwrap().into_path();
let base_dir = root.parent().unwrap();
env::set_current_dir(base_dir).unwrap();
let index_dir = root.file_name().unwrap();
PathBuf::from(index_dir)
}
};
let addr = "127.0.0.1:0".parse().unwrap();

let (serve, addr) = serve(&path, addr).unwrap();
let serve = move || async {
serve.await;
// We need to reference `root` here to make sure that it is
// moved into the closure so that it outlives us serving our
// registry in there.
drop(root);
};
let handle = spawn(serve());
let handle = spawn(serve);
(handle, path, addr)
}


/// Check that we can publish a crate.
#[tokio::test]
async fn publish() {
let (_handle, _reg_root, addr) = serve_registry();
let (_handle, _reg_root, addr) = serve_registry(RegistryRootPath::Absolute);

let src_root = tempdir().unwrap();
let src_root = src_root.path();
let home = setup_cargo_home(src_root, Locator::Socket(addr)).unwrap();

let my_lib = src_root.join("my-lib");
cargo_init(&home, ["--lib", my_lib.to_str().unwrap()])
.await
.unwrap();

cargo_publish(
&home,
[
"--manifest-path",
my_lib.join("Cargo.toml").to_str().unwrap(),
],
)
.await
.unwrap();
}


#[tokio::test]
async fn publish_relative_index_root() {
let (_handle, _reg_root, addr) = serve_registry(RegistryRootPath::Relative);

let src_root = tempdir().unwrap();
let src_root = src_root.path();
Expand All @@ -208,7 +240,7 @@ async fn publish() {
/// Check that we can publish crates with a renamed dependency.
#[tokio::test]
async fn publish_renamed() {
let (_handle, _reg_root, addr) = serve_registry();
let (_handle, _reg_root, addr) = serve_registry(RegistryRootPath::Absolute);

let src_root = tempdir().unwrap();
let src_root = src_root.path();
Expand Down Expand Up @@ -307,14 +339,14 @@ async fn test_publish_and_consume(registry_locator: Locator) {
/// Check that we can consume a published crate over HTTP.
#[tokio::test]
async fn get_http() {
let (_handle, _, addr) = serve_registry();
let (_handle, _, addr) = serve_registry(RegistryRootPath::Absolute);
test_publish_and_consume(Locator::Socket(addr)).await
}


/// Check that we can consume a published crate through the file system.
#[tokio::test]
async fn get_filesystem() {
let (_handle, root, _) = serve_registry();
let (_handle, root, _) = serve_registry(RegistryRootPath::Absolute);
test_publish_and_consume(Locator::Path(root)).await
}

0 comments on commit e79fc79

Please sign in to comment.