-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #4
- Loading branch information
Showing
23 changed files
with
1,069 additions
and
312 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
use std::fmt::Debug; | ||
|
||
use camino::Utf8Path; | ||
use miette::IntoDiagnostic; | ||
use rustc_hash::FxHashSet; | ||
use tracing::instrument; | ||
|
||
/// Check if a set of paths all have the same parent directory and they are the only paths in that | ||
/// directory (other than dotfiles). | ||
#[instrument(level = "trace")] | ||
pub fn only_paths_in_parent_directory<'p, I, P>(paths: I) -> Option<&'p Utf8Path> | ||
where | ||
I: IntoIterator<Item = &'p P> + Debug, | ||
P: AsRef<Utf8Path> + 'p + ?Sized, | ||
{ | ||
let mut paths = paths.into_iter(); | ||
let mut names = FxHashSet::default(); | ||
let first = paths.next()?.as_ref(); | ||
let parent = first.parent()?; | ||
names.insert(first.file_name()?); | ||
|
||
for path in paths { | ||
let path = path.as_ref(); | ||
if path.parent()? != parent { | ||
return None; | ||
} | ||
names.insert(path.file_name()?); | ||
} | ||
|
||
match path_contains_only_names_and_dotfiles(parent, &names) { | ||
Ok(true) => Some(parent), | ||
Ok(false) => None, | ||
Err(error) => { | ||
tracing::debug!( | ||
directory=%parent, | ||
error=%error, | ||
"Error while listing directory" | ||
); | ||
None | ||
} | ||
} | ||
} | ||
|
||
/// Check if a path contains only files listed in the given set of names and dotfiles. | ||
#[instrument(level = "trace")] | ||
fn path_contains_only_names_and_dotfiles( | ||
path: &Utf8Path, | ||
names: &FxHashSet<&str>, | ||
) -> miette::Result<bool> { | ||
for entry in path.read_dir_utf8().into_diagnostic()? { | ||
let entry = entry.into_diagnostic()?; | ||
let name = entry.file_name(); | ||
if !name.starts_with('.') && !names.contains(name) { | ||
tracing::debug!( | ||
directory=%path, | ||
entry=%name, | ||
"Directory entry is not a dotfile or listed in known paths" | ||
); | ||
return Ok(false); | ||
} | ||
} | ||
|
||
Ok(true) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use command_error::CommandExt; | ||
use miette::IntoDiagnostic; | ||
use test_harness::GitProle; | ||
use test_harness::WorktreeState; | ||
|
||
#[test] | ||
fn convert_bare_dot_git() -> miette::Result<()> { | ||
let prole = GitProle::new()?; | ||
prole.sh(r#" | ||
mkdir -p my-repo/.git | ||
cd my-repo/.git || exit | ||
git init --bare | ||
git worktree add ../main | ||
cd ../main || exit | ||
echo "puppy doggy" > README.md | ||
git add . | ||
git commit -m "Initial commit" | ||
git worktree add ../puppy | ||
git worktree add --detach ../doggy | ||
"#)?; | ||
|
||
prole | ||
.cd_cmd("my-repo/main") | ||
.arg("convert") | ||
.status_checked() | ||
.into_diagnostic()?; | ||
|
||
prole | ||
.repo_state("my-repo") | ||
.worktrees([ | ||
WorktreeState::new_bare(), | ||
WorktreeState::new("main").branch("main"), | ||
WorktreeState::new("puppy").branch("puppy"), | ||
WorktreeState::new("doggy").detached("4023d080"), | ||
]) | ||
.assert(); | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use command_error::CommandExt; | ||
use miette::IntoDiagnostic; | ||
use test_harness::GitProle; | ||
use test_harness::WorktreeState; | ||
|
||
#[test] | ||
fn convert_bare_ends_with_dot_git() -> miette::Result<()> { | ||
let prole = GitProle::new()?; | ||
prole.sh(r#" | ||
mkdir -p my-repo.git | ||
cd my-repo.git || exit | ||
git init --bare | ||
git worktree add ../main | ||
cd ../main || exit | ||
echo "puppy doggy" > README.md | ||
git add . | ||
git commit -m "Initial commit" | ||
git worktree add ../puppy | ||
git worktree add --detach ../doggy | ||
"#)?; | ||
|
||
prole | ||
.cd_cmd("my-repo.git") | ||
.arg("convert") | ||
.status_checked() | ||
.into_diagnostic()?; | ||
|
||
prole | ||
.repo_state("my-repo") | ||
.worktrees([ | ||
WorktreeState::new_bare(), | ||
WorktreeState::new("main").branch("main"), | ||
WorktreeState::new("puppy").branch("puppy"), | ||
WorktreeState::new("doggy").detached("4023d080"), | ||
]) | ||
.assert(); | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use command_error::CommandExt; | ||
use miette::IntoDiagnostic; | ||
use test_harness::GitProle; | ||
use test_harness::WorktreeState; | ||
|
||
#[test] | ||
fn convert_bare_no_dot() -> miette::Result<()> { | ||
let prole = GitProle::new()?; | ||
prole.sh(r#" | ||
mkdir -p my-repo | ||
cd my-repo || exit | ||
git init --bare | ||
git worktree add ../main | ||
cd ../main || exit | ||
echo "puppy doggy" > README.md | ||
git add . | ||
git commit -m "Initial commit" | ||
git worktree add ../puppy | ||
git worktree add --detach ../doggy | ||
"#)?; | ||
|
||
prole | ||
.cd_cmd("main") | ||
.arg("convert") | ||
.status_checked() | ||
.into_diagnostic()?; | ||
|
||
prole | ||
.repo_state("my-repo") | ||
.worktrees([ | ||
WorktreeState::new_bare(), | ||
WorktreeState::new("main").branch("main"), | ||
WorktreeState::new("puppy").branch("puppy"), | ||
WorktreeState::new("doggy").detached("4023d080"), | ||
]) | ||
.assert(); | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use command_error::CommandExt; | ||
use miette::IntoDiagnostic; | ||
use test_harness::GitProle; | ||
use test_harness::WorktreeState; | ||
|
||
#[test] | ||
fn convert_bare_starts_with_dot() -> miette::Result<()> { | ||
let prole = GitProle::new()?; | ||
prole.sh(r#" | ||
mkdir -p my-repo/.bare | ||
cd my-repo/.bare || exit | ||
git init --bare | ||
git worktree add ../main | ||
cd ../main || exit | ||
echo "puppy doggy" > README.md | ||
git add . | ||
git commit -m "Initial commit" | ||
git worktree add ../puppy | ||
git worktree add --detach ../doggy | ||
"#)?; | ||
|
||
prole | ||
.cd_cmd("my-repo/main") | ||
.arg("convert") | ||
.status_checked() | ||
.into_diagnostic()?; | ||
|
||
prole | ||
.repo_state("my-repo") | ||
.worktrees([ | ||
WorktreeState::new_bare(), | ||
WorktreeState::new("main").branch("main"), | ||
WorktreeState::new("puppy").branch("puppy"), | ||
WorktreeState::new("doggy").detached("4023d080"), | ||
]) | ||
.assert(); | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use command_error::CommandExt; | ||
use miette::IntoDiagnostic; | ||
use test_harness::GitProle; | ||
use test_harness::WorktreeState; | ||
|
||
#[test] | ||
fn convert_common_parent() -> miette::Result<()> { | ||
let prole = GitProle::new()?; | ||
prole.setup_repo("my-prefix/my-repo")?; | ||
|
||
prole.sh(r#" | ||
cd my-prefix/my-repo | ||
git worktree add ../puppy | ||
git worktree add ../doggy | ||
"#)?; | ||
|
||
prole | ||
.cd_cmd("my-prefix/my-repo") | ||
.arg("convert") | ||
.status_checked() | ||
.into_diagnostic()?; | ||
|
||
prole | ||
.repo_state("my-prefix") | ||
.worktrees([ | ||
WorktreeState::new_bare(), | ||
WorktreeState::new("main").branch("main"), | ||
WorktreeState::new("puppy").branch("puppy"), | ||
WorktreeState::new("doggy").branch("doggy"), | ||
]) | ||
.assert(); | ||
|
||
Ok(()) | ||
} |
Oops, something went wrong.