ok/jj
1
0
Fork 0
forked from mirrors/jj

copies: move CopyRecords to new copies module

Copy/rename handling is complicated. It seems worth having a module
for it. I'm going to add more content to it next.
This commit is contained in:
Martin von Zweigbergk 2024-08-17 13:31:15 -07:00 committed by Martin von Zweigbergk
parent 6101a66a76
commit fd9a236be5
8 changed files with 77 additions and 50 deletions

View file

@ -13,7 +13,7 @@
// limitations under the License.
use itertools::Itertools;
use jj_lib::backend::CopyRecords;
use jj_lib::copies::CopyRecords;
use jj_lib::repo::Repo;
use jj_lib::rewrite::merge_commit_trees;
use tracing::instrument;

View file

@ -19,8 +19,9 @@ use std::io;
use std::rc::Rc;
use itertools::Itertools as _;
use jj_lib::backend::{BackendResult, ChangeId, CommitId, CopyRecords};
use jj_lib::backend::{BackendResult, ChangeId, CommitId};
use jj_lib::commit::Commit;
use jj_lib::copies::CopyRecords;
use jj_lib::extensions_map::ExtensionsMap;
use jj_lib::fileset::{self, FilesetExpression};
use jj_lib::git;

View file

@ -20,11 +20,12 @@ use std::{io, mem};
use futures::StreamExt;
use itertools::Itertools;
use jj_lib::backend::{BackendError, CopyRecords, TreeValue};
use jj_lib::backend::{BackendError, TreeValue};
use jj_lib::commit::Commit;
use jj_lib::conflicts::{
materialized_diff_stream, MaterializedTreeDiffEntry, MaterializedTreeValue,
};
use jj_lib::copies::CopyRecords;
use jj_lib::diff::{Diff, DiffHunk};
use jj_lib::files::{DiffLine, DiffLineHunkSide, DiffLineIterator, DiffLineNumber};
use jj_lib::matchers::Matcher;

View file

@ -15,13 +15,12 @@
#![allow(missing_docs)]
use std::any::Any;
use std::collections::{BTreeMap, HashMap};
use std::collections::BTreeMap;
use std::fmt::Debug;
use std::io::Read;
use std::time::SystemTime;
use async_trait::async_trait;
use futures::executor::block_on_stream;
use futures::stream::BoxStream;
use thiserror::Error;
@ -181,49 +180,6 @@ pub struct CopyRecord {
pub source_commit: CommitId,
}
/// A collection of CopyRecords.
#[derive(Default, Debug)]
pub struct CopyRecords {
records: Vec<CopyRecord>,
// Maps from `target` to the index of the target in `records`. Conflicts
// are excluded by keeping an out of range value.
targets: HashMap<RepoPathBuf, usize>,
}
impl CopyRecords {
/// Adds information about a stream of CopyRecords to `self`. A target with
/// multiple conflicts is discarded and treated as not having an origin.
pub fn add_records(
&mut self,
stream: BoxStream<BackendResult<CopyRecord>>,
) -> BackendResult<()> {
for record in block_on_stream(stream) {
let r = record?;
let value = self
.targets
.entry(r.target.clone())
.or_insert(self.records.len());
if *value != self.records.len() {
// TODO: handle conflicts instead of ignoring both sides.
*value = usize::MAX;
}
self.records.push(r);
}
Ok(())
}
/// Gets any copy record associated with a target path.
pub fn for_target(&self, target: &RepoPath) -> Option<&CopyRecord> {
self.targets.get(target).and_then(|&i| self.records.get(i))
}
/// Gets all copy records.
pub fn iter(&self) -> impl Iterator<Item = &CopyRecord> + '_ {
self.records.iter()
}
}
/// Error that may occur during backend initialization.
#[derive(Debug, Error)]
#[error(transparent)]

66
lib/src/copies.rs Normal file
View file

@ -0,0 +1,66 @@
// Copyright 2024 The Jujutsu Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! Code for working with copies and renames.
use std::collections::HashMap;
use futures::executor::block_on_stream;
use futures::stream::BoxStream;
use crate::backend::{BackendResult, CopyRecord};
use crate::repo_path::{RepoPath, RepoPathBuf};
/// A collection of CopyRecords.
#[derive(Default, Debug)]
pub struct CopyRecords {
records: Vec<CopyRecord>,
// Maps from `target` to the index of the target in `records`. Conflicts
// are excluded by keeping an out of range value.
targets: HashMap<RepoPathBuf, usize>,
}
impl CopyRecords {
/// Adds information about a stream of CopyRecords to `self`. A target with
/// multiple conflicts is discarded and treated as not having an origin.
pub fn add_records(
&mut self,
stream: BoxStream<BackendResult<CopyRecord>>,
) -> BackendResult<()> {
for record in block_on_stream(stream) {
let r = record?;
let value = self
.targets
.entry(r.target.clone())
.or_insert(self.records.len());
if *value != self.records.len() {
// TODO: handle conflicts instead of ignoring both sides.
*value = usize::MAX;
}
self.records.push(r);
}
Ok(())
}
/// Gets any copy record associated with a target path.
pub fn for_target(&self, target: &RepoPath) -> Option<&CopyRecord> {
self.targets.get(target).and_then(|&i| self.records.get(i))
}
/// Gets all copy records.
pub fn iter(&self) -> impl Iterator<Item = &CopyRecord> + '_ {
self.records.iter()
}
}

View file

@ -32,6 +32,7 @@ pub mod backend;
pub mod commit;
pub mod commit_builder;
pub mod conflicts;
pub mod copies;
pub mod dag_walk;
pub mod default_index;
pub mod default_submodule_store;

View file

@ -30,7 +30,8 @@ use futures::{Stream, TryStreamExt};
use itertools::{EitherOrBoth, Itertools};
use crate::backend;
use crate::backend::{BackendResult, CopyRecord, CopyRecords, MergedTreeId, TreeId, TreeValue};
use crate::backend::{BackendResult, CopyRecord, MergedTreeId, TreeId, TreeValue};
use crate::copies::CopyRecords;
use crate::matchers::{EverythingMatcher, Matcher};
use crate::merge::{Merge, MergeBuilder, MergedTreeVal, MergedTreeValue};
use crate::repo_path::{RepoPath, RepoPathBuf, RepoPathComponent};

View file

@ -14,7 +14,8 @@
use futures::StreamExt;
use itertools::Itertools;
use jj_lib::backend::{CommitId, CopyRecord, CopyRecords, FileId, MergedTreeId, TreeValue};
use jj_lib::backend::{CommitId, CopyRecord, FileId, MergedTreeId, TreeValue};
use jj_lib::copies::CopyRecords;
use jj_lib::files::MergeResult;
use jj_lib::matchers::{EverythingMatcher, FilesMatcher, Matcher, PrefixMatcher};
use jj_lib::merge::{Merge, MergeBuilder, MergedTreeValue};