Skip to content

Commit bd286a1

Browse files
committed
Use Project instead of Path for most handlers
Reduce repetitive Project::find_by_path calls by changing many command handler signatures to accept a gitbutler_project::Project reference instead of a &Path. The main entry points now call get_or_init_project(&args.current_dir) once and pass the Project to downstream handlers. This centralizes project initialization and avoids duplicating find_by_path logic across modules. Affected handlers include base, branch, command, commit, describe, log, mark, oplog, rub, status, and others called from main.rs. Subcommands under Subcommands::Cursor and Subcommands::Claude were intentionally left unchanged. Removed unused std::path::Path imports where appropriate.
1 parent a37a829 commit bd286a1

File tree

11 files changed

+78
-97
lines changed

11 files changed

+78
-97
lines changed

crates/but/src/base/mod.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::path::Path;
2-
31
use colored::Colorize;
42
use gitbutler_branch_actions::upstream_integration::{
53
BranchStatus::{Conflicted, Empty, Integrated, SaflyUpdatable},
@@ -21,8 +19,7 @@ pub enum Subcommands {
2119
Update,
2220
}
2321

24-
pub fn handle(cmd: &Subcommands, repo_path: &Path, json: bool) -> anyhow::Result<()> {
25-
let project = Project::find_by_path(repo_path)?;
22+
pub fn handle(cmd: &Subcommands, project: &Project, json: bool) -> anyhow::Result<()> {
2623
match cmd {
2724
Subcommands::Check => {
2825
if !json {

crates/but/src/branch/mod.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::path::Path;
2-
31
use but_settings::AppSettings;
42
use gitbutler_command_context::CommandContext;
53
use gitbutler_project::Project;
@@ -22,15 +20,14 @@ pub enum Subcommands {
2220
},
2321
}
2422

25-
pub fn handle(cmd: &Subcommands, repo_path: &Path, _json: bool) -> anyhow::Result<()> {
26-
let project = Project::find_by_path(repo_path)?;
23+
pub fn handle(cmd: &Subcommands, project: &Project, _json: bool) -> anyhow::Result<()> {
2724
match cmd {
2825
Subcommands::New {
2926
branch_name,
3027
anchor,
3128
} => {
3229
let ctx =
33-
CommandContext::open(&project, AppSettings::load_from_default_path_creating()?)?;
30+
CommandContext::open(project, AppSettings::load_from_default_path_creating()?)?;
3431
// Get branch name or use canned name
3532
let branch_name = if let Some(name) = branch_name {
3633
let repo = ctx.gix_repo()?;

crates/but/src/command/mod.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
1-
use std::path::Path;
2-
31
use but_action::Source;
42
use but_settings::AppSettings;
53
use gitbutler_command_context::CommandContext;
64
use gitbutler_project::Project;
75
use serde::Serialize;
86

97
pub(crate) fn handle_changes(
10-
repo_path: &Path,
8+
project: &Project,
119
json: bool,
1210
handler: impl Into<but_action::ActionHandler>,
1311
change_description: &str,
1412
) -> anyhow::Result<()> {
15-
let project = Project::find_by_path(repo_path).expect("Failed to create project from path");
16-
let ctx = &mut CommandContext::open(&project, AppSettings::load_from_default_path_creating()?)?;
13+
let ctx = &mut CommandContext::open(project, AppSettings::load_from_default_path_creating()?)?;
1714
let response = but_action::handle_changes(
1815
ctx,
1916
change_description,
@@ -34,13 +31,12 @@ impl From<crate::args::actions::Handler> for but_action::ActionHandler {
3431
}
3532

3633
pub(crate) fn list_actions(
37-
repo_path: &Path,
34+
project: &Project,
3835
json: bool,
3936
offset: i64,
4037
limit: i64,
4138
) -> anyhow::Result<()> {
42-
let project = Project::find_by_path(repo_path).expect("Failed to create project from path");
43-
let ctx = &mut CommandContext::open(&project, AppSettings::load_from_default_path_creating()?)?;
39+
let ctx = &mut CommandContext::open(project, AppSettings::load_from_default_path_creating()?)?;
4440

4541
let response = but_action::list_actions(ctx, offset, limit)?;
4642
print(&response, json)

crates/but/src/commit/mod.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,9 @@ use gitbutler_command_context::CommandContext;
1111
use gitbutler_project::Project;
1212
use std::collections::BTreeMap;
1313
use std::io::{self, Write};
14-
use std::path::Path;
1514

16-
pub(crate) fn insert_blank_commit(repo_path: &Path, _json: bool, target: &str) -> Result<()> {
17-
let project = Project::find_by_path(repo_path)?;
18-
let mut ctx = CommandContext::open(&project, AppSettings::load_from_default_path_creating()?)?;
15+
pub(crate) fn insert_blank_commit(project: &Project, _json: bool, target: &str) -> Result<()> {
16+
let mut ctx = CommandContext::open(project, AppSettings::load_from_default_path_creating()?)?;
1917

2018
// Resolve the target ID
2119
let cli_ids = CliId::from_str(&mut ctx, target)?;
@@ -135,14 +133,13 @@ fn find_stack_containing_commit(
135133
}
136134

137135
pub(crate) fn commit(
138-
repo_path: &Path,
136+
project: &Project,
139137
_json: bool,
140138
message: Option<&str>,
141139
branch_hint: Option<&str>,
142140
only: bool,
143141
) -> anyhow::Result<()> {
144-
let project = Project::find_by_path(repo_path)?;
145-
let mut ctx = CommandContext::open(&project, AppSettings::load_from_default_path_creating()?)?;
142+
let mut ctx = CommandContext::open(project, AppSettings::load_from_default_path_creating()?)?;
146143

147144
// Get all stacks using but-api
148145
let project_id = project.id;

crates/but/src/describe/mod.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@ use but_settings::AppSettings;
44
use gitbutler_command_context::CommandContext;
55
use gitbutler_oxidize::ObjectIdExt;
66
use gitbutler_project::Project;
7-
use std::path::Path;
87

98
pub(crate) fn edit_commit_message(
10-
repo_path: &Path,
9+
project: &Project,
1110
_json: bool,
1211
commit_target: &str,
1312
) -> Result<()> {
14-
let project = Project::find_by_path(repo_path)?;
15-
let mut ctx = CommandContext::open(&project, AppSettings::load_from_default_path_creating()?)?;
13+
let mut ctx = CommandContext::open(project, AppSettings::load_from_default_path_creating()?)?;
1614

1715
// Resolve the commit ID
1816
let cli_ids = CliId::from_str(&mut ctx, commit_target)?;
@@ -33,7 +31,7 @@ pub(crate) fn edit_commit_message(
3331

3432
match cli_id {
3533
CliId::Commit { oid } => {
36-
edit_commit_message_by_id(&ctx, &project, *oid)?;
34+
edit_commit_message_by_id(&ctx, project, *oid)?;
3735
}
3836
_ => {
3937
anyhow::bail!("Target must be a commit ID, not {}", cli_id.kind());

crates/but/src/log/mod.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,11 @@ use but_workspace::{
77
use colored::Colorize;
88
use gitbutler_command_context::CommandContext;
99
use gitbutler_project::Project;
10-
use std::path::Path;
1110

1211
use crate::id::CliId;
1312

14-
pub(crate) fn commit_graph(repo_path: &Path, json: bool) -> anyhow::Result<()> {
15-
let project = Project::find_by_path(repo_path).expect("Failed to create project from path");
16-
let ctx = &mut CommandContext::open(&project, AppSettings::load_from_default_path_creating()?)?;
13+
pub(crate) fn commit_graph(project: &Project, json: bool) -> anyhow::Result<()> {
14+
let ctx = &mut CommandContext::open(project, AppSettings::load_from_default_path_creating()?)?;
1715
but_rules::process_rules(ctx).ok(); // TODO: this is doing double work (dependencies can be reused)
1816
let stacks = stacks(ctx)?
1917
.iter()

crates/but/src/main.rs

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,13 @@ async fn main() -> Result<()> {
5454
handler,
5555
}) => {
5656
let handler = *handler;
57-
command::handle_changes(&args.current_dir, args.json, handler, description)
57+
let project = get_or_init_project(&args.current_dir)?;
58+
command::handle_changes(&project, args.json, handler, description)
59+
}
60+
None => {
61+
let project = get_or_init_project(&args.current_dir)?;
62+
command::list_actions(&project, args.json, 0, 10)
5863
}
59-
None => command::list_actions(&args.current_dir, args.json, 0, 10),
6064
},
6165
Subcommands::Metrics {
6266
command_name,
@@ -112,7 +116,8 @@ async fn main() -> Result<()> {
112116
}
113117
},
114118
Subcommands::Base(base::Platform { cmd }) => {
115-
let result = base::handle(cmd, &args.current_dir, args.json);
119+
let project = get_or_init_project(&args.current_dir)?;
120+
let result = base::handle(cmd, &project, args.json);
116121
metrics_if_configured(
117122
app_settings,
118123
match cmd {
@@ -125,39 +130,45 @@ async fn main() -> Result<()> {
125130
Ok(())
126131
}
127132
Subcommands::Branch(branch::Platform { cmd }) => {
128-
let result = branch::handle(cmd, &args.current_dir, args.json);
133+
let project = get_or_init_project(&args.current_dir)?;
134+
let result = branch::handle(cmd, &project, args.json);
129135
metrics_if_configured(app_settings, CommandName::BranchNew, props(start, &result)).ok();
130136
Ok(())
131137
}
132138
Subcommands::Log => {
133-
let result = log::commit_graph(&args.current_dir, args.json);
139+
let project = get_or_init_project(&args.current_dir)?;
140+
let result = log::commit_graph(&project, args.json);
134141
metrics_if_configured(app_settings, CommandName::Log, props(start, &result)).ok();
135142
Ok(())
136143
}
137144
Subcommands::Status {
138145
show_files,
139146
verbose,
140147
} => {
141-
let result = status::worktree(&args.current_dir, args.json, *show_files, *verbose);
148+
let project = get_or_init_project(&args.current_dir)?;
149+
let result = status::worktree(&project, args.json, *show_files, *verbose);
142150
metrics_if_configured(app_settings, CommandName::Status, props(start, &result)).ok();
143151
Ok(())
144152
}
145153
Subcommands::Stf { verbose } => {
146-
let result = status::worktree(&args.current_dir, args.json, true, *verbose);
154+
let project = get_or_init_project(&args.current_dir)?;
155+
let result = status::worktree(&project, args.json, true, *verbose);
147156
metrics_if_configured(app_settings, CommandName::Stf, props(start, &result)).ok();
148157
Ok(())
149158
}
150159
Subcommands::Rub { source, target } => {
151-
let result = rub::handle(&args.current_dir, args.json, source, target)
152-
.context("Rubbed the wrong way.");
160+
let project = get_or_init_project(&args.current_dir)?;
161+
let result =
162+
rub::handle(&project, args.json, source, target).context("Rubbed the wrong way.");
153163
if let Err(e) = &result {
154164
eprintln!("{} {}", e, e.root_cause());
155165
}
156166
metrics_if_configured(app_settings, CommandName::Rub, props(start, &result)).ok();
157167
Ok(())
158168
}
159169
Subcommands::Mark { target, delete } => {
160-
let result = mark::handle(&args.current_dir, args.json, target, *delete)
170+
let project = get_or_init_project(&args.current_dir)?;
171+
let result = mark::handle(&project, args.json, target, *delete)
161172
.context("Can't mark this. Taaaa-na-na-na. Can't mark this.");
162173
if let Err(e) = &result {
163174
eprintln!("{} {}", e, e.root_cause());
@@ -166,7 +177,8 @@ async fn main() -> Result<()> {
166177
Ok(())
167178
}
168179
Subcommands::Unmark => {
169-
let result = mark::unmark(&args.current_dir, args.json)
180+
let project = get_or_init_project(&args.current_dir)?;
181+
let result = mark::unmark(&project, args.json)
170182
.context("Can't unmark this. Taaaa-na-na-na. Can't unmark this.");
171183
if let Err(e) = &result {
172184
eprintln!("{} {}", e, e.root_cause());
@@ -179,8 +191,9 @@ async fn main() -> Result<()> {
179191
branch,
180192
only,
181193
} => {
194+
let project = get_or_init_project(&args.current_dir)?;
182195
let result = commit::commit(
183-
&args.current_dir,
196+
&project,
184197
args.json,
185198
message.as_deref(),
186199
branch.as_deref(),
@@ -190,27 +203,32 @@ async fn main() -> Result<()> {
190203
result
191204
}
192205
Subcommands::New { target } => {
193-
let result = commit::insert_blank_commit(&args.current_dir, args.json, target);
206+
let project = get_or_init_project(&args.current_dir)?;
207+
let result = commit::insert_blank_commit(&project, args.json, target);
194208
metrics_if_configured(app_settings, CommandName::New, props(start, &result)).ok();
195209
result
196210
}
197211
Subcommands::Describe { commit } => {
198-
let result = describe::edit_commit_message(&args.current_dir, args.json, commit);
212+
let project = get_or_init_project(&args.current_dir)?;
213+
let result = describe::edit_commit_message(&project, args.json, commit);
199214
metrics_if_configured(app_settings, CommandName::Describe, props(start, &result)).ok();
200215
result
201216
}
202217
Subcommands::Oplog { since } => {
203-
let result = oplog::show_oplog(&args.current_dir, args.json, since.as_deref());
218+
let project = get_or_init_project(&args.current_dir)?;
219+
let result = oplog::show_oplog(&project, args.json, since.as_deref());
204220
metrics_if_configured(app_settings, CommandName::Oplog, props(start, &result)).ok();
205221
result
206222
}
207223
Subcommands::Restore { oplog_sha } => {
208-
let result = oplog::restore_to_oplog(&args.current_dir, args.json, oplog_sha);
224+
let project = get_or_init_project(&args.current_dir)?;
225+
let result = oplog::restore_to_oplog(&project, args.json, oplog_sha);
209226
metrics_if_configured(app_settings, CommandName::Restore, props(start, &result)).ok();
210227
result
211228
}
212229
Subcommands::Undo => {
213-
let result = oplog::undo_last_operation(&args.current_dir, args.json);
230+
let project = get_or_init_project(&args.current_dir)?;
231+
let result = oplog::undo_last_operation(&project, args.json);
214232
metrics_if_configured(app_settings, CommandName::Undo, props(start, &result)).ok();
215233
result
216234
}

crates/but/src/mark/mod.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{path::Path, str::FromStr};
1+
use std::str::FromStr;
22

33
use crate::rub::branch_name_to_stack_id;
44
use anyhow::bail;
@@ -9,13 +9,12 @@ use gitbutler_command_context::CommandContext;
99
use gitbutler_commit::commit_ext::CommitExt;
1010
use gitbutler_project::Project;
1111
pub(crate) fn handle(
12-
repo_path: &Path,
12+
project: &Project,
1313
_json: bool,
1414
target_str: &str,
1515
delete: bool,
1616
) -> anyhow::Result<()> {
17-
let project = Project::find_by_path(repo_path).expect("Failed to create project from path");
18-
let ctx = &mut CommandContext::open(&project, AppSettings::load_from_default_path_creating()?)?;
17+
let ctx = &mut CommandContext::open(project, AppSettings::load_from_default_path_creating()?)?;
1918
let target_result = crate::id::CliId::from_str(ctx, target_str)?;
2019
if target_result.len() != 1 {
2120
return Err(anyhow::anyhow!(
@@ -115,9 +114,8 @@ pub(crate) fn commit_marked(ctx: &mut CommandContext, commit_id: String) -> anyh
115114
Ok(rules)
116115
}
117116

118-
pub(crate) fn unmark(repo_path: &Path, _json: bool) -> anyhow::Result<()> {
119-
let project = Project::find_by_path(repo_path).expect("Failed to create project from path");
120-
let ctx = &mut CommandContext::open(&project, AppSettings::load_from_default_path_creating()?)?;
117+
pub(crate) fn unmark(project: &Project, _json: bool) -> anyhow::Result<()> {
118+
let ctx = &mut CommandContext::open(project, AppSettings::load_from_default_path_creating()?)?;
121119

122120
let rules = but_rules::list_rules(ctx)?;
123121
let rule_count = rules.len();

crates/but/src/oplog/mod.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
use colored::Colorize;
22
use gitbutler_oplog::entry::OperationKind;
33
use gitbutler_project::Project;
4-
use std::path::Path;
5-
6-
pub(crate) fn show_oplog(repo_path: &Path, json: bool, since: Option<&str>) -> anyhow::Result<()> {
7-
let project = Project::find_by_path(repo_path)?;
84

5+
pub(crate) fn show_oplog(project: &Project, json: bool, since: Option<&str>) -> anyhow::Result<()> {
96
let snapshots = if let Some(since_sha) = since {
107
// Get all snapshots first to find the starting point
118
let all_snapshots = but_api::undo::list_snapshots(project.id, 1000, None, None)?; // Get a large number to find the SHA
@@ -117,11 +114,10 @@ pub(crate) fn show_oplog(repo_path: &Path, json: bool, since: Option<&str>) -> a
117114
}
118115

119116
pub(crate) fn restore_to_oplog(
120-
repo_path: &Path,
117+
project: &Project,
121118
_json: bool,
122119
oplog_sha: &str,
123120
) -> anyhow::Result<()> {
124-
let project = Project::find_by_path(repo_path)?;
125121
let snapshots = but_api::undo::list_snapshots(project.id, 1000, None, None)?;
126122

127123
// Parse the oplog SHA (support partial SHAs)
@@ -196,9 +192,7 @@ pub(crate) fn restore_to_oplog(
196192
Ok(())
197193
}
198194

199-
pub(crate) fn undo_last_operation(repo_path: &Path, _json: bool) -> anyhow::Result<()> {
200-
let project = Project::find_by_path(repo_path)?;
201-
195+
pub(crate) fn undo_last_operation(project: &Project, _json: bool) -> anyhow::Result<()> {
202196
// Get the last two snapshots - restore to the second one back
203197
let snapshots = but_api::undo::list_snapshots(project.id, 2, None, None)?;
204198

0 commit comments

Comments
 (0)