Skip to content

Commit 55ca722

Browse files
id: make from_str use non-legacy Context
This is in preparation for a future commit that changes `IdDb::new` (which `from_str` calls) to also take `Context` instead of `CommandContext`.
1 parent 0e9cbba commit 55ca722

File tree

10 files changed

+78
-97
lines changed

10 files changed

+78
-97
lines changed

crates/but/src/absorb.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@ use but_api::{
66
legacy::{diff, virtual_branches},
77
};
88
use but_core::DiffSpec;
9+
use but_ctx::Context;
910
use but_hunk_assignment::HunkAssignment;
1011
use but_hunk_dependency::ui::HunkDependencies;
11-
use but_settings::AppSettings;
1212
use colored::Colorize;
13-
use gitbutler_command_context::CommandContext;
1413
use gitbutler_project::Project;
1514

1615
use crate::{id::CliId, rub::parse_sources, utils::OutputChannel};
@@ -29,11 +28,11 @@ use crate::{id::CliId, rub::parse_sources, utils::OutputChannel};
2928
/// If a Branch (stack) id is provided, absorb will be performed for all changes assigned to that stack
3029
/// If no source is provided, absorb is performed for all uncommitted changes
3130
pub(crate) fn handle(
32-
project: &Project,
31+
ctx: &Context,
3332
out: &mut OutputChannel,
3433
source: Option<&str>,
3534
) -> anyhow::Result<()> {
36-
let ctx = &mut CommandContext::open(project, AppSettings::load_from_default_path_creating()?)?;
35+
let project = &ctx.legacy_project;
3736
let source: Option<CliId> = source
3837
.and_then(|s| parse_sources(ctx, s).ok())
3938
.and_then(|s| {

crates/but/src/branch/mod.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
use anyhow::bail;
22
pub use branch::{Platform, Subcommands};
33
use but_core::ref_metadata::StackId;
4-
use but_settings::AppSettings;
54
use but_workspace::legacy::ui::StackEntry;
6-
use gitbutler_command_context::CommandContext;
75

86
use crate::{LegacyProject, args::branch, utils::OutputChannel};
97

@@ -151,10 +149,6 @@ pub async fn handle(
151149
branch_name,
152150
anchor,
153151
}) => {
154-
let ctx = CommandContext::open(
155-
legacy_project,
156-
AppSettings::load_from_default_path_creating()?,
157-
)?;
158152
// Get branch name or use canned name
159153
let branch_name = branch_name.map(Ok).unwrap_or_else(|| {
160154
but_api::legacy::workspace::canned_branch_name(legacy_project.id)
@@ -165,10 +159,9 @@ pub async fn handle(
165159

166160
let anchor = if let Some(anchor_str) = anchor {
167161
// Use the new create_reference API when anchor is provided
168-
let mut ctx = ctx; // Make mutable for CliId resolution
169162

170163
// Resolve the anchor string to a CliId
171-
let anchor_ids = crate::id::CliId::from_str(&mut ctx, &anchor_str)?;
164+
let anchor_ids = crate::id::CliId::from_str(ctx, &anchor_str)?;
172165
if anchor_ids.is_empty() {
173166
return Err(anyhow::anyhow!("Could not find anchor: {}", anchor_str));
174167
}

crates/but/src/commit.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ use but_api::{
77
legacy::{diff, virtual_branches, workspace},
88
};
99
use but_core::{DiffSpec, ui::TreeChange};
10+
use but_ctx::Context;
1011
use but_hunk_assignment::HunkAssignment;
11-
use but_settings::AppSettings;
12-
use gitbutler_command_context::CommandContext;
1312
use gitbutler_project::Project;
1413

1514
use crate::{
@@ -18,14 +17,14 @@ use crate::{
1817
};
1918

2019
pub(crate) fn insert_blank_commit(
21-
project: &Project,
20+
ctx: &Context,
2221
out: &mut OutputChannel,
2322
target: &str,
2423
) -> Result<()> {
25-
let mut ctx = CommandContext::open(project, AppSettings::load_from_default_path_creating()?)?;
24+
let project = &ctx.legacy_project;
2625

2726
// Resolve the target ID
28-
let cli_ids = CliId::from_str(&mut ctx, target)?;
27+
let cli_ids = CliId::from_str(ctx, target)?;
2928

3029
if cli_ids.is_empty() {
3130
bail!("Target '{}' not found", target);
@@ -144,14 +143,14 @@ fn find_stack_containing_commit(
144143
}
145144

146145
pub(crate) fn commit(
147-
project: &Project,
146+
ctx: &Context,
148147
out: &mut OutputChannel,
149148
message: Option<&str>,
150149
branch_hint: Option<&str>,
151150
only: bool,
152151
create_branch: bool,
153152
) -> anyhow::Result<()> {
154-
let mut ctx = CommandContext::open(project, AppSettings::load_from_default_path_creating()?)?;
153+
let project = &ctx.legacy_project;
155154

156155
// Get all stacks using but-api
157156
let project_id = project.id;
@@ -171,7 +170,7 @@ pub(crate) fn commit(
171170
.collect();
172171

173172
let (target_stack_id, target_stack) =
174-
select_stack(&mut ctx, project, &stacks, branch_hint, create_branch, out)?;
173+
select_stack(ctx, project, &stacks, branch_hint, create_branch, out)?;
175174

176175
// Get changes and assignments using but-api
177176
let worktree_changes = diff::changes_in_worktree(project_id)?;
@@ -236,7 +235,7 @@ pub(crate) fn commit(
236235
.find(|branch| branch.name == hint)
237236
.or_else(|| {
238237
// If no exact match, try to parse as CLI ID and match
239-
if let Ok(cli_ids) = crate::id::CliId::from_str(&mut ctx, hint) {
238+
if let Ok(cli_ids) = crate::id::CliId::from_str(ctx, hint) {
240239
for cli_id in cli_ids {
241240
if let crate::id::CliId::Branch { name, .. } = cli_id
242241
&& let Some(branch) =
@@ -335,7 +334,7 @@ fn create_independent_branch(
335334
}
336335

337336
fn select_stack(
338-
ctx: &mut CommandContext,
337+
ctx: &Context,
339338
project: &Project,
340339
stacks: &[(
341340
but_core::ref_metadata::StackId,
@@ -396,7 +395,7 @@ fn select_stack(
396395
}
397396

398397
fn find_stack_by_hint(
399-
ctx: &mut CommandContext,
398+
ctx: &Context,
400399
stacks: &[(
401400
but_core::ref_metadata::StackId,
402401
but_workspace::ui::StackDetails,

crates/but/src/describe.rs

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
11
use anyhow::{Result, bail};
2+
use but_ctx::Context;
23
use but_oxidize::{ObjectIdExt, OidExt};
3-
use but_settings::AppSettings;
44
use gitbutler_command_context::CommandContext;
55
use gitbutler_project::Project;
66

77
use crate::{editor::get_text_from_editor_no_comments, id::CliId, utils::OutputChannel};
88

9-
pub(crate) fn describe_target(
10-
project: &Project,
11-
out: &mut OutputChannel,
12-
target: &str,
13-
) -> Result<()> {
14-
let mut ctx = CommandContext::open(project, AppSettings::load_from_default_path_creating()?)?;
9+
pub(crate) fn describe_target(ctx: &Context, out: &mut OutputChannel, target: &str) -> Result<()> {
10+
let project = &ctx.legacy_project;
1511

1612
// Resolve the commit ID
17-
let cli_ids = CliId::from_str(&mut ctx, target)?;
13+
let cli_ids = CliId::from_str(ctx, target)?;
1814

1915
if cli_ids.is_empty() {
2016
bail!("ID '{}' not found", target);
@@ -32,10 +28,10 @@ pub(crate) fn describe_target(
3228

3329
match cli_id {
3430
CliId::Branch { name, .. } => {
35-
edit_branch_name(&ctx, project, name, out)?;
31+
edit_branch_name(project, name, out)?;
3632
}
3733
CliId::Commit { oid } => {
38-
edit_commit_message_by_id(&ctx, project, *oid, out)?;
34+
edit_commit_message_by_id(&ctx.legacy_ctx()?, project, *oid, out)?;
3935
}
4036
_ => {
4137
bail!("Target must be a commit ID, not {}", cli_id.kind());
@@ -45,12 +41,7 @@ pub(crate) fn describe_target(
4541
Ok(())
4642
}
4743

48-
fn edit_branch_name(
49-
_ctx: &CommandContext,
50-
project: &Project,
51-
branch_name: &str,
52-
out: &mut OutputChannel,
53-
) -> Result<()> {
44+
fn edit_branch_name(project: &Project, branch_name: &str, out: &mut OutputChannel) -> Result<()> {
5445
// Find which stack this branch belongs to
5546
let stacks = but_api::legacy::workspace::stacks(
5647
project.id,

crates/but/src/forge/review.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use std::collections::BTreeMap;
22

3-
use anyhow::Context;
3+
use anyhow::Context as _;
44
use bstr::ByteSlice;
55
use but_core::ref_metadata::StackId;
6+
use but_ctx::Context;
67
use but_oxidize::OidExt;
78
use but_settings::AppSettings;
89
use but_workspace::ui::Commit;
@@ -102,21 +103,22 @@ pub fn set_review_template(
102103

103104
/// Publish reviews for active branches in the workspace.
104105
pub async fn publish_reviews(
105-
project: &Project,
106+
ctx: &Context,
106107
branch: Option<String>,
107108
skip_force_push_protection: bool,
108109
with_force: bool,
109110
run_hooks: bool,
110111
default: bool,
111112
out: &mut OutputChannel,
112113
) -> anyhow::Result<()> {
114+
let project = &ctx.legacy_project;
113115
let review_map = get_review_map(project).await?;
114116
let applied_stacks = but_api::legacy::workspace::stacks(
115117
project.id,
116118
Some(but_workspace::legacy::StacksFilter::InWorkspace),
117119
)?;
118120
let maybe_branch_names = branch
119-
.map(|branch_id| get_branch_names(project, &branch_id))
121+
.map(|branch_id| get_branch_names(ctx, &branch_id))
120122
.transpose()?;
121123
handle_multiple_branches_in_workspace(
122124
project,
@@ -132,9 +134,8 @@ pub async fn publish_reviews(
132134
.await
133135
}
134136

135-
fn get_branch_names(project: &Project, branch_id: &str) -> anyhow::Result<Vec<String>> {
136-
let mut ctx = CommandContext::open(project, AppSettings::load_from_default_path_creating()?)?;
137-
let branch_ids = CliId::from_str(&mut ctx, branch_id)?
137+
fn get_branch_names(ctx: &Context, branch_id: &str) -> anyhow::Result<Vec<String>> {
138+
let branch_ids = CliId::from_str(ctx, branch_id)?
138139
.iter()
139140
.filter_map(|clid| match clid {
140141
CliId::Branch { name, .. } => Some(name.clone()),

crates/but/src/id.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::{collections::HashMap, fmt::Display};
22

33
use bstr::ByteSlice;
44
use but_core::ref_metadata::StackId;
5+
use but_ctx::Context;
56
use but_hunk_assignment::HunkAssignment;
67
use gitbutler_command_context::CommandContext;
78

@@ -194,7 +195,7 @@ impl CliId {
194195
}
195196
}
196197

197-
pub fn from_str(ctx: &mut CommandContext, s: &str) -> anyhow::Result<Vec<Self>> {
198+
pub fn from_str(ctx: &Context, s: &str) -> anyhow::Result<Vec<Self>> {
198199
if s.len() < 2 {
199200
return Err(anyhow::anyhow!(
200201
"Id needs to be at least 2 characters long: {}",
@@ -203,37 +204,37 @@ impl CliId {
203204
}
204205

205206
// TODO: make callers of this function pass IdDb instead
206-
let mut id_db = IdDb::new(ctx)?;
207+
let mut id_db = IdDb::new(&ctx.legacy_ctx()?)?;
207208

208209
let mut matches = Vec::new();
209210

210211
// First, try exact branch name match
211-
if let Ok(branch_matches) = id_db.find_branches_by_name(ctx, s) {
212+
if let Ok(branch_matches) = id_db.find_branches_by_name(&ctx.legacy_ctx()?, s) {
212213
matches.extend(branch_matches);
213214
}
214215

215216
// Then try partial SHA matches (for commits)
216-
if let Ok(commit_matches) = Self::find_commits_by_sha(ctx, s) {
217+
if let Ok(commit_matches) = Self::find_commits_by_sha(&ctx.legacy_ctx()?, s) {
217218
matches.extend(commit_matches);
218219
}
219220

220221
// Then try CliId matching (both prefix and exact)
221222
if s.len() > 2 {
222223
// For longer strings, try prefix matching on CliIds
223224
let mut cli_matches = Vec::new();
224-
crate::status::all_files(ctx)?
225+
crate::status::all_files(&mut ctx.legacy_ctx()?)?
225226
.into_iter()
226227
.filter(|id| id.matches_prefix(s))
227228
.for_each(|id| cli_matches.push(id));
228-
crate::status::all_committed_files(ctx)?
229+
crate::status::all_committed_files(&mut ctx.legacy_ctx()?)?
229230
.into_iter()
230231
.filter(|id| id.matches_prefix(s))
231232
.for_each(|id| cli_matches.push(id));
232-
crate::status::all_branches(ctx)?
233+
crate::status::all_branches(&ctx.legacy_ctx()?)?
233234
.into_iter()
234235
.filter(|id| id.matches_prefix(s))
235236
.for_each(|id| cli_matches.push(id));
236-
crate::utils::commits::all_commits(ctx)?
237+
crate::utils::commits::all_commits(&ctx.legacy_ctx()?)?
237238
.into_iter()
238239
.filter(|id| id.matches_prefix(s))
239240
.for_each(|id| cli_matches.push(id));
@@ -244,19 +245,19 @@ impl CliId {
244245
} else {
245246
// For 2-character strings, try exact CliId matching
246247
let mut cli_matches = Vec::new();
247-
crate::status::all_files(ctx)?
248+
crate::status::all_files(&mut ctx.legacy_ctx()?)?
248249
.into_iter()
249250
.filter(|id| id.matches(s))
250251
.for_each(|id| cli_matches.push(id));
251-
crate::status::all_committed_files(ctx)?
252+
crate::status::all_committed_files(&mut ctx.legacy_ctx()?)?
252253
.into_iter()
253254
.filter(|id| id.matches(s))
254255
.for_each(|id| cli_matches.push(id));
255-
crate::status::all_branches(ctx)?
256+
crate::status::all_branches(&ctx.legacy_ctx()?)?
256257
.into_iter()
257258
.filter(|id| id.matches(s))
258259
.for_each(|id| cli_matches.push(id));
259-
crate::utils::commits::all_commits(ctx)?
260+
crate::utils::commits::all_commits(&ctx.legacy_ctx()?)?
260261
.into_iter()
261262
.filter(|id| id.matches(s))
262263
.for_each(|id| cli_matches.push(id));

0 commit comments

Comments
 (0)