ast-merge-git 0.3.0

Git merge contracts for Structured Merge
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
use std::collections::HashMap;

use ast_merge::{
    ConflictAlternativeState, Diagnostic, DiagnosticCategory, DiagnosticSeverity, MergeConflict,
    SourceRevision, ThreeWayMergeOutcome, localized_conflict_render_plan, render_source_plan,
};
use json_merge::{JsonDialect, merge_json_three_way};
use serde::{Deserialize, Serialize};

pub mod conflict_review;
pub mod typed;

pub const PACKAGE_NAME: &str = "ast-merge-git";

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct Merge3Request {
    pub base_source: String,
    pub ours_source: String,
    pub theirs_source: String,
    pub path_name: Option<String>,
    pub language: Option<String>,
    pub dialect: Option<String>,
    pub profile_id: Option<String>,
    pub fallback_policy: Option<String>,
    pub conflict_marker_size: Option<usize>,
    pub render_policy: Option<String>,
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct Merge3Conflict {
    pub conflict_id: String,
    pub category: String,
    pub path: String,
    pub message: String,
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct ChangeClassification {
    pub path: String,
    pub ours: String,
    pub theirs: String,
}

#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct Merge3Response {
    pub ok: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub merged_source: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub conflicted_source: Option<String>,
    pub conflicts: Vec<Merge3Conflict>,
    pub change_classifications: Vec<ChangeClassification>,
    pub diagnostics: Vec<Diagnostic>,
    pub fallbacks: Vec<String>,
    pub profile: Merge3Profile,
    pub render_report: Merge3RenderReport,
    pub owned_regions: Vec<OwnedRegionReport>,
    pub formatting_preservation: FormattingPreservation,
    pub secondary_formatting_metrics: SecondaryFormattingMetrics,
    pub default_driver_evaluation: DefaultDriverEvaluation,
    pub reparse_after_render: Option<bool>,
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct CommentDeltaResult {
    pub ok: bool,
    pub merged_comment: Option<String>,
    pub conflicts: Vec<Merge3Conflict>,
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct Merge3Profile {
    pub profile_id: String,
    pub language: String,
    pub dialect: String,
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct Merge3RenderReport {
    pub strategy: String,
    #[serde(default)]
    pub backend_id: String,
    #[serde(default)]
    pub parser_identity: String,
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct SourceRange {
    pub start: usize,
    pub end: usize,
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct AttachedSpan {
    pub kind: String,
    pub line_range: SourceRange,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub byte_range: Option<SourceRange>,
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct OwnedRegionReport {
    pub owner_path: String,
    pub node_id: String,
    pub region_kind: String,
    pub byte_range: SourceRange,
    pub line_range: SourceRange,
    pub attached_spans: Vec<AttachedSpan>,
    pub backend_id: String,
    pub parser_identity: String,
    pub can_replace: bool,
    pub can_line_merge: bool,
    pub requires_reparse: bool,
}

#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct FormattingPreservation {
    pub line_diff_score: f64,
    pub character_diff_score: f64,
}

#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct SecondaryFormattingMetrics {
    pub unchanged_line_churn: usize,
    pub output_diff_size: usize,
    pub source_fragment_retention: f64,
    pub weighted: bool,
    pub diagnostics: Vec<String>,
}

#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct DefaultDriverEvaluation {
    pub status: String,
    pub formatting_threshold: f64,
    pub formatting_score: f64,
    pub hard_gates: Vec<HardGate>,
    pub blocking_reasons: Vec<String>,
    pub diagnostics: Vec<String>,
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct HardGate {
    pub name: String,
    pub passed: bool,
    pub weighted: bool,
}

pub fn merge3(request: &Merge3Request) -> Merge3Response {
    match normalize_language(request).as_str() {
        "json" | "jsonc" | "json5" => merge3_json(request),
        _ => response(
            request,
            false,
            None,
            None,
            vec![],
            vec![],
            vec![diagnostic(
                DiagnosticCategory::UnsupportedFeature,
                "ast-merge-git currently supports only json merge3.",
            )],
            vec![],
            None,
            None,
            None,
        ),
    }
}

pub fn merge3_json(request: &Merge3Request) -> Merge3Response {
    let mut result = merge_json_three_way(
        &request.base_source,
        &request.ours_source,
        &request.theirs_source,
        json_dialect(request),
    );
    let conflicts = result
        .conflicts
        .iter()
        .map(|conflict| Merge3Conflict {
            conflict_id: conflict.conflict_id.clone(),
            category: conflict.category.clone(),
            path: conflict.path.clone(),
            message: conflict.message.clone(),
        })
        .collect::<Vec<_>>();
    match result.outcome {
        ThreeWayMergeOutcome::Clean => response(
            request,
            true,
            result.output,
            None,
            conflicts,
            vec![],
            result.diagnostics,
            vec![],
            Some(FormattingPreservation { line_diff_score: 1.0, character_diff_score: 1.0 }),
            Some(true),
            Some("source_preserving_edits".to_string()),
        ),
        ThreeWayMergeOutcome::Conflict => {
            let owned_regions = json_owned_region_reports(&result.conflicts);
            let rendered = render_json_conflicts(request, &result.conflicts);
            if let Err(message) = &rendered {
                result.diagnostics.push(Diagnostic {
                    severity: DiagnosticSeverity::Warning,
                    category: DiagnosticCategory::UnsupportedFeature,
                    message: format!("localized conflict render unavailable: {message}"),
                    path: None,
                    review: None,
                });
            }
            let (conflicted_source, strategy, reparse) = match rendered {
                Ok(source) => {
                    let appended = result.conflicts.iter().any(|conflict| {
                        conflict.alternatives.iter().any(|alternative| {
                            alternative.revision == SourceRevision::Ours
                                && alternative.state == ConflictAlternativeState::Absent
                        })
                    });
                    (
                        Some(source),
                        if appended {
                            "absent_owner_review_markers"
                        } else {
                            "owned_region_conflict_markers"
                        }
                        .to_string(),
                        Some(false),
                    )
                }
                Err(_) => (None, "unrendered_structural_conflict".to_string(), None),
            };
            response(
                request,
                false,
                None,
                conflicted_source,
                conflicts,
                vec![],
                result.diagnostics,
                owned_regions,
                None,
                reparse,
                Some(strategy),
            )
        }
        ThreeWayMergeOutcome::Error => response(
            request,
            false,
            None,
            None,
            conflicts,
            vec![],
            result.diagnostics,
            vec![],
            None,
            None,
            Some("not_rendered".to_string()),
        ),
    }
}

fn render_json_conflicts(
    request: &Merge3Request,
    conflicts: &[MergeConflict],
) -> Result<String, String> {
    let sources = HashMap::from([
        (SourceRevision::Base, request.base_source.clone()),
        (SourceRevision::Ours, request.ours_source.clone()),
        (SourceRevision::Theirs, request.theirs_source.clone()),
    ]);
    let plan = localized_conflict_render_plan(
        sources,
        conflicts,
        request.conflict_marker_size.unwrap_or(7),
    )
    .map_err(|error| error.to_string())?;
    render_source_plan(&plan).map(|result| result.content).map_err(|error| error.to_string())
}

fn json_owned_region_reports(conflicts: &[MergeConflict]) -> Vec<OwnedRegionReport> {
    conflicts
        .iter()
        .filter_map(|conflict| {
            let alternative = conflict.alternatives.iter().find(|alternative| {
                alternative.revision == SourceRevision::Ours
                    && alternative.state == ConflictAlternativeState::Present
                    && alternative.regions.len() == 1
            })?;
            let region = &alternative.regions[0];
            Some(OwnedRegionReport {
                owner_path: conflict.path.clone(),
                node_id: region.node_id.clone(),
                region_kind: region.region_kind.clone(),
                byte_range: SourceRange { start: region.start_byte, end: region.end_byte },
                line_range: SourceRange { start: region.start_line, end: region.end_line },
                attached_spans: vec![],
                backend_id: "tree-sitter-language-pack".to_string(),
                parser_identity: "tree-haver".to_string(),
                can_replace: true,
                can_line_merge: false,
                requires_reparse: true,
            })
        })
        .collect()
}

pub fn merge_comment_delta(
    base_comment: Option<&str>,
    ours_comment: Option<&str>,
    theirs_comment: Option<&str>,
    owner_path: &str,
) -> CommentDeltaResult {
    let mut conflicts = Vec::new();
    let merged_comment = if ours_comment == theirs_comment {
        ours_comment.map(str::to_string)
    } else if base_comment == ours_comment {
        theirs_comment.map(str::to_string)
    } else if base_comment == theirs_comment {
        ours_comment.map(str::to_string)
    } else if ours_comment.is_none() {
        conflicts.push(comment_conflict(
            "delete_edit",
            owner_path,
            "ours deleted a comment that theirs edited",
        ));
        None
    } else if theirs_comment.is_none() {
        conflicts.push(comment_conflict(
            "delete_edit",
            owner_path,
            "theirs deleted a comment that ours edited",
        ));
        None
    } else {
        conflicts.push(comment_conflict(
            "edit_edit",
            owner_path,
            "comment changed differently in ours and theirs",
        ));
        None
    };

    CommentDeltaResult { ok: conflicts.is_empty(), merged_comment, conflicts }
}

#[allow(clippy::too_many_arguments)]
fn response(
    request: &Merge3Request,
    ok: bool,
    merged_source: Option<String>,
    conflicted_source: Option<String>,
    conflicts: Vec<Merge3Conflict>,
    change_classifications: Vec<ChangeClassification>,
    diagnostics: Vec<Diagnostic>,
    owned_regions: Vec<OwnedRegionReport>,
    formatting_preservation: Option<FormattingPreservation>,
    reparse_after_render: Option<bool>,
    render_strategy: Option<String>,
) -> Merge3Response {
    let has_merged_source = merged_source.is_some();
    let formatting = formatting_preservation
        .clone()
        .unwrap_or(FormattingPreservation { line_diff_score: 0.0, character_diff_score: 0.0 });
    let render_report = render_report(request, render_strategy);
    Merge3Response {
        ok,
        merged_source,
        conflicted_source,
        conflicts,
        change_classifications,
        diagnostics,
        fallbacks: vec![],
        profile: Merge3Profile {
            profile_id: request.profile_id.clone().unwrap_or_default(),
            language: normalize_language(request),
            dialect: request.dialect.clone().unwrap_or_default(),
        },
        render_report: render_report.clone(),
        owned_regions,
        formatting_preservation: formatting.clone(),
        secondary_formatting_metrics: secondary_formatting_metrics(has_merged_source),
        default_driver_evaluation: default_driver_evaluation(
            &formatting,
            reparse_after_render,
            &render_report,
        ),
        reparse_after_render,
    }
}

fn secondary_formatting_metrics(merged: bool) -> SecondaryFormattingMetrics {
    if merged {
        return SecondaryFormattingMetrics {
            unchanged_line_churn: 0,
            output_diff_size: 0,
            source_fragment_retention: 1.0,
            weighted: false,
            diagnostics: vec!["json-merge rendered source-preserving edits".to_string()],
        };
    }
    SecondaryFormattingMetrics {
        unchanged_line_churn: 0,
        output_diff_size: 0,
        source_fragment_retention: 0.0,
        weighted: false,
        diagnostics: vec![
            "unresolved conflict did not produce a merged source-fragment retention measurement"
                .to_string(),
        ],
    }
}

fn default_driver_evaluation(
    formatting: &FormattingPreservation,
    reparse_after_render: Option<bool>,
    render_report: &Merge3RenderReport,
) -> DefaultDriverEvaluation {
    let threshold = 0.95;
    let score = (formatting.line_diff_score + formatting.character_diff_score) / 2.0;
    let reparse_passed = reparse_after_render == Some(true);
    let no_full_file_rewrite = render_report.strategy != "full_file_conflict_markers";
    let coherent_conflict_markers = render_report.strategy != "full_file_conflict_markers";
    let mut blocking_reasons = Vec::new();
    if !reparse_passed {
        blocking_reasons.push("rendered output did not reparse".to_string());
    }
    if score < threshold {
        blocking_reasons.push("formatting score is below threshold".to_string());
    }
    if !no_full_file_rewrite {
        blocking_reasons.push("full-file rewrite or conflict markers were used".to_string());
    }
    if !coherent_conflict_markers {
        blocking_reasons
            .push("conflict marker placement is not syntactically coherent".to_string());
    }
    DefaultDriverEvaluation {
        status: if blocking_reasons.is_empty() {
            "recommended".to_string()
        } else {
            "not_recommended".to_string()
        },
        formatting_threshold: threshold,
        formatting_score: score,
        hard_gates: vec![
            HardGate {
                name: "reparse_after_render".to_string(),
                passed: reparse_passed,
                weighted: false,
            },
            HardGate {
                name: "no_full_file_rewrite".to_string(),
                passed: no_full_file_rewrite,
                weighted: false,
            },
            HardGate {
                name: "coherent_conflict_marker_placement".to_string(),
                passed: coherent_conflict_markers,
                weighted: false,
            },
        ],
        blocking_reasons,
        diagnostics: vec![
            "default-driver evaluation is advisory unless explicitly required".to_string(),
        ],
    }
}

fn render_report(request: &Merge3Request, render_strategy: Option<String>) -> Merge3RenderReport {
    let strategy = render_strategy
        .or_else(|| request.render_policy.clone())
        .unwrap_or_else(|| "canonical".to_string());
    let (backend_id, parser_identity) = match normalize_language(request).as_str() {
        "json" | "jsonc" | "json5" => {
            ("tree-sitter-language-pack".to_string(), "tree-haver".to_string())
        }
        _ => (String::new(), String::new()),
    };
    Merge3RenderReport { strategy, backend_id, parser_identity }
}

fn diagnostic(category: DiagnosticCategory, message: &str) -> Diagnostic {
    Diagnostic {
        severity: DiagnosticSeverity::Error,
        category,
        message: message.to_string(),
        path: None,
        review: None,
    }
}

fn comment_conflict(category: &str, path: &str, message: &str) -> Merge3Conflict {
    Merge3Conflict {
        conflict_id: "comment-conflict-1".to_string(),
        category: category.to_string(),
        path: if path.is_empty() { "/".to_string() } else { path.to_string() },
        message: message.to_string(),
    }
}

fn normalize_language(request: &Merge3Request) -> String {
    let language = request.language.as_deref().unwrap_or_default().trim().to_ascii_lowercase();
    if matches!(language.as_str(), "json" | "jsonc" | "json5") {
        return language;
    }
    let path = request.path_name.as_deref().unwrap_or_default().to_ascii_lowercase();
    for dialect in ["json", "jsonc", "json5"] {
        if path.ends_with(&format!(".{dialect}")) {
            return dialect.to_string();
        }
    }
    language
}

fn json_dialect(request: &Merge3Request) -> JsonDialect {
    let dialect = request.dialect.clone().unwrap_or_else(|| normalize_language(request));
    match dialect.as_str() {
        "jsonc" => JsonDialect::Jsonc,
        "json5" => JsonDialect::Json5,
        _ => JsonDialect::Json,
    }
}