Java file merge tool

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

AST-based structured merge tool for Java, fully Git compatible

License

KTH/spork

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

Spork — AST based merging for Java files

Spork is an AST based structured merge tool for Java. This means that instead of merging Java files by their raw text, it resolves the abstract syntax trees and merges based on them instead.

If you use Spork in an academic context, please cite the related research paper:

S. Larsen, J. -R. Falleri, B. Baudry and M. Monperrus, «Spork: Structured Merge for Java with Formatting Preservation,» in IEEE Transactions on Software Engineering, doi: 10.1109/TSE.2022.3143766.

You may export a citation in various formats using the Cite this repository button to the right. See the GitHub docs for more info.

Spork was created as part of a master’s thesis project. If you want to learn more about Spork in terms of theory and design, the thesis is freely available.

Spork is built on top of a few pieces of fantastic software, most notably:

  • Spoon provides the AST representation used in Spork.
  • GumTree provides the tree matching.
  • gumtree-spoon-ast-diff bridges the gap between Spoon and GumTree .

The merge implementation in Spork is based on the 3DM merge algorithm by Tancred Lindholm.

Want to just try out Spork on a small merge scenario? Below are a few shell commands that will download Spork along with a sample merge scenario, and then run it!

# Download Spork wget https://github.com/KTH/spork/releases/download/v0.5.0/spork-0.5.0.jar -O spork.jar # Download a sample merge scenario wget https://raw.githubusercontent.com/KTH/spork/fe906f537d1bb7205256d1fe81fda9f323849a60/src/test/resources/clean/both_modified/move_if/Left.java wget https://raw.githubusercontent.com/KTH/spork/fe906f537d1bb7205256d1fe81fda9f323849a60/src/test/resources/clean/both_modified/move_if/Base.java wget https://raw.githubusercontent.com/KTH/spork/fe906f537d1bb7205256d1fe81fda9f323849a60/src/test/resources/clean/both_modified/move_if/Right.java # You should now have spork.jar, Left.java, Base.java and Right.java in your local directory # a line based-merge is not possible diff3 Left.java Base.java Right.java -m -A # an AST-merge with Spork does java -jar spork.jar Left.java Base.java Right.java

It should print the result of the merge to stdout. See Base.java for the original version, and Left.java and Right.java for the respective changes. They should all be neatly integrated into the resulting merge. For more on using Spork, see Usage.

You can find a pre-built jar-file under relases. The jar-file includes all dependencies, so all you need is a Java runtime. Download the jar and run it like so:

Important: Spork requires a Java runtime version 8 or higher to run.

The left , base and right arguments are required, and represent the left, base and right revisions, respectively. The base revision should be the best common ancestor of the left and right revisions.

For a full listing of the command line options, supply the -h option. It will produce the following output.

Usage: spork [-eghlV] [-o=] LEFT BASE RIGHT The Spork command line app. LEFT Path to the left revision. BASE Path to the base revision. RIGHT Path to the right revision. -e, --exit-on-error Disable line-based fallback if the structured merge encounters an error. -g, --git-mode Enable Git compatibility mode. Required to use Spork as a Git merge driver. -h, --help Show this help message and exit. -l, --logging Enable logging output. -o, --output= Path to the output file. Existing files are overwritten. -V, --version Print version information and exit. 

Naturally, if you want the absolute latest version, you will have to build Spork yourself.

Maven can be used to build the latest version of Spork.

Note: Requires JDK8+ to build.

mvn clean compile package -DskipTests 

This will produce a jar-file in the target directory called something along the lines of spork-x.x.x.jar . Run the jar with java -jar path/to/spork/jar .

Configure as a Git merge driver

When Git performs a merge and encounters a file that has been edited in both revisions under merge, it will invoke a merge driver to merge the conflicting versions. It’s a very simple thing to configure Spork as a merge driver for Java files, all you need is to add a couple of lines to a couple of configuration files. First, let’s create a .gitattributes file and specify to use Spork as a merge driver for Java files. Put the following content in your .gitattributes file (you may all ready have one, check your home directory):

