|
1 | 1 | #![forbid(unsafe_code, rust_2018_idioms)]
|
2 | 2 |
|
3 |
| -pub mod index { |
4 |
| - use git_hash::oid; |
5 |
| - |
6 |
| - pub mod checkout { |
7 |
| - use bstr::BString; |
8 |
| - use quick_error::quick_error; |
9 |
| - |
10 |
| - #[derive(Clone, Copy)] |
11 |
| - pub struct Options { |
12 |
| - pub symlinks: bool, |
13 |
| - } |
14 |
| - |
15 |
| - impl Default for Options { |
16 |
| - fn default() -> Self { |
17 |
| - Options { symlinks: true } |
18 |
| - } |
19 |
| - } |
20 |
| - |
21 |
| - quick_error! { |
22 |
| - #[derive(Debug)] |
23 |
| - pub enum Error { |
24 |
| - IllformedUtf8{ path: BString } { |
25 |
| - display("Could not convert path to UTF8: {}", path) |
26 |
| - } |
27 |
| - Time(err: std::time::SystemTimeError) { |
28 |
| - from() |
29 |
| - source(err) |
30 |
| - display("The clock was off when reading file related metadata after updating a file on disk") |
31 |
| - } |
32 |
| - Io(err: std::io::Error) { |
33 |
| - from() |
34 |
| - source(err) |
35 |
| - display("IO error while writing blob or reading file metadata or changing filetype") |
36 |
| - } |
37 |
| - ObjectNotFound{ oid: git_hash::ObjectId, path: std::path::PathBuf } { |
38 |
| - display("object {} for checkout at {} not found in object database", oid.to_hex(), path.display()) |
39 |
| - } |
40 |
| - } |
41 |
| - } |
42 |
| - } |
43 |
| - |
44 |
| - pub fn checkout<Find>( |
45 |
| - index: &mut git_index::State, |
46 |
| - path: impl AsRef<std::path::Path>, |
47 |
| - mut find: Find, |
48 |
| - options: checkout::Options, |
49 |
| - ) -> Result<(), checkout::Error> |
50 |
| - where |
51 |
| - Find: for<'a> FnMut(&oid, &'a mut Vec<u8>) -> Option<git_object::BlobRef<'a>>, |
52 |
| - { |
53 |
| - let root = path.as_ref(); |
54 |
| - let mut buf = Vec::new(); |
55 |
| - for (entry, entry_path) in index.entries_mut_with_paths() { |
56 |
| - // TODO: write test for that |
57 |
| - if entry.flags.contains(git_index::entry::Flags::SKIP_WORKTREE) { |
58 |
| - continue; |
59 |
| - } |
60 |
| - |
61 |
| - entry::checkout(entry, entry_path, &mut find, root, options, &mut buf)?; |
62 |
| - } |
63 |
| - Ok(()) |
64 |
| - } |
65 |
| - |
66 |
| - pub(crate) mod entry { |
67 |
| - use std::{ |
68 |
| - convert::TryInto, |
69 |
| - fs::{create_dir_all, OpenOptions}, |
70 |
| - io::Write, |
71 |
| - time::Duration, |
72 |
| - }; |
73 |
| - |
74 |
| - use bstr::BStr; |
75 |
| - use git_hash::oid; |
76 |
| - use git_index::Entry; |
77 |
| - |
78 |
| - use crate::index; |
79 |
| - |
80 |
| - pub fn checkout<Find>( |
81 |
| - entry: &mut Entry, |
82 |
| - entry_path: &BStr, |
83 |
| - find: &mut Find, |
84 |
| - root: &std::path::Path, |
85 |
| - index::checkout::Options { symlinks }: index::checkout::Options, |
86 |
| - buf: &mut Vec<u8>, |
87 |
| - ) -> Result<(), index::checkout::Error> |
88 |
| - where |
89 |
| - Find: for<'a> FnMut(&oid, &'a mut Vec<u8>) -> Option<git_object::BlobRef<'a>>, |
90 |
| - { |
91 |
| - let dest = root.join(git_features::path::from_byte_slice(entry_path).map_err(|_| { |
92 |
| - index::checkout::Error::IllformedUtf8 { |
93 |
| - path: entry_path.to_owned(), |
94 |
| - } |
95 |
| - })?); |
96 |
| - create_dir_all(dest.parent().expect("entry paths are never empty"))?; // TODO: can this be avoided to create dirs when needed only? |
97 |
| - |
98 |
| - match entry.mode { |
99 |
| - git_index::entry::Mode::FILE | git_index::entry::Mode::FILE_EXECUTABLE => { |
100 |
| - let obj = find(&entry.id, buf).ok_or_else(|| index::checkout::Error::ObjectNotFound { |
101 |
| - oid: entry.id, |
102 |
| - path: root.to_path_buf(), |
103 |
| - })?; |
104 |
| - let mut options = OpenOptions::new(); |
105 |
| - options.write(true).create_new(true); |
106 |
| - #[cfg(unix)] |
107 |
| - if entry.mode == git_index::entry::Mode::FILE_EXECUTABLE { |
108 |
| - use std::os::unix::fs::OpenOptionsExt; |
109 |
| - options.mode(0o777); |
110 |
| - } |
111 |
| - let mut file = options.open(&dest)?; |
112 |
| - file.write_all(obj.data)?; |
113 |
| - let met = file.metadata()?; |
114 |
| - let ctime = met |
115 |
| - .created() |
116 |
| - .map_or(Ok(Duration::default()), |x| x.duration_since(std::time::UNIX_EPOCH))?; |
117 |
| - let mtime = met |
118 |
| - .modified() |
119 |
| - .map_or(Ok(Duration::default()), |x| x.duration_since(std::time::UNIX_EPOCH))?; |
120 |
| - |
121 |
| - update_fstat(entry, ctime, mtime)?; |
122 |
| - } |
123 |
| - git_index::entry::Mode::SYMLINK => { |
124 |
| - let obj = find(&entry.id, buf).ok_or_else(|| index::checkout::Error::ObjectNotFound { |
125 |
| - oid: entry.id, |
126 |
| - path: root.to_path_buf(), |
127 |
| - })?; |
128 |
| - let symlink_destination = git_features::path::from_byte_slice(obj.data) |
129 |
| - .map_err(|_| index::checkout::Error::IllformedUtf8 { path: obj.data.into() })?; |
130 |
| - if symlinks { |
131 |
| - #[cfg(unix)] |
132 |
| - std::os::unix::fs::symlink(symlink_destination, &dest)?; |
133 |
| - #[cfg(windows)] |
134 |
| - if dest.exists() { |
135 |
| - if dest.is_file() { |
136 |
| - std::os::windows::fs::symlink_file(symlink_destination, &dest)?; |
137 |
| - } else { |
138 |
| - std::os::windows::fs::symlink_dir(symlink_destination, &dest)?; |
139 |
| - } |
140 |
| - } |
141 |
| - } else { |
142 |
| - std::fs::write(&dest, obj.data)?; |
143 |
| - } |
144 |
| - let met = std::fs::symlink_metadata(&dest)?; |
145 |
| - let ctime = met |
146 |
| - .created() |
147 |
| - .map_or(Ok(Duration::default()), |x| x.duration_since(std::time::UNIX_EPOCH))?; |
148 |
| - let mtime = met |
149 |
| - .modified() |
150 |
| - .map_or(Ok(Duration::default()), |x| x.duration_since(std::time::UNIX_EPOCH))?; |
151 |
| - update_fstat(entry, ctime, mtime)?; |
152 |
| - } |
153 |
| - git_index::entry::Mode::DIR => todo!(), |
154 |
| - git_index::entry::Mode::COMMIT => todo!(), |
155 |
| - _ => unreachable!(), |
156 |
| - } |
157 |
| - Ok(()) |
158 |
| - } |
159 |
| - |
160 |
| - fn update_fstat(entry: &mut Entry, ctime: Duration, mtime: Duration) -> Result<(), index::checkout::Error> { |
161 |
| - let stat = &mut entry.stat; |
162 |
| - stat.mtime.secs = mtime |
163 |
| - .as_secs() |
164 |
| - .try_into() |
165 |
| - .expect("by 2038 we found a solution for this"); |
166 |
| - stat.mtime.nsecs = mtime.subsec_nanos(); |
167 |
| - stat.ctime.secs = ctime |
168 |
| - .as_secs() |
169 |
| - .try_into() |
170 |
| - .expect("by 2038 we found a solution for this"); |
171 |
| - stat.ctime.nsecs = ctime.subsec_nanos(); |
172 |
| - Ok(()) |
173 |
| - } |
174 |
| - } |
175 |
| -} |
| 3 | +pub mod index; |
0 commit comments