Skip to content

Commit 15d8ddf

Browse files
committed
Initial commit
0 parents  commit 15d8ddf

10 files changed

Lines changed: 1049 additions & 0 deletions

File tree

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto

.github/workflows/test.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
name: test
2+
3+
on:
4+
push:
5+
branches: [main]
6+
pull_request:
7+
8+
jobs:
9+
test:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v4
13+
- uses: actions/setup-go@v5
14+
with:
15+
go-version-file: go.mod
16+
cache: true
17+
- run: go test ./...

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/amend-commit
2+
/bin/

README.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# amend-commit
2+
3+
A command-line tool that takes the current staged or unstaged changes in the branch and ammends a previously added commit with the changes.
4+
It preserves commit naming and order.
5+
6+
7+
## Installation
8+
9+
With Go installed and `$(go env GOPATH)/bin` on your `PATH`:
10+
11+
```
12+
go install github.com/ramzis/amend-commit@latest
13+
```
14+
15+
This drops the binary as `amend-commit` into `$(go env GOPATH)/bin`.
16+
17+
Alternatively, clone the repo and use the included Taskfile:
18+
19+
```
20+
git clone https://github.com/ramzis/amend-commit.git
21+
cd amend-commit
22+
task install
23+
```
24+
25+
26+
## Usage
27+
`amend-commit -[number]`
28+
29+
where `number` is an optional flag with a non-negative integer representing the Nth from last commit to amend.
30+
31+
`amend-commit auto`
32+
33+
amends each file in its respective previous commit or logs warning for the files that haven't been commited yet.
34+
35+
## Examples:
36+
37+
- amend-commit
38+
Amends the last commit.
39+
40+
- amend-commit -1
41+
Amends the commit before the last one.
42+
43+
- amend-commit -N
44+
Amends the Nth commit and so on.
45+
46+
- amend-commit auto
47+
Amends each file into a previous commit where it was edited.
48+
49+
## Verification
50+
51+
- If using `auto` and a previous commit for that file does not exist, it logs a warning and does not amend it.
52+
- If a commit is from the base branch, it logs a warning and does not amend it.
53+
- If a commit amends files that were edited it other commits on this branch, it logs a warning and does not amend it.
54+
This enforces the convention that each commit in a feature branch modifies a file once.
55+
56+
## Tests
57+
The tool includes a Go test suite that runs the command line tool with various branch states and checks the correct behaviour.
58+
59+
Example test case:
60+
61+
Commit history (latest is first):
62+
COMMIT A (changes to files: A)
63+
COMMIT B (changes to files: B)
64+
65+
- Changes: Add line to file A.
66+
- Run `amend-commit`. Expected: changes applied to commit A since it previously edited file A.
67+
- Run `amend-commit -1`. Expected: no changes, warning since another commit on the branch (commit A) already touched the same file.
68+
- Changes: Add line to file B.
69+
- Run `amend-commit`. Expected: no changes, warning since another commit on the branch (commit B) already touched the same file.
70+
- Run `amend-commit -1`. Expected: changes applied to commit B since it previously edited file B.
71+
72+
73+
Example test case using auto:
74+
75+
Commit history (latest is first):
76+
COMMIT A (changes to files: A)
77+
COMMIT B (changes to files: B)
78+
79+
- Changes: Add line to file A.
80+
- Changes: Add line to file B.
81+
- Changes: Add line to file C.
82+
- Changes: Add line to file D.
83+
- Run `amend-commit auto`. Expected: commit A gets changes for file A. commit B gets changes for file B. a warning is logged for file C, D.

Taskfile.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
version: '3'
2+
3+
vars:
4+
# Install as `amend-commit` rather than `ac` to avoid shadowing the
5+
# system `/usr/sbin/ac` accounting tool.
6+
BINARY: amend-commit
7+
# Honour GOBIN if set, otherwise fall back to $(go env GOPATH)/bin.
8+
BIN_DIR:
9+
sh: 'b="$(go env GOBIN)"; [ -n "$b" ] || b="$(go env GOPATH)/bin"; echo "$b"'
10+
11+
tasks:
12+
build:
13+
desc: Build the binary into ./bin
14+
cmds:
15+
- go build -o "bin/{{.BINARY}}" .
16+
17+
install:
18+
desc: Build and install as `amend-commit` into the Go bin directory
19+
cmds:
20+
- go build -o "{{.BIN_DIR}}/{{.BINARY}}" .
21+
- 'echo "installed {{.BINARY}} -> {{.BIN_DIR}}/{{.BINARY}}"'
22+
23+
test:
24+
desc: Run the test suite
25+
cmds:
26+
- go test ./...

go.mod

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module github.com/ramzis/amend-commit
2+
3+
go 1.26
4+
5+
require github.com/stretchr/testify v1.11.1
6+
7+
require (
8+
github.com/davecgh/go-spew v1.1.1 // indirect
9+
github.com/pmezard/go-difflib v1.0.0 // indirect
10+
gopkg.in/yaml.v3 v3.0.1 // indirect
11+
)

go.sum

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
2+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
4+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
5+
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
6+
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
7+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
8+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
9+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
10+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 commit comments

Comments
 (0)