Skip to content

Commit adbc375

Browse files
Merge pull request #10619 from gitbutlerapp/but-worktrees
`but worktree new`
2 parents 953a579 + 2776afa commit adbc375

File tree

21 files changed

+736
-1
lines changed

21 files changed

+736
-1
lines changed

Cargo.lock

Lines changed: 24 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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- Drop worktrees table
2+
DROP TABLE worktrees;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-- Create worktrees table
2+
CREATE TABLE `worktrees`(
3+
`path` TEXT NOT NULL PRIMARY KEY,
4+
`reference` TEXT NOT NULL,
5+
`base` TEXT NOT NULL,
6+
`source` TEXT NOT NULL
7+
);

crates/but-db/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ mod file_write_locks;
1919
pub use file_write_locks::FileWriteLock;
2020
mod workspace_rules;
2121
pub use workspace_rules::WorkspaceRule;
22+
mod worktrees;
23+
pub use worktrees::Worktree;
2224

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

crates/but-db/src/schema.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,12 @@ diesel::table! {
8686
approved -> Nullable<Bool>,
8787
}
8888
}
89+
90+
diesel::table! {
91+
worktrees (path) {
92+
path -> Text,
93+
reference -> Text,
94+
base -> Text,
95+
source -> Text,
96+
}
97+
}

crates/but-db/src/worktrees.rs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
use diesel::{ExpressionMethods, Identifiable, OptionalExtension as _, QueryDsl, RunQueryDsl};
2+
3+
use crate::DbHandle;
4+
use crate::schema::worktrees::dsl::worktrees;
5+
6+
use diesel::prelude::{Insertable, Queryable, Selectable};
7+
use serde::{Deserialize, Serialize};
8+
9+
#[derive(
10+
Debug, Clone, PartialEq, Serialize, Deserialize, Queryable, Selectable, Insertable, Identifiable,
11+
)]
12+
#[diesel(table_name = crate::schema::worktrees)]
13+
#[diesel(check_for_backend(diesel::sqlite::Sqlite))]
14+
#[diesel(primary_key(path))]
15+
pub struct Worktree {
16+
pub path: String,
17+
pub reference: String,
18+
pub base: String,
19+
pub source: String,
20+
}
21+
22+
impl DbHandle {
23+
pub fn worktrees(&mut self) -> WorktreesHandle<'_> {
24+
WorktreesHandle { db: self }
25+
}
26+
}
27+
28+
pub struct WorktreesHandle<'a> {
29+
db: &'a mut DbHandle,
30+
}
31+
32+
impl WorktreesHandle<'_> {
33+
pub fn insert(&mut self, worktree: Worktree) -> Result<(), diesel::result::Error> {
34+
diesel::insert_into(worktrees)
35+
.values(worktree)
36+
.execute(&mut self.db.conn)?;
37+
Ok(())
38+
}
39+
40+
pub fn get(&mut self, path: &str) -> Result<Option<Worktree>, diesel::result::Error> {
41+
let worktree = worktrees
42+
.filter(crate::schema::worktrees::path.eq(path))
43+
.first::<Worktree>(&mut self.db.conn)
44+
.optional()?;
45+
Ok(worktree)
46+
}
47+
48+
pub fn delete(&mut self, path: &str) -> Result<(), diesel::result::Error> {
49+
diesel::delete(worktrees.filter(crate::schema::worktrees::path.eq(path)))
50+
.execute(&mut self.db.conn)?;
51+
Ok(())
52+
}
53+
54+
pub fn list(&mut self) -> Result<Vec<Worktree>, diesel::result::Error> {
55+
let worktree_list = worktrees.load::<Worktree>(&mut self.db.conn)?;
56+
Ok(worktree_list)
57+
}
58+
}

0 commit comments

Comments
 (0)