Skip to content

Commit 2776afa

Browse files
committed
new
1 parent cdd8632 commit 2776afa

File tree

20 files changed

+488
-116
lines changed

20 files changed

+488
-116
lines changed

Cargo.lock

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ but-core = { path = "crates/but-core" }
7676
but-workspace = { path = "crates/but-workspace" }
7777
but-hunk-assignment = { path = "crates/but-hunk-assignment" }
7878
but-hunk-dependency = { path = "crates/but-hunk-dependency" }
79+
but-worktrees = { path = "crates/but-worktrees" }
7980
but-db = { path = "crates/but-db" }
8081
but-path = { path = "crates/but-path" }
8182
but-graph = { path = "crates/but-graph" }

crates/but-api/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ gitbutler-user.workspace = true
2727
gitbutler-project.workspace = true
2828
gitbutler-secret.workspace = true
2929
but-workspace.workspace = true
30+
but-worktrees.workspace = true
3031
but-core.workspace = true
3132
but-hunk-assignment.workspace = true
3233
but-action.workspace = true

crates/but-api/src/commands/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ pub mod stack;
2020
pub mod users;
2121
pub mod virtual_branches;
2222
pub mod workspace;
23+
pub mod worktree;
2324
pub mod zip;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
use crate::error::Error;
2+
use but_api_macros::api_cmd;
3+
use but_settings::AppSettings;
4+
use but_worktrees::new::NewWorktreeOutcome;
5+
use gitbutler_command_context::CommandContext;
6+
use gitbutler_project::ProjectId;
7+
use tracing::instrument;
8+
9+
#[api_cmd]
10+
#[cfg_attr(feature = "tauri", tauri::command(async))]
11+
#[instrument(err(Debug))]
12+
pub fn worktree_new(project_id: ProjectId, reference: String) -> Result<NewWorktreeOutcome, Error> {
13+
let project = gitbutler_project::get(project_id)?;
14+
let guard = project.exclusive_worktree_access();
15+
let mut ctx = CommandContext::open(&project, AppSettings::load_from_default_path_creating()?)?;
16+
17+
but_worktrees::new::worktree_new(&mut ctx, guard.read_permission(), &reference)
18+
.map_err(Into::into)
19+
}
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
-- Create worktrees table
22
CREATE TABLE `worktrees`(
3-
`id` TEXT NOT NULL PRIMARY KEY,
3+
`path` TEXT NOT NULL PRIMARY KEY,
4+
`reference` TEXT NOT NULL,
45
`base` TEXT NOT NULL,
5-
`path` TEXT NOT NULL,
66
`source` TEXT NOT NULL
77
);
8-
9-
CREATE INDEX index_worktrees_on_path ON worktrees (path);

crates/but-db/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub use file_write_locks::FileWriteLock;
2020
mod workspace_rules;
2121
pub use workspace_rules::WorkspaceRule;
2222
mod worktrees;
23-
pub use worktrees::{Worktree, WorktreeSource};
23+
pub use worktrees::Worktree;
2424

2525
use diesel_migrations::{EmbeddedMigrations, MigrationHarness, embed_migrations};
2626
pub const MIGRATIONS: EmbeddedMigrations = embed_migrations!("./migrations");

crates/but-db/src/schema.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,10 @@ diesel::table! {
8888
}
8989

9090
diesel::table! {
91-
worktrees (id) {
92-
id -> Text,
93-
base -> Text,
91+
worktrees (path) {
9492
path -> Text,
93+
reference -> Text,
94+
base -> Text,
9595
source -> Text,
9696
}
9797
}

crates/but-db/src/worktrees.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,16 @@ use crate::schema::worktrees::dsl::worktrees;
66
use diesel::prelude::{Insertable, Queryable, Selectable};
77
use serde::{Deserialize, Serialize};
88

9-
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
10-
pub enum WorktreeSource {
11-
Branch(String),
12-
}
13-
149
#[derive(
1510
Debug, Clone, PartialEq, Serialize, Deserialize, Queryable, Selectable, Insertable, Identifiable,
1611
)]
1712
#[diesel(table_name = crate::schema::worktrees)]
1813
#[diesel(check_for_backend(diesel::sqlite::Sqlite))]
14+
#[diesel(primary_key(path))]
1915
pub struct Worktree {
20-
pub id: String,
21-
pub base: String,
2216
pub path: String,
17+
pub reference: String,
18+
pub base: String,
2319
pub source: String,
2420
}
2521

@@ -41,16 +37,16 @@ impl WorktreesHandle<'_> {
4137
Ok(())
4238
}
4339

44-
pub fn get(&mut self, id: &str) -> Result<Option<Worktree>, diesel::result::Error> {
40+
pub fn get(&mut self, path: &str) -> Result<Option<Worktree>, diesel::result::Error> {
4541
let worktree = worktrees
46-
.filter(crate::schema::worktrees::id.eq(id))
42+
.filter(crate::schema::worktrees::path.eq(path))
4743
.first::<Worktree>(&mut self.db.conn)
4844
.optional()?;
4945
Ok(worktree)
5046
}
5147

52-
pub fn delete(&mut self, id: &str) -> Result<(), diesel::result::Error> {
53-
diesel::delete(worktrees.filter(crate::schema::worktrees::id.eq(id)))
48+
pub fn delete(&mut self, path: &str) -> Result<(), diesel::result::Error> {
49+
diesel::delete(worktrees.filter(crate::schema::worktrees::path.eq(path)))
5450
.execute(&mut self.db.conn)?;
5551
Ok(())
5652
}

crates/but-worktrees/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,17 @@ doctest = false
1111
[dependencies]
1212
anyhow.workspace = true
1313
but-db.workspace = true
14+
but-workspace.workspace = true
15+
but-graph.workspace = true
1416
gix.workspace = true
1517
gitbutler-command-context.workspace = true
1618
gitbutler-project.workspace = true
1719
gitbutler-stack.workspace = true
1820
serde.workspace = true
1921
serde_json.workspace = true
2022
uuid.workspace = true
23+
bstr.workspace = true
24+
tracing.workspace = true
2125

2226
[dev-dependencies]
2327
gix-testtools.workspace = true

0 commit comments

Comments
 (0)