Skip to content

Commit c4ca4bc

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

File tree

6 files changed

+200
-0
lines changed

6 files changed

+200
-0
lines changed

Cargo.lock

Lines changed: 10 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: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,13 @@ 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+
anyhow = "1.0.42"
18+
git-hash = { version = "^0.9.0", path = "../git-hash" }
19+
git-object = { version = "^0.17.0", path = "../git-object" }
20+
21+
[dev-dependencies]
22+
git-odb = { path = "../git-odb" }
23+
walkdir = "2.3.2"
24+
git-testtools = { path = "../tests/tools" }
25+
tempfile = "3.2.0"

git-worktree/src/lib.rs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,70 @@
11
#![forbid(unsafe_code, rust_2018_idioms)]
2+
//! Git Worktree
3+
4+
use anyhow::Result;
5+
use git_hash::oid;
6+
use git_index::{entry::Mode, State};
7+
use git_object::bstr::ByteSlice;
8+
use git_object::Data;
9+
use std::fs::create_dir_all;
10+
use std::path::Path;
11+
12+
/// Copy index to `path`
13+
pub fn copy_index<P, Find>(state: &State, path: P, mut find: Find, opts: Options) -> Result<()>
14+
where
15+
P: AsRef<Path>,
16+
Find: for<'a> FnMut(&oid, &'a mut Vec<u8>) -> Option<Data<'a>>,
17+
{
18+
let path = path.as_ref();
19+
let entries = state.entries();
20+
let mut buf = Vec::new();
21+
for entry in entries {
22+
let dest = path.join(entry.path(state).to_path()?);
23+
create_dir_all(dest.parent().expect("Path has no parent"))?;
24+
match entry.mode {
25+
Mode::FILE | Mode::FILE_EXECUTABLE => {
26+
let obj = find(&entry.id, &mut buf).unwrap();
27+
std::fs::write(dest, obj.data)?;
28+
}
29+
Mode::SYMLINK => {
30+
let obj = find(&entry.id, &mut buf).unwrap();
31+
let linked_to = obj.data.to_path()?;
32+
if opts.symlinks {
33+
#[cfg(unix)]
34+
std::os::unix::fs::symlink(linked_to, dest)?;
35+
#[cfg(windows)]
36+
if dest.exists() {
37+
if dest.is_file() {
38+
std::os::windows::fs::symlink_file(linked_to, dest)?;
39+
} else {
40+
std::os::windows::fs::symlink_dir(linked_to, dest)?;
41+
}
42+
}
43+
} else {
44+
let linked_to_path = path.join(linked_to);
45+
if linked_to_path.exists() {
46+
std::fs::copy(linked_to_path, dest)?;
47+
} else {
48+
std::fs::write(dest, obj.data)?;
49+
}
50+
}
51+
}
52+
Mode::DIR => todo!(),
53+
Mode::COMMIT => todo!(),
54+
_ => {}
55+
}
56+
}
57+
Ok(())
58+
}
59+
60+
/// Options for [copy_index](crate::copy_index)
61+
pub struct Options {
62+
/// Enable/disable symlinks
63+
pub symlinks: bool,
64+
}
65+
66+
impl Default for Options {
67+
fn default() -> Self {
68+
Options { symlinks: true }
69+
}
70+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
use crate::{dir_structure, fixture_path};
2+
use anyhow::Result;
3+
use git_odb::FindExt;
4+
use git_worktree::{copy_index, Options};
5+
use std::fs;
6+
7+
#[test]
8+
fn test_copy_index() -> Result<()> {
9+
let path = fixture_path("make_repo");
10+
let path_git = path.join(".git");
11+
let file = git_index::File::at(path_git.join("index"), git_index::decode::Options::default())?;
12+
let output_dir = tempfile::tempdir()?;
13+
let output = output_dir.path();
14+
let odb_handle = git_odb::at(path_git.join("objects")).unwrap();
15+
16+
let res = copy_index(
17+
&file,
18+
&output,
19+
move |oid, buf| odb_handle.find(oid, buf).ok(),
20+
Options::default(),
21+
);
22+
23+
let repo_files = dir_structure(&path);
24+
let copy_files = dir_structure(output);
25+
26+
let srepo_files: Vec<_> = repo_files.iter().flat_map(|p| p.strip_prefix(&path)).collect();
27+
let scopy_files: Vec<_> = copy_files.iter().flat_map(|p| p.strip_prefix(output)).collect();
28+
assert!(srepo_files == scopy_files);
29+
30+
for (file1, file2) in repo_files.iter().zip(copy_files.iter()) {
31+
assert!(fs::metadata(file1)?.file_type() == fs::metadata(file2)?.file_type());
32+
assert!(fs::read(file1)? == fs::read(file2)?);
33+
}
34+
35+
res
36+
}
37+
38+
#[test]
39+
fn test_copy_index_without_symlinks() -> Result<()> {
40+
let path = fixture_path("make_repo");
41+
let path_git = path.join(".git");
42+
let file = git_index::File::at(path_git.join("index"), git_index::decode::Options::default())?;
43+
let output_dir = tempfile::tempdir()?;
44+
let output = output_dir.path();
45+
let odb_handle = git_odb::at(path_git.join("objects")).unwrap();
46+
47+
let res = copy_index(
48+
&file,
49+
&output,
50+
move |oid, buf| odb_handle.find(oid, buf).ok(),
51+
Options { symlinks: false },
52+
);
53+
54+
let repo_files = dir_structure(&path);
55+
let copy_files = dir_structure(output);
56+
57+
let srepo_files: Vec<_> = repo_files.iter().flat_map(|p| p.strip_prefix(&path)).collect();
58+
let scopy_files: Vec<_> = copy_files.iter().flat_map(|p| p.strip_prefix(output)).collect();
59+
assert!(srepo_files == scopy_files);
60+
61+
for (file1, file2) in repo_files.iter().zip(copy_files.iter()) {
62+
if file2.is_symlink() {
63+
assert!(!file1.is_symlink());
64+
} else {
65+
assert!(fs::metadata(file1)?.file_type() == fs::metadata(file2)?.file_type());
66+
}
67+
assert!(fs::read(file1)? == fs::read(file2)?);
68+
}
69+
70+
res
71+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/bash
2+
set -eu -o pipefail
3+
4+
git init -q
5+
6+
touch a
7+
echo "Test Vals" > a
8+
touch b
9+
touch c
10+
11+
mkdir d
12+
touch d/a
13+
echo "Subdir" > d/a
14+
ln -sf d/a sa
15+
16+
git add -A
17+
git commit -m "Commit"

git-worktree/tests/mod.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use std::path::{Path, PathBuf};
2+
use walkdir::WalkDir;
3+
4+
mod copy_index;
5+
6+
pub fn dir_structure<P: AsRef<Path>>(path: P) -> Vec<PathBuf> {
7+
let path = path.as_ref();
8+
let mut ps: Vec<_> = WalkDir::new(path)
9+
.into_iter()
10+
.filter_entry(|e| e.path() == path || !e.file_name().to_str().map(|s| s.starts_with('.')).unwrap_or(false))
11+
.flatten()
12+
.filter(|e| e.path().is_file())
13+
.map(|p| p.path().to_path_buf())
14+
.collect();
15+
ps.sort();
16+
ps
17+
}
18+
19+
pub fn fixture_path(name: &str) -> PathBuf {
20+
let dir =
21+
git_testtools::scripted_fixture_repo_read_only(Path::new(name).with_extension("sh")).expect("script works");
22+
dir
23+
}

0 commit comments

Comments
 (0)