Skip to content

Commit 8f61377

Browse files
committed
If PKGX_PANTRY_DIR is set, don’t blow it away
We assume if the user sets PKGX_PANTRY_DIR that they are managing its updates themselves.
1 parent 4cb6d60 commit 8f61377

3 files changed

Lines changed: 47 additions & 3 deletions

File tree

.github/workflows/ci.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,22 @@ jobs:
127127
exit 1
128128
fi
129129
130+
# check that we update the pantry for unknown programs
131+
# this works by deleting the entry for git then forcing
132+
# pkgx to update the db, then trying to get git again
133+
- run: |
134+
set -x
135+
rm -rf ~/.pkgx
136+
rm -rf ~/.cache/pkgx/pantry/projects/git-scm.org
137+
rm ~/.cache/pkgx/pantry.2.db
138+
pkgx curl --version
139+
test -f ~/.cache/pkgx/pantry.2.db
140+
test ! -d ~/.cache/pkgx/pantry/projects/git-scm.org
141+
pkgx git --version
142+
test -d ~/.cache/pkgx/pantry/projects/git-scm.org
143+
if: ${{ matrix.os == 'ubuntu-latest' }}
144+
# ^^ only on one platform as wasteful otherwise
145+
130146
- name: generate coverage
131147
run: |
132148
cargo install rustfilt

crates/cli/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
6060
if let Some(spinner) = &spinner {
6161
spinner.set_message("syncing pkg-db…");
6262
}
63-
sync::replace(&config, &mut conn).await?;
63+
sync::ensure(&config, &mut conn).await?;
6464
true
6565
} else {
6666
false
@@ -112,7 +112,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
112112
spinner.set_message(msg);
113113
}
114114
// cmd not found ∴ sync in case it is new
115-
sync::replace(&config, &mut conn).await?;
115+
sync::update(&config, &mut conn).await?;
116116
if let Some(spinner) = &spinner {
117117
spinner.set_message("resolving pkg graph…");
118118
}

crates/lib/src/sync.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,35 @@ pub fn should(config: &Config) -> Result<bool, Box<dyn Error>> {
1919
}
2020
}
2121

22-
pub async fn replace(config: &Config, conn: &mut Connection) -> Result<(), Box<dyn Error>> {
22+
// doesn’t replace pantry clone, will build db
23+
// essential for working in a local pantry clone with PKGX_PANTRY_DIR set
24+
pub async fn ensure(config: &Config, conn: &mut Connection) -> Result<(), Box<dyn Error>> {
25+
if !config.pantry_dir.join("projects").is_dir() {
26+
replace(config, conn).await
27+
} else {
28+
let dest = &config.pantry_dir;
29+
std::fs::create_dir_all(dest.clone())?;
30+
let dir = OpenOptions::new()
31+
.read(true) // Open in read-only mode; no need to write.
32+
.open(dest)?;
33+
dir.lock_exclusive()?;
34+
35+
pantry_db::cache(config, conn)?;
36+
37+
FileExt::unlock(&dir)?;
38+
39+
Ok(())
40+
}
41+
}
42+
43+
pub async fn update(config: &Config, conn: &mut Connection) -> Result<(), Box<dyn Error>> {
44+
if std::env::var("PKGX_PANTRY_DIR").is_ok() {
45+
return Err("PKGX_PANTRY_DIR is set, refusing to update pantry")?;
46+
}
47+
replace(config, conn).await
48+
}
49+
50+
async fn replace(config: &Config, conn: &mut Connection) -> Result<(), Box<dyn Error>> {
2351
let url = env!("PKGX_PANTRY_TARBALL_URL");
2452
let dest = &config.pantry_dir;
2553

0 commit comments

Comments
 (0)