GroupDocs.Comparison can merge source code files by exposing each detected change as a ChangeInfo and letting you set comparison_action to ComparisonAction.ACCEPT (apply without highlighting) or ComparisonAction.REJECT (discard from the result).
Steps to apply or reject changes
Instantiate Comparer with the source document path or stream.
Call add() with the target document.
Call compare() to run the comparison.
Call get_changes() to retrieve the change collection.
Set comparison_action on each change to ComparisonAction.ACCEPT or ComparisonAction.REJECT.
Call apply_changes() with ApplyChangeOptions(changes=...) to produce the merged file.
Example scenario
You need to compare several versions of source code files and selectively accept or discard changes made by different authors.
Before comparison:
The differences show two methods in source.cs: AddNumbers and Sum. Without using comparison_action, all changes are auto-applied to the output and both methods are removed. With comparison_action you can choose which method survives.
Example: Merge two source code files
fromgroupdocs.comparisonimportComparerfromgroupdocs.comparison.optionsimportApplyChangeOptionsfromgroupdocs.comparison.resultimportComparisonActiondefmerge_source_code_files():withComparer("./source.cs")ascomparer:comparer.add("./target.cs")comparer.compare("./result.cs")changes=comparer.get_changes()# Accept the first 10 changes; reject the restfori,changeinenumerate(changes):change.comparison_action=ComparisonAction.ACCEPTifi<10elseComparisonAction.REJECTwithopen("./result.cs","wb")asresult_file:comparer.apply_changes(result_file,ApplyChangeOptions(changes=changes))if__name__=="__main__":merge_source_code_files()
source.cs is the source file used in this example. Click here to download it.
target.cs is the target file used in this example. Click here to download it.
The merged file marks deleted elements in red, added elements in blue, and modified elements in green. An accompanying HTML file highlights every changed code region.
Result source code file
Result HTML file
As shown, only one of the two methods was removed.
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.
On this page
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.