Skip to content

Commit 8c5e2f4

Browse files
committed
Implemented git-worktree
1 parent 28e3251 commit 8c5e2f4

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

Cargo.lock

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

git-worktree/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@ doctest = false
1313
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1414

1515
[dependencies]
16+
git-index = { version = "^0.1.0", path = "../git-index" }
17+
rayon = "1.5.1"

git-worktree/src/lib.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,52 @@
11
#![forbid(unsafe_code, rust_2018_idioms)]
2+
3+
use git_index::State;
4+
use rayon::prelude::*;
5+
use std::convert::From;
6+
use std::path::{Path, PathBuf};
7+
use std::{fs, io::Result};
8+
9+
/// Git Worktree
10+
pub struct Worktree {
11+
pub paths: Vec<PathBuf>,
12+
}
13+
14+
impl Worktree {
15+
/// Copy all `files` in `self.paths` to `path`
16+
pub fn save<P: AsRef<Path>>(&mut self, path: P) -> Result<()> {
17+
let path = path.as_ref();
18+
self.paths
19+
.par_iter()
20+
.try_for_each(|p| Self::recur_copy(p, path.join(p)))
21+
}
22+
/// Recursively copies directory/file `path` to `copy`
23+
fn recur_copy<P, Q>(path: P, copy: Q) -> Result<()>
24+
where
25+
P: AsRef<Path>,
26+
Q: AsRef<Path>,
27+
{
28+
let (path, copy) = (path.as_ref(), copy.as_ref());
29+
if path.is_file() {
30+
return fs::copy(path, copy).map(|_| ());
31+
}
32+
if !copy.exists() {
33+
fs::create_dir(copy)?;
34+
}
35+
fs::read_dir(path)?
36+
.flatten()
37+
.par_bridge()
38+
.try_for_each(|p| Self::recur_copy(p.path(), copy.join(p.file_name())))
39+
}
40+
}
41+
42+
impl From<State> for Worktree {
43+
fn from(state: State) -> Self {
44+
Worktree {
45+
paths: state
46+
.entries()
47+
.iter()
48+
.map(|x| PathBuf::from(x.path(&state).to_string()))
49+
.collect(),
50+
}
51+
}
52+
}

0 commit comments

Comments
 (0)