mergiraf 0.19.0

A syntax-aware merge driver for Git
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
use assert_cmd::prelude::*;
use mergiraf::{EXIT_SOLVE_HAS_CONFLICTS, git, utils::write_string_to_file};
use std::fs;
use std::path::{Path, PathBuf};

mod common;
use common::{DEFAULT_FILE_FOR_SOLVE, create_file_for_solve, create_files_for_merge, merge, solve};

#[test]
fn keep_backup_keeps_backup() {
    let repo_dir = tempfile::tempdir().expect("failed to create the temp dir");
    let repo_path = repo_dir.path();

    let test_file_name = "test.c";

    let test_file_abs_path = repo_path.join(test_file_name);
    fs::write(&test_file_abs_path, "hello\nworld\n")
        .expect("failed to write test file to git repository");

    let test_file_orig_file_path = repo_path.join(format!("{test_file_name}.orig"));

    // `solve` without keeping backup
    solve()
        .arg("--keep-backup=false")
        .arg(&test_file_abs_path)
        .assert()
        .success();

    assert!(!test_file_orig_file_path.exists());

    // `solve` once again but with backup this time
    solve()
        .arg("--keep-backup=true")
        .arg(&test_file_abs_path)
        .assert()
        .success();

    assert!(test_file_orig_file_path.exists());
}

#[test]
fn manual_language_selection_for_solve() {
    let repo_dir = tempfile::tempdir().expect("failed to create the temp dir");
    let repo_path = repo_dir.path();

    let test_file_abs_path = create_file_for_solve(repo_path, DEFAULT_FILE_FOR_SOLVE);

    // first try without specifying a language -- should fail
    solve()
        // language not specified
        .arg(&test_file_abs_path)
        .assert()
        .failure();

    // then try with a language specified on the CLI -- should succeed
    solve()
        .arg("--language=json")
        .arg(&test_file_abs_path)
        .assert()
        .success();

    let merge_result =
        fs::read_to_string(test_file_abs_path).expect("couldn't read the merge result");
    assert_eq!(merge_result, "[0, 1, 2, 3, 4]\n");
}

#[test]
fn manual_language_selection_for_merge() {
    let repo_dir = tempfile::tempdir().expect("failed to create the temp dir");
    let repo_path = repo_dir.path();

    let (base_file_abs_path, left_file_abs_path, right_file_abs_path, _) =
        create_files_for_merge(repo_path, "[1, 2, 3]\n", "[1, 2, 3, 4]\n", "[0, 1, 2, 3]\n");

    // first try without specifying a language -- should fail
    merge()
        .arg(&base_file_abs_path)
        .arg(&left_file_abs_path)
        .arg(&right_file_abs_path)
        // language not specified
        .assert()
        .failure();

    // then try with a language specified on the CLI -- should succeed
    merge()
        .arg("--language=json")
        .arg(base_file_abs_path)
        .arg(left_file_abs_path)
        .arg(right_file_abs_path)
        .assert()
        .success()
        .stdout("[0, 1, 2, 3, 4]\n");
}

#[test]
fn debug_dir_is_created_for_solve() {
    let repo_dir = tempfile::tempdir().expect("failed to create the temp dir");
    let repo_path = repo_dir.path();

    let test_file_abs_path = create_file_for_solve(repo_path, DEFAULT_FILE_FOR_SOLVE);

    let debug_dir = tempfile::tempdir().unwrap();
    let debug_dir_path = debug_dir.path().to_path_buf();
    // hopefully no one creates a tmp file with the same exact name directly after we've
    // deleted our one
    debug_dir.close().unwrap();

    solve()
        .arg("--language=json")
        .arg(test_file_abs_path)
        .arg("--debug")
        .arg(&debug_dir_path)
        .assert()
        .success();

    assert!(fs::exists(debug_dir_path).unwrap());
}

#[test]
fn debug_dir_is_created_for_merge() {
    let repo_dir = tempfile::tempdir().expect("failed to create the temp dir");
    let repo_path = repo_dir.path();

    let (base_file_abs_path, left_file_abs_path, right_file_abs_path, _) =
        create_files_for_merge(repo_path, "[1, 2, 3]", "[1, 2, 3, 4]", "[0, 1, 2, 3]");

    let debug_dir = tempfile::tempdir().unwrap();
    let debug_dir_path = debug_dir.path().to_path_buf();
    // hopefully no one creates a tmp file with the same exact name directly after we've
    // deleted our one
    debug_dir.close().unwrap();

    merge()
        .arg("--language=json")
        .arg(base_file_abs_path)
        .arg(left_file_abs_path)
        .arg(right_file_abs_path)
        .arg("--debug")
        .arg(&debug_dir_path)
        .assert()
        .success();

    assert!(fs::exists(debug_dir_path).unwrap());
}

