Skip to content

Commit ccccda1

Browse files
authored
Merge pull request #1996 from ako/claude/issue-triage-prioritization-24rqgv
fix(git merge): keep text resolutions across the exit-2 abort
2 parents 4b63cb5 + 4ced127 commit ccccda1

6 files changed

Lines changed: 290 additions & 15 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"area": "cmd/mxcli", "date": "2026-11-06", "symptom": "ako/mxcli#1982 (mendixlabs/mxcli#1751): a merge conflicting on a text file and the model: `git merge` stopped on the text file, the human resolved and staged it, the re-run hit ManualResolutionRequired (exit 2) and aborted — and `git merge --abort` reset the index with the resolution in it. `--plan` then re-merged, met the text conflict unresolved, wrote the model, repaired the hashes and failed at the commit with 'Committing is not possible because you have unmerged files': half done, the resolution gone.", "cause": "The exit-2 path aborts with `git merge --abort` and recorded nothing about the index; applyPlan never looked for non-model conflicts (only mergeRun did), so it went straight to the engine and the model write.", "file": "cmd/mxcli/cmd_git_merge_text.go (stagedTextResolutions / restoreTextResolutions / openTextConflicts), cmd/mxcli/cmd_git_merge.go (exit-2 path records the resolutions in the plan template's `resolved`; applyPlan restores them onto paths that conflict again, then stops on any open text conflict before the stale check, the engine and the model)", "insight": "An abort that resets the index is a data-loss step for whatever the human staged, so record it before aborting — and the index blob of a staged path is already in the object store, which `merge --abort` does not prune, so the plan template only needs path + blob (or deleted). Restoring only onto a path that conflicts again leaves a cleanly merged one as the merge made it. The fake engine (engineDecides) and real git make the reporter's three steps a unit test; the first run reproduced the exact 'unmerged files' commit error. Revert proofs: with the restore stubbed, the open-conflict check now stops the plan safely (resolution lost but nothing half-written); with the check stubbed, the engine runs with the text conflict open. Real-mx integration tests (11.12.6) still pass.", "refs": ["ako/mxcli#1982", "mendixlabs/mxcli#1751", "ako/mxcli#1981"], "test": "cmd/mxcli/gitmerge_text_resolution_test.go"}

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
1515

1616
### Fixed
1717

