Repository navigation
Expand file tree
/
Copy pathrestore.rs
More file actions
54 lines (51 loc) · 1.97 KB
/
Copy pathrestore.rs
File metadata and controls
54 lines (51 loc) · 1.97 KB
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
use std::env;
use std::path::{Path, PathBuf};
use std::str::FromStr;
use git_rs::{HostFileSystem, ObjectId, Repository, RestoreOptions, RestoreTarget};
fn main() -> git_rs::Result<()> {
let repository_path = env::args().nth(1).unwrap_or_else(|| ".".to_owned());
let arguments = env::args().skip(2).collect::<Vec<_>>();
let staged = arguments.iter().any(|argument| argument == "--staged");
let worktree = arguments.iter().any(|argument| argument == "--worktree");
let target = match (staged, worktree) {
(true, true) => RestoreTarget::Both,
(true, false) => RestoreTarget::Index,
(false, _) => RestoreTarget::Worktree,
};
let source = arguments
.iter()
.find_map(|argument| argument.strip_prefix("--source="))
.map(ObjectId::from_str)
.transpose()?;
let paths = arguments
.iter()
.filter(|argument| !argument.starts_with("--"))
.map(PathBuf::from)
.collect::<Vec<_>>();
assert!(
!paths.is_empty(),
"usage: restore <repository> <path>... [--staged] [--worktree] [--source=<object-id>] [--force] [--dry-run]"
);
let repository_path = std::fs::canonicalize(Path::new(&repository_path))?;
let storage_root = repository_path
.parent()
.expect("repository path must have a parent");
let repository_name = repository_path
.file_name()
.expect("repository path must name a directory");
let repository = Repository::open(HostFileSystem::new(storage_root)?, repository_name)?;
let restored = repository.restore_paths(
&paths,
&RestoreOptions {
source,
target,
force: arguments.iter().any(|argument| argument == "--force"),
dry_run: arguments.iter().any(|argument| argument == "--dry-run"),
..RestoreOptions::default()
},
)?;
for path in restored {
println!("restored '{}'", String::from_utf8_lossy(&path));
}
Ok(())
}