|
1 | 1 | #![forbid(unsafe_code, rust_2018_idioms)] |
| 2 | +//! Git Worktree |
| 3 | +
|
| 4 | +use anyhow::Result; |
| 5 | +use git_hash::oid; |
| 6 | +use git_index::{ |
| 7 | + entry::{Flags, Mode}, |
| 8 | + State, |
| 9 | +}; |
| 10 | +use git_object::bstr::ByteSlice; |
| 11 | +use git_object::Data; |
| 12 | +use std::convert::TryFrom; |
| 13 | +use std::fs; |
| 14 | +use std::fs::create_dir_all; |
| 15 | +use std::path::Path; |
| 16 | +use std::time::SystemTime; |
| 17 | + |
| 18 | +#[cfg(unix)] |
| 19 | +use std::os::unix::fs::PermissionsExt; |
| 20 | + |
| 21 | +/// Copy index to `path` |
| 22 | +pub fn copy_index<P, Find>(state: &mut State, path: P, mut find: Find, opts: Options) -> Result<()> |
| 23 | +where |
| 24 | + P: AsRef<Path>, |
| 25 | + Find: for<'a> FnMut(&oid, &'a mut Vec<u8>) -> Option<Data<'a>>, |
| 26 | +{ |
| 27 | + let path = path.as_ref(); |
| 28 | + let paths = state |
| 29 | + .entries() |
| 30 | + .iter() |
| 31 | + .map(|e| e.path(state).to_path().map(|p| p.to_path_buf())) |
| 32 | + .collect::<Result<Vec<_>, _>>()?; |
| 33 | + let mut buf = Vec::new(); |
| 34 | + for (i, entry) in state.entries_mut().iter_mut().enumerate() { |
| 35 | + if entry.flags.contains(Flags::SKIP_WORKTREE) { |
| 36 | + continue; |
| 37 | + } |
| 38 | + let time = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH)?; |
| 39 | + entry.stat.mtime.secs = u32::try_from(time.as_secs())?; |
| 40 | + entry.stat.mtime.nsecs = time.subsec_nanos(); |
| 41 | + entry.stat.ctime.secs = u32::try_from(time.as_secs())?; |
| 42 | + entry.stat.ctime.nsecs = time.subsec_nanos(); |
| 43 | + let dest = path.join(&paths[i]); |
| 44 | + create_dir_all(dest.parent().expect("path is empty"))?; |
| 45 | + match entry.mode { |
| 46 | + Mode::FILE | Mode::FILE_EXECUTABLE => { |
| 47 | + let obj = find(&entry.id, &mut buf).unwrap(); |
| 48 | + std::fs::write(&dest, obj.data)?; |
| 49 | + if entry.mode == Mode::FILE_EXECUTABLE { |
| 50 | + #[cfg(unix)] |
| 51 | + fs::set_permissions(dest, fs::Permissions::from_mode(0o777)).unwrap(); |
| 52 | + } |
| 53 | + } |
| 54 | + Mode::SYMLINK => { |
| 55 | + let obj = find(&entry.id, &mut buf).unwrap(); |
| 56 | + let linked_to = obj.data.to_path()?; |
| 57 | + if opts.symlinks { |
| 58 | + #[cfg(unix)] |
| 59 | + std::os::unix::fs::symlink(linked_to, dest)?; |
| 60 | + #[cfg(windows)] |
| 61 | + if dest.exists() { |
| 62 | + if dest.is_file() { |
| 63 | + std::os::windows::fs::symlink_file(linked_to, dest)?; |
| 64 | + } else { |
| 65 | + std::os::windows::fs::symlink_dir(linked_to, dest)?; |
| 66 | + } |
| 67 | + } |
| 68 | + } else { |
| 69 | + let linked_to_path = path.join(linked_to); |
| 70 | + if linked_to_path.exists() { |
| 71 | + std::fs::copy(linked_to_path, dest)?; |
| 72 | + } else { |
| 73 | + std::fs::write(dest, obj.data)?; |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + Mode::DIR => todo!(), |
| 78 | + Mode::COMMIT => todo!(), |
| 79 | + _ => unreachable!(), |
| 80 | + } |
| 81 | + } |
| 82 | + Ok(()) |
| 83 | +} |
| 84 | + |
| 85 | +/// Options for [copy_index](crate::copy_index) |
| 86 | +pub struct Options { |
| 87 | + /// Enable/disable symlinks |
| 88 | + pub symlinks: bool, |
| 89 | +} |
| 90 | + |
| 91 | +impl Default for Options { |
| 92 | + fn default() -> Self { |
| 93 | + Options { symlinks: true } |
| 94 | + } |
| 95 | +} |
0 commit comments