From bd03ce5c4c8234e7a21c44719ba2200eb84bb290 Mon Sep 17 00:00:00 2001 From: Layla Manley Date: Tue, 5 Dec 2023 19:39:21 +0000 Subject: [PATCH] # Squashed 3 commits - main3 - test2 - test1 --- git/cmd.go | 11 +++++++++-- git/squash.go | 16 ++++++++++++++-- main.go | 11 ++++------- 3 files changed, 27 insertions(+), 11 deletions(-) diff --git a/git/cmd.go b/git/cmd.go index 788ece7..ed2f35c 100644 --- a/git/cmd.go +++ b/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 } diff --git a/git/squash.go b/git/squash.go index 3a13b0a..4ea4877 100644 --- a/git/squash.go +++ b/git/squash.go @@ -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 } diff --git a/main.go b/main.go index 4ac4a26..3663308 100644 --- a/main.go +++ b/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) } }