pijul_org / pijul
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
use clap;
use clap::ArgMatches;
pub type StaticSubcommand = clap::App<'static, 'static>;
mod ask;
mod fs_operation;
pub mod remote;
mod ssh_auth_attempts;
pub mod add;
pub mod apply;
pub mod branches;
pub mod checkout;
pub mod clone;
pub mod credit;
pub mod dependencies;
pub mod diff;
pub mod dist;
pub mod fork;
pub mod generate_completions;
pub mod grep;
pub mod hooks;
pub mod info;
pub mod init;
pub mod key;
pub mod log;
pub mod ls;
pub mod mv;
pub mod patch;
pub mod prune;
pub mod pull;
pub mod push;
pub mod record;
pub mod remote_command;
pub mod remove;
pub mod revert;
pub mod rollback;
pub mod sign;
pub mod status;
pub mod tag;
pub mod unrecord;
mod fold_until;
use error::Error;
use libpijul::fs_representation::{RepoPath, RepoRoot};
use libpijul::Hash;
use libpijul::{fs_representation, Inode, Repository, Txn, DEFAULT_BRANCH};
use rand;
use std::borrow::Cow;
use std::env::current_dir;
use std::env::var;
use std::fs::{canonicalize, create_dir, metadata};
use std::io::{stderr, Write};
use std::path::{Path, PathBuf};
use std::process::exit;
pub fn all_command_invocations() -> Vec<StaticSubcommand> {
return vec![
log::invocation(),
info::invocation(),
init::invocation(),
record::invocation(),
unrecord::invocation(),
add::invocation(),
pull::invocation(),
push::invocation(),
apply::invocation(),
clone::invocation(),
remove::invocation(),
mv::invocation(),
ls::invocation(),
revert::invocation(),
patch::invocation(),
fork::invocation(),
branches::invocation(),
prune::invocation(),
checkout::invocation(),
diff::invocation(),
credit::invocation(),
dist::invocation(),
key::invocation(),
rollback::invocation(),
status::invocation(),
dependencies::invocation(),
tag::invocation(),
sign::invocation(),
generate_completions::invocation(),
grep::invocation(),
remote_command::invocation(),
];
}
pub fn get_wd(repository_path: Option<&Path>) -> Result<PathBuf, Error> {
debug!("get_wd: {:?}", repository_path);
match repository_path {
None => Ok(canonicalize(current_dir()?)?),
Some(a) if a.is_relative() => Ok(canonicalize(current_dir()?.join(a))?),
Some(a) => Ok(canonicalize(a)?),
}
}
/// Returns an error if the `dir` is contained in a repository.
pub fn assert_no_containing_repo(dir: &Path) -> Result<(), Error> {
if metadata(dir).is_ok() {
if fs_representation::find_repo_root(&canonicalize(dir)?).is_some() {
return Err(Error::InARepository {
path: dir.to_owned(),
});
}
}
Ok(())
}
/// Creates an empty pijul repository in the given directory.
pub fn create_repo(dir: &Path) -> Result<(), Error> {
// Check that a repository does not already exist.
if metadata(dir).is_err() {
create_dir(dir)?;
}
let dir = canonicalize(dir)?;
if fs_representation::find_repo_root(&dir).is_some() {
return Err(Error::InARepository {
path: dir.to_owned(),
});
}
let repo_root = fs_representation::create(&dir, rand::thread_rng())?;
let repo = repo_root.open_repo(None)?;
repo.mut_txn_begin(rand::thread_rng())?.commit()?;
Ok(())
}
fn default_explain<R>(command_result: Result<R, Error>) {
debug!("default_explain");
match command_result {
Ok(_) => (),
Err(e) => {
writeln!(stderr(), "error: {}", e).unwrap();
exit(1)
}
}
}
fn validate_base58(x: String) -> ::std::result::Result<(), String> {
if Hash::from_base58(&x).is_some() {
Ok(())
} else {
Err(format!("\"{}\" is invalid base58", x))
}
}
/// Almost all commands want to know the current directory and the repository root. This struct
/// fills that need, and also provides methods for other commonly-used tasks.
pub struct BasicOptions<'a> {
/// This isn't 100% the same as the actual current working directory, so pay attention: this
/// will be the current directory, unless the user specifies `--repository`, in which case
/// `cwd` will actually be the path of the repository root. In other words, specifying
/// `--repository` has the same effect as changing directory to the repository root before
/// running `pijul`.
pub cwd: PathBuf,
pub repo_root: RepoRoot<PathBuf>,
args: &'a ArgMatches<'a>,
}
pub enum ScanScope {
FromRoot,
WithPrefix(RepoPath<PathBuf>, String),
}
impl<'a> BasicOptions<'a> {
/// Reads the options from command line arguments.
pub fn from_args(args: &'a ArgMatches<'a>) -> Result<BasicOptions<'a>, Error> {
let wd = get_wd(args.value_of("repository").map(Path::new))?;
let repo_root = if let Some(r) = fs_representation::find_repo_root(&canonicalize(&wd)?) {
r
} else {
return Err(Error::NotInARepository);
};
Ok(BasicOptions {
cwd: wd,
repo_root: repo_root,
args: args,
})
}
/// Gets the name of the desired branch.
pub fn branch(&self) -> String {
if let Some(b) = self.args.value_of("branch") {
b.to_string()
} else if let Ok(b) = self.repo_root.get_current_branch() {
b
} else {
DEFAULT_BRANCH.to_string()
}
}
pub fn repo_root(&self) -> PathBuf {
self.repo_root.repo_root.clone()
}
pub fn open_repo(&self) -> Result<Repository, Error> {
self.repo_root.open_repo(None).map_err(|e| e.into())
fs_representation::open_repo(&self.repo_root, None).map_err(|e| e.into())
}
pub fn open_and_grow_repo(&self, increase: u64) -> Result<Repository, Error> {
fs_representation::open_repo(&self.repo_root, Some(increase)).map_err(|e| e.into())
self.repo_root
.open_repo(Some(increase))
.map_err(|e| e.into())
}
pub fn pristine_dir(&self) -> PathBuf {
self.repo_root.pristine_dir()
}
pub fn patches_dir(&self) -> PathBuf {
self.repo_root.patches_dir()
}
pub fn scan_scope(&self) -> Result<ScanScope, Error> {
if let Some(prefix) = self.args.value_of("dir") {
let root = self
.args
.value_of("repository")
.map(|root| Path::new(root).to_path_buf())
.unwrap_or(current_dir()?);
Ok(ScanScope::WithPrefix(
relative_repo_path(&self.repo_root, &root, prefix)?,
prefix.into(),
))
} else {
Ok(ScanScope::FromRoot)
}
}
fn dir_inode(&self, txn: &Txn) -> Result<Inode, Error> {
use libpijul::ROOT_INODE;
if let Some(dir) = self.args.value_of("dir") {
let dir = if Path::new(dir).is_relative() {
let root = if let Some(root) = self.args.value_of("repository") {
Path::new(root).to_path_buf()
} else {
current_dir()?
};
root.join(&dir).canonicalize()?
} else {
Path::new(dir).canonicalize()?
};
let dir = self.repo_root.relativize(&dir)?;
debug!("{:?}", dir);
let inode = txn.find_inode(&dir)?;
debug!("{:?}", inode);
Ok(inode)
} else {
Ok(ROOT_INODE)
}
}
}
fn remote_pijul_cmd() -> Cow<'static, str> {
if let Ok(cmd) = var("REMOTE_PIJUL") {
Cow::Owned(cmd)
} else {
Cow::Borrowed("pijul")
}
}
pub fn relative_repo_path(
repo_root: &RepoRoot<PathBuf>,
base: &PathBuf,
dir: &str,
) -> Result<RepoPath<PathBuf>, Error> {
let dir = if Path::new(dir).is_relative() {
base.join(&dir).canonicalize()?
} else {
Path::new(dir).canonicalize()?
};
Ok(repo_root.relativize(&dir)?.to_owned())
}
pub fn pretty_repo_path(
repo_root: &RepoRoot<impl AsRef<Path>>,
path: &RepoPath<impl AsRef<Path>>,
cwd: &Path,
) -> PathBuf {
let abs_path = repo_root.absolutize(path);
pathdiff::diff_paths(&abs_path, cwd).unwrap_or(abs_path)
}