Skip to content

Commit b442b90

Browse files
committed
cram: add location for conflict markers
Signed-off-by: Ali Caglayan <[email protected]>
1 parent d523bed commit b442b90

File tree

4 files changed

+128
-32
lines changed

4 files changed

+128
-32
lines changed

src/dune_rules/cram/cram_exec.ml

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,28 @@ let quote_for_sh fn =
4040
Buffer.contents buf
4141
;;
4242

43+
let map_loc_to_source_path loc =
44+
Loc.map_pos loc ~f:(fun (pos : Lexing.position) ->
45+
{ pos with
46+
pos_fname =
47+
(let path = Path.of_string pos.pos_fname in
48+
Path.drop_optional_build_context_maybe_sandboxed path |> Path.to_string)
49+
})
50+
;;
51+
4352
let cram_stanzas =
4453
let is_conflict_marker line =
4554
[ "======="; "%%%%%%%"; "+++++++"; "-------"; "|||||||" ]
4655
|> List.exists ~f:(fun prefix -> String.is_prefix line ~prefix)
4756
in
48-
let find_conflict state line =
57+
let find_conflict ~loc state line =
4958
match state with
50-
| `No_conflict when String.is_prefix ~prefix:"<<<<<<<" line -> `Started
51-
| `Started when is_conflict_marker line -> `Has_markers
52-
| `Has_markers when is_conflict_marker line -> `Has_markers
53-
| `Has_markers when String.is_prefix ~prefix:">>>>>>>" line ->
54-
(* CR-someday rgrinberg for alizter: insert a location spanning the
55-
entire once we start extracting it *)
59+
| `No_conflict when String.is_prefix ~prefix:"<<<<<<<" line -> `Started loc
60+
| `Started loc when is_conflict_marker line -> `Has_markers loc
61+
| `Has_markers loc when is_conflict_marker line -> `Has_markers loc
62+
| `Has_markers start_loc when String.is_prefix ~prefix:">>>>>>>" line ->
5663
User_error.raise
64+
~loc:(Loc.span start_loc loc)
5765
[ Pp.text
5866
"Conflict marker found. Please remove it or set (conflict_markers allow)"
5967
]
@@ -64,13 +72,14 @@ let cram_stanzas =
6472
match Cram_lexer.block lexbuf with
6573
| None -> List.rev acc
6674
| Some (loc, block) ->
75+
let loc = map_loc_to_source_path loc in
6776
let conflict_state =
6877
match block with
6978
| Command _ -> conflict_state
7079
| Comment lines ->
7180
(match conflict_markers with
7281
| Ignore -> conflict_state
73-
| Error -> List.fold_left lines ~init:conflict_state ~f:find_conflict)
82+
| Error -> List.fold_left lines ~init:conflict_state ~f:(find_conflict ~loc))
7483
in
7584
loop ((loc, block) :: acc) conflict_state
7685
in

test/blackbox-tests/test-cases/cram/conflict-markers-diff3.t

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,17 @@ Full diff3 conflict without command and output interleaving:
2222
> EOF
2323

2424
$ dune runtest test.t
25+
File "test.t", lines 1-8, characters 0-73:
26+
1 | <<<<<<<
27+
2 | Left side
28+
3 | |||||||
29+
4 | Original
30+
5 | =======
31+
6 | Right side
32+
7 | >>>>>>>
33+
8 | $ echo tada
2534
Error: Conflict marker found. Please remove it or set (conflict_markers
2635
allow)
27-
-> required by _build/default/.cram.test.t/cram.sh
28-
-> required by _build/default/.cram.test.t/cram.out
29-
-> required by alias test
3036
[1]
3137

3238
Full diff3 conflict with command and output interleaving:
@@ -43,11 +49,17 @@ Full diff3 conflict with command and output interleaving:
4349
> EOF
4450

