Skip to content

Commit

Permalink
Implement squash-push command
Browse files Browse the repository at this point in the history
  • Loading branch information
draftcode committed Nov 8, 2024
1 parent cc0552c commit 87cc684
Show file tree
Hide file tree
Showing 7 changed files with 407 additions and 1 deletion.
9 changes: 9 additions & 0 deletions cmd/pipe.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"io"
"os"

nichegit "github.com/aviator-co/niche-git"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -51,7 +52,15 @@ var pipeCmd = &cobra.Command{
}
output := GetModifiedFilesRegexpMatches(input)
return writeJSON(pipeArg.outputFile, output)
case "squash-push":
input := nichegit.SquashPushArgs{}
if err := dec.Decode(&input); err != nil {
return err
}
output := SquashPush(input)
return writeJSON(pipeArg.outputFile, output)
}

return fmt.Errorf("unknown command: %s", pipeArg.command)
},
}
Expand Down
15 changes: 15 additions & 0 deletions cmd/squash_push.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2024 Aviator Technologies, Inc.
// SPDX-License-Identifier: MIT

package cmd

import (
"net/http"

nichegit "github.com/aviator-co/niche-git"
)

func SquashPush(args nichegit.SquashPushArgs) nichegit.SquashPushOutput {
client := &http.Client{Transport: &authnRoundtripper{}}
return nichegit.SquashPush(client, args)
}
112 changes: 112 additions & 0 deletions e2e_tests/squash_push_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// Copyright 2024 Aviator Technologies, Inc.
// SPDX-License-Identifier: MIT

package e2e_tests

import (
"testing"

nichegit "github.com/aviator-co/niche-git"
"github.com/aviator-co/niche-git/cmd"
"github.com/stretchr/testify/require"
)

func TestSquashCherryPick_Resolve_Conflict(t *testing.T) {
repo := NewTempRepo(t)

baseHash := repo.CommitFile(t, "file1", `
def foo():
return 1
def bar():
return 1
def baz():
return 1
`)

repo.Git(t, "checkout", "main", "-b", "feature1")
feature1Hash := repo.CommitFile(t, "file1", `
def foo():
return 1
def bar():
return 2
def baz():
return 1
`)

repo.Git(t, "checkout", "main", "-b", "feature2")
feature2Hash := repo.CommitFile(t, "file1", `
def foo():
return 1
def bar():
return 1
def baz():
return 2
`)

repo.Git(t, "checkout", "main")
baseHash2 := repo.CommitFile(t, "file1", `
def foo():
return 2
def bar():
return 1
def baz():
return 1
`)

output := cmd.SquashPush(
nichegit.SquashPushArgs{
RepoURL: "file://" + repo.RepoDir,
BaseCommitHash: baseHash2.String(),
SquashCommands: []nichegit.SquashCommand{
{
CommitHashStart: baseHash.String(),
CommitHashEnd: feature1Hash.String(),
CommitMessage: "feature1",
Committer: "aviator-bot",
CommitterEmail: "aviator-bot@nonexistent",
CommitterTime: "2024-08-22T00:00:00Z",
Author: "aviator-bot",
AuthorEmail: "aviator-bot@nonexistent",
AuthorTime: "2024-08-22T00:00:00Z",
},
{
CommitHashStart: baseHash.String(),
CommitHashEnd: feature2Hash.String(),
CommitMessage: "feature2",
Committer: "aviator-bot",
CommitterEmail: "aviator-bot@nonexistent",
CommitterTime: "2024-08-22T00:00:00Z",
Author: "aviator-bot",
AuthorEmail: "aviator-bot@nonexistent",
AuthorTime: "2024-08-22T00:00:00Z",
},
},
Ref: "refs/heads/mq-tmp-branch",
CurrentRefHash: "",
},
)
require.Equal(t, "", output.Error)
require.Equal(t, 2, len(output.CommandResults))

repo.Git(t, "checkout", "mq-tmp-branch")
require.Equal(t, `
def foo():
return 2
def bar():
return 2
def baz():
return 2
` + "\n",
repo.ReadFile(t, "file1"))

}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ require (
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/crypto v0.26.0 // indirect
golang.org/x/exp v0.0.0-20241009180824-f66d83c29e7c
golang.org/x/net v0.28.0 // indirect
golang.org/x/sys v0.24.0 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ golang.org/x/crypto v0.3.1-0.20221117191849-2c476679df9a/go.mod h1:hebNnKkNXi2Uz
golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU=
golang.org/x/crypto v0.26.0 h1:RrRspgV4mU+YwB4FYnuBoKsUapNIL5cohGAmSH3azsw=
golang.org/x/crypto v0.26.0/go.mod h1:GY7jblb9wI+FOo5y8/S2oY4zWP07AkOJ4+jxCqdqn54=
golang.org/x/exp v0.0.0-20241009180824-f66d83c29e7c h1:7dEasQXItcW1xKJ2+gg5VOiBnqWrJc+rq0DPKyvvdbY=
golang.org/x/exp v0.0.0-20241009180824-f66d83c29e7c/go.mod h1:NQtJDoLvd6faHhE7m4T/1IY708gDefGGjR/iUW8yQQ8=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc=
Expand Down
1 change: 0 additions & 1 deletion modified_files_regexp_matches.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,6 @@ func matchPattern(pattern ModifiedFilePattern, path string, status ModificationS
}
before := len(pattern.FileContentPattern.FindAll(content1, -1))
after := len(pattern.FileContentPattern.FindAll(content2, -1))
println(before, after)
if before == 0 && after == 0 {
// No match.
return nil, nil
Expand Down
Loading

0 comments on commit 87cc684

Please sign in to comment.