Skip to content

Commit fa861fe

Browse files
committed
Auto merge of #2469 - jtgeibel:fix-dev-git-index-endpoint, r=JohnTitor
Fix serving `/git/index` during local development The "index checkout" directory is used to initialize the bare index repo but is not kept in sync after that. It seems unnecessary to require all environments to define the `GIT_REPO_CHECKOUT` variable, so it is dropped as well. During local development, the `tmp/index-bare` repo is automatically served at `/git/index` by the HTTP backend. r? @JohnTitor
2 parents 08e9646 + 10a1eb8 commit fa861fe

File tree

10 files changed

+18
-30
lines changed

10 files changed

+18
-30
lines changed

.env.sample

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,10 @@ export TEST_DATABASE_URL=
2525
# not needed if the S3 bucket is in US standard
2626
# export S3_REGION=
2727

28-
# Remote and local locations of the registry index. You can leave these to
29-
# use a `tmp` subdirectory of the working directory, which is what the
30-
# script in `./script/init-local-index.sh` will set up for you.
28+
# Upstream location of the registry index. Background jobs will push to
29+
# this URL. The default points to a local index for development.
30+
# Run `./script/init-local-index.sh` to initialize this repo.
3131
export GIT_REPO_URL=file://$PWD/tmp/index-bare
32-
export GIT_REPO_CHECKOUT=./tmp/index-co
3332

3433
# Credentials for talking to github. You can leave these blank if you're
3534
# not logging into your crates.io instance.

