Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

repo_path: split RepoPath into owned and borrowed types #2643

Merged
merged 5 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions cli/examples/custom-working-copy/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use jj_lib::local_working_copy::LocalWorkingCopy;
use jj_lib::merged_tree::MergedTree;
use jj_lib::op_store::{OperationId, WorkspaceId};
use jj_lib::repo::ReadonlyRepo;
use jj_lib::repo_path::RepoPath;
use jj_lib::repo_path::RepoPathBuf;
use jj_lib::settings::UserSettings;
use jj_lib::store::Store;
use jj_lib::working_copy::{
Expand Down Expand Up @@ -171,7 +171,7 @@ impl WorkingCopy for ConflictsWorkingCopy {
self.inner.tree_id()
}

fn sparse_patterns(&self) -> Result<&[RepoPath], WorkingCopyStateError> {
fn sparse_patterns(&self) -> Result<&[RepoPathBuf], WorkingCopyStateError> {
self.inner.sparse_patterns()
}

Expand Down Expand Up @@ -225,13 +225,13 @@ impl LockedWorkingCopy for LockedConflictsWorkingCopy {
self.inner.reset(new_tree)
}

fn sparse_patterns(&self) -> Result<&[RepoPath], WorkingCopyStateError> {
fn sparse_patterns(&self) -> Result<&[RepoPathBuf], WorkingCopyStateError> {
self.inner.sparse_patterns()
}

fn set_sparse_patterns(
&mut self,
new_sparse_patterns: Vec<RepoPath>,
new_sparse_patterns: Vec<RepoPathBuf>,
) -> Result<CheckoutStats, CheckoutError> {
self.inner.set_sparse_patterns(new_sparse_patterns)
}
Expand Down
8 changes: 4 additions & 4 deletions cli/src/cli_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ use jj_lib::repo::{
CheckOutCommitError, EditCommitError, MutableRepo, ReadonlyRepo, Repo, RepoLoader,
RepoLoaderError, RewriteRootCommit, StoreFactories, StoreLoadError,
};
use jj_lib::repo_path::{FsPathParseError, RepoPath};
use jj_lib::repo_path::{FsPathParseError, RepoPath, RepoPathBuf};
use jj_lib::revset::{
DefaultSymbolResolver, Revset, RevsetAliasesMap, RevsetCommitRef, RevsetEvaluationError,
RevsetExpression, RevsetIteratorExt, RevsetParseContext, RevsetParseError,
Expand Down Expand Up @@ -904,8 +904,8 @@ impl WorkspaceCommandHelper {

/// Parses a path relative to cwd into a RepoPath, which is relative to the
/// workspace root.
pub fn parse_file_path(&self, input: &str) -> Result<RepoPath, FsPathParseError> {
RepoPath::parse_fs_path(&self.cwd, self.workspace_root(), input)
pub fn parse_file_path(&self, input: &str) -> Result<RepoPathBuf, FsPathParseError> {
RepoPathBuf::parse_fs_path(&self.cwd, self.workspace_root(), input)
}

pub fn matcher_from_values(&self, values: &[String]) -> Result<Box<dyn Matcher>, CommandError> {
Expand All @@ -917,7 +917,7 @@ impl WorkspaceCommandHelper {
.iter()
.map(|v| self.parse_file_path(v))
.try_collect()?;
Ok(Box::new(PrefixMatcher::new(&paths)))
Ok(Box::new(PrefixMatcher::new(paths)))
}
}

Expand Down
4 changes: 2 additions & 2 deletions cli/src/commands/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1120,12 +1120,12 @@ fn cmd_git_submodule_print_gitmodules(
let commit = workspace_command.resolve_single_rev(&args.revisions, ui)?;
let tree = commit.tree()?;
let gitmodules_path = RepoPath::from_internal_string(".gitmodules");
let mut gitmodules_file = match tree.path_value(&gitmodules_path).into_resolved() {
let mut gitmodules_file = match tree.path_value(gitmodules_path).into_resolved() {
Ok(None) => {
writeln!(ui.stderr(), "No submodules!")?;
return Ok(());
}
Ok(Some(TreeValue::File { id, .. })) => repo.store().read_file(&gitmodules_path, &id)?,
Ok(Some(TreeValue::File { id, .. })) => repo.store().read_file(gitmodules_path, &id)?,
_ => {
return Err(user_error(".gitmodules is not a file."));
}
Expand Down
4 changes: 2 additions & 2 deletions cli/src/commands/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use std::io::Write;
use itertools::Itertools;
use jj_lib::backend::{ObjectId, TreeValue};
use jj_lib::merge::MergedTreeValue;
use jj_lib::repo_path::RepoPath;
use jj_lib::repo_path::RepoPathBuf;
use tracing::instrument;

use crate::cli_util::{CommandError, CommandHelper, WorkspaceCommandHelper};
Expand Down Expand Up @@ -127,7 +127,7 @@ pub(crate) fn cmd_resolve(

#[instrument(skip_all)]
pub(crate) fn print_conflicted_paths(
conflicts: &[(RepoPath, MergedTreeValue)],
conflicts: &[(RepoPathBuf, MergedTreeValue)],
formatter: &mut dyn Formatter,
workspace_command: &WorkspaceCommandHelper,
) -> Result<(), CommandError> {
Expand Down
10 changes: 5 additions & 5 deletions cli/src/commands/sparse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use std::path::Path;
use clap::Subcommand;
use itertools::Itertools;
use jj_lib::file_util;
use jj_lib::repo_path::RepoPath;
use jj_lib::repo_path::RepoPathBuf;
use jj_lib::settings::UserSettings;
use tracing::instrument;

Expand Down Expand Up @@ -122,7 +122,7 @@ fn cmd_sparse_set(
let (mut locked_ws, wc_commit) = workspace_command.start_working_copy_mutation()?;
let mut new_patterns = HashSet::new();
if args.reset {
new_patterns.insert(RepoPath::root());
new_patterns.insert(RepoPathBuf::root());
} else {
if !args.clear {
new_patterns.extend(locked_ws.locked_wc().sparse_patterns()?.iter().cloned());
Expand Down Expand Up @@ -161,9 +161,9 @@ fn cmd_sparse_set(
fn edit_sparse(
workspace_root: &Path,
repo_path: &Path,
sparse: &[RepoPath],
sparse: &[RepoPathBuf],
settings: &UserSettings,
) -> Result<Vec<RepoPath>, CommandError> {
) -> Result<Vec<RepoPathBuf>, CommandError> {
let file = (|| -> Result<_, io::Error> {
let mut file = tempfile::Builder::new()
.prefix("editor-")
Expand Down Expand Up @@ -216,7 +216,7 @@ fn edit_sparse(
path = file_path.display()
))
})?;
Ok::<_, CommandError>(RepoPath::parse_fs_path(
Ok::<_, CommandError>(RepoPathBuf::parse_fs_path(
workspace_root,
workspace_root,
line.trim(),
Expand Down
46 changes: 25 additions & 21 deletions cli/src/merge_tools/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use jj_lib::files::{self, ContentHunk, MergeResult};
use jj_lib::matchers::Matcher;
use jj_lib::merge::Merge;
use jj_lib::merged_tree::{MergedTree, MergedTreeBuilder};
use jj_lib::repo_path::RepoPath;
use jj_lib::repo_path::{RepoPath, RepoPathBuf};
use jj_lib::store::Store;
use pollster::FutureExt;
use thiserror::Error;
Expand All @@ -23,7 +23,7 @@ pub enum BuiltinToolError {
ReadFileBackend(BackendError),
#[error("Failed to read file {path:?} with ID {id:?}: {source}")]
ReadFileIo {
path: RepoPath,
path: RepoPathBuf,
id: FileId,
source: std::io::Error,
},
Expand Down Expand Up @@ -119,7 +119,7 @@ fn read_file_contents(
reader
.read_to_end(&mut buf)
.map_err(|err| BuiltinToolError::ReadFileIo {
path: path.clone(),
path: path.to_owned(),
id: id.clone(),
source: err,
})?;
Expand Down Expand Up @@ -235,7 +235,7 @@ pub fn make_diff_files(
store: &Arc<Store>,
left_tree: &MergedTree,
right_tree: &MergedTree,
changed_files: &[RepoPath],
changed_files: &[RepoPathBuf],
) -> Result<Vec<scm_record::File<'static>>, BuiltinToolError> {
let mut files = Vec::new();
for changed_path in changed_files {
Expand Down Expand Up @@ -361,7 +361,7 @@ pub fn apply_diff_builtin(
store: Arc<Store>,
left_tree: &MergedTree,
right_tree: &MergedTree,
changed_files: Vec<RepoPath>,
changed_files: Vec<RepoPathBuf>,
files: &[scm_record::File],
) -> Result<MergedTreeId, BackendError> {
let mut tree_builder = MergedTreeBuilder::new(left_tree.id().clone());
Expand Down Expand Up @@ -537,7 +537,7 @@ pub fn edit_merge_builtin(
tree.store().clone(),
tree,
tree,
vec![path.clone()],
vec![path.to_owned()],
&[file],
)
.map_err(BuiltinToolError::BackendError)
Expand Down Expand Up @@ -565,22 +565,26 @@ mod tests {
let left_tree = testutils::create_tree(
&test_repo.repo,
&[
(&unused_path, "unused\n"),
(&unchanged, "unchanged\n"),
(&changed_path, "line1\nline2\nline3\n"),
(unused_path, "unused\n"),
(unchanged, "unchanged\n"),
(changed_path, "line1\nline2\nline3\n"),
],
);
let right_tree = testutils::create_tree(
&test_repo.repo,
&[
(&unused_path, "unused\n"),
(&unchanged, "unchanged\n"),
(&changed_path, "line1\nchanged1\nchanged2\nline3\nadded1\n"),
(&added_path, "added\n"),
(unused_path, "unused\n"),
(unchanged, "unchanged\n"),
(changed_path, "line1\nchanged1\nchanged2\nline3\nadded1\n"),
(added_path, "added\n"),
],
);

let changed_files = vec![unchanged.clone(), changed_path.clone(), added_path.clone()];
let changed_files = vec![
unchanged.to_owned(),
changed_path.to_owned(),
added_path.to_owned(),
];
let files = make_diff_files(store, &left_tree, &right_tree, &changed_files).unwrap();
insta::assert_debug_snapshot!(files, @r###"
[
Expand Down Expand Up @@ -715,15 +719,15 @@ mod tests {
let path = RepoPath::from_internal_string("file");
let base_tree = testutils::create_tree(
&test_repo.repo,
&[(&path, "base 1\nbase 2\nbase 3\nbase 4\nbase 5\n")],
&[(path, "base 1\nbase 2\nbase 3\nbase 4\nbase 5\n")],
);
let left_tree = testutils::create_tree(
&test_repo.repo,
&[(&path, "left 1\nbase 2\nbase 3\nbase 4\nleft 5\n")],
&[(path, "left 1\nbase 2\nbase 3\nbase 4\nleft 5\n")],
);
let right_tree = testutils::create_tree(
&test_repo.repo,
&[(&path, "right 1\nbase 2\nbase 3\nbase 4\nright 5\n")],
&[(path, "right 1\nbase 2\nbase 3\nbase 4\nright 5\n")],
);

fn to_file_id(tree_value: MergedTreeValue) -> Option<FileId> {
Expand All @@ -735,11 +739,11 @@ mod tests {
}
}
let merge = Merge::from_vec(vec![
to_file_id(left_tree.path_value(&path)),
to_file_id(base_tree.path_value(&path)),
to_file_id(right_tree.path_value(&path)),
to_file_id(left_tree.path_value(path)),
to_file_id(base_tree.path_value(path)),
to_file_id(right_tree.path_value(path)),
]);
let content = extract_as_single_hunk(&merge, store, &path).block_on();
let content = extract_as_single_hunk(&merge, store, path).block_on();
let slices = content.map(|ContentHunk(buf)| buf.as_slice());
let merge_result = files::merge(&slices);
let sections = make_merge_sections(merge_result).unwrap();
Expand Down
6 changes: 3 additions & 3 deletions cli/src/merge_tools/external.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use jj_lib::local_working_copy::{TreeState, TreeStateError};
use jj_lib::matchers::Matcher;
use jj_lib::merge::{Merge, MergedTreeValue};
use jj_lib::merged_tree::{MergedTree, MergedTreeBuilder};
use jj_lib::repo_path::RepoPath;
use jj_lib::repo_path::{RepoPath, RepoPathBuf};
use jj_lib::settings::UserSettings;
use jj_lib::store::Store;
use jj_lib::working_copy::{CheckoutError, SnapshotOptions};
Expand Down Expand Up @@ -189,7 +189,7 @@ fn check_out(
wc_dir: PathBuf,
state_dir: PathBuf,
tree: &MergedTree,
sparse_patterns: Vec<RepoPath>,
sparse_patterns: Vec<RepoPathBuf>,
) -> Result<TreeState, DiffCheckoutError> {
std::fs::create_dir(&wc_dir).map_err(DiffCheckoutError::SetUpDir)?;
std::fs::create_dir(&state_dir).map_err(DiffCheckoutError::SetUpDir)?;
Expand Down Expand Up @@ -384,7 +384,7 @@ pub fn run_mergetool_external(
Err(new_file_ids) => conflict.with_new_file_ids(&new_file_ids),
};
let mut tree_builder = MergedTreeBuilder::new(tree.id());
tree_builder.set_or_remove(repo_path.clone(), new_tree_value);
tree_builder.set_or_remove(repo_path.to_owned(), new_tree_value);
let new_tree = tree_builder.write_tree(tree.store())?;
Ok(new_tree)
}
Expand Down
18 changes: 9 additions & 9 deletions cli/src/merge_tools/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use jj_lib::conflicts::extract_as_single_hunk;
use jj_lib::gitignore::GitIgnoreFile;
use jj_lib::matchers::Matcher;
use jj_lib::merged_tree::MergedTree;
use jj_lib::repo_path::RepoPath;
use jj_lib::repo_path::{RepoPath, RepoPathBuf};
use jj_lib::settings::{ConfigResultExt as _, UserSettings};
use jj_lib::working_copy::SnapshotError;
use pollster::FutureExt;
Expand Down Expand Up @@ -66,16 +66,16 @@ pub enum ConflictResolveError {
#[error(transparent)]
ExternalTool(#[from] ExternalToolError),
#[error("Couldn't find the path {0:?} in this revision")]
PathNotFound(RepoPath),
PathNotFound(RepoPathBuf),
#[error("Couldn't find any conflicts at {0:?} in this revision")]
NotAConflict(RepoPath),
NotAConflict(RepoPathBuf),
#[error(
"Only conflicts that involve normal files (not symlinks, not executable, etc.) are \
supported. Conflict summary for {0:?}:\n{1}"
)]
NotNormalFiles(RepoPath, String),
NotNormalFiles(RepoPathBuf, String),
#[error("The conflict at {path:?} has {sides} sides. At most 2 sides are supported.")]
ConflictTooComplicated { path: RepoPath, sides: usize },
ConflictTooComplicated { path: RepoPathBuf, sides: usize },
#[error(
"The output file is either unchanged or empty after the editor quit (run with --verbose \
to see the exact invocation)."
Expand All @@ -93,23 +93,23 @@ pub fn run_mergetool(
) -> Result<MergedTreeId, ConflictResolveError> {
let conflict = match tree.path_value(repo_path).into_resolved() {
Err(conflict) => conflict,
Ok(Some(_)) => return Err(ConflictResolveError::NotAConflict(repo_path.clone())),
Ok(None) => return Err(ConflictResolveError::PathNotFound(repo_path.clone())),
Ok(Some(_)) => return Err(ConflictResolveError::NotAConflict(repo_path.to_owned())),
Ok(None) => return Err(ConflictResolveError::PathNotFound(repo_path.to_owned())),
};
let file_merge = conflict.to_file_merge().ok_or_else(|| {
let mut summary_bytes: Vec<u8> = vec![];
conflict
.describe(&mut summary_bytes)
.expect("Writing to an in-memory buffer should never fail");
ConflictResolveError::NotNormalFiles(
repo_path.clone(),
repo_path.to_owned(),
String::from_utf8_lossy(summary_bytes.as_slice()).to_string(),
)
})?;
// We only support conflicts with 2 sides (3-way conflicts)
if file_merge.num_sides() > 2 {
return Err(ConflictResolveError::ConflictTooComplicated {
path: repo_path.clone(),
path: repo_path.to_owned(),
sides: file_merge.num_sides(),
});
};
Expand Down
2 changes: 1 addition & 1 deletion lib/src/default_revset_engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -885,7 +885,7 @@ fn has_diff_from_parent(
if let [parent] = parents.as_slice() {
// Fast path: no need to load the root tree
let unchanged = commit.tree_id() == parent.tree_id();
if matcher.visit(&RepoPath::root()) == Visit::AllRecursively {
if matcher.visit(RepoPath::root()) == Visit::AllRecursively {
return !unchanged;
} else if unchanged {
return false;
Expand Down
4 changes: 2 additions & 2 deletions lib/src/git_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1232,7 +1232,7 @@ mod tests {

let root_tree = backend
.read_tree(
&RepoPath::root(),
RepoPath::root(),
&TreeId::from_bytes(root_tree_id.as_bytes()),
)
.block_on()
Expand All @@ -1248,7 +1248,7 @@ mod tests {

let dir_tree = backend
.read_tree(
&RepoPath::from_internal_string("dir"),
RepoPath::from_internal_string("dir"),
&TreeId::from_bytes(dir_tree_id.as_bytes()),
)
.block_on()
Expand Down
2 changes: 1 addition & 1 deletion lib/src/local_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ impl LocalBackend {
fs::create_dir(store_path.join("conflicts")).unwrap();
let backend = Self::load(store_path);
let empty_tree_id = backend
.write_tree(&RepoPath::root(), &Tree::default())
.write_tree(RepoPath::root(), &Tree::default())
.unwrap();
assert_eq!(empty_tree_id, backend.empty_tree_id);
backend
Expand Down
Loading