Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions crates/but-auto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@ test = false
serde = { workspace = true, features = ["std"] }
serde-error = "0.1.3"
anyhow = "1.0.98"
strum = { version = "0.27", features = ["derive"] }
gix.workspace = true
gitbutler-command-context.workspace = true
gitbutler-stack.workspace = true
gitbutler-operating-modes.workspace = true
gitbutler-branch-actions.workspace = true
gitbutler-branch.workspace = true
gitbutler-oplog.workspace = true
gitbutler-oxidize.workspace = true
gitbutler-project.workspace = true
but-core.workspace = true
but-workspace.workspace = true
but-hunk-assignment.workspace = true
60 changes: 43 additions & 17 deletions crates/but-auto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use std::collections::HashMap;

mod state;
use anyhow::anyhow;
use but_workspace::{DiffSpec, StackId, VirtualBranchesTomlMetadata, ui::StackEntry};
use gitbutler_branch::BranchCreateRequest;
Expand All @@ -11,6 +12,8 @@ use gitbutler_oplog::{
OplogExt,
entry::{OperationKind, SnapshotDetails},
};
use gitbutler_oxidize::OidExt;
use gitbutler_project::access::WorktreeWritePermission;
use gitbutler_stack::VirtualBranchesHandle;
use serde::{Deserialize, Serialize};

Expand All @@ -28,10 +31,46 @@ use serde::{Deserialize, Serialize};
/// TODO:
/// - Handle the case of target branch not being configured
/// - Persistence of the request context and oplog snapshot IDs
#[allow(unused)]
pub fn handle_changes_simple(
ctx: &mut CommandContext,
request_ctx: &str,
change_description: &str,
) -> anyhow::Result<HandleChangesResponse> {
let mut guard = ctx.project().exclusive_worktree_access();
let perm = guard.write_permission();

let snapshot_before = ctx
.create_snapshot(
SnapshotDetails::new(OperationKind::AutoHandleChangesBefore),
perm,
)?
.to_gix();

let response = handle_changes_simple_inner(ctx, change_description, perm);

let snapshot_after = ctx
.create_snapshot(
SnapshotDetails::new(OperationKind::AutoHandleChangesAfter),
perm,
)?
.to_gix();

// Add a checkpoint entry
state::persist_checkpoint(state::ButCheckpoint::new(
state::AutoHandler::HandleChangesSimple,
change_description.to_owned(),
snapshot_before,
snapshot_after,
&response,
))?;

response
// Ok(response)
}

fn handle_changes_simple_inner(
ctx: &mut CommandContext,
change_description: &str,
perm: &mut WorktreeWritePermission,
) -> anyhow::Result<HandleChangesResponse> {
let vb_state = VirtualBranchesHandle::new(ctx.project().gb_dir());
match gitbutler_operating_modes::operating_mode(ctx) {
Expand All @@ -45,7 +84,7 @@ pub fn handle_changes_simple(
}
OperatingMode::OutsideWorkspace(_) => {
let default_target = vb_state.get_default_target()?;
gitbutler_branch_actions::set_base_branch(ctx, &default_target.branch, true);
gitbutler_branch_actions::set_base_branch(ctx, &default_target.branch, true)?;
}
}

Expand Down Expand Up @@ -88,14 +127,6 @@ pub fn handle_changes_simple(
*specs = flatten_diff_specs(specs.clone());
}

let mut guard = ctx.project().exclusive_worktree_access();
let perm = guard.write_permission();

ctx.create_snapshot(
SnapshotDetails::new(OperationKind::BeforeAutoHandleChanges),
perm,
);

let mut updated_branches = vec![];

for (stack_id, diff_specs) in stack_assignments {
Expand All @@ -114,7 +145,7 @@ pub fn handle_changes_simple(
stack_id,
None,
diff_specs,
request_ctx.to_owned(),
change_description.to_owned(),
stack_branch_name.clone(),
perm,
)?;
Expand All @@ -127,11 +158,6 @@ pub fn handle_changes_simple(
}
}

ctx.create_snapshot(
SnapshotDetails::new(OperationKind::AfterAutoHandleChanges),
perm,
);

Ok(HandleChangesResponse { updated_branches })
}

Expand Down
53 changes: 53 additions & 0 deletions crates/but-auto/src/state.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use serde::{Deserialize, Serialize};
use strum::EnumString;