app.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@
4242
"generator": "secret"
4343
},
4444
"HEROKU": "1",
45-
"MIRROR": "1",
46-
"GIT_REPO_CHECKOUT": "./tmp/index-co"
45+
"MIRROR": "1"
4746
},
4847
"formation": {
4948
"web": {

docker-compose.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ services:
1818
DATABASE_URL: postgres://postgres:password@postgres/cargo_registry
1919
SESSION_KEY: badkeyabcdefghijklmnopqrstuvwxyzabcdef
2020
GIT_REPO_URL: file://./tmp/index-bare
21-
GIT_REPO_CHECKOUT: ./tmp/index-co
2221
GH_CLIENT_ID: ""
2322
GH_CLIENT_SECRET: ""
2423
links:

script/init-local-index.sh

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ if [ -d tmp/index-bare ]; then
88
fi
99

1010
mkdir -p tmp
11-
rm -rf tmp/index-bare tmp/index-co
11+
rm -rf tmp/index-bare tmp/index-tmp
1212

1313
echo "Initializing repository in tmp/index-bare..."
1414
git init -q --bare tmp/index-bare
1515

16-
echo "Creating checkout in tmp/index-bare..."
17-
git init -q tmp/index-co
18-
cd tmp/index-co
16+
echo "Creating temporary clone in tmp/index-tmp..."
17+
git init -q tmp/index-tmp
18+
cd tmp/index-tmp
1919
cat > config.json <<-EOF
2020
{
2121
"dl": "http://localhost:8888/api/v1/crates",
@@ -27,7 +27,12 @@ git commit -qm 'Initial commit'
2727
git remote add origin file://`pwd`/../index-bare
2828
git push -q origin master -u > /dev/null
2929
cd ../..
30-
touch tmp/index-co/.git/git-daemon-export-ok
30+
31+
# Remove the temporary checkout
32+
rm -rf tmp/index-tmp
33+
34+
# Allow the index to be exported via HTTP during local development
35+
touch tmp/index-bare/git-daemon-export-ok
3136

3237
cat - <<-EOF
3338
Your local git index is ready to go!

src/app.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Application-wide components in a struct accessible from each request
22
33
use crate::{db, Config, Env};
4-
use std::{path::PathBuf, sync::Arc, time::Duration};
4+
use std::{sync::Arc, time::Duration};
55

66
use diesel::r2d2;
77
use oauth2::basic::BasicClient;
@@ -25,10 +25,6 @@ pub struct App {
2525
/// A unique key used with conduit_cookie to generate cookies
2626
pub session_key: String,
2727

28-
/// The location on disk of the checkout of the crate index git repository
29-
/// Only used in the development environment.
30-
pub git_repo_checkout: PathBuf,
31-
3228
/// The server configuration
3329
pub config: Config,
3430

@@ -130,7 +126,6 @@ impl App {
130126
read_only_replica_database,
131127
github,
132128
session_key: config.session_key.clone(),
133-
git_repo_checkout: config.git_repo_checkout.clone(),
134129
config: config.clone(),
135130
http_client,
136131
}

src/config.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
use crate::publish_rate_limit::PublishRateLimit;
22
use crate::{env, uploaders::Uploader, Env, Replica};
3-
use std::path::PathBuf;
43

54
#[derive(Clone, Debug)]
65
pub struct Config {
76
pub uploader: Uploader,
87
pub session_key: String,
9-
pub git_repo_checkout: PathBuf,
108
pub gh_client_id: String,
119
pub gh_client_secret: String,
1210
pub db_url: String,
@@ -30,7 +28,6 @@ impl Default for Config {
3028
///
3129
/// Pulls values from the following environment variables:
3230
///
33-
/// - `GIT_REPO_CHECKOUT`: The directory where the registry index was cloned.
3431
/// - `MIRROR`: Is this instance of cargo_registry a mirror of crates.io.
3532
/// - `HEROKU`: Is this instance of cargo_registry currently running on Heroku.
3633
/// - `S3_BUCKET`: The S3 bucket used to store crate files. If not present during development,
@@ -46,7 +43,6 @@ impl Default for Config {
4643
/// - `BLOCKED_TRAFFIC`: A list of headers and environment variables to use for blocking
4744
///. traffic. See the `block_traffic` module for more documentation.
4845
fn default() -> Config {
49-
let checkout = PathBuf::from(env("GIT_REPO_CHECKOUT"));
5046
let api_protocol = String::from("https");
5147
let mirror = if dotenv::var("MIRROR").is_ok() {
5248
Replica::ReadOnlyMirror
@@ -127,7 +123,6 @@ impl Default for Config {
127123
Config {
128124
uploader,
129125
session_key: env("SESSION_KEY"),
130-
git_repo_checkout: checkout,
131126
gh_client_id: env("GH_CLIENT_ID"),
132127
gh_client_secret: env("GH_CLIENT_SECRET"),
133128
db_url: env("DATABASE_URL"),

src/middleware/ember_index_rewrite.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ impl Handler for EmberIndexRewrite {
4040
fn call(&self, req: &mut dyn RequestExt) -> HandlerResult {
4141
let handler = self.handler.as_ref().unwrap();
4242

43-
if req.path().starts_with("/api") {
43+
// The "/git/" prefix is only used in development (when within a docker container)
44+
if req.path().starts_with("/api/") || req.path().starts_with("/git/") {
4445
handler.call(req)
4546
} else {
4647
if let Some(client) = &self.fastboot_client {

src/router.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ pub fn build_router(app: &App) -> R404 {
127127
// In production, for crates.io, cargo gets the index from
128128
// https://github.com/rust-lang/crates.io-index directly.
129129
if app.config.env == Env::Development {
130-
let s = conduit_git_http_backend::Serve(app.git_repo_checkout.clone());
130+
let s = conduit_git_http_backend::Serve("./tmp/index-bare".into());
131131
let s = Arc::new(s);
132132
router.get("/git/index/*path", R(Arc::clone(&s)));
133133
router.post("/git/index/*path", R(s));

src/tests/all.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@ fn simple_config() -> Config {
131131
Config {
132132
uploader,
133133
session_key: "test this has to be over 32 bytes long".to_string(),
134-
git_repo_checkout: git::checkout(),
135134
gh_client_id: dotenv::var("GH_CLIENT_ID").unwrap_or_default(),
136135
gh_client_secret: dotenv::var("GH_CLIENT_SECRET").unwrap_or_default(),
137136
db_url: env("TEST_DATABASE_URL"),

src/tests/git.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,12 @@ fn root() -> PathBuf {
1212
.join(thread::current().name().unwrap())
1313
}
1414

15-
pub fn checkout() -> PathBuf {
16-
root().join("checkout")
17-
}
1815
pub fn bare() -> PathBuf {
1916
root().join("bare")
2017
}
2118

2219
pub fn init() {
2320
static INIT: Once = Once::new();
24-
let _ = fs::remove_dir_all(&checkout());
2521
let _ = fs::remove_dir_all(&bare());
2622

2723
INIT.call_once(|| {

0 commit comments

Comments
 (0)