Skip to content

Commit fc6cb4f

Browse files
committed
Claude code stop hook should not commit in other branches
1 parent f434341 commit fc6cb4f

File tree

6 files changed

+34
-7
lines changed

6 files changed

+34
-7
lines changed

crates/but-action/src/lib.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ pub fn freestyle(
8484
2. Take a look at the project status and see what changes are present in the project. It's important to understand what stacks and branche are present, and what the file changes are.
8585
3. Try to correlate the prompt with the project status and determine what actions you can take to help the user.
8686
4. Use the tools provided to you to perform the actions.
87-
87+
8888
### Capabilities
8989
You can generally perform the normal Git operations, such as creating branches and committing to them.
9090
You can also perform more advanced operations, such as:
@@ -95,7 +95,7 @@ pub fn freestyle(
9595
- `split a branch`: Take an existing branch and split it into two branches. This basically takes a set of committed file changes and moves them to a new branch, removing them from the original branch.
9696
This is useful when you want to separate the changes into a new branch for further work.
9797
In order to do this, you will need to get the branch changes for the intended source branch (call the `get_branch_changes` tool), and then call the split branch tool with the changes you want to split off.
98-
98+
9999
### Important notes
100100
- Only perform the action on the file changes specified in the prompt.
101101
- If the prompt is not clear, ask the user for clarification.
@@ -167,11 +167,16 @@ pub fn handle_changes(
167167
external_prompt: Option<String>,
168168
handler: ActionHandler,
169169
source: Source,
170+
exclusive_stack: Option<StackId>,
170171
) -> anyhow::Result<(Uuid, Outcome)> {
171172
match handler {
172-
ActionHandler::HandleChangesSimple => {
173-
simple::handle_changes(ctx, change_summary, external_prompt, source)
174-
}
173+
ActionHandler::HandleChangesSimple => simple::handle_changes(
174+
ctx,
175+
change_summary,
176+
external_prompt,
177+
source,
178+
exclusive_stack,
179+
),
175180
}
176181
}
177182

crates/but-action/src/simple.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ pub fn handle_changes(
2929
change_summary: &str,
3030
external_prompt: Option<String>,
3131
source: Source,
32+
exclusive_stack: Option<StackId>,
3233
) -> anyhow::Result<(Uuid, Outcome)> {
3334
let mut guard = ctx.project().exclusive_worktree_access();
3435
let perm = guard.write_permission();
@@ -43,8 +44,14 @@ pub fn handle_changes(
4344
)?
4445
.to_gix();
4546

46-
let response =
47-
handle_changes_simple_inner(ctx, change_summary, external_prompt.clone(), vb_state, perm);
47+
let response = handle_changes_simple_inner(
48+
ctx,
49+
change_summary,
50+
external_prompt.clone(),
51+
vb_state,
52+
perm,
53+
exclusive_stack,
54+
);
4855

4956
let snapshot_after = ctx
5057
.create_snapshot(
@@ -73,6 +80,7 @@ fn handle_changes_simple_inner(
7380
external_prompt: Option<String>,
7481
vb_state: &VirtualBranchesHandle,
7582
perm: &mut WorktreeWritePermission,
83+
exclusive_stack: Option<StackId>,
7684
) -> anyhow::Result<Outcome> {
7785
match gitbutler_operating_modes::operating_mode(ctx) {
7886
OperatingMode::OpenWorkspace => {
@@ -145,6 +153,11 @@ fn handle_changes_simple_inner(
145153
if diff_specs.is_empty() {
146154
continue;
147155
}
156+
if let Some(exclusive_stack) = exclusive_stack {
157+
if exclusive_stack != stack_id {
158+
continue; // Skip stacks that are not the exclusive stack.
159+
}
160+
}
148161

149162
let stack_branch_name = stacks
150163
.iter()

crates/but/src/command/claude.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,18 @@ pub(crate) async fn handle_stop() -> anyhow::Result<ClaudeHookOutput> {
103103
file_path: None,
104104
};
105105

106+
let session = list_sessions(defer.ctx)?
107+
.into_iter()
108+
.find(|s| s.id == input.session_id)
109+
.ok_or_else(|| anyhow::anyhow!("Lane for session {} not found", input.session_id))?;
110+
106111
let (id, outcome) = but_action::handle_changes(
107112
defer.ctx,
108113
&summary,
109114
Some(prompt.clone()),
110115
ActionHandler::HandleChangesSimple,
111116
Source::ClaudeCode(input.session_id),
117+
Some(StackId::from_str(&session.stack_id)?),
112118
)?;
113119

114120
let stacks = crate::log::stacks(defer.ctx)?;

crates/but/src/command/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pub(crate) fn handle_changes(
2323
None,
2424
handler.into(),
2525
Source::ButCli,
26+
None,
2627
)?;
2728
print(&response, json)
2829
}

crates/but/src/mcp/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ impl Mcp {
141141
Some(request.full_prompt.clone()),
142142
ActionHandler::HandleChangesSimple,
143143
Source::Mcp(client_info.clone().map(Into::into)),
144+
None,
144145
)
145146
.map_err(|e| McpError::internal_error(e.to_string(), None))?;
146147
// Trigger commit message generation for newly created commits

crates/gitbutler-tauri/src/action.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ pub fn handle_changes(
3636
None,
3737
handler,
3838
but_action::Source::GitButler,
39+
None,
3940
)
4041
.map(|(_id, outcome)| outcome)
4142
.map_err(|e| Error::from(anyhow::anyhow!(e)))

0 commit comments

Comments
 (0)