feat: add --exec option to run commands after each patch during rebase#608
Merged
jpgrayson merged 2 commits intostacked-git:masterfrom Dec 30, 2025
Merged
Conversation
Add support for `stg rebase --exec <cmd>` which executes a shell command after each patch is successfully applied during the rebase operation. This is modeled after `git rebase --exec`. Key changes: - Add `exec_cmd()` method to StupidContext for running shell commands - Add `push_patches_with_exec()` method to StackTransaction for pushing patches with exec commands between each push - Add `print_exec()` method to TransactionUserInterface for output - Add `--exec` / `-x` argument to the rebase command The exec command is run via the user's shell ($SHELL or "sh" as fallback). Multiple --exec options can be specified to run multiple commands in sequence after each patch. If any command fails, the rebase halts. Note: When an exec command fails, the entire transaction is rolled back (no patches remain applied). This differs from git rebase --exec which leaves you at the failing point. The rollback behavior is safer and consistent with how stgit transactions work. Implements: stacked-git#469
831682d to
fa8c852
Compare
…g the non-rollback behaviour
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add --exec option to run commands after each patch during rebases
Disclaimer
I wrote this pull request with the assistance of Claude Opus 4.5.
I can note this in the commit log if that is a desirable flag.
I am an experienced programmer and do not think this is slop, I have tried to actually implement this in a reasonable/sane manner.
I used a tool I made called deciduous while writing this and it provides a neat flow diagram of how it made the choices that are implemented, so I am including that just for fun.
The PR
This PR implements the
--execoption forstg rebase, as requested in #469. It allows running a shell command after each patch is successfully applied during a rebase operation, similar togit rebase --exec.Key features:
--execoptions (run in sequence)$SHELL(orshas fallback)Implementation Approach
Design Decision: Modular Architecture
We considered two approaches:
--execin rebase.rs only - simpler, more localizedtransaction/push_patches- more modular, reusableWe chose Option 2 (modular approach) because:
push_patches_with_execmethod could be reused by other commands in the futureCode Changes
src/stupid/context.rs: Addedexec_cmd()method to run shell commands via the user's$SHELLsrc/stack/transaction/mod.rs: Addedpush_patches_with_exec()method that pushes patches and runs exec commands after each successful pushsrc/stack/transaction/ui.rs: Addedprint_exec()method for user feedbacksrc/cmd/rebase.rs: Added--exec/-xargument with appropriate conflictsDesign Discussion Point: Failure Behavior
When an exec command fails, the entire transaction is rolled back (no patches remain applied). This differs from
git rebase --execwhich leaves you at the failing commit to fix things.Current behavior (rollback):
Git's behavior (partial state):
Rationale for rollback:
stg pushpatches one at a time with manual checksHowever, you / the community may prefer git's behavior for consistency. I'm happy to discuss and modify this if desired.
Personally, I have never wanted to keep the mid-run state that comes with an issue when running this command, so I just went with my own personal preference here. It would not be hard to change, let me know what you think @jpgrayson @fbenkstein
Usage Examples
Testing
t/t2206-rebase-exec.shManual Test Output
Implements: #469