GroupDocs.Comparison allows you to merge source code files by using the ComparisonActionproperty:
ComparisonAction.Accept accepts the found changes and adds them to the file without highlighting
ComparisonAction.Reject rejects the found changes and removes them from the output file
Steps to apply or reject changes
Instantiate the Comparer object. Specify the source document path or stream.
Call the add method. Specify the target document path or stream.
Call the compare method.
Call the get_changes) method to get the list of changes.
Set the comparison_action property of the appropriate change object to either ComparisonAction.ACCEPT or ComparisonAction.REJECT.
Call the apply_changes method. Pass the collection of changes to apply.
Example scenario
You need to compare and merge several versions of source code files and selectively accept or discard changes made by different authors.
Before comparison:
The differences show that two methods are in the source.cs file: AddNumbers and Sum.
If you do not use the comparison_action property, all changes are automatically applied to the output file, and these methods are removed. If you need to control the merging process, the comparison_action property helps you do that.
Python example: merge two source code files
importgroupdocs.comparisonasgcfromgroupdocs.comparison.resultimportComparisonActionfromgroupdocs.comparison.optionsimportApplyChangeOptionswithgc.Comparer("source.docx")ascomparer:comparer.add("target.docx")comparer.compare("result.docx")changes=comparer.get_changes()# Accept first 10 changesforiinrange(min(10,len(changes))):changes[i].comparison_action=ComparisonAction.ACCEPT# Reject the restforiinrange(10,len(changes)):changes[i].comparison_action=ComparisonAction.REJECToptions=ApplyChangeOptions()options.changes=changeswithopen("result.docx","wb")asresult_file:comparer.apply_changes(result_file,options)
Result
As a result, you get a merged source code file where:
Deleted elements are marked in red
Added elements are marked in blue
Modified elements are marked in green
Also, you receive an HTML file that highlights all the changed places in the code.
Result source code file
Result HTML file
As you can see from the resulting files, 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.