Skip to content

Commit 84c998b

Browse files
committed
Merge branch 'organize-into-own-child'
2 parents b6f2b81 + f1b33ba commit 84c998b

File tree

4 files changed

+45
-2
lines changed

4 files changed

+45
-2
lines changed

gitoxide-core/src/organize.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::borrow::Cow;
12
use std::{
23
ffi::OsStr,
34
path::{Path, PathBuf},
@@ -198,9 +199,25 @@ fn handle(
198199
destination.display()
199200
)),
200201
Mode::Execute => {
201-
std::fs::create_dir_all(destination.parent().expect("repo destination is not the root"))?;
202+
if destination.starts_with(
203+
git_workdir
204+
.canonicalize()
205+
.ok()
206+
.map(Cow::Owned)
207+
.unwrap_or(Cow::Borrowed(git_workdir)),
208+
) {
209+
let tempdir = tempfile::tempdir_in(canonicalized_destination)?;
210+
let tempdest = tempdir
211+
.path()
212+
.join(destination.file_name().expect("repo destination is not the root"));
213+
std::fs::rename(git_workdir, &tempdest)?;
214+
std::fs::create_dir_all(destination.parent().expect("repo destination is not the root"))?;
215+
std::fs::rename(&tempdest, &destination)?;
216+
} else {
217+
std::fs::create_dir_all(destination.parent().expect("repo destination is not the root"))?;
218+
std::fs::rename(git_workdir, &destination)?;
219+
}
202220
progress.done(format!("Moving {} to {}", git_workdir.display(), destination.display()));
203-
std::fs::rename(git_workdir, &destination)?;
204221
}
205222
}
206223
Ok(())

tests/journey/ein.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,24 @@ title "Porcelain ${kind}"
139139
expect_run_sh $SUCCESSFULLY 'find . -maxdepth 2 | sort'
140140
}
141141
)
142+
143+
(with "--execute with move into subdirectory of itself"
144+
(cd example.com
145+
rm -Rf a-repo-with-extension origin-and-fork
146+
mv one-origin ../
147+
cd ..
148+
rmdir example.com && mv one-origin example.com
149+
)
150+
it "succeeds" && {
151+
WITH_SNAPSHOT="$snapshot/execute-success-new-root" \
152+
expect_run_sh $SUCCESSFULLY "$exe tool organize --execute 2>/dev/null"
153+
}
154+
155+
it "does alter the directory structure as these are already in place" && {
156+
WITH_SNAPSHOT="$snapshot/directory-structure-after-organize-to-new-root" \
157+
expect_run_sh $SUCCESSFULLY 'find . -maxdepth 2 -type d | sort'
158+
}
159+
)
142160
)
143161
if test "$kind" != "max-pure"; then
144162
(with "running with no further arguments"
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.
2+
./dir
3+
./example.com
4+
./example.com/one-origin
5+
./no-origin
6+
./no-origin/.git
7+
./special-origin
8+
./special-origin/.git

tests/snapshots/porcelain/tool/organize/execute-success-new-root

Whitespace-only changes.

0 commit comments

Comments
 (0)