4551
$ dune runtest test.t
52+
File "test.t", lines 1-8, characters 0-87:
53+
1 | <<<<<<<
54+
2 | $ foo left
55+
3 | |||||||
56+
4 | $ foo original
57+
5 | =======
58+
6 | > bar right
59+
7 | >>>>>>>
60+
8 | $ echo tada
4661
Error: Conflict marker found. Please remove it or set (conflict_markers
4762
allow)
48-
-> required by _build/default/.cram.test.t/cram.sh
49-
-> required by _build/default/.cram.test.t/cram.out
50-
-> required by alias test
5163
[1]
5264

5365
Partial diff3 conflicts are ignored:

test/blackbox-tests/test-cases/cram/conflict-markers-jj.t

Lines changed: 79 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,15 @@ Full jujutsu conflict without command and output interleaving:
2020
> EOF
2121

2222
$ dune runtest test.t
23+
File "test.t", lines 1-6, characters 0-89:
24+
1 | <<<<<<< Conflict 1 of 2
25+
2 | A Small
26+
3 | %%%%%%%
27+
4 | Conflict
28+
5 | >>>>>>> Conflict 1 of 2 ends
29+
6 | $ echo tada
2330
Error: Conflict marker found. Please remove it or set (conflict_markers
2431
allow)
25-
-> required by _build/default/.cram.test.t/cram.sh
26-
-> required by _build/default/.cram.test.t/cram.out
27-
-> required by alias test
2832
[1]
2933

3034
Full jujutsu conflict with command and output interleaving:
@@ -39,11 +43,15 @@ Full jujutsu conflict with command and output interleaving:
3943
> EOF
4044

4145
$ dune runtest test.t
46+
File "test.t", lines 1-6, characters 0-88:
47+
1 | <<<<<<< Conflict 2 of 2
48+
2 | $ foo
49+
3 | %%%%%%%
50+
4 | > bar
51+
5 | >>>>>>> Conflict 2 of 2 ends
52+
6 | $ echo tada
4253
Error: Conflict marker found. Please remove it or set (conflict_markers
4354
allow)
44-
-> required by _build/default/.cram.test.t/cram.sh
45-
-> required by _build/default/.cram.test.t/cram.out
46-
-> required by alias test
4755
[1]
4856

4957
Jujutsu default style conflict (diff + snapshot):
@@ -61,11 +69,18 @@ Jujutsu default style conflict (diff + snapshot):
6169
> EOF
6270

6371
$ dune runtest test.t
72+
File "test.t", lines 1-9, characters 0-160:
73+
1 | <<<<<<< Conflict 1 of 1
74+
2 | %%%%%%% Changes from base to side #1
75+
3 | -apple
76+
4 | +grapefruit
77+
5 | +++++++ Contents of side #2
78+
6 | APPLE
79+
7 | GRAPE
80+
8 | >>>>>>> Conflict 1 of 1 ends
81+
9 | $ echo tada
6482
Error: Conflict marker found. Please remove it or set (conflict_markers
6583
allow)
66-
-> required by _build/default/.cram.test.t/cram.sh
67-
-> required by _build/default/.cram.test.t/cram.out
68-
-> required by alias test
6984
[1]
7085

7186
Jujutsu snapshot style conflict:
@@ -83,11 +98,18 @@ Jujutsu snapshot style conflict:
8398
> EOF
8499

85100
$ dune runtest test.t
101+
File "test.t", lines 1-9, characters 0-175:
102+
1 | <<<<<<< Conflict 1 of 1
103+
2 | +++++++ Contents of side #1
104+
3 | Left side
105+
4 | ------- Contents of base
106+
5 | Original
107+
6 | +++++++ Contents of side #2
108+
7 | Right side
109+
8 | >>>>>>> Conflict 1 of 1 ends
110+
9 | $ echo tada
86111
Error: Conflict marker found. Please remove it or set (conflict_markers
87112
allow)
88-
-> required by _build/default/.cram.test.t/cram.sh
89-
-> required by _build/default/.cram.test.t/cram.out
90-
-> required by alias test
91113
[1]
92114

