|
1 | 1 | #![forbid(unsafe_code, rust_2018_idioms)]
|
| 2 | +//! Git Worktree |
| 3 | +
|
| 4 | +use git_hash::oid; |
| 5 | +use git_object::bstr::ByteSlice; |
| 6 | +use quick_error::quick_error; |
| 7 | +use std::convert::TryFrom; |
| 8 | +use std::fs; |
| 9 | +use std::fs::create_dir_all; |
| 10 | +use std::path::Path; |
| 11 | +use std::time::Duration; |
| 12 | + |
| 13 | +#[cfg(unix)] |
| 14 | +use std::os::unix::fs::PermissionsExt; |
| 15 | + |
| 16 | +quick_error! { |
| 17 | + #[derive(Debug)] |
| 18 | + pub enum Error { |
| 19 | + Utf8Error(err: git_object::bstr::Utf8Error) { |
| 20 | + from() |
| 21 | + display("Could not convert path to UTF8: {}", err) |
| 22 | + } |
| 23 | + TimeError(err: std::time::SystemTimeError) { |
| 24 | + from() |
| 25 | + display("Could not read file time in proper format: {}", err) |
| 26 | + } |
| 27 | + ToU32Error(err: std::num::TryFromIntError) { |
| 28 | + from() |
| 29 | + display("Could not convert seconds to u32: {}", err) |
| 30 | + } |
| 31 | + IoError(err: std::io::Error) { |
| 32 | + from() |
| 33 | + display("IO Error: {}", err) |
| 34 | + } |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +/// Copy index to `path` |
| 39 | +pub fn copy_index<P, Find>(state: &mut git_index::State, path: P, mut find: Find, opts: Options) -> Result<(), Error> |
| 40 | +where |
| 41 | + P: AsRef<Path>, |
| 42 | + Find: for<'a> FnMut(&oid, &'a mut Vec<u8>) -> Option<git_object::BlobRef<'a>>, |
| 43 | +{ |
| 44 | + let path = path.as_ref(); |
| 45 | + let mut buf = Vec::new(); |
| 46 | + let mut entry_time = Vec::new(); // Entries whose timestamps have to be updated |
| 47 | + for (i, entry) in state.entries().iter().enumerate() { |
| 48 | + if entry.flags.contains(git_index::entry::Flags::SKIP_WORKTREE) { |
| 49 | + continue; |
| 50 | + } |
| 51 | + let dest = path.join(entry.path(state).to_path()?); |
| 52 | + create_dir_all(dest.parent().expect("path is empty"))?; |
| 53 | + match entry.mode { |
| 54 | + git_index::entry::Mode::FILE | git_index::entry::Mode::FILE_EXECUTABLE => { |
| 55 | + let obj = find(&entry.id, &mut buf).unwrap(); |
| 56 | + std::fs::write(&dest, obj.data)?; |
| 57 | + if entry.mode == git_index::entry::Mode::FILE_EXECUTABLE { |
| 58 | + #[cfg(unix)] |
| 59 | + fs::set_permissions(&dest, fs::Permissions::from_mode(0o777)).unwrap(); |
| 60 | + } |
| 61 | + let met = std::fs::symlink_metadata(&dest)?; |
| 62 | + // Set both fields to mtime if ctime is not present and vice-versa |
| 63 | + // If both fields are not present, will raise an Err |
| 64 | + let ctime = met |
| 65 | + .created() |
| 66 | + .map_or(Ok(Duration::from_secs(0)), |x| x.duration_since(std::time::UNIX_EPOCH)); |
| 67 | + let mtime = met |
| 68 | + .modified() |
| 69 | + .map_or(Ok(Duration::from_secs(0)), |x| x.duration_since(std::time::UNIX_EPOCH)); |
| 70 | + entry_time.push((ctime?, mtime?, i)); |
| 71 | + } |
| 72 | + git_index::entry::Mode::SYMLINK => { |
| 73 | + let obj = find(&entry.id, &mut buf).unwrap(); |
| 74 | + let linked_to = obj.data.to_path()?; |
| 75 | + if opts.symlinks { |
| 76 | + #[cfg(unix)] |
| 77 | + std::os::unix::fs::symlink(linked_to, &dest)?; |
| 78 | + #[cfg(windows)] |
| 79 | + if dest.exists() { |
| 80 | + if dest.is_file() { |
| 81 | + std::os::windows::fs::symlink_file(linked_to, &dest)?; |
| 82 | + } else { |
| 83 | + std::os::windows::fs::symlink_dir(linked_to, &dest)?; |
| 84 | + } |
| 85 | + } |
| 86 | + } else { |
| 87 | + std::fs::write(&dest, obj.data)?; |
| 88 | + } |
| 89 | + let met = std::fs::symlink_metadata(&dest)?; |
| 90 | + // Set both fields to mtime if ctime is not present and vice-versa |
| 91 | + // If both fields are not present, will raise an Err |
| 92 | + let ctime = met |
| 93 | + .created() |
| 94 | + .map_or(Ok(Duration::from_secs(0)), |x| x.duration_since(std::time::UNIX_EPOCH)); |
| 95 | + let mtime = met |
| 96 | + .modified() |
| 97 | + .map_or(Ok(Duration::from_secs(0)), |x| x.duration_since(std::time::UNIX_EPOCH)); |
| 98 | + entry_time.push((ctime?, mtime?, i)); |
| 99 | + } |
| 100 | + git_index::entry::Mode::DIR => todo!(), |
| 101 | + git_index::entry::Mode::COMMIT => todo!(), |
| 102 | + _ => unreachable!(), |
| 103 | + } |
| 104 | + } |
| 105 | + let entries = state.entries_mut(); |
| 106 | + for (ctime, mtime, i) in entry_time { |
| 107 | + let stat = &mut entries[i].stat; |
| 108 | + stat.mtime.secs = u32::try_from(mtime.as_secs())?; |
| 109 | + stat.mtime.nsecs = mtime.subsec_nanos(); |
| 110 | + stat.ctime.secs = u32::try_from(ctime.as_secs())?; |
| 111 | + stat.ctime.nsecs = ctime.subsec_nanos(); |
| 112 | + } |
| 113 | + Ok(()) |
| 114 | +} |
| 115 | + |
| 116 | +/// Options for [copy_index](crate::copy_index) |
| 117 | +pub struct Options { |
| 118 | + /// Enable/disable symlinks |
| 119 | + pub symlinks: bool, |
| 120 | +} |
| 121 | + |
| 122 | +impl Default for Options { |
| 123 | + fn default() -> Self { |
| 124 | + Options { symlinks: true } |
| 125 | + } |
| 126 | +} |
0 commit comments