#[test]
fn line_ending_preservation_for_solve() {
    let repo_dir = tempfile::tempdir().expect("failed to create the temp dir");
    let repo_path = repo_dir.path();

    let test_file_abs_path = create_file_for_solve(
        repo_path,
        "<<<<<<< LEFT\r\n[1, 2, 3, 4]\r\n||||||| BASE\r\n[1, 2, 3]\r\n=======\r\n[0, 1, 2, 3]\r\n>>>>>>> RIGHT\r\n",
    );

    solve()
        .arg("--language=json")
        .arg(&test_file_abs_path)
        .assert()
        .success();

    let merge_result =
        fs::read_to_string(&test_file_abs_path).expect("couldn't read the merge result");
    assert_eq!(merge_result, "[0, 1, 2, 3, 4]\r\n");

    let backup_contents = fs::read_to_string(test_file_abs_path.with_extension("txt.orig"))
        .expect("couldn't read the backup file");
    assert!(backup_contents.contains("\r\n"));
}

#[test]
fn line_ending_preservation_for_merge() {
    let repo_dir = tempfile::tempdir().expect("failed to create the temp dir");
    let repo_path = repo_dir.path();

    let (base_file_abs_path, left_file_abs_path, right_file_abs_path, _) = create_files_for_merge(
        repo_path,
        "[1, 2, 3]\r\n",
        "[1, 2, 3, 4]\r\n",
        "[0, 1, 2, 3]\r\n",
    );

    merge()
        .arg("--language=json")
        .arg(base_file_abs_path)
        .arg(left_file_abs_path)
        .arg(right_file_abs_path)
        .assert()
        .success()
        .stdout("[0, 1, 2, 3, 4]\r\n");
}

fn create_iso8859_input_files(repo_path: &Path) -> (PathBuf, PathBuf, PathBuf, PathBuf) {
    create_files_for_merge(
        repo_path,
        b"\x68\xe9\x0a\x6c\xe0\x0a",
        b"\x68\xe9\x0a\x6c\xe0\x0a\x74\x6f\x69\x0a",
        b"\x79\x6f\x0a\x68\xe9\x0a\x6c\xe0\x0a",
    )
}

#[test]
fn merging_non_utf8_files_line_based_git_mode() {
    let repo_dir = tempfile::tempdir().expect("failed to create the temp dir");
    let repo_path = repo_dir.path();

    let (base_file_abs_path, left_file_abs_path, right_file_abs_path, _) =
        create_iso8859_input_files(repo_path);

    merge()
        .arg("--git")
        .arg("--language=json") // pretend those are JSON files so that we attempt to read them
        .arg(base_file_abs_path)
        .arg(&left_file_abs_path)
        .arg(right_file_abs_path)
        .assert()
        .success();

    let merge_result = fs::read(left_file_abs_path).expect("couldn't read the merge result");
    assert_eq!(
        merge_result,
        b"\x79\x6f\x0a\x68\xe9\x0a\x6c\xe0\x0a\x74\x6f\x69\x0a"
    );
}

#[test]
fn merging_non_utf8_files_line_based_with_output_file() {
    let repo_dir = tempfile::tempdir().expect("failed to create the temp dir");
    let repo_path = repo_dir.path();

    let (base_file_abs_path, left_file_abs_path, right_file_abs_path, output_file_abs_path) =
        create_iso8859_input_files(repo_path);

    // `mergiraf merge` should do line-based merges via git for files in ISO-8859-1
    merge()
        .arg("--language=json") // pretend those are JSON files so that we attempt to read them
        .arg(base_file_abs_path)
        .arg(left_file_abs_path)
        .arg(right_file_abs_path)
        .arg("--output")
        .arg(&output_file_abs_path)
        .assert()
        .success();

    let merge_result = fs::read(output_file_abs_path).expect("couldn't read the merge result");
    assert_eq!(
        merge_result,
        b"\x79\x6f\x0a\x68\xe9\x0a\x6c\xe0\x0a\x74\x6f\x69\x0a"
    );
}

#[test]
fn merging_non_existing_files() {
    let repo_dir = tempfile::tempdir().expect("failed to create the temp dir");
    let repo_path = repo_dir.path();

    let base_file_abs_path = repo_path.join("does_not_exist_1.json");
    let left_file_abs_path = repo_path.join("does_not_exist_2.json");
    let right_file_abs_path = repo_path.join("does_not_exist_3.json");

    merge()
        .arg("--git")
        .arg(base_file_abs_path)
        .arg(left_file_abs_path)
        .arg(right_file_abs_path)
        .assert()
        .append_context(
            "main",
            "`mergiraf merge` should return exit code 255 when passed non-existing files",
        )
        .code(255);
}

#[test]
fn merging_files_with_conflict_markers_cause_fallback_to_git_merge_file() {
    let contents_base = "\
/**
 * Doc comment
 */
class MyClass {
}";
    let contents_left = "\
/**
<<<<<<< HEAD
 * Doc comment
=======
 * Better docs
>>>>>>> origin/main
 */
class MyClass {
}";
    let contents_right = "\
/**
 * Doc comment
 */
class MyClass {
}

class OtherClass {
}";
    let contents_expected = "\
/**
<<<<<<< HEAD
 * Doc comment
=======
 * Better docs
>>>>>>> origin/main
 */
class MyClass {
}

class OtherClass {
}";

    let repo_dir = tempfile::tempdir().expect("failed to create the temp dir");
    let repo_path = repo_dir.path();

    let (base_file_abs_path, left_file_abs_path, right_file_abs_path, _) =
        create_files_for_merge(repo_path, contents_base, contents_left, contents_right);

    merge()
        .arg("--language=java")
        .arg(base_file_abs_path)
        .arg(left_file_abs_path)
        .arg(right_file_abs_path)
        .assert()
        .success()
        .stdout(contents_expected)
        .stderr("WARN left side contains conflict markers, falling back to Git\n");
}

