mirror of
https://github.com/ent/ent.git
synced 2026-04-28 05:30:56 +03:00
entc/gen: resolve merge conflicts on file load (#4313)
If there is no conflict in the schema, the existing logic will not attempt to resolve a conflict. Therefore, if there is a conflict on file load, there now also is an attempt to fix the conflict.
This commit is contained in:
@@ -393,15 +393,15 @@ func mayRecover(err error, schemaPath string, cfg *gen.Config) error {
|
||||
if err := internal.CheckDir(schemaPath); err != nil {
|
||||
return fmt.Errorf("schema failure: %w", err)
|
||||
}
|
||||
target := filepath.Join(cfg.Target, "internal/schema.go")
|
||||
if ok, _ := cfg.FeatureEnabled(gen.FeatureGlobalID.Name); ok {
|
||||
if internal.CheckDir(gen.IncrementStartsFilePath(target)) != nil {
|
||||
if internal.CheckDir(filepath.Dir(gen.IncrementStartsFilePath(cfg.Target))) != nil {
|
||||
// Resolve the conflict by accepting the remote version of the file.
|
||||
if err := gen.ResolveIncrementStartsConflict(cfg.Target); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
target := filepath.Join(cfg.Target, "internal/schema.go")
|
||||
return (&internal.Snapshot{Path: target, Config: cfg}).Restore()
|
||||
}
|
||||
|
||||
|
||||
@@ -33,6 +33,15 @@ func IncrementStartAnnotation(g *Graph) error {
|
||||
case err != nil:
|
||||
return err
|
||||
default:
|
||||
if ok, _ := g.FeatureEnabled(FeatureSnapshot.Name); ok {
|
||||
if err = ResolveIncrementStartsConflict(g.Target); err != nil {
|
||||
return err
|
||||
}
|
||||
buf, err = os.ReadFile(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
var (
|
||||
matches = make([][]byte, 0, 2)
|
||||
lines = bytes.Split(buf, []byte("\n"))
|
||||
@@ -158,16 +167,16 @@ func ResolveIncrementStartsConflict(dir string) error {
|
||||
return err
|
||||
}
|
||||
var (
|
||||
fixed [][]byte
|
||||
skipped bool
|
||||
fixed [][]byte
|
||||
conflict, skipped bool
|
||||
lines = bytes.Split(c, []byte("\n"))
|
||||
)
|
||||
lines := bytes.Split(c, []byte("\n"))
|
||||
for _, l := range lines {
|
||||
switch {
|
||||
case bytes.HasPrefix(l, []byte("<<<<<<<")),
|
||||
bytes.HasPrefix(l, []byte("=======")),
|
||||
bytes.HasPrefix(l, []byte(">>>>>>>")):
|
||||
case bytes.HasPrefix(l, []byte(incrementIdent)) && !skipped:
|
||||
case bytes.HasPrefix(l, []byte("<<<<<<<")):
|
||||
conflict = true
|
||||
case bytes.HasPrefix(l, []byte("=======")), bytes.HasPrefix(l, []byte(">>>>>>>")):
|
||||
case bytes.HasPrefix(l, []byte(incrementIdent)) && conflict && !skipped:
|
||||
skipped = true
|
||||
default:
|
||||
fixed = append(fixed, l)
|
||||
|
||||
@@ -97,8 +97,8 @@ const IncrementStarts = %s
|
||||
require.NoError(t, os.WriteFile(p, []byte(cflct), 0644))
|
||||
|
||||
// Expect an error when there is a file conflict.
|
||||
// g, err := gen.NewGraph(c, s...)
|
||||
// require.Error(t, err)
|
||||
_, err := gen.NewGraph(c, s...)
|
||||
require.Error(t, err)
|
||||
// Conflict is resolved to "accept theirs".
|
||||
require.NoError(t, gen.ResolveIncrementStartsConflict(c.Target))
|
||||
require.NoError(t, internal.CheckDir(filepath.Dir(p)))
|
||||
|
||||
Reference in New Issue
Block a user