Skip to content

Commit 5fb536a

Browse files
commit {create, amend}: Adds --allow-empty flag (#647)
This allows empty commits to be created and amended. Empty commits contain an message, but no changes to the repository. Resolves #646
1 parent 6f4ef95 commit 5fb536a

File tree

6 files changed

+84
-15
lines changed

6 files changed

+84
-15
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
kind: Added
2+
body: 'commit {create, amend}: Add --allow-empty flag to allow commits without any changes.'
3+
time: 2025-04-25T16:10:27.297298-07:00

commit_amend.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ import (
1313
)
1414

1515
type commitAmendCmd struct {
16-
All bool `short:"a" help:"Stage all changes before committing."`
17-
Message string `short:"m" placeholder:"MSG" help:"Use the given message as the commit message."`
16+
All bool `short:"a" help:"Stage all changes before committing."`
17+
AllowEmpty bool `help:"Create a commit even if it contains no changes."`
18+
Message string `short:"m" placeholder:"MSG" help:"Use the given message as the commit message."`
1819

1920
NoEdit bool `help:"Don't edit the commit message"`
2021
NoVerify bool `help:"Bypass pre-commit and commit-msg hooks."`
@@ -46,11 +47,12 @@ func (cmd *commitAmendCmd) Run(
4647
}
4748

4849
if err := repo.Commit(ctx, git.CommitRequest{
49-
Message: cmd.Message,
50-
Amend: true,
51-
NoEdit: cmd.NoEdit,
52-
NoVerify: cmd.NoVerify,
53-
All: cmd.All,
50+
Message: cmd.Message,
51+
AllowEmpty: cmd.AllowEmpty,
52+
Amend: true,
53+
NoEdit: cmd.NoEdit,
54+
NoVerify: cmd.NoVerify,
55+
All: cmd.All,
5456
}); err != nil {
5557
return fmt.Errorf("commit: %w", err)
5658
}

commit_create.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ import (
1313
)
1414

1515
type commitCreateCmd struct {
16-
All bool `short:"a" help:"Stage all changes before committing."`
17-
Fixup string `help:"Create a fixup commit."`
18-
Message string `short:"m" help:"Use the given message as the commit message."`
19-
NoVerify bool `help:"Bypass pre-commit and commit-msg hooks."`
16+
All bool `short:"a" help:"Stage all changes before committing."`
17+
AllowEmpty bool `help:"Create a new commit even if it contains no changes."`
18+
Fixup string `help:"Create a fixup commit."`
19+
Message string `short:"m" help:"Use the given message as the commit message."`
20+
NoVerify bool `help:"Bypass pre-commit and commit-msg hooks."`
2021
}
2122

2223
func (*commitCreateCmd) Help() string {
@@ -36,10 +37,11 @@ func (cmd *commitCreateCmd) Run(
3637
svc *spice.Service,
3738
) error {
3839
if err := repo.Commit(ctx, git.CommitRequest{
39-
Message: cmd.Message,
40-
All: cmd.All,
41-
Fixup: cmd.Fixup,
42-
NoVerify: cmd.NoVerify,
40+
Message: cmd.Message,
41+
All: cmd.All,
42+
AllowEmpty: cmd.AllowEmpty,
43+
Fixup: cmd.Fixup,
44+
NoVerify: cmd.NoVerify,
4345
}); err != nil {
4446
return fmt.Errorf("commit: %w", err)
4547
}

doc/includes/cli-reference.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -844,6 +844,7 @@ followed by 'gs upstack restack'.
844844
**Flags**
845845

846846
* `-a`, `--all`: Stage all changes before committing.
847+
* `--allow-empty`: Create a new commit even if it contains no changes.
847848
* `--fixup=STRING`: Create a fixup commit.
848849
* `-m`, `--message=STRING`: Use the given message as the commit message.
849850
* `--no-verify`: Bypass pre-commit and commit-msg hooks.
@@ -864,6 +865,7 @@ followed by 'gs upstack restack'.
864865
**Flags**
865866

866867
* `-a`, `--all`: Stage all changes before committing.
868+
* `--allow-empty`: Create a commit even if it contains no changes.
867869
* `-m`, `--message=MSG`: Use the given message as the commit message.
868870
* `--no-edit`: Don't edit the commit message
869871
* `--no-verify`: Bypass pre-commit and commit-msg hooks.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Commit amend with --allow-empty
2+
3+
as 'Test <[email protected]>'
4+
at '2024-03-30T14:59:32Z'
5+
6+
# setup
7+
mkdir repo
8+
cd repo
9+
git init
10+
git commit --allow-empty -m 'Initial commit'
11+
gs repo init
12+
13+
git checkout -b feature
14+
gs branch track --base main
15+
16+
gs ca --allow-empty -m 'Allow empty commit'
17+
18+
# verify the output
19+
git log
20+
cmp stdout $WORK/golden/log.1.txt
21+
22+
-- golden/log.1.txt --
23+
commit 083acb009676ede496ab78b128ab6a4dc1c15acb
24+
Author: Test <[email protected]>
25+
Date: Sat Mar 30 14:59:32 2024 +0000
26+
27+
Allow empty commit
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Commit create with --allow-empty
2+
3+
as 'Test <[email protected]>'
4+
at '2024-03-30T14:59:32Z'
5+
6+
# setup
7+
mkdir repo
8+
cd repo
9+
git init
10+
git commit --allow-empty -m 'Initial commit'
11+
gs repo init
12+
13+
git checkout -b feature
14+
gs branch track --base main
15+
16+
gs cc -m 'Allow empty commit' --allow-empty
17+
18+
# verify the output
19+
git log
20+
cmp stdout $WORK/golden/log.1.txt
21+
22+
-- golden/log.1.txt --
23+
commit 0eab9a007f15066d7308788ce1ef8a664cc27d6a
24+
Author: Test <[email protected]>
25+
Date: Sat Mar 30 14:59:32 2024 +0000
26+
27+
Allow empty commit
28+
29+
commit 9bad92b764fe1d56cb99b394f373a71cdefd8e86
30+
Author: Test <[email protected]>
31+
Date: Sat Mar 30 14:59:32 2024 +0000
32+
33+
Initial commit

0 commit comments

Comments
 (0)