Skip to content

Commit d3b0ea7

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

File tree

6 files changed

+215
-0
lines changed

6 files changed

+215
-0
lines changed

Cargo.lock

+10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

git-worktree/Cargo.toml

+10
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

+77
Original file line numberDiff line numberDiff line change
@@ -1 +1,78 @@
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;
10+
use std::fs::create_dir_all;
11+
use std::path::Path;
12+
13+
#[cfg(unix)]
14+
use std::os::unix::fs::PermissionsExt;
15+
16+
/// Copy index to `path`
17+
pub fn copy_index<P, Find>(state: &State, path: P, mut find: Find, opts: Options) -> Result<()>
18+
where
19+
P: AsRef<Path>,
20+
Find: for<'a> FnMut(&oid, &'a mut Vec<u8>) -> Option<Data<'a>>,
21+
{
22+
let path = path.as_ref();
23+
let entries = state.entries();
24+
let mut buf = Vec::new();
25+
for entry in entries {
26+
let dest = path.join(entry.path(state).to_path()?);
27+
create_dir_all(dest.parent().expect("path is empty"))?;
28+
match entry.mode {
29+
Mode::FILE | Mode::FILE_EXECUTABLE => {
30+
let obj = find(&entry.id, &mut buf).unwrap();
31+
std::fs::write(&dest, obj.data)?;
32+
if entry.mode == Mode::FILE_EXECUTABLE {
33+
#[cfg(unix)]
34+
fs::set_permissions(dest, fs::Permissions::from_mode(0o777)).unwrap();
35+
}
36+
}
37+
Mode::SYMLINK => {
38+
let obj = find(&entry.id, &mut buf).unwrap();
39+
let linked_to = obj.data.to_path()?;
40+
if opts.symlinks {
41+
#[cfg(unix)]
42+
std::os::unix::fs::symlink(linked_to, dest)?;
43+
#[cfg(windows)]
44+
if dest.exists() {
45+
if dest.is_file() {
46+
std::os::windows::fs::symlink_file(linked_to, dest)?;
47+
} else {
48+
std::os::windows::fs::symlink_dir(linked_to, dest)?;
49+
}
50+
}
51+
} else {
52+
let linked_to_path = path.join(linked_to);
53+
if linked_to_path.exists() {
54+
std::fs::copy(linked_to_path, dest)?;
55+
} else {
56+
std::fs::write(dest, obj.data)?;
57+
}
58+
}
59+
}
60+
Mode::DIR => todo!(),
61+
Mode::COMMIT => todo!(),
62+
_ => unreachable!(),
63+
}
64+
}
65+
Ok(())
66+
}
67+
68+
/// Options for [copy_index](crate::copy_index)
69+
pub struct Options {
70+
/// Enable/disable symlinks
71+
pub symlinks: bool,
72+
}
73+
74+
impl Default for Options {
75+
fn default() -> Self {
76+
Options { symlinks: true }
77+
}
78+
}

git-worktree/tests/copy_index/mod.rs

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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+
#[cfg(unix)]
8+
use std::os::unix::prelude::MetadataExt;
9+
10+
#[test]
11+
fn test_copy_index() -> Result<()> {
12+
let path = fixture_path("make_repo");
13+
let path_git = path.join(".git");
14+
let file = git_index::File::at(path_git.join("index"), git_index::decode::Options::default())?;
15+
let output_dir = tempfile::tempdir()?;
16+
let output = output_dir.path();
17+
let odb_handle = git_odb::at(path_git.join("objects")).unwrap();
18+
19+
let res = copy_index(
20+
&file,
21+
&output,
22+
move |oid, buf| odb_handle.find(oid, buf).ok(),
23+
Options::default(),
24+
);
25+
26+
let repo_files = dir_structure(&path);
27+
let copy_files = dir_structure(output);
28+
29+
let srepo_files: Vec<_> = repo_files.iter().flat_map(|p| p.strip_prefix(&path)).collect();
30+
let scopy_files: Vec<_> = copy_files.iter().flat_map(|p| p.strip_prefix(output)).collect();
31+
assert!(srepo_files == scopy_files);
32+
33+
for (file1, file2) in repo_files.iter().zip(copy_files.iter()) {
34+
assert!(fs::read(file1)? == fs::read(file2)?);
35+
#[cfg(unix)]
36+
assert!(fs::metadata(file1)?.mode() & 0b111 << 6 == fs::metadata(file2)?.mode() & 0b111 << 6);
37+
}
38+
39+
res
40+
}
41+
42+
#[test]
43+
fn test_copy_index_without_symlinks() -> Result<()> {
44+
let path = fixture_path("make_repo");
45+
let path_git = path.join(".git");
46+
let file = git_index::File::at(path_git.join("index"), git_index::decode::Options::default())?;
47+
let output_dir = tempfile::tempdir()?;
48+
let output = output_dir.path();
49+
let odb_handle = git_odb::at(path_git.join("objects")).unwrap();
50+
51+
let res = copy_index(
52+
&file,
53+
&output,
54+
move |oid, buf| odb_handle.find(oid, buf).ok(),
55+
Options { symlinks: false },
56+
);
57+
58+
let repo_files = dir_structure(&path);
59+
let copy_files = dir_structure(output);
60+
61+
let srepo_files: Vec<_> = repo_files.iter().flat_map(|p| p.strip_prefix(&path)).collect();
62+
let scopy_files: Vec<_> = copy_files.iter().flat_map(|p| p.strip_prefix(output)).collect();
63+
assert!(srepo_files == scopy_files);
64+
65+
for (file1, file2) in repo_files.iter().zip(copy_files.iter()) {
66+
if file2.is_symlink() {
67+
assert!(!file1.is_symlink());
68+
} else {
69+
#[cfg(unix)]
70+
assert!(fs::metadata(file1)?.mode() & 0b111 << 6 == fs::metadata(file2)?.mode() & 0b111 << 6);
71+
}
72+
assert!(fs::read(file1)? == fs::read(file2)?);
73+
}
74+
75+
res
76+
}
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
touch executable.sh
11+
chmod +x executable.sh
12+
13+
mkdir d
14+
touch d/a
15+
echo "Subdir" > d/a
16+
ln -sf d/a sa
17+
18+
git add -A
19+
git commit -m "Commit"

git-worktree/tests/mod.rs

+23
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)