With LibGit2Sharp, what is the best way to force specific processing for some files in the merge operation?

Viewed 112

We have an authoring tool that stores different kind of textual datas in a Git Repository.

For most of the files we use, the way LibGit2Sharp handles the merge of the files is OK. Basically, when the changes are simple enough, the merge is done automatically. Otherwise, we get a list of conflicts that we use to manage by calling a manual merge editor (KDiff3 by default).

But we have some specific files that are sometimes corrupted by the automatic processing. For instance, if we have a file with 100 lines, in branch A, the line 50 is moved to the 5th position, and in branch B this same line 50 is moved to line 95. In that case the automatic merge will remove the line 50 and add it both on position 5 and 95. And in our application it is not acceptable: here we have to ask the user which position is valid (when we can't figure it out automatically).

So what would be the best solution to handle a specific processing for that kind of file, either by forcing the usage of some manual merge tool, or do some specific automatic processing ?

Here is an extract of the code that handles Git merge with LibGit2Sharp:

CheckoutNotifyFlags flags =  CheckoutNotifyFlags.Dirty |
                             CheckoutNotifyFlags.Conflict |
                             CheckoutNotifyFlags.Updated;

MergeOptions mo = new MergeOptions() {
  CheckoutNotifyFlags = flags,
  OnCheckoutNotify = CheckNotifyHandler,
  FileConflictStrategy = CheckoutFileConflictStrategy.Diff3,
  OnCheckoutProgress = CheckoutProgressHandler,
  CommitOnSuccess = true,
};
var mergeResult = Repository.Merge(commonBranch, Signature, mo);

bool NoConflict = Repository.Index.Conflicts.Count() == 0;

if (!NoConflict)
{
  if (!CheckMergeConfig()) return false;
  var conflicts = Repository.Index.Conflicts;
  var indexParConflict = conflicts.FirstOrDefault(conf => IsIndexParConflict(conf));
  if (ProcessIndexParConflict(indexParConflict))
  {
    foreach (var conflict in conflicts)
    {
      if (!ProcessConflict(conflict))
      {
        Reset();  // Reset all changes                
        return false;
      }
    }
  }
}

We already have written a method that, provided the 3 input files used for the merge, says if the result file is valid or not.

If no better way to do it, we could analyse the result files at the end of the merge operation, and give a chance to the user to change it before we commit the merge results. But that would increase the overall complexity, and doesn't seem to be very clean way to do it.

Thanks for any input on that topic.

0 Answers
Related