# Squashed 3 commits
- main3 - test2 - test1
This commit is contained in:
parent
7c332f66ba
commit
bd03ce5c4c
11
git/cmd.go
11
git/cmd.go
@ -1,10 +1,16 @@
|
||||
package git
|
||||
|
||||
import "os/exec"
|
||||
import (
|
||||
"log"
|
||||
"os/exec"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Execute runs a git command and returns the stdout
|
||||
func (g *Git) Execute(args ...string) (string, error) {
|
||||
|
||||
log.Println("git", strings.Join(args, " "))
|
||||
|
||||
cmd := exec.Command("git", args...)
|
||||
if g.WorkingDir != "" {
|
||||
cmd.Dir = g.WorkingDir
|
||||
@ -15,5 +21,6 @@ func (g *Git) Execute(args ...string) (string, error) {
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(out), nil
|
||||
|
||||
return strings.TrimSuffix(string(out), "\n"), nil
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
11
main.go
11
main.go
@ -17,7 +17,6 @@ func main() {
|
||||
|
||||
// Parse flags
|
||||
flagset.Parse(os.Args[1:])
|
||||
args := flagset.Args()
|
||||
|
||||
// Print version
|
||||
if *version {
|
||||
@ -26,7 +25,7 @@ func main() {
|
||||
}
|
||||
|
||||
// Help
|
||||
if *help || len(args) == 0 {
|
||||
if *help {
|
||||
println("Usage: gsquash [options] [message]")
|
||||
flagset.PrintDefaults()
|
||||
return
|
||||
@ -51,11 +50,9 @@ func main() {
|
||||
}
|
||||
|
||||
// Squash
|
||||
if len(args) > 0 {
|
||||
err := g.Squash(squashInput)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
err := g.Squash(squashInput)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user