# Squashed 3 commits

- main3
- test2
- test1
This commit is contained in:
2023-12-05 19:39:21 +00:00
parent 7c332f66ba
commit bd03ce5c4c
3 changed files with 27 additions and 11 deletions

View File

@@ -1,6 +1,7 @@
package git
import (
"log"
"strconv"
"strings"
)
@@ -16,26 +17,32 @@ func (g *Git) Squash(input SquashInput) error {
// Get the current commit hash
hash, err := g.GetHash()
if err != nil {
log.Panic(err)
return err
}
// Get the commit hash on the remote
remote, err := g.GetCommitRemote()
if err != nil {
log.Panic(err)
return err
}
// If the hashes are the same, there's nothing to squash
if hash == remote {
log.Println("Already up to date")
return nil
}
// Get the number of commits ahead of main
commits, err := g.GetCommitsAhead()
if err != nil {
log.Panic(err)
return err
}
log.Println(commits, "commits ahead of main")
message := ""
if input.Message != nil {
@@ -45,6 +52,7 @@ func (g *Git) Squash(input SquashInput) error {
// Get array of commit messages
out, err := g.Execute("log", "--pretty=format:%s", "-n", strconv.Itoa(commits))
if err != nil {
log.Println(out)
return err
}
messages := strings.Split(out, "\n")
@@ -62,8 +70,12 @@ func (g *Git) Squash(input SquashInput) error {
}
// Squash the current commit into the previous commits
_, err = g.Execute("rebase", "-i", hash+"~"+strconv.Itoa(commits), "-x", "git commit --amend -m \""+message+"\" ")
_, err = g.Execute("reset", "--soft", "HEAD~"+strconv.Itoa(commits))
if err != nil {
return err
}
_, err = g.Execute("commit", "-m", message)
if err != nil {
return err
}