# Squashed 3 commits

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

View File

@ -1,10 +1,16 @@
package git package git
import "os/exec" import (
"log"
"os/exec"
"strings"
)
// Execute runs a git command and returns the stdout // Execute runs a git command and returns the stdout
func (g *Git) Execute(args ...string) (string, error) { func (g *Git) Execute(args ...string) (string, error) {
log.Println("git", strings.Join(args, " "))
cmd := exec.Command("git", args...) cmd := exec.Command("git", args...)
if g.WorkingDir != "" { if g.WorkingDir != "" {
cmd.Dir = g.WorkingDir cmd.Dir = g.WorkingDir
@ -15,5 +21,6 @@ func (g *Git) Execute(args ...string) (string, error) {
if err != nil { if err != nil {
return "", err return "", err
} }
return string(out), nil
return strings.TrimSuffix(string(out), "\n"), nil
} }

View File

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

11
main.go
View File

@ -17,7 +17,6 @@ func main() {
// Parse flags // Parse flags
flagset.Parse(os.Args[1:]) flagset.Parse(os.Args[1:])
args := flagset.Args()
// Print version // Print version
if *version { if *version {
@ -26,7 +25,7 @@ func main() {
} }
// Help // Help
if *help || len(args) == 0 { if *help {
println("Usage: gsquash [options] [message]") println("Usage: gsquash [options] [message]")
flagset.PrintDefaults() flagset.PrintDefaults()
return return
@ -51,11 +50,9 @@ func main() {
} }
// Squash // Squash
if len(args) > 0 { err := g.Squash(squashInput)
err := g.Squash(squashInput) if err != nil {
if err != nil { panic(err)
panic(err)
}
} }
} }