Skip to content

Commit e4dc964

Browse files
committed
A first stab at finding git repositories
1 parent 038475e commit e4dc964

File tree

2 files changed

+45
-6
lines changed

2 files changed

+45
-6
lines changed

git-features/src/fs.rs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,38 @@
99
#[cfg(feature = "parallel")]
1010
///
1111
pub mod walkdir {
12-
pub use jwalk::{Error, WalkDir};
12+
pub use jwalk::{DirEntry as DirEntryGeneric, Error, WalkDir};
13+
use std::path::PathBuf;
14+
15+
/// An alias for an uncustomized directory entry to match the one of the non-parallel version offered by `walkdir`.
16+
pub type DirEntry = DirEntryGeneric<((), ())>;
17+
18+
/// Enable sorting by filename
19+
pub fn sorted(w: WalkDir) -> WalkDir {
20+
w.sort(true)
21+
}
22+
23+
/// Obtain the owned, full path the `entry` points to
24+
pub fn direntry_path(entry: &DirEntry) -> PathBuf {
25+
entry.path()
26+
}
1327
}
1428

1529
#[cfg(not(feature = "parallel"))]
1630
///
1731
pub mod walkdir {
18-
pub use walkdir::{Error, WalkDir};
32+
use std::path::PathBuf;
33+
pub use walkdir::{DirEntry, Error, WalkDir};
34+
35+
/// Enable sorting by filename
36+
pub fn sorted(w: WalkDir) -> WalkDir {
37+
w.sort_by(|lhs, rhs| lhs.file_name().cmp(rhs.file_name()))
38+
}
39+
40+
/// Obtain the owned, full path the `entry` points to
41+
pub fn direntry_path(entry: &DirEntry) -> PathBuf {
42+
entry.path().to_owned()
43+
}
1944
}
2045

21-
pub use self::walkdir::WalkDir;
46+
pub use self::walkdir::{direntry_path, sorted, DirEntry, WalkDir};

gitoxide-core/src/organize.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use git_features::progress::Progress;
2-
use std::path::PathBuf;
1+
use git_features::{fs, progress::Progress};
2+
use std::path::{Path, PathBuf};
33

44
#[derive(Copy, Clone, Eq, PartialEq)]
55
pub enum Mode {
@@ -13,7 +13,21 @@ impl Default for Mode {
1313
}
1414
}
1515

16-
pub fn run(_mode: Mode, _source_dir: PathBuf, _destination: PathBuf, _progress: impl Progress) -> anyhow::Result<()> {
16+
// TODO: handle nested repos, skip everything inside a parent directory.
17+
fn find_git_repositories(root: impl AsRef<Path>) -> impl Iterator<Item = PathBuf> {
18+
fn is_repository(path: &PathBuf) -> bool {
19+
path.is_dir() && path.ends_with(".git")
20+
}
21+
22+
let walk = fs::sorted(fs::WalkDir::new(root).follow_links(false));
23+
walk.into_iter()
24+
.filter_map(Result::ok)
25+
.map(|entry: fs::DirEntry| fs::direntry_path(&entry))
26+
.filter(is_repository)
27+
}
28+
29+
pub fn run(_mode: Mode, source_dir: PathBuf, _destination: PathBuf, _progress: impl Progress) -> anyhow::Result<()> {
30+
let _repo_paths = find_git_repositories(source_dir);
1731
Ok(())
1832
}
1933

0 commit comments

Comments
 (0)