Skip to content

Commit 6bef26f

Browse files
authored
Merge pull request #10335 from gitbutlerapp/copilot/fix-0bb3fa6d-d622-42b8-a419-8514052670ca
Fix project title extraction for current directory in `but init`
2 parents 3f24793 + f8d6002 commit 6bef26f

File tree

4 files changed

+44
-10
lines changed

4 files changed

+44
-10
lines changed

crates/gitbutler-project/src/controller.rs

Lines changed: 19 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,18 +101,29 @@ impl Controller {
101101
}
102102
}
103103

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

106-
// title is the base name of the file
107-
let title = path
108-
.iter()
106+
// Resolve the path first to get the actual directory name
107+
let resolved_path = gix::path::realpath(path)?;
108+
let title_is_not_normal_component = path
109+
.components()
109110
.next_back()
110-
.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+
);
111122

112123
let project = Project {
113-
id: ProjectId::generate(),
124+
id,
114125
title,
115-
path: gix::path::realpath(path)?,
126+
path: resolved_path,
116127
api: None,
117128
..Default::default()
118129
};

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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ mod add {
4949
let data_dir = paths::data_dir();
5050
let tmp = tempfile::tempdir().unwrap();
5151
let outcome =
52-
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"))
5353
.unwrap();
5454
assert!(matches!(outcome, AddProjectOutcome::PathNotFound));
5555
}

0 commit comments

Comments
 (0)