93115
Partial jujutsu conflicts are ignored:
@@ -117,3 +139,48 @@ Partial jujutsu conflicts are ignored:
117139
Error: Files _build/default/test.t and _build/default/test.t.corrected
118140
differ.
119141
[1]
142+
<<<<<<< Conflict 1 of 1
143+
+++++++ Contents of side #1
144+
%%%%%%% Changes from base to side #2
145+
146+
Real jujutsu conflict with command output containing conflict markers:
147+
148+
$ cat >test.t <<EOF
149+
> <<<<<<< Conflict 1 of 4
150+
> %%%%%%% Changes from base to side #1
151+
> - Error: Conflict found. Please remove it or set (conflict allow)
152+
> + Error: Conflict marker found. Please remove it or set (conflict_markers
153+
> + allow)
154+
> -> required by _build/default/.cram.test.t/cram.sh
155+
> -> required by _build/default/.cram.test.t/cram.out
156+
> -> required by alias test
157+
> +++++++ Contents of side #2
158+
> File "_build/default/test.t", lines 1-6, characters 0-52:
159+
> 1 | <<<<<<<
160+
> 2 | A Small
161+
> 3 | %%%%%%%
162+
> 4 | Conflict
163+
> 5 | >>>>>>>
164+
> 6 | $ echo tada
165+
> Error: Conflict found. Please remove it or set (conflict allow)
166+
> >>>>>>> Conflict 1 of 4 ends
167+
> [1]
168+
> EOF
169+
170+
$ dune runtest test.t
171+
+ File "test.t", lines 1-18, characters 0-622:
172+
+ 1 | <<<<<<< Conflict 1 of 4
173+
+ 2 | %%%%%%% Changes from base to side #1
174+
+ 3 | - Error: Conflict found. Please remove it or set (conflict allow)
175+
+ ....
176+
+ 16 | 6 | $ echo tada
177+
+ 17 | Error: Conflict found. Please remove it or set (conflict allow)
178+
+ 18 | >>>>>>> Conflict 1 of 4 ends
179+
Error: Conflict marker found. Please remove it or set (conflict_markers
180+
allow)
181+
- -> required by _build/default/.cram.test.t/cram.sh
182+
- -> required by _build/default/.cram.test.t/cram.out
183+
- -> required by alias test
184+
[1]
185+
186+
>>>>>>> Conflict 1 of 1 ends

test/blackbox-tests/test-cases/cram/conflict-markers.t

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,15 @@ Full conflict without command and output interleaving:
2020
> EOF
2121

2222
$ dune runtest test.t
23+
File "test.t", lines 1-6, characters 0-52:
24+
1 | <<<<<<<
25+
2 | A Small
26+
3 | =======
27+
4 | Conflict
28+
5 | >>>>>>>
29+
6 | $ echo tada
2330
Error: Conflict marker found. Please remove it or set (conflict_markers
2431
allow)
25-
-> required by _build/default/.cram.test.t/cram.sh
26-
-> required by _build/default/.cram.test.t/cram.out
27-
-> required by alias test
2832
[1]
2933

3034
Full conflict with command and output interleaving:
@@ -39,11 +43,15 @@ Full conflict with command and output interleaving:
3943
> EOF
4044

4145
$ dune runtest test.t
46+
File "test.t", lines 1-6, characters 0-51:
47+
1 | <<<<<<<
48+
2 | $ foo
49+
3 | =======
50+
4 | > bar
51+
5 | >>>>>>>
52+
6 | $ echo tada
4253
Error: Conflict marker found. Please remove it or set (conflict_markers
4354
allow)
44-
-> required by _build/default/.cram.test.t/cram.sh
45-
-> required by _build/default/.cram.test.t/cram.out
46-
-> required by alias test
4755
[1]
4856

4957
Partial conflicts are ignored:

0 commit comments

Comments
 (0)