Skip to content
This repository has been archived by the owner on Jan 2, 2025. It is now read-only.

Commit

Permalink
Fix file browsing on Windows (#789)
Browse files Browse the repository at this point in the history
To enforce consistency, we now use forward slashes (`/`) throughout the
app, on both unix and Windows platforms.
  • Loading branch information
calyptobai authored Jul 28, 2023
1 parent 5a651d6 commit aeba010
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 30 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions server/bleep/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,6 @@ comrak = { default-features = false, git = "https://github.com/kivikakk/comrak"
lazy-regex = "3.0.0"
quick-xml = { version = "0.29.0", features = ["serialize"] }

[target.'cfg(windows)'.dependencies]
dunce = "1.0.4"

[dev-dependencies]
criterion = { version = "0.5.1", features = ["async_tokio"] }
pretty_assertions = "1.3.0"
Expand Down
11 changes: 8 additions & 3 deletions server/bleep/src/indexes/file.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{
collections::{HashMap, HashSet},
path::{Path, PathBuf, MAIN_SEPARATOR},
path::{Path, PathBuf},
sync::{
atomic::{AtomicU64, Ordering},
Arc,
Expand Down Expand Up @@ -267,7 +267,7 @@ impl Indexer<File> {
.map(|f| f.is_match(&doc.relative_path))
.unwrap_or_default()
})
.filter(|doc| !doc.relative_path.ends_with(MAIN_SEPARATOR)) // omit directories
.filter(|doc| !doc.relative_path.ends_with('/')) // omit directories
.take(limit)
}

Expand Down Expand Up @@ -535,7 +535,9 @@ impl RepoDir {
last_commit: u64,
tantivy_cache_key: String,
) -> tantivy::schema::Document {
let relative_path_str = format!("{}{MAIN_SEPARATOR}", relative_path.to_string_lossy());
let relative_path_str = format!("{}/", relative_path.to_string_lossy());
#[cfg(windows)]
let relative_path_str = relative_path_str.replace('\\', "/");

let branches = self.branches.join("\n");

Expand Down Expand Up @@ -580,6 +582,9 @@ impl RepoFile {
file_cache: &FileCache,
) -> Option<tantivy::schema::Document> {
let relative_path_str = relative_path.to_string_lossy().to_string();
#[cfg(windows)]
let relative_path_str = relative_path_str.replace('\\', "/");

let branches = self.branches.join("\n");
let lang_str = repo_metadata
.langs
Expand Down
11 changes: 3 additions & 8 deletions server/bleep/src/indexes/reader.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::path::MAIN_SEPARATOR;

use anyhow::Result;
use async_trait::async_trait;
use tantivy::{
Expand Down Expand Up @@ -339,9 +337,7 @@ impl DocumentRead for OpenReader {
/// - `"bar/" -> "bar/"`
/// - `"foo.txt" -> ""`
pub fn base_name(path: &str) -> &str {
path.rfind(MAIN_SEPARATOR)
.map(|i| &path[..i + 1])
.unwrap_or("")
path.rfind('/').map(|i| &path[..i + 1]).unwrap_or("")
}

fn read_text_field(doc: &tantivy::Document, field: Field) -> String {
Expand Down Expand Up @@ -371,9 +367,8 @@ mod test {

#[test]
fn test_base_name() {
let s = MAIN_SEPARATOR;
assert_eq!(base_name(&format!("bar{s}foo.txt")), format!("bar{s}"));
assert_eq!(base_name(&format!("bar{s}")), format!("bar{s}"));
assert_eq!(base_name(&format!("bar/foo.txt")), format!("bar/"));
assert_eq!(base_name(&format!("bar/")), format!("bar/"));
assert_eq!(base_name("foo.txt"), "");
}
}
4 changes: 0 additions & 4 deletions server/bleep/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,8 @@ use git_version as _;
#[cfg(all(feature = "debug", not(tokio_unstable)))]
use console_subscriber as _;

#[cfg(windows)]
use dunce::canonicalize;

use secrecy::SecretString;
use state::PersistedState;
#[cfg(not(windows))]
use std::fs::canonicalize;
use user::UserProfile;

Expand Down
13 changes: 6 additions & 7 deletions server/bleep/src/query/execute.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::{
collections::{HashMap, HashSet},
path::MAIN_SEPARATOR,
sync::Arc,
};

Expand Down Expand Up @@ -627,14 +626,14 @@ impl ExecuteQuery for OpenReader {
// because the `BytesFilterCollector` operates on one field. So we sort through this
// later. It's unlikely that a search will use more than one open query.
relative_paths.iter().any(|rp| {
let rp = rp.trim_end_matches(|c| c != MAIN_SEPARATOR);
let rp = rp.trim_end_matches(|c| c != '/');

matches!(
// Trim trailing suffix and avoid returning results for an empty string
// (this means that the document we are looking at is the folder itself; a
// redundant result).
relative_path.strip_prefix(rp).map(|p| p.trim_end_matches(MAIN_SEPARATOR)),
Some(p) if !p.is_empty() && !p.contains(MAIN_SEPARATOR)
relative_path.strip_prefix(rp).map(|p| p.trim_end_matches('/')),
Some(p) if !p.is_empty() && !p.contains('/')
)
})
},
Expand All @@ -653,7 +652,7 @@ impl ExecuteQuery for OpenReader {
// Set of (repo_name, relative_path) that should be returned.
let directories = open_directives
.iter()
.filter(|d| d.relative_path.is_empty() || d.relative_path.ends_with(MAIN_SEPARATOR))
.filter(|d| d.relative_path.is_empty() || d.relative_path.ends_with('/'))
.map(|d| (&d.repo_name, &d.relative_path))
.collect::<HashSet<_>>();

Expand Down Expand Up @@ -694,15 +693,15 @@ impl ExecuteQuery for OpenReader {
if let Some(entry) = doc
.relative_path
.strip_prefix(relative_path)
.and_then(|s| s.split_inclusive(MAIN_SEPARATOR).next())
.and_then(|s| s.split_inclusive('/').next())
{
dir_entries
.entry((&directive.repo_name, relative_path))
.or_insert_with(|| (doc.repo_ref.to_owned(), HashSet::default()))
.1
.insert(DirEntry {
name: entry.to_owned(),
entry_data: if entry.contains(MAIN_SEPARATOR) {
entry_data: if entry.contains('/') {
EntryData::Directory
} else {
EntryData::File {
Expand Down
4 changes: 3 additions & 1 deletion server/bleep/src/repo/iterator/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use super::*;
use anyhow::Result;
use gix::ThreadSafeRepository;
use regex::RegexSet;
use tracing::error;
use tracing::{error, trace};

use std::{
collections::{BTreeSet, HashMap},
Expand Down Expand Up @@ -137,6 +137,7 @@ impl GitWalker {
.map(move |entry| {
let strpath = String::from_utf8_lossy(entry.filepath.as_ref());
let full_path = root_dir.join(strpath.as_ref());
trace!(?strpath, ?full_path, "got path from gix");
(
is_head,
branch.clone(),
Expand Down Expand Up @@ -182,6 +183,7 @@ impl FileSource for GitWalker {
self.entries
.into_par_iter()
.filter_map(|((path, kind, oid), branches)| {
trace!(?path, "walking over path");
let git = self.git.to_thread_local();
let Ok(Some(object)) = git.try_find_object(oid) else {
error!(?path, ?branches, "can't find object for file");
Expand Down
3 changes: 0 additions & 3 deletions server/bleep/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,6 @@ impl StateSource {
}

pub(crate) fn initialize_pool(&self) -> Result<RepositoryPool, RepoError> {
#[cfg(target = "windows")]
use dunce::canonicalize;
#[cfg(not(target = "windows"))]
use std::fs::canonicalize;

match (self.directory.as_ref(), self.state_file.as_ref()) {
Expand Down

0 comments on commit aeba010

Please sign in to comment.