use crate::HandleChangesResponse;

#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize, EnumString, Default)]
pub enum AutoHandler {
#[default]
HandleChangesSimple,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ButCheckpoint {
created_at: std::time::SystemTime,
handler: AutoHandler,
change_description: String,
snapshot_before: gix::ObjectId,
snapshot_after: gix::ObjectId,
response: Option<HandleChangesResponse>,
error: Option<String>,
}

impl ButCheckpoint {
pub fn new(
handler: AutoHandler,
change_description: String,
snapshot_before: gix::ObjectId,
snapshot_after: gix::ObjectId,
response: &anyhow::Result<HandleChangesResponse>,
) -> Self {
let (rsp, error) = if let Err(e) = response {
(None, Some(e.to_string()))
} else {
(response.as_ref().ok(), None)
};

Self {
created_at: std::time::SystemTime::now(),
handler,
change_description,
snapshot_before,
snapshot_after,
response: rsp.cloned(),
error,
}
}
}

#[allow(unused)]
pub(crate) fn persist_checkpoint(checkpoint: ButCheckpoint) -> anyhow::Result<()> {
// TODO
Ok(())
}
4 changes: 2 additions & 2 deletions crates/but/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ pub enum Subcommands {
/// Automatically handle changes in the current repository, creating a commit with the provided context.
HandleChanges {
/// A context describing the changes that are currently uncommitted
#[clap(long, short = 'c')]
context: String,
#[clap(long, short = 'd', alias = "desc", visible_alias = "description")]
change_description: String,
/// If true, this will perform simple, non-AI based handling.
#[clap(long, short = 's', default_value_t = true)]
simple: bool,
Expand Down
4 changes: 2 additions & 2 deletions crates/but/src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ pub(crate) fn handle_changes(
repo_path: &Path,
json: bool,
simple: bool,
request_ctx: &str,
change_description: &str,
) -> anyhow::Result<()> {
if !simple {
bail!("Only simple mode is supported");
}
let project = Project::from_path(repo_path).expect("Failed to create project from path");
let ctx = &mut CommandContext::open(&project, AppSettings::default())?;
let response = but_auto::handle_changes_simple(ctx, request_ctx)?;
let response = but_auto::handle_changes_simple(ctx, change_description)?;
print(&response, json)
}

Expand Down
7 changes: 4 additions & 3 deletions crates/but/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ async fn main() -> Result<()> {
match &args.cmd {
args::Subcommands::McpInternal => mcp_internal::start(&args.current_dir).await,
args::Subcommands::Mcp => mcp::start(&args.current_dir).await,
args::Subcommands::HandleChanges { context, simple } => {
command::handle_changes(&args.current_dir, args.json, *simple, context)
}
args::Subcommands::HandleChanges {
change_description,
simple,
} => command::handle_changes(&args.current_dir, args.json, *simple, change_description),
}
}
6 changes: 3 additions & 3 deletions crates/but/src/mcp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ impl Mcp {
&self,
#[tool(aggr)] request: HandleChangesRequest,
) -> Result<CallToolResult, McpError> {
if request.context.is_empty() {
if request.change_description.is_empty() {
return Err(McpError::invalid_request(
"Context cannot be empty".to_string(),
None,
));
}
let ctx = &mut CommandContext::open(&self.project, AppSettings::default())
.map_err(|e| McpError::internal_error(e.to_string(), None))?;
let response = but_auto::handle_changes_simple(ctx, &request.context)
let response = but_auto::handle_changes_simple(ctx, &request.change_description)
.map_err(|e| McpError::internal_error(e.to_string(), None))?;
Ok(CallToolResult::success(vec![Content::json(response)?]))
}
Expand All @@ -55,7 +55,7 @@ pub struct HandleChangesRequest {
#[schemars(
description = "Information about what has changed and why - i.e. the user prompt etc."
)]
pub context: String,
pub change_description: String,
}

#[tool(tool_box)]
Expand Down
4 changes: 2 additions & 2 deletions crates/gitbutler-oplog/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,8 @@ pub enum OperationKind {
UpdateDependentBranchName,
UpdateDependentBranchDescription,
UpdateDependentBranchPrNumber,
BeforeAutoHandleChanges,
AfterAutoHandleChanges,
AutoHandleChangesBefore,
AutoHandleChangesAfter,
#[default]
Unknown,
}
Expand Down
Loading