Skip to content

Commit eb4b6e4

Browse files
authored
New command stacky branch commit which will create a branch and commit (#3)
* New command stacky branch commit which will create a branch and commit * Updating readme for stacky branch commit
1 parent 93ce44c commit eb4b6e4

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ Syntax is as follows:
3838
- `stacky branch up` (`stacky b u`): move down the stack (towards `master`)
3939
- `stacky branch down` (`stacky b d`): move down the stack (towards `master`)
4040
- `stacky branch new <name>`: create a new branch on top of the current one
41-
- `stacky commit [-m <message>] [--amend] [--allow-empty]`: wrapper around `git commit` that syncs everything upstack
41+
- `stacky branch commit <name> [-m <message>] [-a]`: create a new branch and commit changes in one command
42+
- `stacky commit [-m <message>] [--amend] [--allow-empty] [-a]`: wrapper around `git commit` that syncs everything upstack
4243
- `stacky amend`: will amend currently tracked changes to top commit
4344
- Based on the first argument (`stack` vs `upstack` vs `downstack`), the following commands operate on the entire current stack, everything upstack from the current PR (inclusive), or everything downstack from the current PR:
4445
- `stacky stack info [--pr]`

src/stacky/stacky.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,34 @@ def cmd_branch_new(stack: StackBranchSet, args):
763763
run(CmdArgs(["git", "update-ref", "refs/stack-parent/{}".format(name), b.commit, ""]))
764764

765765

766+
def cmd_branch_commit(stack: StackBranchSet, args):
767+
"""Create a new branch and commit all changes with the provided message"""
768+
global CURRENT_BRANCH
769+
770+
# First create the new branch (same logic as cmd_branch_new)
771+
b = stack.stack[CURRENT_BRANCH]
772+
assert b.commit
773+
name = args.name
774+
create_branch(name)
775+
run(CmdArgs(["git", "update-ref", "refs/stack-parent/{}".format(name), b.commit, ""]))
776+
777+
# Update global CURRENT_BRANCH since we just checked out the new branch
778+
CURRENT_BRANCH = BranchName(name)
779+
780+
# Reload the stack to include the new branch
781+
load_stack_for_given_branch(stack, CURRENT_BRANCH)
782+
783+
# Now commit all changes with the provided message (or open editor if no message)
784+
do_commit(
785+
stack,
786+
message=args.message,
787+
amend=False,
788+
allow_empty=False,
789+
edit=True,
790+
add_all=args.add_all,
791+
)
792+
793+
766794
def cmd_branch_checkout(stack: StackBranchSet, args):
767795
branch_name = args.name
768796
if branch_name is None:
@@ -1726,6 +1754,12 @@ def main():
17261754
branch_new_parser.add_argument("name", help="Branch name")
17271755
branch_new_parser.set_defaults(func=cmd_branch_new)
17281756

1757+
branch_commit_parser = branch_subparsers.add_parser("commit", help="Create a new branch and commit all changes")
1758+
branch_commit_parser.add_argument("name", help="Branch name")
1759+
branch_commit_parser.add_argument("-m", help="Commit message", dest="message")
1760+
branch_commit_parser.add_argument("-a", action="store_true", help="Add all files to commit", dest="add_all")
1761+
branch_commit_parser.set_defaults(func=cmd_branch_commit)
1762+
17291763
branch_checkout_parser = branch_subparsers.add_parser("checkout", aliases=["co"], help="Checkout a branch")
17301764
branch_checkout_parser.add_argument("name", help="Branch name", nargs="?")
17311765
branch_checkout_parser.set_defaults(func=cmd_branch_checkout)

0 commit comments

Comments
 (0)