merge
This commit is contained in:
parent
dbee0e8bd3
commit
c0c467d72b
@ -4,6 +4,8 @@ import (
|
|||||||
"bufio"
|
"bufio"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
"slices"
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@ -55,11 +57,15 @@ func FetchPRSet(gitea common.GiteaPRFetcher, org, repo string, num int64, config
|
|||||||
return &PRSet{prs: prs, config: config}, nil
|
return &PRSet{prs: prs, config: config}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (rs *PRSet) IsPrjGitPR(pr *models.PullRequest) bool {
|
||||||
|
return pr.Base.Repo.Name == rs.config.GitProjectName && pr.Base.Repo.Owner.UserName == rs.config.Organization
|
||||||
|
}
|
||||||
|
|
||||||
func (rs *PRSet) GetPrjGitPR() (*models.PullRequest, error) {
|
func (rs *PRSet) GetPrjGitPR() (*models.PullRequest, error) {
|
||||||
var ret *models.PullRequest
|
var ret *models.PullRequest
|
||||||
|
|
||||||
for _, prinfo := range rs.prs {
|
for _, prinfo := range rs.prs {
|
||||||
if prinfo.pr.Base.Repo.Name == rs.config.GitProjectName && prinfo.pr.Base.Repo.Owner.UserName == rs.config.Organization {
|
if rs.IsPrjGitPR(prinfo.pr) {
|
||||||
if ret == nil {
|
if ret == nil {
|
||||||
ret = prinfo.pr
|
ret = prinfo.pr
|
||||||
} else {
|
} else {
|
||||||
@ -153,34 +159,91 @@ func (rs *PRSet) Merge() error {
|
|||||||
if s.Path != ".gitmodules" {
|
if s.Path != ".gitmodules" {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
/*
|
|
||||||
|
|
||||||
submodules, err := git.GitSubmoduleList(common.DefaultGitPrj, "MERGE_HEAD")
|
submodules, err := git.GitSubmoduleList(common.DefaultGitPrj, "MERGE_HEAD")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Failed to fetch submodules during merge resolution: %w", err)
|
return fmt.Errorf("Failed to fetch submodules during merge resolution: %w", err)
|
||||||
}
|
}
|
||||||
s1, err := git.GitExecWithOutput(common.DefaultGitPrj, "git", "cat-file", "blob", s.States[0])
|
s1, err := git.GitExecWithOutput(common.DefaultGitPrj, "cat-file", "blob", s.States[0])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Failed fetching data during .gitmodules merge resoulution: %w", err)
|
return fmt.Errorf("Failed fetching data during .gitmodules merge resoulution: %w", err)
|
||||||
}
|
}
|
||||||
s2, err := git.GitExecWithOutput(common.DefaultGitPrj, "git", "cat-file", "blob", s.States[1])
|
s2, err := git.GitExecWithOutput(common.DefaultGitPrj, "cat-file", "blob", s.States[1])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Failed fetching data during .gitmodules merge resoulution: %w", err)
|
return fmt.Errorf("Failed fetching data during .gitmodules merge resoulution: %w", err)
|
||||||
}
|
}
|
||||||
s3, err := git.GitExecWithOutput(common.DefaultGitPrj, "git", "cat-file", "blob", s.States[2])
|
s3, err := git.GitExecWithOutput(common.DefaultGitPrj, "cat-file", "blob", s.States[2])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Failed fetching data during .gitmodules merge resoulution: %w", err)
|
return fmt.Errorf("Failed fetching data during .gitmodules merge resoulution: %w", err)
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
|
||||||
|
subs1, err := ParseSubmodulesFile(strings.NewReader(s1))
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Failed parsing submodule file [%s] in merge: %w", s.States[0], err)
|
||||||
|
}
|
||||||
|
subs2, err := ParseSubmodulesFile(strings.NewReader(s2))
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Failed parsing submodule file [%s] in merge: %w", s.States[0], err)
|
||||||
|
}
|
||||||
|
subs3, err := ParseSubmodulesFile(strings.NewReader(s3))
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Failed parsing submodule file [%s] in merge: %w", s.States[0], err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// merge from subs3 (target), subs1 (orig), subs2 (2-nd base that is missing from target base)
|
||||||
|
// this will update submodules
|
||||||
|
mergedSubs := slices.Concat(subs1, subs2, subs3)
|
||||||
|
|
||||||
|
var filteredSubs []Submodule = make([]Submodule, 0, max(len(subs1), len(subs2), len(subs3)))
|
||||||
|
nextSub:
|
||||||
|
for subName := range submodules {
|
||||||
|
|
||||||
|
for i := range mergedSubs {
|
||||||
|
if path.Base(mergedSubs[i].Path) == subName {
|
||||||
|
filteredSubs = append(filteredSubs, mergedSubs[i])
|
||||||
|
continue nextSub
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return fmt.Errorf("Cannot find submodule for path: %s", subName)
|
||||||
|
}
|
||||||
|
|
||||||
|
out, err := os.Create(path.Join(git.GetPath(), common.DefaultGitPrj, ".gitmodules"))
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Can't open .gitmodules for writing: %w", err)
|
||||||
|
}
|
||||||
|
if err = WriteSubmodules(filteredSubs, out); err != nil {
|
||||||
|
return fmt.Errorf("Can't write .gitmodules: %w", err)
|
||||||
|
}
|
||||||
|
if out.Close(); err != nil {
|
||||||
|
return fmt.Errorf("Can't close .gitmodules: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
os.CopyFS("/tmp/test", os.DirFS(git.GetPath()))
|
||||||
|
|
||||||
|
git.GitExecOrPanic(common.DefaultGitPrj, "add", ".gitmodules")
|
||||||
|
git.GitExecOrPanic(common.DefaultGitPrj, "-c", "core.editor=true", "merge", "--continue")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if strings.Contains(err.Error(), "Aborting merge") {
|
// FF all non-prj git
|
||||||
return errors.New("stuff")
|
for _, prinfo := range rs.prs {
|
||||||
|
if rs.IsPrjGitPR(prinfo.pr) {
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
return err
|
git.GitExecOrPanic("", "clone", prinfo.pr.Base.Repo.SSHURL, prinfo.pr.Base.Name)
|
||||||
|
git.GitExecOrPanic(prinfo.pr.Base.Name, "fetch", "origin", prinfo.pr.Head.Sha)
|
||||||
|
git.GitExecOrPanic(prinfo.pr.Base.Name, "merge", "--ff", prinfo.pr.Head.Sha)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// push changes
|
||||||
git.GitExecOrPanic(common.DefaultGitPrj, "push", "origin")
|
git.GitExecOrPanic(common.DefaultGitPrj, "push", "origin")
|
||||||
|
for _, prinfo := range rs.prs {
|
||||||
|
if rs.IsPrjGitPR(prinfo.pr) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
git.GitExecOrPanic(prinfo.pr.Base.Name, "push", "origin")
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -1,13 +1,8 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
|
||||||
|
|
||||||
"go.uber.org/mock/gomock"
|
|
||||||
"src.opensuse.org/autogits/common"
|
|
||||||
mock_common "src.opensuse.org/autogits/common/mock"
|
|
||||||
)
|
)
|
||||||
|
/*
|
||||||
func TestPRReviewed(t *testing.T) {
|
func TestPRReviewed(t *testing.T) {
|
||||||
testData := []struct {
|
testData := []struct {
|
||||||
title string
|
title string
|
||||||
@ -65,3 +60,4 @@ func TestPRReviewed(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
@ -23,6 +23,7 @@ create_prjgit_sample() {
|
|||||||
git init -q --object-format=sha256 -b main
|
git init -q --object-format=sha256 -b main
|
||||||
echo Project git is here > README.md
|
echo Project git is here > README.md
|
||||||
git add README.md
|
git add README.md
|
||||||
|
git config receive.denyCurrentBranch ignore
|
||||||
|
|
||||||
git submodule init
|
git submodule init
|
||||||
git submodule -q add ../pkgA pkgA
|
git submodule -q add ../pkgA pkgA
|
||||||
@ -52,6 +53,7 @@ create_pkgA() {
|
|||||||
pushd pkgA
|
pushd pkgA
|
||||||
|
|
||||||
git init -q --object-format=sha256
|
git init -q --object-format=sha256
|
||||||
|
git config receive.denyCurrentBranch ignore
|
||||||
echo "Package A" > README.md
|
echo "Package A" > README.md
|
||||||
git add README.md
|
git add README.md
|
||||||
|
|
||||||
@ -65,6 +67,7 @@ create_pkgB() {
|
|||||||
pushd pkgB
|
pushd pkgB
|
||||||
|
|
||||||
git init -q --object-format=sha256
|
git init -q --object-format=sha256
|
||||||
|
git config receive.denyCurrentBranch ignore
|
||||||
echo "Package B" > README.md
|
echo "Package B" > README.md
|
||||||
git add README.md
|
git add README.md
|
||||||
|
|
||||||
@ -78,6 +81,7 @@ create_pkgB1() {
|
|||||||
pushd pkgB1
|
pushd pkgB1
|
||||||
|
|
||||||
git init -q --object-format=sha256
|
git init -q --object-format=sha256
|
||||||
|
git config receive.denyCurrentBranch ignore
|
||||||
echo "Package B1" > README.md
|
echo "Package B1" > README.md
|
||||||
git add README.md
|
git add README.md
|
||||||
|
|
||||||
@ -91,6 +95,7 @@ create_pkgB2() {
|
|||||||
pushd pkgB2
|
pushd pkgB2
|
||||||
|
|
||||||
git init -q --object-format=sha256
|
git init -q --object-format=sha256
|
||||||
|
git config receive.denyCurrentBranch ignore
|
||||||
echo "Package B2" > README.md
|
echo "Package B2" > README.md
|
||||||
git add README.md
|
git add README.md
|
||||||
|
|
||||||
@ -104,6 +109,7 @@ create_pkgC() {
|
|||||||
pushd pkgC
|
pushd pkgC
|
||||||
|
|
||||||
git init -q --object-format=sha256
|
git init -q --object-format=sha256
|
||||||
|
git config receive.denyCurrentBranch ignore
|
||||||
echo "Package C" > README.md
|
echo "Package C" > README.md
|
||||||
git add README.md
|
git add README.md
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user