Skip to content

Commit 44fc7d7

Browse files
committed
add rga-no-prefix-filenames flag (fixes #154)
1 parent 3b94d93 commit 44fc7d7

File tree

6 files changed

+53
-18
lines changed

6 files changed

+53
-18
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
# 0.10.3
1+
# 0.10.4 (2024-01-16)
2+
3+
- add `--rga-no-prefix-filenames` flag (https://github.com/phiresky/ripgrep-all/issues/154)
4+
5+
# 0.10.3 (2024-01-16)
26

37
This was originally supposed to be version 1.0.0, but I don't feel confident enough in the stability to call it that.
48

src/adapters/postproc.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,6 @@ impl FileAdapter for PostprocPageBreaks {
183183
// keep adapt info (filename etc) except replace inp
184184
let ai = AdaptInfo {
185185
inp: Box::pin(read),
186-
postprocess: true,
187186
archive_recursion_depth: a.archive_recursion_depth + 1,
188187
filepath_hint: a
189188
.filepath_hint

src/bin/rga-preproc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ async fn main() -> anyhow::Result<()> {
3030
is_real_file: true,
3131
line_prefix: "".to_string(),
3232
archive_recursion_depth: 0,
33-
postprocess: true,
33+
postprocess: !config.no_prefix_filenames,
3434
config,
3535
};
3636

src/config.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@ pub struct RgaConfig {
144144
pub cache: CacheConfig,
145145

146146
/// Maximum nestedness of archives to recurse into
147+
///
148+
/// When searching in archives, rga will recurse into archives inside archives.
149+
/// This option limits the depth.
147150
#[serde(default, skip_serializing_if = "is_default")]
148151
#[structopt(
149152
default_value,
@@ -153,6 +156,14 @@ pub struct RgaConfig {
153156
)]
154157
pub max_archive_recursion: MaxArchiveRecursion,
155158

159+
/// Don't prefix lines of files within archive with the path inside the archive.
160+
///
161+
/// Inside archives, by default rga prefixes the content of each file with the file path within the archive.
162+
/// This is usually useful, but can cause problems because then the inner path is also searched for the pattern.
163+
#[serde(default, skip_serializing_if = "is_default")]
164+
#[structopt(long = "--rga-no-prefix-filenames")]
165+
pub no_prefix_filenames: bool,
166+
156167
//////////////////////////////////////////
157168
//////////////////////////// Config file only
158169
//////////////////////////////////////////

src/preproc.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,12 @@ async fn adapt_caching(
153153
};
154154

155155
let mut cache = cache.context("No cache?")?;
156-
let cache_key = CacheKey::new(&ai.filepath_hint, adapter.as_ref(), &active_adapters)?;
156+
let cache_key = CacheKey::new(
157+
ai.postprocess,
158+
&ai.filepath_hint,
159+
adapter.as_ref(),
160+
&active_adapters,
161+
)?;
157162
// let dbg_ctx = format!("adapter {}", &adapter.metadata().name);
158163
let cached = cache.get(&cache_key).await.context("cache.get")?;
159164
match cached {

src/preproc_cache.rs

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
use crate::{adapters::FileAdapter, preproc::ActiveAdapters};
22
use anyhow::{Context, Result};
3+
use log::warn;
34
use path_clean::PathClean;
45
use rusqlite::{named_params, OptionalExtension};
56
use std::{path::Path, time::UNIX_EPOCH};
67
use tokio_rusqlite::Connection;
78

9+
static SCHEMA_VERSION: i32 = 3;
810
#[derive(Clone)]
911
pub struct CacheKey {
12+
config_hash: String,
1013
adapter: String,
1114
adapter_version: i32,
1215
active_adapters: String,
@@ -15,6 +18,7 @@ pub struct CacheKey {
1518
}
1619
impl CacheKey {
1720
pub fn new(
21+
postprocess: bool,
1822
filepath_hint: &Path,
1923
adapter: &dyn FileAdapter,
2024
active_adapters: &ActiveAdapters,
@@ -34,6 +38,11 @@ impl CacheKey {
3438
"null".to_string()
3539
};
3640
Ok(CacheKey {
41+
config_hash: if postprocess {
42+
"a41e2e9".to_string()
43+
} else {
44+
"f1502a3".to_string()
45+
}, // todo: when we add more config options that affect caching, create a struct and actually hash it
3746
adapter: adapter.metadata().name.clone(),
3847
adapter_version: adapter.metadata().version,
3948
file_path: filepath_hint.clean().to_string_lossy().to_string(),
@@ -63,6 +72,7 @@ async fn connect_pragmas(db: &Connection) -> Result<()> {
6372
db.pragma_update(None, "mmap_size", "2000000000")?;
6473
db.execute("
6574
create table if not exists preproc_cache (
75+
config_hash text not null,
6676
adapter text not null,
6777
adapter_version integer not null,
6878
created_unix_ms integer not null default (unixepoch() * 1000),
@@ -73,7 +83,7 @@ async fn connect_pragmas(db: &Connection) -> Result<()> {
7383
) strict", []
7484
)?;
7585

76-
db.execute("create unique index if not exists preproc_cache_idx on preproc_cache (adapter, adapter_version, file_path, active_adapters)", [])?;
86+
db.execute("create unique index if not exists preproc_cache_idx on preproc_cache (config_hash, adapter, adapter_version, file_path, active_adapters)", [])?;
7787

7888
Ok(())
7989
})
@@ -83,26 +93,29 @@ async fn connect_pragmas(db: &Connection) -> Result<()> {
8393
.await?;
8494
if jm != 924716026 {
8595
// (probably) newly created db
86-
create_pragmas(db).await.context("create_pragmas")?;
96+
db.call(|db| Ok(db.pragma_update(None, "application_id", "924716026")?))
97+
.await?;
8798
}
8899
Ok(())
89100
}
90101

91-
async fn create_pragmas(db: &Connection) -> Result<()> {
92-
db.call(|db| {
93-
db.pragma_update(None, "application_id", "924716026")?;
94-
db.pragma_update(None, "user_version", "2")?; // todo: on upgrade clear db if version is unexpected
95-
Ok(())
96-
})
97-
.await?;
98-
Ok(())
99-
}
100102
struct SqliteCache {
101103
db: Connection,
102104
}
103105
impl SqliteCache {
104106
async fn new(path: &Path) -> Result<SqliteCache> {
105107
let db = Connection::open(path.join("cache.sqlite3")).await?;
108+
db.call(|db| {
109+
let schema_version: i32 = db.pragma_query_value(None, "user_version", |r| r.get(0))?;
110+
if schema_version != SCHEMA_VERSION {
111+
warn!("Cache schema version mismatch, clearing cache");
112+
db.execute("drop table if exists preproc_cache", [])?;
113+
db.pragma_update(None, "user_version", format!("{SCHEMA_VERSION}"))?;
114+
}
115+
Ok(())
116+
})
117+
.await?;
118+
106119
connect_pragmas(&db).await?;
107120

108121
Ok(SqliteCache { db })
@@ -120,12 +133,14 @@ impl PreprocCache for SqliteCache {
120133
.query_row(
121134
"select text_content_zstd from preproc_cache where
122135
adapter = :adapter
136+
and config_hash = :config_hash
123137
and adapter_version = :adapter_version
124138
and active_adapters = :active_adapters
125139
and file_path = :file_path
126140
and file_mtime_unix_ms = :file_mtime_unix_ms
127141
",
128142
named_params! {
143+
":config_hash": &key.config_hash,
129144
":adapter": &key.adapter,
130145
":adapter_version": &key.adapter_version,
131146
":active_adapters": &key.active_adapters,
@@ -152,13 +167,14 @@ impl PreprocCache for SqliteCache {
152167
.db
153168
.call(move |db| {
154169
db.execute(
155-
"insert into preproc_cache (adapter, adapter_version, active_adapters, file_path, file_mtime_unix_ms, text_content_zstd) values
156-
(:adapter, :adapter_version, :active_adapters, :file_path, :file_mtime_unix_ms, :text_content_zstd)
157-
on conflict (adapter, adapter_version, active_adapters, file_path) do update set
170+
"insert into preproc_cache (config_hash, adapter, adapter_version, active_adapters, file_path, file_mtime_unix_ms, text_content_zstd) values
171+
(:config_hash, :adapter, :adapter_version, :active_adapters, :file_path, :file_mtime_unix_ms, :text_content_zstd)
172+
on conflict (config_hash, adapter, adapter_version, active_adapters, file_path) do update set
158173
file_mtime_unix_ms = :file_mtime_unix_ms,
159174
created_unix_ms = unixepoch() * 1000,
160175
text_content_zstd = :text_content_zstd",
161176
named_params! {
177+
":config_hash": &key.config_hash,
162178
":adapter": &key.adapter,
163179
":adapter_version": &key.adapter_version,
164180
":active_adapters": &key.active_adapters,

0 commit comments

Comments
 (0)