Debian Codegraph @

cli/examples/custom-backend/main.rs

raw
1 // Copyright 2022 The Jujutsu Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 use std::path::Path;
16 use std::pin::Pin;
17 use std::time::SystemTime;
18
19 use async_trait::async_trait;
20 use futures::AsyncRead;
21 use futures::stream::BoxStream;
22 use jj_cli::cli_util::CliRunner;
23 use jj_cli::cli_util::CommandHelper;
24 use jj_cli::command_error::CommandError;
25 use jj_cli::ui::Ui;
26 use jj_lib::backend::Backend;
27 use jj_lib::backend::BackendInitError;
28 use jj_lib::backend::BackendLoadError;
29 use jj_lib::backend::BackendResult;
30 use jj_lib::backend::ChangeId;
31 use jj_lib::backend::Commit;
32 use jj_lib::backend::CommitId;
33 use jj_lib::backend::CopyHistory;
34 use jj_lib::backend::CopyId;
35 use jj_lib::backend::CopyRecord;
36 use jj_lib::backend::FileId;
37 use jj_lib::backend::RelatedCopy;
38 use jj_lib::backend::SigningFn;
39 use jj_lib::backend::SymlinkId;
40 use jj_lib::backend::Tree;
41 use jj_lib::backend::TreeId;
42 use jj_lib::git_backend::GitBackend;
43 use jj_lib::index::Index;
44 use jj_lib::repo::StoreFactories;
45 use jj_lib::repo_path::RepoPath;
46 use jj_lib::repo_path::RepoPathBuf;
47 use jj_lib::settings::UserSettings;
48 use jj_lib::signing::Signer;
49 use jj_lib::workspace::Workspace;
50 use jj_lib::workspace::WorkspaceInitError;
51
52 #[derive(clap::Parser, Clone, Debug)]
53 enum CustomCommand {
54 /// Initialize a workspace using the Jit backend
55 InitJit,
56 }
57
58 fn create_store_factories() -> StoreFactories {
59 let mut store_factories = StoreFactories::empty();
60 // Register the backend so it can be loaded when the repo is loaded. The name
61 // must match `Backend::name()`.
62 store_factories.add_backend(
63 "jit",
64 Box::new(|settings, store_path| Ok(Box::new(JitBackend::load(settings, store_path)?))),
65 );
66 store_factories
67 }
68
69 async fn run_custom_command(
70 ui: &mut Ui,
71 command_helper: &CommandHelper,
72 command: CustomCommand,
73 ) -> Result<(), CommandError> {
74 match command {
75 CustomCommand::InitJit => {
76 let wc_path = command_helper.cwd();
77 let settings = command_helper.settings_for_new_workspace(ui, wc_path)?.0;
78 // Initialize a workspace with the custom backend
79 Workspace::init_with_backend(
80 &settings,
81 wc_path,
82 &|settings, store_path| Ok(Box::new(JitBackend::init(settings, store_path)?)),
83 Signer::from_settings(&settings).map_err(WorkspaceInitError::SignInit)?,
84 )
85 .await?;
86 Ok(())
87 }
88 }
89 }
90
91 fn main() -> std::process::ExitCode {
92 CliRunner::init()
93 .add_store_factories(create_store_factories())
94 .add_subcommand(run_custom_command)
95 .run()
96 .into()
97 }
98
99 /// A commit backend that's extremely similar to the Git backend
100 #[derive(Debug)]
101 struct JitBackend {
102 inner: GitBackend,
103 }
104
105 impl JitBackend {
106 fn init(settings: &UserSettings, store_path: &Path) -> Result<Self, BackendInitError> {
107 let inner = GitBackend::init_internal(settings, store_path, gix::hash::Kind::default())?;
108 Ok(Self { inner })
109 }
110
111 fn load(settings: &UserSettings, store_path: &Path) -> Result<Self, BackendLoadError> {
112 let inner = GitBackend::load(settings, store_path)?;
113 Ok(Self { inner })
114 }
115 }
116
117 #[async_trait]
118 impl Backend for JitBackend {
119 fn name(&self) -> &'static str {
120 "jit"
121 }
122
123 fn commit_id_length(&self) -> usize {
124 self.inner.commit_id_length()
125 }
126
127 fn change_id_length(&self) -> usize {
128 self.inner.change_id_length()
129 }
130
131 fn root_commit_id(&self) -> &CommitId {
132 self.inner.root_commit_id()
133 }
134
135 fn root_change_id(&self) -> &ChangeId {
136 self.inner.root_change_id()
137 }
138
139 fn empty_tree_id(&self) -> &TreeId {
140 self.inner.empty_tree_id()
141 }
142
143 fn concurrency(&self) -> usize {
144 1
145 }
146
147 async fn read_file(
148 &self,
149 path: &RepoPath,
150 id: &FileId,
151 ) -> BackendResult<Pin<Box<dyn AsyncRead + Send>>> {
152 self.inner.read_file(path, id).await
153 }
154
155 async fn write_file(
156 &self,
157 path: &RepoPath,
158 contents: &mut (dyn AsyncRead + Send + Unpin),
159 ) -> BackendResult<FileId> {
160 self.inner.write_file(path, contents).await
161 }
162
163 async fn read_symlink(&self, path: &RepoPath, id: &SymlinkId) -> BackendResult<String> {
164 self.inner.read_symlink(path, id).await
165 }
166
167 async fn write_symlink(&self, path: &RepoPath, target: &str) -> BackendResult<SymlinkId> {
168 self.inner.write_symlink(path, target).await
169 }
170
171 async fn read_copy(&self, id: &CopyId) -> BackendResult<CopyHistory> {
172 self.inner.read_copy(id).await
173 }
174
175 async fn write_copy(&self, contents: &CopyHistory) -> BackendResult<CopyId> {
176 self.inner.write_copy(contents).await
177 }
178
179 async fn get_related_copies(&self, copy_id: &CopyId) -> BackendResult<Vec<RelatedCopy>> {
180 self.inner.get_related_copies(copy_id).await
181 }
182
183 async fn read_tree(&self, path: &RepoPath, id: &TreeId) -> BackendResult<Tree> {
184 self.inner.read_tree(path, id).await
185 }
186
187 async fn write_tree(&self, path: &RepoPath, contents: &Tree) -> BackendResult<TreeId> {
188 self.inner.write_tree(path, contents).await
189 }
190
191 async fn read_commit(&self, id: &CommitId) -> BackendResult<Commit> {
192 self.inner.read_commit(id).await
193 }
194
195 async fn write_commit(
196 &self,
197 contents: Commit,
198 sign_with: Option<&mut SigningFn>,
199 ) -> BackendResult<(CommitId, Commit)> {
200 self.inner.write_commit(contents, sign_with).await
201 }
202
203 fn get_copy_records(
204 &self,
205 paths: Option<&[RepoPathBuf]>,
206 root: &CommitId,
207 head: &CommitId,
208 ) -> BackendResult<BoxStream<'_, BackendResult<CopyRecord>>> {
209 self.inner.get_copy_records(paths, root, head)
210 }
211
212 fn gc(&self, index: &dyn Index, keep_newer: SystemTime) -> BackendResult<()> {
213 self.inner.gc(index, keep_newer)
214 }
215 }