2020-12-12 08:00:42 +00:00
|
|
|
// Copyright 2020 Google LLC
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2021-05-09 06:37:05 +00:00
|
|
|
use std::collections::{BTreeMap, HashMap, HashSet};
|
2020-12-12 08:00:42 +00:00
|
|
|
use std::fmt::{Debug, Error, Formatter};
|
|
|
|
|
2021-03-14 17:37:28 +00:00
|
|
|
use crate::store::{CommitId, Timestamp};
|
|
|
|
|
2020-12-12 08:00:42 +00:00
|
|
|
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Hash)]
|
|
|
|
pub struct ViewId(pub Vec<u8>);
|
|
|
|
|
|
|
|
impl Debug for ViewId {
|
|
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
|
|
|
|
f.debug_tuple("ViewId").field(&self.hex()).finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ViewId {
|
|
|
|
pub fn hex(&self) -> String {
|
|
|
|
hex::encode(&self.0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Hash)]
|
|
|
|
pub struct OperationId(pub Vec<u8>);
|
|
|
|
|
|
|
|
impl Debug for OperationId {
|
|
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
|
|
|
|
f.debug_tuple("OperationId").field(&self.hex()).finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl OperationId {
|
|
|
|
pub fn hex(&self) -> String {
|
|
|
|
hex::encode(&self.0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-11 17:58:01 +00:00
|
|
|
#[derive(PartialEq, Eq, Clone, Debug)]
|
|
|
|
pub enum RefTarget {
|
|
|
|
Normal(CommitId),
|
|
|
|
Conflict {
|
|
|
|
removes: Vec<CommitId>,
|
|
|
|
adds: Vec<CommitId>,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RefTarget {
|
|
|
|
pub fn is_conflict(&self) -> bool {
|
|
|
|
matches!(self, RefTarget::Conflict { .. })
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn adds(&self) -> Vec<CommitId> {
|
|
|
|
match self {
|
|
|
|
RefTarget::Normal(id) => {
|
|
|
|
vec![id.clone()]
|
|
|
|
}
|
|
|
|
RefTarget::Conflict { removes: _, adds } => adds.clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn has_add(&self, needle: &CommitId) -> bool {
|
|
|
|
match self {
|
|
|
|
RefTarget::Normal(id) => id == needle,
|
|
|
|
RefTarget::Conflict { removes: _, adds } => adds.contains(needle),
|
|
|
|
}
|
|
|
|
}
|
2021-07-19 06:04:21 +00:00
|
|
|
|
|
|
|
pub fn removes(&self) -> Vec<CommitId> {
|
|
|
|
match self {
|
|
|
|
RefTarget::Normal(_) => {
|
|
|
|
vec![]
|
|
|
|
}
|
|
|
|
RefTarget::Conflict { removes, adds: _ } => removes.clone(),
|
|
|
|
}
|
|
|
|
}
|
2021-07-11 17:58:01 +00:00
|
|
|
}
|
|
|
|
|
2020-12-12 08:00:42 +00:00
|
|
|
/// Represents the way the repo looks at a given time, just like how a Tree
|
|
|
|
/// object represents how the file system looks at a given time.
|
2021-07-31 00:47:30 +00:00
|
|
|
#[derive(PartialEq, Eq, Clone, Debug)]
|
2020-12-12 08:00:42 +00:00
|
|
|
pub struct View {
|
|
|
|
/// All head commits
|
|
|
|
pub head_ids: HashSet<CommitId>,
|
2021-01-16 18:42:22 +00:00
|
|
|
/// Heads of the set of public commits.
|
|
|
|
pub public_head_ids: HashSet<CommitId>,
|
2021-07-11 17:58:01 +00:00
|
|
|
pub git_refs: BTreeMap<String, RefTarget>,
|
2020-12-12 08:00:42 +00:00
|
|
|
// The commit that *should be* checked out in the (default) working copy. Note that the
|
|
|
|
// working copy (.jj/working_copy/) has the source of truth about which commit *is* checked out
|
|
|
|
// (to be precise: the commit to which we most recently completed a checkout to).
|
|
|
|
// TODO: Allow multiple working copies
|
|
|
|
pub checkout: CommitId,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl View {
|
|
|
|
pub fn new(checkout: CommitId) -> Self {
|
|
|
|
Self {
|
|
|
|
head_ids: HashSet::new(),
|
2021-01-16 18:42:22 +00:00
|
|
|
public_head_ids: HashSet::new(),
|
2021-01-03 08:26:57 +00:00
|
|
|
git_refs: BTreeMap::new(),
|
2020-12-12 08:00:42 +00:00
|
|
|
checkout,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Represents an operation (transaction) on the repo view, just like how a
|
|
|
|
/// Commit object represents an operation on the tree.
|
|
|
|
///
|
|
|
|
/// Operations and views are not meant to be exchanged between repos or users;
|
|
|
|
/// they represent local state and history.
|
|
|
|
///
|
|
|
|
/// The operation history will almost always be linear. It will only have
|
|
|
|
/// forks when parallel operations occurred. The parent is determined when
|
|
|
|
/// the transaction starts. When the transaction commits, a lock will be
|
|
|
|
/// taken and it will be checked that the current head of the operation
|
|
|
|
/// graph is unchanged. If the current head has changed, there has been
|
|
|
|
/// concurrent operation.
|
2021-07-31 00:47:30 +00:00
|
|
|
#[derive(PartialEq, Eq, Clone, Debug)]
|
2020-12-12 08:00:42 +00:00
|
|
|
pub struct Operation {
|
|
|
|
pub view_id: ViewId,
|
|
|
|
pub parents: Vec<OperationId>,
|
|
|
|
pub metadata: OperationMetadata,
|
|
|
|
}
|
|
|
|
|
2021-07-31 00:47:30 +00:00
|
|
|
#[derive(PartialEq, Eq, Clone, Debug)]
|
2020-12-12 08:00:42 +00:00
|
|
|
pub struct OperationMetadata {
|
|
|
|
pub start_time: Timestamp,
|
|
|
|
pub end_time: Timestamp,
|
|
|
|
// Whatever is useful to the user, such as exact command line call
|
|
|
|
pub description: String,
|
|
|
|
pub hostname: String,
|
|
|
|
pub username: String,
|
2021-05-09 06:37:05 +00:00
|
|
|
pub tags: HashMap<String, String>,
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl OperationMetadata {
|
2021-03-14 05:23:30 +00:00
|
|
|
pub fn new(description: String, start_time: Timestamp) -> Self {
|
|
|
|
let end_time = Timestamp::now();
|
2020-12-12 08:00:42 +00:00
|
|
|
let hostname = whoami::hostname();
|
|
|
|
let username = whoami::username();
|
|
|
|
OperationMetadata {
|
2021-03-14 05:23:30 +00:00
|
|
|
start_time,
|
|
|
|
end_time,
|
2020-12-12 08:00:42 +00:00
|
|
|
description,
|
|
|
|
hostname,
|
|
|
|
username,
|
2021-05-09 06:37:05 +00:00
|
|
|
tags: Default::default(),
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum OpStoreError {
|
|
|
|
NotFound,
|
|
|
|
Other(String),
|
|
|
|
}
|
|
|
|
|
|
|
|
pub type OpStoreResult<T> = Result<T, OpStoreError>;
|
|
|
|
|
|
|
|
pub trait OpStore: Send + Sync + Debug {
|
|
|
|
fn read_view(&self, id: &ViewId) -> OpStoreResult<View>;
|
|
|
|
|
|
|
|
fn write_view(&self, contents: &View) -> OpStoreResult<ViewId>;
|
|
|
|
|
|
|
|
fn read_operation(&self, id: &OperationId) -> OpStoreResult<Operation>;
|
|
|
|
|
|
|
|
fn write_operation(&self, contents: &Operation) -> OpStoreResult<OperationId>;
|
|
|
|
}
|