Skip to content

Commit

Permalink
object_id: extract ObjectId trait and macros to separate module
Browse files Browse the repository at this point in the history
I'm going to add a prefix resolution method to OpStore, but OpStore is
unrelated to the index. I think ObjectId, HexPrefix, and PrefixResolution can
be extracted to this module.
  • Loading branch information
yuja committed Jan 5, 2024
1 parent dbaee19 commit fa5e407
Show file tree
Hide file tree
Showing 62 changed files with 187 additions and 132 deletions.
3 changes: 2 additions & 1 deletion cli/src/cli_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use clap::builder::{NonEmptyStringValueParser, TypedValueParser, ValueParserFact
use clap::{Arg, ArgAction, ArgMatches, Command, FromArgMatches};
use indexmap::{IndexMap, IndexSet};
use itertools::Itertools;
use jj_lib::backend::{BackendError, ChangeId, CommitId, MergedTreeId, ObjectId};
use jj_lib::backend::{BackendError, ChangeId, CommitId, MergedTreeId};
use jj_lib::commit::Commit;
use jj_lib::git::{
FailedRefExport, FailedRefExportReason, GitConfigParseError, GitExportError, GitImportError,
Expand All @@ -42,6 +42,7 @@ use jj_lib::hex_util::to_reverse_hex;
use jj_lib::id_prefix::IdPrefixContext;
use jj_lib::matchers::{EverythingMatcher, Matcher, PrefixMatcher};
use jj_lib::merged_tree::MergedTree;
use jj_lib::object_id::ObjectId;
use jj_lib::op_heads_store::{self, OpHeadResolutionError};
use jj_lib::op_store::{OpStoreError, OperationId, WorkspaceId};
use jj_lib::op_walk::OpsetEvaluationError;
Expand Down
2 changes: 1 addition & 1 deletion cli/src/commands/abandon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

use std::io::Write;

use jj_lib::backend::ObjectId;
use jj_lib::object_id::ObjectId;
use tracing::instrument;

use crate::cli_util::{
Expand Down
2 changes: 1 addition & 1 deletion cli/src/commands/backout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use jj_lib::backend::ObjectId;
use jj_lib::object_id::ObjectId;
use jj_lib::rewrite::back_out_commit;
use tracing::instrument;

Expand Down
3 changes: 2 additions & 1 deletion cli/src/commands/branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ use std::str::FromStr;

use clap::builder::NonEmptyStringValueParser;
use itertools::Itertools;
use jj_lib::backend::{CommitId, ObjectId};
use jj_lib::backend::CommitId;
use jj_lib::git;
use jj_lib::object_id::ObjectId;
use jj_lib::op_store::{RefTarget, RemoteRef};
use jj_lib::repo::Repo;
use jj_lib::revset::{self, RevsetExpression};
Expand Down
2 changes: 1 addition & 1 deletion cli/src/commands/checkout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use jj_lib::backend::ObjectId;
use jj_lib::object_id::ObjectId;
use tracing::instrument;

use crate::cli_util::{join_message_paragraphs, CommandError, CommandHelper, RevisionArg};
Expand Down
3 changes: 2 additions & 1 deletion cli/src/commands/chmod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
// limitations under the License.

use itertools::Itertools;
use jj_lib::backend::{ObjectId, TreeValue};
use jj_lib::backend::TreeValue;
use jj_lib::merged_tree::MergedTreeBuilder;
use jj_lib::object_id::ObjectId;
use tracing::instrument;

use crate::cli_util::{user_error, CommandError, CommandHelper, RevisionArg};
Expand Down
2 changes: 1 addition & 1 deletion cli/src/commands/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use jj_lib::backend::ObjectId;
use jj_lib::object_id::ObjectId;
use jj_lib::repo::Repo;
use jj_lib::rewrite::merge_commit_trees;
use tracing::instrument;
Expand Down
2 changes: 1 addition & 1 deletion cli/src/commands/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ use std::fmt::Debug;
use std::io::Write as _;

use clap::Subcommand;
use jj_lib::backend::ObjectId;
use jj_lib::default_index::{AsCompositeIndex as _, DefaultIndexStore, DefaultReadonlyIndex};
use jj_lib::local_working_copy::LocalWorkingCopy;
use jj_lib::object_id::ObjectId;
use jj_lib::repo::Repo;
use jj_lib::working_copy::WorkingCopy;
use jj_lib::{op_walk, revset};
Expand Down
2 changes: 1 addition & 1 deletion cli/src/commands/describe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

use std::io::{self, Read, Write};

use jj_lib::backend::ObjectId;
use jj_lib::object_id::ObjectId;
use tracing::instrument;

use crate::cli_util::{join_message_paragraphs, CommandError, CommandHelper, RevisionArg};
Expand Down
2 changes: 1 addition & 1 deletion cli/src/commands/diffedit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

use std::io::Write;

use jj_lib::backend::ObjectId;
use jj_lib::matchers::EverythingMatcher;
use jj_lib::object_id::ObjectId;
use jj_lib::rewrite::merge_commit_trees;
use tracing::instrument;

Expand Down
2 changes: 1 addition & 1 deletion cli/src/commands/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

use std::io::Write;

use jj_lib::backend::ObjectId;
use jj_lib::object_id::ObjectId;
use tracing::instrument;

use crate::cli_util::{CommandError, CommandHelper, RevisionArg};
Expand Down
3 changes: 2 additions & 1 deletion cli/src/commands/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ use std::{fmt, fs, io};

use clap::{ArgGroup, Subcommand};
use itertools::Itertools;
use jj_lib::backend::{ObjectId, TreeValue};
use jj_lib::backend::TreeValue;
use jj_lib::git::{
self, parse_gitmodules, GitBranchPushTargets, GitFetchError, GitFetchStats, GitPushError,
};
use jj_lib::git_backend::GitBackend;
use jj_lib::object_id::ObjectId;
use jj_lib::op_store::RefTarget;
use jj_lib::refs::{
classify_branch_push_action, BranchPushAction, BranchPushUpdate, TrackingRefPair,
Expand Down
2 changes: 1 addition & 1 deletion cli/src/commands/move.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.

use clap::ArgGroup;
use jj_lib::backend::ObjectId;
use jj_lib::object_id::ObjectId;
use jj_lib::repo::Repo;
use jj_lib::rewrite::merge_commit_trees;
use tracing::instrument;
Expand Down
2 changes: 1 addition & 1 deletion cli/src/commands/operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use std::slice;

use clap::Subcommand;
use itertools::Itertools as _;
use jj_lib::backend::ObjectId;
use jj_lib::object_id::ObjectId;
use jj_lib::op_store::OperationId;
use jj_lib::op_walk;
use jj_lib::repo::Repo;
Expand Down
3 changes: 2 additions & 1 deletion cli/src/commands/rebase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ use std::sync::Arc;
use clap::ArgGroup;
use indexmap::IndexSet;
use itertools::Itertools;
use jj_lib::backend::{CommitId, ObjectId};
use jj_lib::backend::CommitId;
use jj_lib::commit::Commit;
use jj_lib::object_id::ObjectId;
use jj_lib::repo::{ReadonlyRepo, Repo};
use jj_lib::revset::{RevsetExpression, RevsetIteratorExt};
use jj_lib::rewrite::{rebase_commit, rebase_commit_with_options, EmptyBehaviour, RebaseOptions};
Expand Down
3 changes: 2 additions & 1 deletion cli/src/commands/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ use std::collections::BTreeMap;
use std::io::Write;

use itertools::Itertools;
use jj_lib::backend::{ObjectId, TreeValue};
use jj_lib::backend::TreeValue;
use jj_lib::merge::MergedTreeValue;
use jj_lib::object_id::ObjectId;
use jj_lib::repo_path::RepoPathBuf;
use tracing::instrument;

Expand Down
2 changes: 1 addition & 1 deletion cli/src/commands/restore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

use std::io::Write;

use jj_lib::backend::ObjectId;
use jj_lib::object_id::ObjectId;
use jj_lib::rewrite::{merge_commit_trees, restore_tree};
use tracing::instrument;

Expand Down
2 changes: 1 addition & 1 deletion cli/src/commands/split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.
use std::io::Write;

use jj_lib::backend::ObjectId;
use jj_lib::object_id::ObjectId;
use jj_lib::repo::Repo;
use jj_lib::rewrite::merge_commit_trees;
use tracing::instrument;
Expand Down
2 changes: 1 addition & 1 deletion cli/src/commands/squash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.

use clap::parser::ValueSource;
use jj_lib::backend::ObjectId;
use jj_lib::object_id::ObjectId;
use jj_lib::revset;
use tracing::instrument;

Expand Down
2 changes: 1 addition & 1 deletion cli/src/commands/unsquash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use jj_lib::backend::ObjectId;
use jj_lib::matchers::EverythingMatcher;
use jj_lib::object_id::ObjectId;
use jj_lib::rewrite::merge_commit_trees;
use tracing::instrument;

Expand Down
2 changes: 1 addition & 1 deletion cli/src/commands/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ use std::io::Write;

use clap::Subcommand;
use itertools::Itertools;
use jj_lib::backend::ObjectId;
use jj_lib::file_util;
use jj_lib::object_id::ObjectId;
use jj_lib::op_store::WorkspaceId;
use jj_lib::repo::Repo;
use jj_lib::rewrite::merge_commit_trees;
Expand Down
3 changes: 2 additions & 1 deletion cli/src/commit_templater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ use std::io;
use std::rc::Rc;

use itertools::Itertools as _;
use jj_lib::backend::{ChangeId, CommitId, ObjectId as _};
use jj_lib::backend::{ChangeId, CommitId};
use jj_lib::commit::Commit;
use jj_lib::hex_util::to_reverse_hex;
use jj_lib::id_prefix::IdPrefixContext;
use jj_lib::object_id::ObjectId as _;
use jj_lib::op_store::{RefTarget, WorkspaceId};
use jj_lib::repo::Repo;
use jj_lib::{git, rewrite};
Expand Down
3 changes: 2 additions & 1 deletion cli/src/diff_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ use std::ops::Range;

use futures::{try_join, Stream, StreamExt};
use itertools::Itertools;
use jj_lib::backend::{BackendResult, ObjectId, TreeValue};
use jj_lib::backend::{BackendResult, TreeValue};
use jj_lib::commit::Commit;
use jj_lib::conflicts::{materialize_tree_value, MaterializedTreeValue};
use jj_lib::diff::{Diff, DiffHunk};
use jj_lib::files::DiffLine;
use jj_lib::matchers::Matcher;
use jj_lib::merge::MergedTreeValue;
use jj_lib::merged_tree::{MergedTree, TreeDiffStream};
use jj_lib::object_id::ObjectId;
use jj_lib::repo::Repo;
use jj_lib::repo_path::{RepoPath, RepoPathBuf};
use jj_lib::settings::{ConfigResultExt as _, UserSettings};
Expand Down
3 changes: 2 additions & 1 deletion cli/src/merge_tools/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ use std::sync::Arc;

use futures::{StreamExt, TryStreamExt};
use itertools::Itertools;
use jj_lib::backend::{BackendError, FileId, MergedTreeId, ObjectId, TreeValue};
use jj_lib::backend::{BackendError, FileId, MergedTreeId, TreeValue};
use jj_lib::diff::{find_line_ranges, Diff, DiffHunk};
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::object_id::ObjectId;
use jj_lib::repo_path::{RepoPath, RepoPathBuf};
use jj_lib::store::Store;
use pollster::FutureExt;
Expand Down
2 changes: 1 addition & 1 deletion cli/src/operation_templater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use std::io;

use itertools::Itertools as _;
use jj_lib::backend::ObjectId;
use jj_lib::object_id::ObjectId;
use jj_lib::op_store::{OperationId, OperationMetadata};
use jj_lib::operation::Operation;
use jj_lib::repo::ReadonlyRepo;
Expand Down
3 changes: 2 additions & 1 deletion cli/tests/test_git_import_export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
use std::path::Path;

use itertools::Itertools as _;
use jj_lib::backend::{CommitId, ObjectId as _};
use jj_lib::backend::CommitId;
use jj_lib::object_id::ObjectId as _;

use crate::common::TestEnvironment;

Expand Down
67 changes: 1 addition & 66 deletions lib/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,75 +26,10 @@ use thiserror::Error;

use crate::content_hash::ContentHash;
use crate::merge::Merge;
use crate::object_id::{id_type, ObjectId};
use crate::repo_path::{RepoPath, RepoPathComponent, RepoPathComponentBuf};
use crate::signing::SignResult;

pub trait ObjectId {
fn new(value: Vec<u8>) -> Self;
fn object_type(&self) -> String;
fn from_bytes(bytes: &[u8]) -> Self;
fn as_bytes(&self) -> &[u8];
fn to_bytes(&self) -> Vec<u8>;
fn from_hex(hex: &str) -> Self;
fn hex(&self) -> String;
}

macro_rules! id_type {
($vis:vis $name:ident) => {
content_hash! {
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Hash)]
$vis struct $name(Vec<u8>);
}
$crate::backend::impl_id_type!($name);
};
}

macro_rules! impl_id_type {
($name:ident) => {
impl std::fmt::Debug for $name {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
f.debug_tuple(stringify!($name)).field(&self.hex()).finish()
}
}

impl crate::backend::ObjectId for $name {
fn new(value: Vec<u8>) -> Self {
Self(value)
}

fn object_type(&self) -> String {
stringify!($name)
.strip_suffix("Id")
.unwrap()
.to_ascii_lowercase()
.to_string()
}

fn from_bytes(bytes: &[u8]) -> Self {
Self(bytes.to_vec())
}

fn as_bytes(&self) -> &[u8] {
&self.0
}

fn to_bytes(&self) -> Vec<u8> {
self.0.clone()
}

fn from_hex(hex: &str) -> Self {
Self(hex::decode(hex).unwrap())
}

fn hex(&self) -> String {
hex::encode(&self.0)
}
}
};
}

pub(crate) use {id_type, impl_id_type};

id_type!(pub CommitId);
id_type!(pub ChangeId);
id_type!(pub TreeId);
Expand Down
3 changes: 2 additions & 1 deletion lib/src/default_index/composite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ use super::entry::{
};
use super::readonly::ReadonlyIndexSegment;
use super::rev_walk::RevWalk;
use crate::backend::{ChangeId, CommitId, ObjectId};
use crate::backend::{ChangeId, CommitId};
use crate::index::{HexPrefix, Index, PrefixResolution};
use crate::object_id::ObjectId;
use crate::revset::{ResolvedExpression, Revset, RevsetEvaluationError};
use crate::store::Store;
use crate::{default_revset_engine, hex_util};
Expand Down
3 changes: 2 additions & 1 deletion lib/src/default_index/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ use std::hash::{Hash, Hasher};
use smallvec::SmallVec;

use super::composite::{CompositeIndex, IndexSegment};
use crate::backend::{ChangeId, CommitId, ObjectId};
use crate::backend::{ChangeId, CommitId};
use crate::object_id::ObjectId;

/// Global index position.
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash)]
Expand Down
3 changes: 2 additions & 1 deletion lib/src/default_index/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ mod tests {
use super::entry::SmallIndexPositionsVec;
use super::mutable::MutableIndexSegment;
use super::*;
use crate::backend::{ChangeId, CommitId, ObjectId};
use crate::backend::{ChangeId, CommitId};
use crate::index::{HexPrefix, Index, PrefixResolution};
use crate::object_id::ObjectId;

/// Generator of unique 16-byte ChangeId excluding root id
fn change_id_generator() -> impl FnMut() -> ChangeId {
Expand Down
Loading

0 comments on commit fa5e407

Please sign in to comment.