Skip to content

Commit f8d6002

Browse files
committed
refactor
1 parent aa08ec0 commit f8d6002

File tree

4 files changed

+41
-33
lines changed

4 files changed

+41
-33
lines changed

crates/gitbutler-project/src/controller.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::path::{Path, PathBuf};
1+
use std::path::{Component, Path, PathBuf};
22

33
use anyhow::{anyhow, bail, Context, Result};
44
use gitbutler_error::error;
@@ -101,19 +101,27 @@ impl Controller {
101101
}
102102
}
103103

104-
let id = uuid::Uuid::new_v4().to_string();
104+
let id = ProjectId::generate();
105105

106106
// Resolve the path first to get the actual directory name
107107
let resolved_path = gix::path::realpath(path)?;
108-
109-
// title is the base name of the resolved path
110-
let title = resolved_path
111-
.iter()
108+
let title_is_not_normal_component = path
109+
.components()
112110
.next_back()
113-
.map_or_else(|| id.clone(), |p| p.to_str().unwrap().to_string());
111+
.is_none_or(|c| !matches!(c, Component::Normal(_)));
112+
let path_for_title = if title_is_not_normal_component {
113+
&resolved_path
114+
} else {
115+
path
116+
};
117+
118+
let title = path_for_title.file_name().map_or_else(
119+
|| id.to_string(),
120+
|name| name.to_string_lossy().into_owned(),
121+
);
114122

115123
let project = Project {
116-
id: ProjectId::generate(),
124+
id,
117125
title,
118126
path: resolved_path,
119127
api: None,

crates/gitbutler-project/src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,10 @@ pub fn add<P: AsRef<Path>>(path: P) -> anyhow::Result<AddProjectOutcome> {
6868
}
6969

7070
/// Testing purpose only.
71-
pub fn add_with_path<P: AsRef<Path>>(data_dir: P, path: P) -> anyhow::Result<AddProjectOutcome> {
71+
pub fn add_with_path(
72+
data_dir: impl AsRef<Path>,
73+
path: impl AsRef<Path>,
74+
) -> anyhow::Result<AddProjectOutcome> {
7275
let controller = Controller::from_path(data_dir.as_ref());
7376
controller.add(path)
7477
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use gitbutler_testsupport::paths;
2+
3+
#[test]
4+
fn current_directory_dot() -> anyhow::Result<()> {
5+
let tmp = paths::data_dir();
6+
let repository = gitbutler_testsupport::TestProject::default();
7+
let repo_path = repository.path();
8+
9+
// Change to the repository directory and add "." as the path
10+
std::env::set_current_dir(repo_path)?;
11+
12+
let project = gitbutler_project::add_with_path(tmp.path(), ".")?.unwrap_project();
13+
14+
let expected_title = repo_path.file_name().unwrap().to_str().unwrap();
15+
assert_eq!(
16+
project.title, expected_title,
17+
"Project title should be the actual directory name, not '.'"
18+
);
19+
Ok(())
20+
}

crates/gitbutler-project/tests/projects/main.rs renamed to crates/gitbutler-project/tests/project/main.rs

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,29 +23,6 @@ mod add {
2323
);
2424
}
2525

26-
#[test]
27-
fn current_directory_dot() {
28-
let tmp = paths::data_dir();
29-
let repository = gitbutler_testsupport::TestProject::default();
30-
let repo_path = repository.path();
31-
32-
// Change to the repository directory and add "." as the path
33-
let original_dir = std::env::current_dir().unwrap();
34-
std::env::set_current_dir(&repo_path).unwrap();
35-
36-
let project = gitbutler_project::add_with_path(tmp.path(), std::path::Path::new("."))
37-
.unwrap()
38-
.unwrap_project();
39-
40-
// Restore original directory
41-
std::env::set_current_dir(original_dir).unwrap();
42-
43-
// Project title should be the actual directory name, not "."
44-
let expected_title = repo_path.file_name().unwrap().to_str().unwrap();
45-
assert_eq!(project.title, expected_title);
46-
assert_ne!(project.title, "."); // Should NOT be "."
47-
}
48-
4926
mod error {
5027
use super::*;
5128
use gitbutler_project::AddProjectOutcome;
@@ -72,7 +49,7 @@ mod add {
7249
let data_dir = paths::data_dir();
7350
let tmp = tempfile::tempdir().unwrap();
7451
let outcome =
75-
gitbutler_project::add_with_path(data_dir.path(), &tmp.path().join("missing"))
52+
gitbutler_project::add_with_path(data_dir.path(), tmp.path().join("missing"))
7653
.unwrap();
7754
assert!(matches!(outcome, AddProjectOutcome::PathNotFound));
7855
}

0 commit comments

Comments
 (0)