How to merge source code files
GroupDocs.Comparison for Java provides an ability to merge source code files by using the ComparisonAction properties:
- ComparisonAction.ACCEPT accepts the found changes and adds them to the file without highlighting;
- ComparisonAction.REJECT cancels found changes and removes them from the result file.
The following are the steps to apply/reject changes to resultant file.
- Instantiate Comparer object with source document path or stream;
- Call add method and specify path target document path or stream;
- Call compare method;
- Call getChanges method and obtain detected changes list;
- Set ComparisonAction of needed change object to ComparisonAction.ACCEPT or ComparisonAction.REJECT value;
- Call applyChanges method and pass collection of changes to it.
Example of merge source code file by using GroupDocs.Comparison
For example, you need to compare and merge several versions of source code files and you need to accept or discard changes made by different persons.
The differences show that two methods are written in the source.cs file: AddNumbers and Sum.
If you did not use ComparisonAction, then in the resulting file, all changes will be committed, and these methods will be removed, but if you need to control the merging of files, the ComparisonAction property will help you with this.
Example of using ComparisonAction
The following code samples demonstrate how to merge two source code files.
The result of merging files
As a result, we get a merged source code file where the deleted elements are marked in red, the added – in blue, and the modified – in green.
Also, you will receive a file in HTML format with 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.
More resources
GitHub Examples
You may easily run the code above and see the feature in action in our GitHub examples:
- GroupDocs.Comparison for Java examples, plugins, and showcase
- GroupDocs.Comparison for .NET examples, plugins, and showcase
- Document Comparison for Java App Dropwizard UI Modern Example
- Document Comparison for Java Spring UI Example
- Document Comparison for .NET MVC UI Example
- Document Comparison for .NET App WebForms UI Modern Example
Free Online App
Along with full-featured Java library we provide simple, but powerful free Apps. You are welcome to compare your DOC or DOCX, XLS or XLSX, PPT or PPTX, PDF, EML, EMLX, MSGand other documents with free to use online GroupDocs Comparison App.