spork doesn’t mean anything to Git yet, we need to actually define the merge driver called spork . We do that in the .gitattributes file, typically located in your home directory. You should put the following content into it:

[core] attributesfile = /path/to/.gitattributes [merge "spork"] name = spork driver = java -jar /path/to/spork.jar merge --git-mode %A %O %B -o %A 

Then replace /path/to/.gitattributes with the absolute path to the .gitattributes file you edited/created first, and replace /path/to/spork.jar with the absolute path to the Spork jar-file. With that done, Spork will be used as the merge driver for Java files!

Important: The —git-mode option is required to use Spork as a Git merge driver. If you find that Spork always reverts to line-based merge, then that option is probably missing in the driver option that invokes Spork.

Unless otherwise stated, files in Spork are under the MIT license.

  • The GumTreeSpoonAstDiff file is composed of code from gumtree-spoon-ast-diff and is therefore individually licensed under Apache License 2.0.

About

AST-based structured merge tool for Java, fully Git compatible

Источник

Merge Files in Java

I often need to merge multiple files into one in Java. So I write a reusable method to do this job. It works very well for me to merge a set of txt files.

The method accepts an array of File and the merged file path. After running the method, the set of files to be merged will be merged into the specified file.

package com.programcreek; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; public class MergerFiles { public static void main(String[] args) { String sourceFile1Path = "/home/programcreek/Desktop/s1"; String sourceFile2Path = "/home/programcreek/Desktop/s2"; String mergedFilePath = "/home/programcreek/Desktop/m"; File[] files = new File[2]; files[0] = new File(sourceFile1Path); files[1] = new File(sourceFile2Path); File mergedFile = new File(mergedFilePath); mergeFiles(files, mergedFile); } public static void mergeFiles(File[] files, File mergedFile) { FileWriter fstream = null; BufferedWriter out = null; try { fstream = new FileWriter(mergedFile, true); out = new BufferedWriter(fstream); } catch (IOException e1) { e1.printStackTrace(); } for (File f : files) { System.out.println("merging: " + f.getName()); FileInputStream fis; try { fis = new FileInputStream(f); BufferedReader in = new BufferedReader(new InputStreamReader(fis)); String aLine; while ((aLine = in.readLine()) != null) { out.write(aLine); out.newLine(); } in.close(); } catch (IOException e) { e.printStackTrace(); } } try { out.close(); } catch (IOException e) { e.printStackTrace(); } } }

If you want someone to read your code, please put the code inside

 and 

tags. For example:

Using a common base path
public class Merge @Test
public void mergeFiles() String basePath = «/home/programcreek/Desktop/»;
mergeFiles(basePath, «m», «s1», «s2»);
> public void mergeFiles(String basePath, String mergedFilePath, String . files) File mergedFile = new File(basePath + mergedFilePath);
FileWriter fstream = null;
BufferedWriter out = null;
try fstream = new FileWriter(mergedFile, true);
out = new BufferedWriter(fstream);
> catch (IOException e1) e1.printStackTrace();
> for (String file : files) File f = new File(basePath + file);
System.out.println(«merging: » + f.getName());
FileInputStream fis;
try fis = new FileInputStream(f);
BufferedReader in = new BufferedReader(new InputStreamReader(fis)); String aLine;
while ((aLine = in.readLine()) != null) out.write(aLine);
out.newLine();
>
in.close();
out.flush(); /* Release the output buffer */
> catch (IOException e) e.printStackTrace();
>
> try out.close();
> catch (IOException e) e.printStackTrace();
>
>
>

Can this be done with files other than text files? Say word documents or Excel documents for example?

When I merge 10 large files (around 180 MB each) using your program, merge file size increase by 4KB from expected 🙂

This was great exactly what I needed. To build upon this you can also merge all of the files in an entire directory. I did this by incorporating the following method into your code. public static File[] getAllLogFiles() <
File folder = new File(“path/toYour/dir”);
File[] files = folder.listFiles();
>
return files;
> Thanks again for this post!

Источник

Читайте также:  Python set non blocking
Оцените статью