18+
- **`git merge` keeps your text resolutions when it stops for a model decision** (ako/mxcli#1982, mendixlabs/mxcli#1751) — after you resolved and staged a non-model conflict and re-ran, a model conflict that needs a decision aborted the merge and threw the resolution away; `--plan` then wrote the model and failed at the commit on "unmerged files", leaving the merge half done. The abort now records your resolutions in the plan template and `--plan` restores them, and `--plan` stops before it touches the model while a text conflict is still open.
1819
- **`create constant` writes `ExportLevel: Hidden`, as Studio Pro does** (ako/mxcli#1980, mendixlabs/mxcli#1749) — it wrote `""`, which `mx check` accepts but `mx diff` crashes on against a `Hidden` copy (exit 129), taking `git merge`'s report with it. `create or modify constant` now keeps a stored level instead of clearing it, and repairs a stored `""`.
1920
- **`git merge` reports the side whose `mx diff` failed instead of dropping the report** (ako/mxcli#1981, mendixlabs/mxcli#1750) — when one side's diff failed, no property-level report was printed at all. The side that worked is shown, and the failing side reads `(diff unavailable: <why>)`.
2021
- **`widget sync` keeps the integers it does not change as integers** (ako/mxcli#1976, mendixlabs/mxcli#1748) — syncing a widget wrote every int32 and int64 in it back as a double, a text box in the widget's slot included, whose `MaxLengthCode -1.0` Mendix reads as unlimited (CE6510). A unit already synced by an earlier version keeps its doubles; re-create the page to repair it.

‎cmd/mxcli/cmd_git_merge.go‎

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,9 @@ the engine chokes on the conflicted state. Its result is copied back:
8585
- a conflict in non-model files (text sources) is left in place for manual
8686
resolution, like plain git: resolve them, 'git add' them, and re-run
8787
this command — it finishes the merge (model conflicts included) and
88-
commits.
88+
commits. If that re-run then needs a model decision, the abort keeps
89+
what you resolved: the plan template records it and --plan restores
90+
it, and --plan stops before the model while a text conflict is open.
8991
9092
--ours/--theirs take one side of every conflicted unit — never a side of the
9193
.mpr without the hash check, because the .mpr lists every document: a whole
@@ -233,11 +235,7 @@ func mergeRun(mprPath, ref, message, strategy, plan string, push bool, runners m
233235
}
234236
}
235237
if len(other) > 0 {
236-
fmt.Fprintln(out, "Non-model conflicts, resolve by hand, then 'git add' them and re-run this command to resolve the model and commit:")
237-
for _, p := range other {
238-
fmt.Fprintf(out, " %s\n", p)
239-
}
240-
return errors.New("merge has non-model conflicts left for manual resolution")
238+
return reportTextConflicts(out, other)
241239
}
242240

243241
baseSha := strings.TrimSpace(gitOutOr(root, "merge-base", "HEAD", "MERGE_HEAD"))
@@ -317,7 +315,11 @@ func mergeRun(mprPath, ref, message, strategy, plan string, push bool, runners m
317315
// "the diff crashed" read as "no details" (ako/mxcli#1981).
318316
fmt.Fprintln(out, propertyReport(diffSide{oursJSON, d1}, diffSide{theirsJSON, d2}, manual))
319317
}
320-
if err := writePlanTemplate(mprPath, baseSha, oursSha, theirsSha, manual); err != nil {
318+
// The abort below resets the index; what the human resolved and staged
319+
// for the non-model files is recorded first, and --plan restores it
320+
// (mendixlabs/mxcli#1751).
321+
resolved := stagedTextResolutions(root, repo, mprPath)
322+
if err := writePlanTemplate(mprPath, baseSha, oursSha, theirsSha, manual, resolved); err != nil {
321323
fmt.Fprintf(out, " (could not write the plan template: %v)\n", err)
322324
} else {
323325
fmt.Fprintf(out, "Plan template: %s — set \"take\" per unit, re-run with --plan.\n", filepath.Base(mprPath)+".merge-plan.json")
@@ -804,15 +806,18 @@ type mergePlanTemplate struct {
804806
Ours string `json:"ours"`
805807
Theirs string `json:"theirs"`
806808
Units []planUnit `json:"units"`
809+
// Resolved are the non-model files the human had resolved and staged when
810+
// the merge was aborted for this plan; --plan restores them.
811+
Resolved []textResolution `json:"resolved,omitempty"`
807812
}
808813

809814
type planUnit struct {
810815
ID string `json:"id"`
811816
Take string `json:"take"`
812817
}
813818

814-
func writePlanTemplate(mprPath, baseSha, oursSha, theirsSha string, unitIDs []string) error {
815-
tmpl := mergePlanTemplate{Base: baseSha, Ours: oursSha, Theirs: theirsSha}
819+
func writePlanTemplate(mprPath, baseSha, oursSha, theirsSha string, unitIDs []string, resolved []textResolution) error {
820+
tmpl := mergePlanTemplate{Base: baseSha, Ours: oursSha, Theirs: theirsSha, Resolved: resolved}
816821
for _, id := range unitIDs {
817822
tmpl.Units = append(tmpl.Units, planUnit{ID: id, Take: "ours"})
818823
}
@@ -842,12 +847,7 @@ func applyPlan(root string, repo *gitNoteRepo, mprAbs, mprPath, ref, planPath, m
842847
if err != nil {
843848
return fmt.Errorf("read plan: %w", err)
844849
}
845-
var plan struct {
846-
Base string `json:"base"`
847-
Ours string `json:"ours"`
848-
Theirs string `json:"theirs"`
849-
Units []planUnit `json:"units"`
850-
}
850+
var plan mergePlanTemplate
851851
if err := json.Unmarshal(raw, &plan); err != nil {
852852
return fmt.Errorf("plan %s is not valid JSON: %w", planPath, err)
853853
}
@@ -863,6 +863,20 @@ func applyPlan(root string, repo *gitNoteRepo, mprAbs, mprPath, ref, planPath, m
863863
return fmt.Errorf("git merge %s: %v", ref, mergeErr)
864864
}
865865
}
866+
// The text resolutions the abort recorded go back first; a non-model
867+
// conflict still open then stops the plan before it touches the model,
868+
// the merge left in progress (mendixlabs/mxcli#1751).
869+
restored, err := restoreTextResolutions(root, plan.Resolved)
870+
if err != nil {
871+
return err
872+
}
873+
if len(restored) > 0 {
874+
fmt.Fprintf(out, "Restored your resolution of %s.\n", strings.Join(restored, ", "))
875+
}
876+
if other := openTextConflicts(root, repo); len(other) > 0 {
877+
return reportTextConflicts(out, other)
878+
}
879+
866880
baseSha := strings.TrimSpace(gitOutOr(root, "merge-base", "HEAD", "MERGE_HEAD"))
867881
oursSha := strings.TrimSpace(gitOutOr(root, "rev-parse", "HEAD"))
868882
theirsSha := strings.TrimSpace(gitOutOr(root, "rev-parse", "MERGE_HEAD"))

‎cmd/mxcli/cmd_git_merge_text.go‎

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
package main
4+
5+
import (
6+
"errors"
7+
"fmt"
8+
"io"
9+
"os"
10+
"path/filepath"
11+
"strings"
12+
)
13+
14+
// Text resolutions across the ManualResolutionRequired abort
15+
// (mendixlabs/mxcli#1751).
16+
//
17+
// A merge that conflicts on both a non-model file and the model goes in
18+
// steps: the run stops on the text conflict, the human resolves and stages
19+
// it, the re-run reaches the engine, and the engine needs a decision (exit 2)
20+
// — which aborts the merge, and `git merge --abort` resets the index with the
21+
// human's resolution in it. The --plan run then re-merged, met the same text
22+
// conflict unresolved, wrote the model anyway and failed at the commit:
23+
// half done, the resolution gone.
24+
//
25+
// So the abort records each staged non-model path in the plan template — its
26+
// index blob, which git keeps in the object store after the abort — and the
27+
// --plan run puts it back on a path that conflicts again. And before the plan
28+
// touches the model, any non-model conflict still open stops it, as it stops
29+
// the plain run, with the merge left in progress for the human.
30+
31+
// textResolution is one staged non-model path at the abort: the index blob it
32+
// had, or Deleted when the resolution removed it.
33+
type textResolution struct {
34+
Path string `json:"path"`
35+
Blob string `json:"blob,omitempty"`
36+
Deleted bool `json:"deleted,omitempty"`
37+
}
38+
39+
// stagedTextResolutions lists the non-model paths the index changes against
40+
// HEAD — the human's resolutions, and the side's clean changes, which restoring
41+
// writes back unchanged.
42+
func stagedTextResolutions(root string, repo *gitNoteRepo, mprPath string) []textResolution {
43+
plan := filepath.ToSlash(filepath.Base(mprPath) + ".merge-plan.json")
44+
var out []textResolution
45+
for _, p := range nulFields(gitOutOr(root, "diff", "--cached", "--name-only", "-z")) {
46+
if isModelPath(repo, p) || strings.HasSuffix(filepath.ToSlash(p), plan) {
47+
continue
48+
}
49+
blob, err := gitOut(root, "rev-parse", "--verify", "--quiet", ":0:"+p)
50+
if blob = strings.TrimSpace(blob); err != nil || blob == "" {
51+
out = append(out, textResolution{Path: p, Deleted: true})
52+
continue
53+
}
54+
out = append(out, textResolution{Path: p, Blob: blob})
55+
}
56+
return out
57+
}
58+
59+
// restoreTextResolutions puts each recorded resolution back on a path that
60+
// conflicts again, and stages it. A path that merged cleanly this time is left
61+
// as the merge made it. It reports the paths it restored.
62+
func restoreTextResolutions(root string, saved []textResolution) ([]string, error) {
63+
if len(saved) == 0 {
64+
return nil, nil
65+
}
66+
unmerged := map[string]bool{}
67+
for _, p := range nulFields(gitOutOr(root, "diff", "--name-only", "--diff-filter=U", "-z")) {
68+
unmerged[p] = true
69+
}
70+
var restored []string
71+
for _, r := range saved {
72+
if !unmerged[r.Path] {
73+
continue
74+
}
75+
if r.Deleted {
76+
if _, err := gitOut(root, "rm", "-q", "--", r.Path); err != nil {
77+
return restored, fmt.Errorf("restore the resolution of %s (deleted): %w", r.Path, err)
78+
}
79+
restored = append(restored, r.Path)
80+
continue
81+
}
82+
content, err := gitOut(root, "cat-file", "blob", r.Blob)
83+
if err != nil {
84+
return restored, fmt.Errorf("restore the resolution of %s: blob %s is gone (%v); resolve it again", r.Path, shortCommit(r.Blob), err)
85+
}
86+
dst := filepath.Join(root, filepath.FromSlash(r.Path))
87+
if err := os.WriteFile(dst, []byte(content), 0o644); err != nil {
88+
return restored, err
89+
}
90+
if _, err := gitOut(root, "add", "--", r.Path); err != nil {
91+
return restored, fmt.Errorf("stage the resolution of %s: %w", r.Path, err)
92+
}
93+
restored = append(restored, r.Path)
94+
}
95+
return restored, nil
96+
}
97+
98+
// openTextConflicts lists the non-model paths still unmerged.
99+
func openTextConflicts(root string, repo *gitNoteRepo) []string {
100+
var other []string
101+
for _, p := range nulFields(gitOutOr(root, "diff", "--name-only", "--diff-filter=U", "-z")) {
102+
if !isModelPath(repo, p) {
103+
other = append(other, p)
104+
}
105+
}
106+
return other
107+
}
108+
109+
// reportTextConflicts prints the plain run's "resolve by hand" list and
110+
// returns its error; the merge is left in progress.
111+
func reportTextConflicts(out io.Writer, other []string) error {
112+
fmt.Fprintln(out, "Non-model conflicts, resolve by hand, then 'git add' them and re-run this command to resolve the model and commit:")
113+
for _, p := range other {
114+
fmt.Fprintf(out, " %s\n", p)
115+
}
116+
return errors.New("merge has non-model conflicts left for manual resolution")
117+
}
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
package main
4+
5+
import (
6+
"bytes"
7+
"encoding/json"
8+
"os"
9+
"path/filepath"
10+
"strings"
11+
"testing"
12+
)
13+
14+
// mendixlabs/mxcli#1751 (ako/mxcli#1982), as reported:
15+
//
16+
// 1. `git merge <ref>` stops on a non-model conflict ("resolve by hand…").
17+
// 2. The human resolves it, `git add`s it and re-runs: the engine needs a
18+
// decision (exit 2) and the merge is aborted — and the text resolution
19+
// goes with the abort.
20+
// 3. `--plan` then wrote the model, repaired the hashes and failed at the
21+
// commit on "unmerged files": a half-done merge, the resolution lost.
22+
//
23+
// The staged resolution must survive the abort and come back on the --plan
24+
// run; and a --plan run with a text conflict still open must stop before it
25+
// touches the model.
26+
27+
// modelAndTextConflictRepo is a modelConflictRepo where each side also changed
28+
// NOTES.txt, so the merge conflicts on both the domain model and a text file.
29+
func modelAndTextConflictRepo(t *testing.T) (string, string) {
30+
t.Helper()
31+
dir, mprPath := doctorNoteRepo(t)
32+
write := func(name, content string) {
33+
if err := os.WriteFile(filepath.Join(dir, name), []byte(content), 0o644); err != nil {
34+
t.Fatal(err)
35+
}
36+
}
37+
units := baseUnits(t)
38+
units[tDM] = tUnit{tMod, unitDoc(t, "DomainModels$DomainModel", "", mainSideKey)}
39+
writeProject(t, dir, "11.13.0", units)
40+
write("NOTES.txt", "main\n")
41+
gitIn(t, dir, "add", "-A")
42+
gitIn(t, dir, "commit", "-q", "-m", "main side")
43+
gitIn(t, dir, "switch", "-q", "-c", "side", "HEAD~1")
44+
side := baseUnits(t)
45+
side[tDM] = tUnit{tMod, unitDoc(t, "DomainModels$DomainModel", "", sideKey)}
46+
writeProject(t, dir, "11.13.0", side)
47+
write("NOTES.txt", "side\n")
48+
gitIn(t, dir, "add", "-A")
49+
gitIn(t, dir, "commit", "-q", "-m", "side change")
50+
gitIn(t, dir, "switch", "-q", "feature")
51+
return dir, mprPath
52+
}
53+
54+
// engineResult is what the fake engine writes for the undecided unit: ours'
55+
// content, marked.
56+
func engineResult(t *testing.T) map[string]tUnit {
57+
result := baseUnits(t)
58+
result[tDM] = tUnit{tMod, unitDoc(t, "DomainModels$DomainModel", "", mainSideKey)}
59+
return result
60+
}
61+
62+
func TestGitMerge_TextResolutionSurvivesTheAbortAndThePlan(t *testing.T) {
63+
dir, mprPath := modelAndTextConflictRepo(t)
64+
doctorEnv(t, dir)
65+
mx := mxRunners{merge: engineDecides(t, engineResult(t), []string{tDM}, nil)}
66+
67+
// 1. The text conflict stops the run before mx.
68+
var out bytes.Buffer
69+
if err := mergeRun(mprPath, "side", "", "", "", false, mx, &out); err == nil ||
70+
!strings.Contains(out.String(), "NOTES.txt") {
71+
t.Fatalf("step 1: want the non-model conflict reported, got %v\n%s", err, out.String())
72+
}
73+
74+
// 2. Resolved by hand and staged; the re-run needs a model decision and
75+
// aborts.
76+
if err := os.WriteFile(filepath.Join(dir, "NOTES.txt"), []byte("resolved by hand\n"), 0o644); err != nil {
77+
t.Fatal(err)
78+
}
79+
gitIn(t, dir, "add", "NOTES.txt")
80+
out.Reset()
81+
if err := mergeRun(mprPath, "side", "", "", "", false, mx, &out); err == nil {
82+
t.Fatalf("step 2: want the exit-2 abort, got success\n%s", out.String())
83+
}
84+
planPath := mprPath + ".merge-plan.json"
85+
if _, err := os.Stat(planPath); err != nil {
86+
t.Fatalf("step 2: no plan template written: %v\n%s", err, out.String())
87+
}
88+
89+
// 3. --plan resolves the model and commits — with the text resolution.
90+
out.Reset()
91+
if err := mergeRun(mprPath, "side", "", "", planPath, false, mx, &out); err != nil {
92+
t.Fatalf("step 3: mergeRun --plan: %v\n%s", err, out.String())
93+
}
94+
if got := gitOutT(t, dir, "show", "HEAD:NOTES.txt"); got != "resolved by hand\n" {
95+
t.Errorf("the merge commit's NOTES.txt = %q, want the hand resolution", got)
96+
}
97+
os.Remove(planPath) // the template is not part of the merge
98+
assertPlanMergeCommitted(t, dir, mprPath)
99+
}
100+
101+
// With a text conflict open and no resolution to restore, --plan stops before
102+
// the model: the engine does not run, nothing is written, and the merge stays
103+
// in progress for the human to resolve and re-run.
104+
func TestGitMerge_PlanStopsOnAnOpenTextConflict(t *testing.T) {
105+
dir, mprPath := modelAndTextConflictRepo(t)
106+
doctorEnv(t, dir)
107+
plan := mergePlanTemplate{
108+
Base: strings.TrimSpace(gitOutT(t, dir, "merge-base", "HEAD", "side")),
109+
Ours: strings.TrimSpace(gitOutT(t, dir, "rev-parse", "HEAD")),
110+
Theirs: strings.TrimSpace(gitOutT(t, dir, "rev-parse", "side")),
111+
Units: []planUnit{{ID: tDM, Take: "ours"}},
112+
}
113+
raw, _ := json.Marshal(plan)
114+
planPath := filepath.Join(t.TempDir(), "plan.json")
115+
if err := os.WriteFile(planPath, raw, 0o644); err != nil {
116+
t.Fatal(err)
117+
}
118+
calls := 0
119+
mx := mxRunners{merge: func(base, mine, theirs string, shas [3]string) (int, string, error) {
120+
calls++
121+
return 0, "", nil
122+
}}
123+
var out bytes.Buffer
124+
err := mergeRun(mprPath, "side", "", "", planPath, false, mx, &out)
125+
if err == nil || !strings.Contains(out.String(), "NOTES.txt") {
126+
t.Fatalf("want the open text conflict reported, got %v\n%s", err, out.String())
127+
}
128+
if calls != 0 {
129+
t.Errorf("the engine ran %d time(s) with a text conflict open", calls)
130+
}
131+
if _, mhErr := gitInE(t, dir, "rev-parse", "--verify", "--quiet", "MERGE_HEAD"); mhErr != nil {
132+
t.Error("the merge was aborted; it must stay in progress for the human to resolve")
133+
}
134+
uu := gitOutT(t, dir, "diff", "--name-only", "--diff-filter=U")
135+
if !strings.Contains(uu, "NOTES.txt") || !strings.Contains(uu, ".mxunit") {
136+
t.Errorf("the conflicts must be left as they were, got:\n%s", uu)
137+
}
138+
gitInE(t, dir, "merge", "--abort")
139+
}

‎docs-site/src/tools/outside-studio-pro.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,9 @@ it is copied back, so the engine owns the `.mpr`:
285285
- a conflict in non-model files (text sources) is left in place for manual
286286
resolution, like plain git: resolve them, `git add` them, and re-run the
287287
command — it finishes the merge (model conflicts included) and commits.
288+
When that re-run needs a model decision and aborts, your text resolutions
289+
are recorded in the plan template and `--plan` puts them back; `--plan`
290+
stops before it touches the model while a text conflict is still open.
288291

289292
Merging is still never done by plain `git merge` on a Mendix repository: use
290293
`mxcli git merge` or Studio Pro, never a textual merge of `mprcontents/`.

0 commit comments

Comments
 (0)