Skip to content

Commit 72af261

Browse files
committed
refactor (#301)
1 parent a4b880c commit 72af261

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

git-worktree/src/lib.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#![forbid(unsafe_code, rust_2018_idioms)]
2-
//! Git Worktree
32

43
use git_hash::oid;
54
use git_object::bstr::ByteSlice;
@@ -10,9 +9,6 @@ use std::io::Write;
109
use std::path::{Path, PathBuf};
1110
use std::time::Duration;
1211

13-
#[cfg(unix)]
14-
use std::os::unix::fs::OpenOptionsExt;
15-
1612
quick_error! {
1713
#[derive(Debug)]
1814
pub enum Error {
@@ -39,28 +35,34 @@ quick_error! {
3935
}
4036

4137
/// Copy index to `path`
42-
pub fn copy_index<P, Find>(state: &mut git_index::State, path: P, mut find: Find, opts: Options) -> Result<(), Error>
38+
pub fn copy_index<Find>(
39+
index: &mut git_index::State,
40+
path: impl AsRef<Path>,
41+
mut find: Find,
42+
opts: Options,
43+
) -> Result<(), Error>
4344
where
44-
P: AsRef<Path>,
4545
Find: for<'a> FnMut(&oid, &'a mut Vec<u8>) -> Option<git_object::BlobRef<'a>>,
4646
{
4747
let path = path.as_ref();
4848
let mut buf = Vec::new();
4949
let mut entry_time = Vec::new(); // Entries whose timestamps have to be updated
50-
for (i, entry) in state.entries().iter().enumerate() {
50+
for (i, entry) in index.entries().iter().enumerate() {
5151
if entry.flags.contains(git_index::entry::Flags::SKIP_WORKTREE) {
5252
continue;
5353
}
54-
let entry_path = entry.path(state).to_path()?;
54+
let entry_path = entry.path(index).to_path()?;
5555
let dest = path.join(entry_path);
5656
create_dir_all(dest.parent().expect("entry paths are never empty"))?;
57+
5758
match entry.mode {
5859
git_index::entry::Mode::FILE | git_index::entry::Mode::FILE_EXECUTABLE => {
5960
let obj = find(&entry.id, &mut buf).ok_or_else(|| Error::NotFound(entry.id, path.to_path_buf()))?;
6061
let mut options = OpenOptions::new();
6162
options.write(true).create_new(true);
6263
#[cfg(unix)]
6364
if entry.mode == git_index::entry::Mode::FILE_EXECUTABLE {
65+
use std::os::unix::fs::OpenOptionsExt;
6466
options.mode(0o777);
6567
}
6668
let mut file = options.open(&dest)?;
@@ -105,7 +107,7 @@ where
105107
_ => unreachable!(),
106108
}
107109
}
108-
let entries = state.entries_mut();
110+
let entries = index.entries_mut();
109111
for (ctime, mtime, i) in entry_time {
110112
let stat = &mut entries[i].stat;
111113
stat.mtime.secs = u32::try_from(mtime.as_secs())?;

0 commit comments

Comments
 (0)