-
-
Notifications
You must be signed in to change notification settings - Fork 398
Checkout of index entries into a directory #315
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
4177d72
eaee855
e838eaa
25a9dc1
a4b880c
2abacf9
73a063a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,129 @@ | ||
| #![forbid(unsafe_code, rust_2018_idioms)] | ||
| //! Git Worktree | ||
|
|
||
| use git_hash::oid; | ||
| use git_object::bstr::ByteSlice; | ||
| use quick_error::quick_error; | ||
| use std::convert::TryFrom; | ||
| use std::fs::{create_dir_all, OpenOptions}; | ||
| use std::io::Write; | ||
| use std::path::{Path, PathBuf}; | ||
| use std::time::Duration; | ||
|
|
||
| #[cfg(unix)] | ||
| use std::os::unix::fs::OpenOptionsExt; | ||
|
|
||
| quick_error! { | ||
| #[derive(Debug)] | ||
| pub enum Error { | ||
| Utf8(err: git_object::bstr::Utf8Error) { | ||
| from() | ||
| display("Could not convert path to UTF8: {}", err) | ||
| } | ||
| Time(err: std::time::SystemTimeError) { | ||
| from() | ||
| display("Could not read file time in proper format: {}", err) | ||
| } | ||
| U32Conversion(err: std::num::TryFromIntError) { | ||
| from() | ||
| display("Could not convert seconds to u32: {}", err) | ||
| } | ||
| Io(err: std::io::Error) { | ||
| from() | ||
| display("IO error while writing blob or reading file metadata or changing filetype: {}", err) | ||
| } | ||
| NotFound(oid: git_hash::ObjectId, path: PathBuf) { | ||
| display("unable find object of {} ({})", path.display(), oid.to_hex()) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Copy index to `path` | ||
| pub fn copy_index<P, Find>(state: &mut git_index::State, path: P, mut find: Find, opts: Options) -> Result<(), Error> | ||
| where | ||
| P: AsRef<Path>, | ||
| Find: for<'a> FnMut(&oid, &'a mut Vec<u8>) -> Option<git_object::BlobRef<'a>>, | ||
| { | ||
| let path = path.as_ref(); | ||
| let mut buf = Vec::new(); | ||
| let mut entry_time = Vec::new(); // Entries whose timestamps have to be updated | ||
| for (i, entry) in state.entries().iter().enumerate() { | ||
| if entry.flags.contains(git_index::entry::Flags::SKIP_WORKTREE) { | ||
| continue; | ||
| } | ||
| let entry_path = entry.path(state).to_path()?; | ||
| let dest = path.join(entry_path); | ||
| create_dir_all(dest.parent().expect("entry paths are never empty"))?; | ||
| match entry.mode { | ||
| git_index::entry::Mode::FILE | git_index::entry::Mode::FILE_EXECUTABLE => { | ||
| let obj = find(&entry.id, &mut buf).ok_or_else(|| Error::NotFound(entry.id, path.to_path_buf()))?; | ||
| let mut options = OpenOptions::new(); | ||
| options.write(true).create_new(true); | ||
| #[cfg(unix)] | ||
| if entry.mode == git_index::entry::Mode::FILE_EXECUTABLE { | ||
| options.mode(0o777); | ||
| } | ||
| let mut file = options.open(&dest)?; | ||
| file.write_all(obj.data)?; | ||
| let met = file.metadata()?; | ||
| let ctime = met | ||
| .created() | ||
| .map_or(Ok(Duration::default()), |x| x.duration_since(std::time::UNIX_EPOCH)); | ||
| let mtime = met | ||
| .modified() | ||
| .map_or(Ok(Duration::default()), |x| x.duration_since(std::time::UNIX_EPOCH)); | ||
| entry_time.push((ctime?, mtime?, i)); | ||
| } | ||
| git_index::entry::Mode::SYMLINK => { | ||
| let obj = find(&entry.id, &mut buf).ok_or_else(|| Error::NotFound(entry.id, path.to_path_buf()))?; | ||
| let linked_to = obj.data.to_path()?; | ||
| if opts.symlinks { | ||
| #[cfg(unix)] | ||
| std::os::unix::fs::symlink(linked_to, &dest)?; | ||
| #[cfg(windows)] | ||
| if dest.exists() { | ||
| if dest.is_file() { | ||
| std::os::windows::fs::symlink_file(linked_to, &dest)?; | ||
| } else { | ||
| std::os::windows::fs::symlink_dir(linked_to, &dest)?; | ||
| } | ||
| } | ||
| } else { | ||
| std::fs::write(&dest, obj.data)?; | ||
| } | ||
| let met = std::fs::symlink_metadata(&dest)?; | ||
| let ctime = met | ||
| .created() | ||
| .map_or(Ok(Duration::default()), |x| x.duration_since(std::time::UNIX_EPOCH)); | ||
| let mtime = met | ||
| .modified() | ||
| .map_or(Ok(Duration::default()), |x| x.duration_since(std::time::UNIX_EPOCH)); | ||
| entry_time.push((ctime?, mtime?, i)); | ||
| } | ||
| git_index::entry::Mode::DIR => todo!(), | ||
| git_index::entry::Mode::COMMIT => todo!(), | ||
| _ => unreachable!(), | ||
| } | ||
| } | ||
| let entries = state.entries_mut(); | ||
| for (ctime, mtime, i) in entry_time { | ||
| let stat = &mut entries[i].stat; | ||
| stat.mtime.secs = u32::try_from(mtime.as_secs())?; | ||
| stat.mtime.nsecs = mtime.subsec_nanos(); | ||
| stat.ctime.secs = u32::try_from(ctime.as_secs())?; | ||
| stat.ctime.nsecs = ctime.subsec_nanos(); | ||
| } | ||
| Ok(()) | ||
| } | ||
|
|
||
| /// Options for [copy_index](crate::copy_index) | ||
| pub struct Options { | ||
| /// Enable/disable symlinks | ||
| pub symlinks: bool, | ||
| } | ||
|
|
||
| impl Default for Options { | ||
| fn default() -> Self { | ||
| Options { symlinks: true } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| use crate::{dir_structure, fixture_path}; | ||
| use git_object::bstr::ByteSlice; | ||
| use git_odb::FindExt; | ||
| use git_worktree::{copy_index, Options}; | ||
| use std::fs; | ||
|
|
||
| #[cfg(unix)] | ||
| use std::os::unix::prelude::MetadataExt; | ||
|
|
||
| #[test] | ||
| fn test_copy_index() -> crate::Result<()> { | ||
| let path = fixture_path("make_repo"); | ||
| let path_git = path.join(".git"); | ||
| let mut file = git_index::File::at(path_git.join("index"), git_index::decode::Options::default())?; | ||
| let output_dir = tempfile::tempdir()?; | ||
| let output = output_dir.path(); | ||
| let odb_handle = git_odb::at(path_git.join("objects"))?; | ||
|
|
||
| copy_index( | ||
| &mut file, | ||
| &output, | ||
| move |oid, buf| odb_handle.find_blob(oid, buf).ok(), | ||
| Options::default(), | ||
| )?; | ||
|
|
||
| let repo_files = dir_structure(&path); | ||
| let copy_files = dir_structure(output); | ||
|
|
||
| assert_eq!( | ||
| repo_files | ||
| .iter() | ||
| .flat_map(|p| p.strip_prefix(&path)) | ||
| .collect::<Vec<_>>(), | ||
| copy_files | ||
| .iter() | ||
| .flat_map(|p| p.strip_prefix(output)) | ||
| .collect::<Vec<_>>() | ||
| ); | ||
|
|
||
| for (file1, file2) in repo_files.iter().zip(copy_files.iter()) { | ||
| assert_eq!(fs::read(file1)?, fs::read(file2)?); | ||
| #[cfg(unix)] | ||
| assert_eq!( | ||
| fs::symlink_metadata(file1)?.mode() & 0o700, | ||
| fs::symlink_metadata(file2)?.mode() & 0o700, | ||
| "Testing if the permissions (normal/executable) of {} and {} are the same", | ||
| file1.display(), | ||
| file2.display(), | ||
| ); | ||
| } | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_copy_index_without_symlinks() -> crate::Result<()> { | ||
| let path = fixture_path("make_repo"); | ||
| let path_git = path.join(".git"); | ||
| let mut file = git_index::File::at(path_git.join("index"), git_index::decode::Options::default())?; | ||
| let output_dir = tempfile::tempdir()?; | ||
| let output = output_dir.path(); | ||
| let odb_handle = git_odb::at(path_git.join("objects"))?; | ||
|
|
||
| copy_index( | ||
| &mut file, | ||
| &output, | ||
| move |oid, buf| odb_handle.find_blob(oid, buf).ok(), | ||
| Options { symlinks: false }, | ||
| )?; | ||
|
|
||
| let repo_files = dir_structure(&path); | ||
| let copy_files = dir_structure(output); | ||
|
|
||
| assert_eq!( | ||
| repo_files | ||
| .iter() | ||
| .flat_map(|p| p.strip_prefix(&path)) | ||
| .collect::<Vec<_>>(), | ||
| copy_files | ||
| .iter() | ||
| .flat_map(|p| p.strip_prefix(output)) | ||
| .collect::<Vec<_>>() | ||
| ); | ||
|
|
||
| for (file1, file2) in repo_files.iter().zip(copy_files.iter()) { | ||
| if file1.is_symlink() { | ||
| assert!(!file2.is_symlink()); | ||
| assert_eq!(fs::read(file2)?.to_path()?, fs::read_link(file1)?); | ||
| } else { | ||
| assert_eq!(fs::read(file1)?, fs::read(file2)?); | ||
| #[cfg(unix)] | ||
| assert_eq!( | ||
| fs::symlink_metadata(file1)?.mode() & 0o700, | ||
| fs::symlink_metadata(file2)?.mode() & 0o700, | ||
| "Testing if the permissions (normal/executable) of {} and {} are the same", | ||
| file1.display(), | ||
| file2.display(), | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| Ok(()) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| #!/bin/bash | ||
| set -eu -o pipefail | ||
|
|
||
| git init -q | ||
| git config commit.gpgsign false | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here must be
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed: a4b880c |
||
| touch a | ||
| echo "Test Vals" > a | ||
| touch b | ||
| touch c | ||
| touch executable.sh | ||
| chmod +x executable.sh | ||
|
|
||
| mkdir d | ||
| touch d/a | ||
| echo "Subdir" > d/a | ||
| ln -sf d/a sa | ||
|
|
||
| git add -A | ||
| git commit -m "Commit" | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| use std::path::{Path, PathBuf}; | ||
| use walkdir::WalkDir; | ||
|
|
||
| mod copy_index; | ||
|
|
||
| type Result<T = ()> = std::result::Result<T, Box<dyn std::error::Error>>; | ||
|
|
||
| pub fn dir_structure<P: AsRef<Path>>(path: P) -> Vec<PathBuf> { | ||
| let path = path.as_ref(); | ||
| let mut ps: Vec<_> = WalkDir::new(path) | ||
| .into_iter() | ||
| .filter_entry(|e| e.path() == path || !e.file_name().to_str().map(|s| s.starts_with('.')).unwrap_or(false)) | ||
| .flatten() | ||
| .filter(|e| e.path().is_file()) | ||
| .map(|p| p.path().to_path_buf()) | ||
| .collect(); | ||
| ps.sort(); | ||
| ps | ||
| } | ||
|
|
||
| pub fn fixture_path(name: &str) -> PathBuf { | ||
| let dir = | ||
| git_testtools::scripted_fixture_repo_read_only(Path::new(name).with_extension("sh")).expect("script works"); | ||
| dir | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.