Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions git-index/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ mod access {
pub fn entries(&self) -> &[Entry] {
&self.entries
}

pub fn entries_mut(&mut self) -> &mut [Entry] {
&mut self.entries
}
}
}

Expand Down
10 changes: 10 additions & 0 deletions git-worktree/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,13 @@ doctest = false
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
git-index = { version = "^0.1.0", path = "../git-index" }
quick-error = "2.0.1"
git-hash = { version = "^0.9.0", path = "../git-hash" }
git-object = { version = "^0.17.0", path = "../git-object" }

[dev-dependencies]
git-odb = { path = "../git-odb" }
walkdir = "2.3.2"
git-testtools = { path = "../tests/tools" }
tempfile = "3.2.0"
125 changes: 125 additions & 0 deletions git-worktree/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1 +1,126 @@
#![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;
use std::fs::create_dir_all;
use std::path::Path;
use std::time::Duration;

#[cfg(unix)]
use std::os::unix::fs::PermissionsExt;

quick_error! {
#[derive(Debug)]
pub enum Error {
Utf8Error(err: git_object::bstr::Utf8Error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The individual variants never repeat the word Error.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed: eaee855

from()
display("Could not convert path to UTF8: {}", err)
}
TimeError(err: std::time::SystemTimeError) {
from()
display("Could not read file time in proper format: {}", err)
}
ToU32Error(err: std::num::TryFromIntError) {
from()
display("Could not convert seconds to u32: {}", err)
}
IoError(err: std::io::Error) {
from()
display("IO error while writing blob or reading file metadata or changing filetype: {}", err)
}
FindOidError(oid: git_hash::ObjectId, path: std::path::PathBuf) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For consistency, this should be called NotFound { oid: git_hash::ObjectId, path: PathBuf}.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed: eaee855

display("unable to read sha1 file of {} ({})", path.display(), oid.to_hex())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no such thing as a SHA1 file, for all you know an object couldn't be found. This text should be reworded to reflect that.
I like that the path at which the object would be checked out at is mentioned as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same error is used in the git source code you linked: https://github.com/git/git/blob/main/entry.c#L297:L299

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This message originates from a commit made 11 years ago, possibly from a time where packs weren't quite a thing yet, nor SHA256. gitoxide doesn't transcribe git to Rust, but does its best to improve on it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done: a4b880c

}
}
}

/// 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::FindOidError(entry.id, path.to_path_buf()))?;
std::fs::write(&dest, obj.data)?;
if entry.mode == git_index::entry::Mode::FILE_EXECUTABLE {
#[cfg(unix)]
fs::set_permissions(&dest, fs::Permissions::from_mode(0o777))?;
}
let met = std::fs::symlink_metadata(&dest)?;
let ctime = met
.created()
.map_or(Ok(Duration::from_secs(0)), |x| x.duration_since(std::time::UNIX_EPOCH));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be let ctime = met.created()?.duration_since(std::time::UNIX_EPOCH)?;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will fail on Unix as unix does not store ctime. My implementation just sets the ctime to 0 if it does not exist as you had previously suggested.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a great catch!

I always felt a bit estranged about Ok(Duration::from_secs(0)) which can be transformed into (this time with correct error handling) …

                let ctime = met
                    .created()
                    .ok()
                    .map(|x| x.duration_since(std::time::UNIX_EPOCH))
                    .transpose()?
                    .unwrap_or_default();

… but it's definitely a bit wordy then.

Let's change Ok(Duration::from_secs(0)) into Ok(Duration::default()) at least.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done: a4b880c

let mtime = met
.modified()
.map_or(Ok(Duration::from_secs(0)), |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).unwrap();
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::from_secs(0)), |x| x.duration_since(std::time::UNIX_EPOCH));
let mtime = met
.modified()
.map_or(Ok(Duration::from_secs(0)), |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 }
}
}
83 changes: 83 additions & 0 deletions git-worktree/tests/copy_index/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
use crate::{dir_structure, fixture_path, Result};
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() -> 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);

let srepo_files: Vec<_> = repo_files.iter().flat_map(|p| p.strip_prefix(&path)).collect();
let scopy_files: Vec<_> = copy_files.iter().flat_map(|p| p.strip_prefix(output)).collect();
assert_eq!(srepo_files, scopy_files);

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() & 0b111 << 6,
fs::symlink_metadata(file2)?.mode() & 0b111 << 6
);
}

Ok(())
}

#[test]
fn test_copy_index_without_symlinks() -> 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);

let srepo_files: Vec<_> = repo_files.iter().flat_map(|p| p.strip_prefix(&path)).collect();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why prefix repo_files with an s? What does s mean? Or asked differently, could these just be inline to avoid having to make up a new name?

If you think it should be clear these are stripped, maybe create a little closure like and use it like this:

assert_eq!(strip_root_path(path, repo_files), strip_root_path(path, copy_files));

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inlined: a4b880c

let scopy_files: Vec<_> = copy_files.iter().flat_map(|p| p.strip_prefix(output)).collect();
assert_eq!(srepo_files, scopy_files);

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() & 0b111 << 6,
fs::symlink_metadata(file2)?.mode() & 0b111 << 6
);
}
}

Ok(())
}
19 changes: 19 additions & 0 deletions git-worktree/tests/fixtures/make_repo.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash
set -eu -o pipefail

git init -q

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here must be git config commit.gpgsign false for this to work everywhere.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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"
25 changes: 25 additions & 0 deletions git-worktree/tests/mod.rs
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
}