Skip to content

Commit

Permalink
commit create: Add --fixup flag (#500)
Browse files Browse the repository at this point in the history
Adds a new --fixup flag for allowing you to create fixup commits.

--fixup takes a commitish which targets where the fixup will be applied.
--fixup also takes the search :/<text> to target the subject of a commit
message to target the correct commit.

Using gs branch edit with the autosquash option set in git allows us to
automatically fixup our commits.

Resolves #492
  • Loading branch information
frank-west-iii authored Nov 28, 2024
1 parent 285a611 commit 1731f3e
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changes/unreleased/Added-20241127-125953.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
kind: Added
body: 'commit create: Added a --fixup flag to allow adding a fixup commit.'
time: 2024-11-27T12:59:53.626214-08:00
2 changes: 2 additions & 0 deletions commit_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

type commitCreateCmd struct {
All bool `short:"a" help:"Stage all changes before committing."`
Fixup string `help:"Create a fixup commit."`
Message string `short:"m" help:"Use the given message as the commit message."`
}

Expand All @@ -36,6 +37,7 @@ func (cmd *commitCreateCmd) Run(ctx context.Context, log *log.Logger, view ui.Vi
if err := repo.Commit(ctx, git.CommitRequest{
Message: cmd.Message,
All: cmd.All,
Fixup: cmd.Fixup,
}); err != nil {
return fmt.Errorf("commit: %w", err)
}
Expand Down
1 change: 1 addition & 0 deletions doc/includes/cli-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,7 @@ followed by 'gs upstack restack'.
**Flags**

* `-a`, `--all`: Stage all changes before committing.
* `--fixup=STRING`: Create a fixup commit.
* `-m`, `--message=STRING`: Use the given message as the commit message.

### gs commit amend
Expand Down
6 changes: 6 additions & 0 deletions internal/git/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ type CommitRequest struct {

// AllowEmpty allows a commit with no changes.
AllowEmpty bool

// Create a new commit which "fixes up" the commit at the given commitish.
Fixup string
}

// Commit runs the 'git commit' command,
Expand All @@ -144,6 +147,9 @@ func (r *Repository) Commit(ctx context.Context, req CommitRequest) error {
if req.ReuseMessage != "" {
args = append(args, "-C", req.ReuseMessage)
}
if req.Fixup != "" {
args = append(args, "--fixup", req.Fixup)
}

err := r.gitCmd(ctx, args...).
Stdin(os.Stdin).
Expand Down
60 changes: 60 additions & 0 deletions testdata/script/commit_create_fixup.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Commit create usage with --fixup option.

as 'Test <[email protected]>'
at '2024-03-30T14:59:32Z'

# setup
cd repo
git init
git commit --allow-empty -m 'Initial commit'
gs repo init

git checkout -b feature
gs branch track --base main

# create a couple commits
git add foo.txt
gs cc -m 'Add foo'
mv $WORK/extra/bar.txt bar.txt
git add bar.txt
gs cc -m 'Add bar'
mv $WORK/extra/new_foo.txt foo.txt
gs cc -a --fixup :/foo

# verify the output
git log
cmp stdout $WORK/golden/log.1.txt

-- repo/foo.txt --
Contents of foo.

-- extra/new_foo.txt --
New contents of foo.

-- extra/bar.txt --
Contents of bar.txt

-- golden/log.1.txt --
commit 09309bfe664697b2e69d883ccd6ab7cbef505d0d
Author: Test <[email protected]>
Date: Sat Mar 30 14:59:32 2024 +0000

fixup! Add foo

commit 6295ed5efe1d126f9b26c33d699b971e15589a0b
Author: Test <[email protected]>
Date: Sat Mar 30 14:59:32 2024 +0000

Add bar

commit 91582344149997d5a513acf7b4d56a03452e23cd
Author: Test <[email protected]>
Date: Sat Mar 30 14:59:32 2024 +0000

Add foo

commit 9bad92b764fe1d56cb99b394f373a71cdefd8e86
Author: Test <[email protected]>
Date: Sat Mar 30 14:59:32 2024 +0000

Initial commit

0 comments on commit 1731f3e

Please sign in to comment.