Created
August 28, 2017 15:09
-
-
Save myarichuk/e0ce76c14c8609e2b49b54ab34822148 to your computer and use it in GitHub Desktop.
GetConflictStatus
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static ConflictStatus GetConflictStatus(string remoteAsString, string localAsString) | |
{ | |
if (remoteAsString == localAsString) | |
return ConflictStatus.AlreadyMerged; | |
if(string.IsNullOrEmpty(remoteAsString)) | |
return ConflictStatus.AlreadyMerged; | |
if (string.IsNullOrEmpty(remoteAsString) || string.IsNullOrEmpty(localAsString)) | |
return ConflictStatus.Update; | |
var local = localAsString.ToChangeVector(); | |
var remote = remoteAsString.ToChangeVector(); | |
//any missing entries from a change vector are assumed to have zero value | |
var localHasLargerEntries = false; | |
var remoteHasLargerEntries = false; | |
int numOfMatches = 0; | |
for (int i = 0; i < remote.Length; i++) | |
{ | |
bool found = false; | |
for (int j = 0; j < local.Length; j++) | |
{ | |
if (remote[i].DbId == local[j].DbId) | |
{ | |
found = true; | |
numOfMatches++; | |
if (remote[i].Etag > local[j].Etag) | |
{ | |
remoteHasLargerEntries = true; | |
} | |
else if (remote[i].Etag < local[j].Etag) | |
{ | |
localHasLargerEntries = true; | |
} | |
break; | |
} | |
} | |
if (found == false) | |
{ | |
remoteHasLargerEntries = true; | |
} | |
} | |
if (numOfMatches < local.Length) | |
{ | |
localHasLargerEntries = true; | |
} | |
if (remoteHasLargerEntries && localHasLargerEntries) | |
return ConflictStatus.Conflict; | |
if (remoteHasLargerEntries == false && localHasLargerEntries == false) | |
return ConflictStatus.AlreadyMerged; // change vectors identical | |
return remoteHasLargerEntries ? ConflictStatus.Update : ConflictStatus.AlreadyMerged; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment