1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
use std::collections::{BTreeMap, HashMap, HashSet};
use std::fmt::{Debug, Error, Formatter};
use crate::backend::{CommitId, Timestamp};
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Hash)]
pub struct WorkspaceId(String);
impl Debug for WorkspaceId {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
f.debug_tuple("WorkspaceId").field(&self.0).finish()
}
}
impl WorkspaceId {
pub fn new(value: String) -> Self {
Self(value)
}
pub fn default() -> Self {
Self("default".to_string())
}
pub fn as_str(&self) -> &str {
&self.0
}
}
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Hash)]
pub struct ViewId(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 new(value: Vec<u8>) -> Self {
Self(value)
}
pub fn from_hex(hex: &str) -> Self {
Self(hex::decode(hex).unwrap())
}
pub fn as_bytes(&self) -> &[u8] {
&self.0
}
pub fn to_bytes(&self) -> Vec<u8> {
self.0.clone()
}
pub fn hex(&self) -> String {
hex::encode(&self.0)
}
}
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Hash)]
pub struct OperationId(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 new(value: Vec<u8>) -> Self {
Self(value)
}
pub fn from_hex(hex: &str) -> Self {
Self(hex::decode(hex).unwrap())
}
pub fn as_bytes(&self) -> &[u8] {
&self.0
}
pub fn to_bytes(&self) -> Vec<u8> {
self.0.clone()
}
pub fn hex(&self) -> String {
hex::encode(&self.0)
}
}
#[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),
}
}
pub fn removes(&self) -> Vec<CommitId> {
match self {
RefTarget::Normal(_) => {
vec![]
}
RefTarget::Conflict { removes, adds: _ } => removes.clone(),
}
}
}
#[derive(Default, PartialEq, Eq, Clone, Debug)]
pub struct BranchTarget {
pub local_target: Option<RefTarget>,
pub remote_targets: BTreeMap<String, RefTarget>,
}
#[derive(PartialEq, Eq, Clone, Debug, Default)]
pub struct View {
pub head_ids: HashSet<CommitId>,
pub public_head_ids: HashSet<CommitId>,
pub branches: BTreeMap<String, BranchTarget>,
pub tags: BTreeMap<String, RefTarget>,
pub git_refs: BTreeMap<String, RefTarget>,
pub git_head: Option<CommitId>,
pub checkouts: HashMap<WorkspaceId, CommitId>,
}
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct Operation {
pub view_id: ViewId,
pub parents: Vec<OperationId>,
pub metadata: OperationMetadata,
}
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct OperationMetadata {
pub start_time: Timestamp,
pub end_time: Timestamp,
pub description: String,
pub hostname: String,
pub username: String,
pub tags: HashMap<String, String>,
}
impl OperationMetadata {
pub fn new(description: String, start_time: Timestamp) -> Self {
let end_time = Timestamp::now();
let hostname = whoami::hostname();
let username = whoami::username();
OperationMetadata {
start_time,
end_time,
description,
hostname,
username,
tags: Default::default(),
}
}
}
#[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>;
}