Skip to main content

Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EGit / JGit » JGIT -> How to apply a patch to a file outside a repository
JGIT -> How to apply a patch to a file outside a repository [message #1288082] Tue, 08 April 2014 09:02 Go to next message
Juan Pablo Barani is currently offline Juan Pablo BaraniFriend
Messages: 1
Registered: April 2014
Junior Member
I am building a solution to update files for a certain software in Java. I want to create something like the diff/patch Unix functionality, but with Java, for files that are directly in a FS, that is, outside a repository.

At the moment, I could easily get a Diff with JGit by using the following code:

public static EditList computeDifferencesGit(final File p_fileOld,
                                             final File p_fileNew) throws IOException
    RawText l_contentFileOld = new RawText(p_fileOld);
    RawText l_contentFileNew = new RawText(p_fileNew);

    EditList l_listDiffs = new EditList();
    l_listDiffs.addAll(new HistogramDiff().diff(RawTextComparator.DEFAULT, l_contentFileOld, l_contentFileNew));

    String l_strUnidiff = _getUnidiff(l_contentFileOld, l_contentFileNew, l_listDiffs);
    System.out.println("UNIDIFF:\n" + l_strUnidiff);

    return l_listDiffs;

However, I would like to apply the results of that diff to a third file as a patch.

I investigated and found there is an ApplyCommand class in JGit package. However, to instantiate it, I think I need a repository, which I don't have.

Any idea about how can I apply the patch to the file?

Re: JGIT -> How to apply a patch to a file outside a repository [message #1288284 is a reply to message #1288082] Tue, 08 April 2014 12:34 Go to previous message
Matthias Sohn is currently offline Matthias SohnFriend
Messages: 1152
Registered: July 2009
Senior Member
ApplyCommand uses the repository to locate the working tree defining the root folder for applying the patch.
So you could add a second constructor accepting this root path to enable using the ApplyCommand without a repository.
If you want to contribute this patch follow the contributor guide [1].

Previous Topic:Clone Gist failed
Next Topic:Git Staging: No repository selected
Goto Forum:

Current Time: Sun Aug 09 00:44:31 GMT 2020

Powered by FUDForum. Page generated in 0.02218 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top