#[test]
fn verify_cli_solve_has_conflicts() {
    let repo_dir = tempfile::tempdir().expect("failed to create the temp dir");
    let repo_path = repo_dir.path();

    let content = r#"
<html>
   <body>
       <foo
<<<<<<< LEFT
          iota="i"
||||||| BASE
=======
          iota="i2"
>>>>>>> RIGHT
          alpha="a"
          beta="b"
          gamma="c"
          delta="d"
       />
       <bar />
   </body>
</html>
         "#;
    let test_file_abs_path = create_file_for_solve(repo_path, content);

    solve()
        .arg(&test_file_abs_path)
        .arg("--language=html")
        .arg("--stdout")
        .assert()
        .code(EXIT_SOLVE_HAS_CONFLICTS)
        .stdout(content);
}

#[test]
fn solve_respects_conflict_marker_size_attr() {
    let repo_dir = tempfile::tempdir().expect("failed to create the temp dir");
    let repo_path = repo_dir.path();
    git::init(repo_path);

    let contents = "\
<<<<<<<<<< LEFT
[1, 2]
|||||||||| BASE
[1, 1]
==========
[2, 1]
>>>>>>>>>> RIGHT
";
    let contents_after_solve = "\
[2, 2]
";

    let conflict_path = create_file_for_solve(repo_path, contents);

    solve()
        .arg("--language=json")
        .arg(&conflict_path)
        .arg("--stdout")
        .current_dir(repo_path)
        .assert()
        .append_context(
            "main",
            "should fail to solve a conflict (and therefore leave it unchanged) with a non-standard conflict marker size",
        )
        // FIXME: this should arguably have been 1, as the conflict wasn't solved.
        // But to be fair to Mergiraf, it can't even see that this is a conflict,
        // precisely because of the non-standard conflict marker sizes
        .code(0)
        .stdout(contents);

    write_string_to_file(
        repo_path.join(".gitattributes"),
        "* conflict-marker-size=10",
    )
    .unwrap();

    solve()
        .arg("--language=json")
        .arg(conflict_path)
        .arg("--stdout")
        .current_dir(repo_path)
        .assert()
        .append_context(
            "main",
            "should be able to solve a conflict with a provided conflict marker size",
        )
        .code(0)
        .stdout(contents_after_solve);
}

#[test]
fn jj() {
    let repo_dir = tempfile::tempdir().expect("failed to create the temp dir");
    let repo_path = repo_dir.path();

    std::process::Command::new("jj")
        .args(["git", "init"])
        .arg(repo_path)
        .output()
        .expect("failed to initialize jj repo");

    // A file with an actual jj-style conflict
    let testfile = repo_path.join("testfile.json");
    let contents = r#"<<<<<<< conflict 1 of 1
%%%%%%% diff from: vpxusssl 38d49363 "merge base"
\\\\\\\        to: rtsqusxu 2768b0b9 "commit A"
 apple
-grape
+grapefruit
 orange
+++++++ ysrnknol 7a20f389 "commit B"
APPLE
GRAPE
ORANGE
>>>>>>> conflict 1 of 1 ends
"#;
    write_string_to_file(&testfile, contents).unwrap();

    // Should print a warning.
    solve()
        .arg(&testfile)
        .arg("--stdout")
        .assert()
        // The warning doesn't make Mergiraf fail
        // (The parse error _also_ doesn't make it fail -- Mergiraf can't parse the file and
        // thus decides it doesn't have any conflicts. This is arguably silly.)
        .success()
        .stderr("\
WARN You seem to be using Jujutsu instead of Git. Please use `jj resolve --tool mergiraf [file]`.
WARN Jujutsu has its own style of conflict markers, which Mergiraf doesn't understand.
WARN Jujutsu users shouldn't call `mergiraf solve` directly, because Jujutsu has a builtin configuration to resolve conflicts manually using `mergiraf merge`.
WARN Error while resolving conflicts: parse error at 0:0..0:16, starting with: `<<<<<<< conflict`
WARN Couldn't retrieve the original revisions from Git. This limits Mergiraf's ability to solve certain types of conflicts.
INFO 0 conflicts remaining.
");

    // This file has Git-style conflicts, and so we want to run `mergiraf solve` on it.
    let testfile2 = repo_path.join("testfile2.json");
    write_string_to_file(&testfile2, DEFAULT_FILE_FOR_SOLVE).unwrap();

    // Should recognize that no jj-style conflicts are present, and solve everything normally
    solve()
        .arg(&testfile2)
        .arg("--stdout")
        .assert()
        .success()
        .stderr(
            "\
INFO Solved all conflicts.
",
        );
}