Add a helper file to resolve merge conflicts

I'm not sure how to integrate this into our merging code.
This commit is contained in:
Vishesh Handa
2020-01-27 23:28:13 +01:00
parent 150543e065
commit ec959ebbed
2 changed files with 71 additions and 0 deletions

View File

@ -0,0 +1,36 @@
import 'dart:convert';
String resolveMergeConflict(String fileContents) {
var lines = const LineSplitter().convert(fileContents);
var newLines = <String>[];
var inMergeConflict = false;
var seenStartMarker = false;
var seenMiddleMarker = false;
lines.forEach((line) {
if (line.startsWith("<<<<<<<") && !inMergeConflict) {
inMergeConflict = true;
seenStartMarker = true;
return;
}
if (line == "=======" && inMergeConflict && seenStartMarker) {
seenMiddleMarker = true;
return;
}
if (line.startsWith(">>>>>>>") && inMergeConflict && seenMiddleMarker) {
inMergeConflict = false;
seenStartMarker = false;
seenMiddleMarker = false;
return;
}
if (inMergeConflict && seenMiddleMarker) {
return;
}
newLines.add(line);
});
return newLines.join('\n');
}

View File

@ -0,0 +1,35 @@
import 'package:test/test.dart';
import 'package:gitjournal/utils/merge_conflict_resolver.dart';
void main() {
test("Body only conflict", () {
String input = '''---
title: Foo
---
<<<<<<< HEAD
This is the body in GitJournal
=======
This is the body from the remote/origin
>>>>>>> remote/origin
Some more text.''';
String expectedOutput = '''---
title: Foo
---
This is the body in GitJournal
Some more text.''';
expect(resolveMergeConflict(input), equals(expectedOutput));
});
/*
test("YAML Conflict", () {});
test("YAML Conflict from 1st line", () {});
test("YAML Conflict different modified", () {});
test("YAML and body conflict", () {});
*/
}