Skip to content

Commit

Permalink
Switch to ignore crate for gitignore handling.
Browse files Browse the repository at this point in the history
Co-authored-by: Waleed Khan <[email protected]>
  • Loading branch information
daehyeok and arxanas committed Feb 16, 2024
1 parent ce295f8 commit 71ae793
Show file tree
Hide file tree
Showing 9 changed files with 173 additions and 205 deletions.
30 changes: 30 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ gix = { version = "0.58.0", default-features = false, features = [
] }
glob = "0.3.1"
hex = "0.4.3"
ignore = "0.4.20"
indexmap = "2.2.3"
insta = { version = "1.34.0", features = ["filters"] }
itertools = "0.12.1"
Expand Down
2 changes: 1 addition & 1 deletion cli/examples/custom-working-copy/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ impl LockedWorkingCopy for LockedConflictsWorkingCopy {
}

fn snapshot(&mut self, mut options: SnapshotOptions) -> Result<MergedTreeId, SnapshotError> {
options.base_ignores = options.base_ignores.chain("", "/.conflicts".as_bytes());
options.base_ignores = options.base_ignores.chain("", "/.conflicts".as_bytes())?;
self.inner.snapshot(options)
}

Expand Down
22 changes: 14 additions & 8 deletions cli/src/cli_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use jj_lib::backend::{BackendError, ChangeId, CommitId, MergedTreeId};
use jj_lib::commit::Commit;
use jj_lib::git::{GitConfigParseError, GitExportError, GitImportError, GitRemoteManagementError};
use jj_lib::git_backend::GitBackend;
use jj_lib::gitignore::GitIgnoreFile;
use jj_lib::gitignore::{GitIgnoreError, GitIgnoreFile};
use jj_lib::hex_util::to_reverse_hex;
use jj_lib::id_prefix::IdPrefixContext;
use jj_lib::matchers::{EverythingMatcher, Matcher, PrefixMatcher};
Expand Down Expand Up @@ -481,6 +481,12 @@ impl From<WorkingCopyStateError> for CommandError {
}
}

impl From<GitIgnoreError> for CommandError {
fn from(err: GitIgnoreError) -> Self {
user_error(format!("Failed to process .gitignore: {err}"))
}
}

#[derive(Clone)]
struct ChromeTracingFlushGuard {
_inner: Option<Rc<tracing_chrome::FlushGuard>>,
Expand Down Expand Up @@ -1047,7 +1053,7 @@ impl WorkspaceCommandHelper {
}

#[instrument(skip_all)]
pub fn base_ignores(&self) -> Arc<GitIgnoreFile> {
pub fn base_ignores(&self) -> Result<Arc<GitIgnoreFile>, GitIgnoreError> {
fn get_excludes_file_path(config: &gix::config::File) -> Option<PathBuf> {
// TODO: maybe use path_by_key() and interpolate(), which can process non-utf-8
// path on Unix.
Expand All @@ -1073,16 +1079,16 @@ impl WorkspaceCommandHelper {
if let Some(git_backend) = self.git_backend() {
let git_repo = git_backend.git_repo();
if let Some(excludes_file_path) = get_excludes_file_path(&git_repo.config_snapshot()) {
git_ignores = git_ignores.chain_with_file("", excludes_file_path);
git_ignores = git_ignores.chain_with_file("", excludes_file_path)?;
}
git_ignores = git_ignores
.chain_with_file("", git_backend.git_repo_path().join("info").join("exclude"));
.chain_with_file("", git_backend.git_repo_path().join("info").join("exclude"))?;
} else if let Ok(git_config) = gix::config::File::from_globals() {
if let Some(excludes_file_path) = get_excludes_file_path(&git_config) {
git_ignores = git_ignores.chain_with_file("", excludes_file_path);
git_ignores = git_ignores.chain_with_file("", excludes_file_path)?;
}
}
git_ignores
Ok(git_ignores)
}

pub fn resolve_single_op(&self, op_str: &str) -> Result<Operation, OpsetEvaluationError> {
Expand Down Expand Up @@ -1353,7 +1359,7 @@ Set which revision the branch points to with `jj branch set {branch_name} -r <RE
// committing the working copy.
return Ok(());
};
let base_ignores = self.base_ignores();
let base_ignores = self.base_ignores()?;

// Compare working-copy tree and operation with repo's, and reload as needed.
let mut locked_ws = self.workspace.start_working_copy_mutation()?;
Expand Down Expand Up @@ -1729,7 +1735,7 @@ impl WorkspaceCommandTransaction<'_> {
matcher: &dyn Matcher,
instructions: &str,
) -> Result<MergedTreeId, CommandError> {
let base_ignores = self.helper.base_ignores();
let base_ignores = self.helper.base_ignores()?;
let settings = &self.helper.settings;
Ok(crate::merge_tools::edit_diff(
ui,
Expand Down
2 changes: 1 addition & 1 deletion cli/src/commands/untrack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub(crate) fn cmd_untrack(
let matcher = workspace_command.matcher_from_values(&args.paths)?;

let mut tx = workspace_command.start_transaction().into_inner();
let base_ignores = workspace_command.base_ignores();
let base_ignores = workspace_command.base_ignores()?;
let (mut locked_ws, wc_commit) = workspace_command.start_working_copy_mutation()?;
// Create a new tree without the unwanted files
let mut tree_builder = MergedTreeBuilder::new(wc_commit.tree_id().clone());
Expand Down
1 change: 1 addition & 0 deletions lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ git2 = { workspace = true }
gix = { workspace = true }
glob = { workspace = true }
hex = { workspace = true }
ignore = { workspace = true }
itertools = { workspace = true }
maplit = { workspace = true }
once_cell = { workspace = true }
Expand Down
Loading

0 comments on commit 71ae793

Please sign in to comment.