2022-12-13 18:20:04 +08:00
|
|
|
package gitutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2023-02-16 14:34:54 +08:00
|
|
|
"strings"
|
2022-12-13 18:20:04 +08:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2022-12-16 04:16:37 +08:00
|
|
|
func GitInit(c *Git, tb testing.TB) {
|
2022-12-13 18:20:04 +08:00
|
|
|
tb.Helper()
|
2022-12-16 04:16:37 +08:00
|
|
|
out, err := fakeGit(c, "init")
|
2022-12-13 18:20:04 +08:00
|
|
|
require.NoError(tb, err)
|
|
|
|
require.Contains(tb, out, "Initialized empty Git repository")
|
|
|
|
require.NoError(tb, err)
|
2022-12-16 04:16:37 +08:00
|
|
|
GitCheckoutBranch(c, tb, "main")
|
|
|
|
_, _ = fakeGit(c, "branch", "-D", "master")
|
2022-12-13 18:20:04 +08:00
|
|
|
}
|
|
|
|
|
2022-12-16 04:16:37 +08:00
|
|
|
func GitCommit(c *Git, tb testing.TB, msg string) {
|
2022-12-13 18:20:04 +08:00
|
|
|
tb.Helper()
|
2022-12-16 04:16:37 +08:00
|
|
|
out, err := fakeGit(c, "commit", "--allow-empty", "-m", msg)
|
2022-12-13 18:20:04 +08:00
|
|
|
require.NoError(tb, err)
|
|
|
|
require.Contains(tb, out, "main", msg)
|
|
|
|
}
|
|
|
|
|
2022-12-16 04:16:37 +08:00
|
|
|
func GitTag(c *Git, tb testing.TB, tag string) {
|
2022-12-13 18:20:04 +08:00
|
|
|
tb.Helper()
|
2022-12-16 04:16:37 +08:00
|
|
|
out, err := fakeGit(c, "tag", tag)
|
2022-12-13 18:20:04 +08:00
|
|
|
require.NoError(tb, err)
|
|
|
|
require.Empty(tb, out)
|
|
|
|
}
|
|
|
|
|
2022-12-16 04:16:37 +08:00
|
|
|
func GitCheckoutBranch(c *Git, tb testing.TB, name string) {
|
2022-12-13 18:20:04 +08:00
|
|
|
tb.Helper()
|
2022-12-16 04:16:37 +08:00
|
|
|
out, err := fakeGit(c, "checkout", "-b", name)
|
2022-12-13 18:20:04 +08:00
|
|
|
require.NoError(tb, err)
|
|
|
|
require.Empty(tb, out)
|
|
|
|
}
|
|
|
|
|
2023-05-25 20:55:26 +08:00
|
|
|
func GitAdd(c *Git, tb testing.TB, files ...string) {
|
2022-12-13 18:20:04 +08:00
|
|
|
tb.Helper()
|
2023-05-25 20:55:26 +08:00
|
|
|
args := append([]string{"add"}, files...)
|
|
|
|
_, err := fakeGit(c, args...)
|
2022-12-13 18:20:04 +08:00
|
|
|
require.NoError(tb, err)
|
|
|
|
}
|
|
|
|
|
2023-01-24 23:16:57 +08:00
|
|
|
func GitSetRemote(c *Git, tb testing.TB, name string, url string) {
|
2022-12-13 18:20:04 +08:00
|
|
|
tb.Helper()
|
2023-01-24 23:16:57 +08:00
|
|
|
_, err := fakeGit(c, "remote", "add", name, url)
|
2022-12-13 18:20:04 +08:00
|
|
|
require.NoError(tb, err)
|
|
|
|
}
|
|
|
|
|
2023-12-01 14:07:50 +08:00
|
|
|
func GitSetMainUpstream(c *Git, tb testing.TB, remote, target string) {
|
|
|
|
tb.Helper()
|
|
|
|
_, err := fakeGit(c, "fetch", "--depth", "1", remote, target)
|
|
|
|
require.NoError(tb, err)
|
|
|
|
|
|
|
|
_, err = fakeGit(c, "branch", "--set-upstream-to", remote+"/"+target, "main")
|
|
|
|
require.NoError(tb, err)
|
|
|
|
}
|
|
|
|
|
2022-12-13 18:20:04 +08:00
|
|
|
func Mktmp(tb testing.TB) string {
|
|
|
|
tb.Helper()
|
|
|
|
folder := tb.TempDir()
|
|
|
|
current, err := os.Getwd()
|
|
|
|
require.NoError(tb, err)
|
|
|
|
require.NoError(tb, os.Chdir(folder))
|
|
|
|
tb.Cleanup(func() {
|
|
|
|
require.NoError(tb, os.Chdir(current))
|
|
|
|
})
|
|
|
|
return folder
|
|
|
|
}
|
|
|
|
|
2022-12-16 04:16:37 +08:00
|
|
|
func fakeGit(c *Git, args ...string) (string, error) {
|
2022-12-13 18:20:04 +08:00
|
|
|
allArgs := []string{
|
|
|
|
"-c", "user.name=buildx",
|
|
|
|
"-c", "user.email=buildx@docker.com",
|
|
|
|
"-c", "commit.gpgSign=false",
|
|
|
|
"-c", "tag.gpgSign=false",
|
|
|
|
"-c", "log.showSignature=false",
|
|
|
|
}
|
|
|
|
allArgs = append(allArgs, args...)
|
|
|
|
return c.clean(c.run(allArgs...))
|
|
|
|
}
|
2023-02-16 14:34:54 +08:00
|
|
|
|
|
|
|
func IsAmbiguousArgument(err error) bool {
|
|
|
|
if err == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
errMsg := strings.ToLower(err.Error())
|
|
|
|
return strings.Contains(errMsg, "use '--' to separate paths from revisions")
|
|
|
|
}
|