···1515#![allow(missing_docs)]
16161717use std::any::Any;
1818-use std::collections::{BTreeMap, HashMap};
1818+use std::collections::BTreeMap;
1919use std::fmt::Debug;
2020use std::io::Read;
2121use std::time::SystemTime;
22222323use async_trait::async_trait;
2424-use futures::executor::block_on_stream;
2524use futures::stream::BoxStream;
2625use thiserror::Error;
2726···179178 /// It is required that the commit id is an ancestor of the commit with
180179 /// which this copy source is associated.
181180 pub source_commit: CommitId,
182182-}
183183-184184-/// A collection of CopyRecords.
185185-#[derive(Default, Debug)]
186186-pub struct CopyRecords {
187187- records: Vec<CopyRecord>,
188188- // Maps from `target` to the index of the target in `records`. Conflicts
189189- // are excluded by keeping an out of range value.
190190- targets: HashMap<RepoPathBuf, usize>,
191191-}
192192-193193-impl CopyRecords {
194194- /// Adds information about a stream of CopyRecords to `self`. A target with
195195- /// multiple conflicts is discarded and treated as not having an origin.
196196- pub fn add_records(
197197- &mut self,
198198- stream: BoxStream<BackendResult<CopyRecord>>,
199199- ) -> BackendResult<()> {
200200- for record in block_on_stream(stream) {
201201- let r = record?;
202202- let value = self
203203- .targets
204204- .entry(r.target.clone())
205205- .or_insert(self.records.len());
206206-207207- if *value != self.records.len() {
208208- // TODO: handle conflicts instead of ignoring both sides.
209209- *value = usize::MAX;
210210- }
211211- self.records.push(r);
212212- }
213213- Ok(())
214214- }
215215-216216- /// Gets any copy record associated with a target path.
217217- pub fn for_target(&self, target: &RepoPath) -> Option<&CopyRecord> {
218218- self.targets.get(target).and_then(|&i| self.records.get(i))
219219- }
220220-221221- /// Gets all copy records.
222222- pub fn iter(&self) -> impl Iterator<Item = &CopyRecord> + '_ {
223223- self.records.iter()
224224- }
225181}
226182227183/// Error that may occur during backend initialization.
+66
lib/src/copies.rs
···11+// Copyright 2024 The Jujutsu Authors
22+//
33+// Licensed under the Apache License, Version 2.0 (the "License");
44+// you may not use this file except in compliance with the License.
55+// You may obtain a copy of the License at
66+//
77+// https://www.apache.org/licenses/LICENSE-2.0
88+//
99+// Unless required by applicable law or agreed to in writing, software
1010+// distributed under the License is distributed on an "AS IS" BASIS,
1111+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212+// See the License for the specific language governing permissions and
1313+// limitations under the License.
1414+1515+//! Code for working with copies and renames.
1616+1717+use std::collections::HashMap;
1818+1919+use futures::executor::block_on_stream;
2020+use futures::stream::BoxStream;
2121+2222+use crate::backend::{BackendResult, CopyRecord};
2323+use crate::repo_path::{RepoPath, RepoPathBuf};
2424+2525+/// A collection of CopyRecords.
2626+#[derive(Default, Debug)]
2727+pub struct CopyRecords {
2828+ records: Vec<CopyRecord>,
2929+ // Maps from `target` to the index of the target in `records`. Conflicts
3030+ // are excluded by keeping an out of range value.
3131+ targets: HashMap<RepoPathBuf, usize>,
3232+}
3333+3434+impl CopyRecords {
3535+ /// Adds information about a stream of CopyRecords to `self`. A target with
3636+ /// multiple conflicts is discarded and treated as not having an origin.
3737+ pub fn add_records(
3838+ &mut self,
3939+ stream: BoxStream<BackendResult<CopyRecord>>,
4040+ ) -> BackendResult<()> {
4141+ for record in block_on_stream(stream) {
4242+ let r = record?;
4343+ let value = self
4444+ .targets
4545+ .entry(r.target.clone())
4646+ .or_insert(self.records.len());
4747+4848+ if *value != self.records.len() {
4949+ // TODO: handle conflicts instead of ignoring both sides.
5050+ *value = usize::MAX;
5151+ }
5252+ self.records.push(r);
5353+ }
5454+ Ok(())
5555+ }
5656+5757+ /// Gets any copy record associated with a target path.
5858+ pub fn for_target(&self, target: &RepoPath) -> Option<&CopyRecord> {
5959+ self.targets.get(target).and_then(|&i| self.records.get(i))
6060+ }
6161+6262+ /// Gets all copy records.
6363+ pub fn iter(&self) -> impl Iterator<Item = &CopyRecord> + '_ {
6464+ self.records.iter()
6565+ }
6666+}
+1
lib/src/lib.rs
···3232pub mod commit;
3333pub mod commit_builder;
3434pub mod conflicts;
3535+pub mod copies;
3536pub mod dag_walk;
3637pub mod default_index;
3738pub mod default_submodule_store;