Bug 183226 - [Apply Patch] API to instantiate and apply arbitrary IHunks
Summary: [Apply Patch] API to instantiate and apply arbitrary IHunks
Status: RESOLVED FIXED
Alias: None
Product: Platform
Classification: Eclipse Project
Component: Compare (show other bugs)
Version: 3.3   Edit
Hardware: Other All
: P3 normal (vote)
Target Milestone: 3.5 M6   Edit
Assignee: Pawel Pogorzelski CLA
QA Contact:
URL:
Whiteboard:
Keywords: api
Depends on:
Blocks:
 
Reported: 2007-04-19 13:22 EDT by Stefan Xenos CLA
Modified: 2009-06-02 06:48 EDT (History)
3 users (show)

See Also:


Attachments
Patch_v01 (6.67 KB, patch)
2009-02-17 11:02 EST, Pawel Pogorzelski CLA
no flags Details | Diff
Patch_v01 (6.67 KB, patch)
2009-02-17 11:04 EST, Pawel Pogorzelski CLA
no flags Details | Diff
Patch_v02 (47.66 KB, patch)
2009-02-20 11:25 EST, Pawel Pogorzelski CLA
no flags Details | Diff
Patch_v03 (47.50 KB, patch)
2009-02-23 09:12 EST, Pawel Pogorzelski CLA
no flags Details | Diff
Patch_v04 (47.78 KB, patch)
2009-02-23 10:47 EST, Pawel Pogorzelski CLA
pawel.pogorzelski1: iplog+
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Stefan Xenos CLA 2007-04-19 13:22:07 EDT
I often need to instantiate an IHunk. I have all the fields I want its members to return, but actually obtaining the IHunk is complicated:

1. Generate a patch that will result in an IHunk with the given values.
2. Generate a file against which the hunk will fail to apply. (You need to be careful here because if you accidentally generate a file against which the hunk successfully applies, this doesn't work - for example, the empty file won't work when trying to create a hunk that creates a file)
3. Parse the patch using ApplyPatchOperation.parsePatch
4. Try to apply the patch to the generated file using IFilePatch.apply
5. Extract the IHunk from IFilePatchResult.getRejects().

Something like a static factory method for IHunks would be a great improvement.

static IHunk createHunk (String label, int startPosition, IInputStreamFactory originalContents, IInputStreamFactory patchedContents, String charset);
Comment 1 Szymon Brandys CLA 2008-04-15 09:18:05 EDT
Since it involves API changes, it will not be fixed for 3.4. Reassigning to 3.5 for further investigation.
Comment 2 Pawel Pogorzelski CLA 2009-02-11 06:47:48 EST
Also a way to create a IFilePatch from a set of IHunks would be good. Otherwise the usability of instantiated hunks would be somehow limited.
Comment 3 Pawel Pogorzelski CLA 2009-02-17 11:02:40 EST
Created attachment 125904 [details]
Patch_v01

An POC patch. Any comments are welcome.

I added two methods to IFilePatch2 interface:
addHunk(IHunk hunk);
removeHunk(IHunk hunk);

And two static methods in new PatchFactory class:
IFilePatch2 createFilePatch(IPath oldPath, long oldDate, IPath newPath, long newDate);
IHunk createHunk(int oldStart, int oldLength, int newStart, int newLength, String[] lines);

My main concern is whether to allow API clients to add and remove IHunks from IFilePatch2. As an alternative we can give them a way to create an instance of IFilePatch2 from a set of hunks (which would be read-only in terms of contained hunks).
Comment 4 Pawel Pogorzelski CLA 2009-02-17 11:04:13 EST
Created attachment 125905 [details]
Patch_v01

An POC patch. Any comments are welcome.

I added two methods to IFilePatch2 interface:
addHunk(IHunk hunk);
removeHunk(IHunk hunk);

And two static methods in new PatchFactory class:
IFilePatch2 createFilePatch(IPath oldPath, long oldDate, IPath newPath, long newDate);
IHunk createHunk(int oldStart, int oldLength, int newStart, int newLength, String[] lines);

My main concern is whether to allow API clients to add and remove IHunks from IFilePatch2. As an alternative we can give them a way to create an instance of IFilePatch2 from a set of hunks (which would be read-only in terms of contained hunks).
Comment 5 Pawel Pogorzelski CLA 2009-02-17 11:07:22 EST
Sorry for the doubled comment. It isn't Bugzilla greatest day.
Comment 6 Szymon Brandys CLA 2009-02-18 06:08:48 EST
(In reply to comment #4)
To sum up our morning discussion:

> I added two methods to IFilePatch2 interface:
> addHunk(IHunk hunk);
> removeHunk(IHunk hunk);

We're not adding add/remove methods to IFilePatch2 . We agreed that allowing clients to add/remove hunks to the patch can be error-prone.

> And two static methods in new PatchFactory class:
> IFilePatch2 createFilePatch(IPath oldPath, long oldDate, IPath newPath, long
> newDate);
> IHunk createHunk(int oldStart, int oldLength, int newStart, int newLength,
> String[] lines);

We will add PatchBuilder class. This facility class would help to create IFilePatch2 from arbitrary set of hunks. Its methods could look like this.

#add(IFilePatch2 filePatch, Set hunksToAdd, boolean recalculate) returns IFilePatch2
#remove(IFilePatch2 filePatch, Set hunksToRemove, boolean recalculate) returns IFilePatch2

or

#modify(IFilePatch2 filePatch, Set hunksToAdd, Set hunksToRemove, boolean recalculate) returns IFilePatch2

'recalculate' flag indicates whether to recalculate hunks position/lenght at the end of the operation. These methods would create new patches, so original patches would be untouched.

Comment 7 Pawel Pogorzelski CLA 2009-02-20 11:25:40 EST
Created attachment 126312 [details]
Patch_v02

Patch addressing Szymon's comments. It's still rough a bit but I still expect some input from the review about the proposed API shape.
Comment 8 Pawel Pogorzelski CLA 2009-02-23 09:12:47 EST
Created attachment 126453 [details]
Patch_v03

I talk to Szymon about the previous patch and we decided to change API little bit.

The most important change is that through exposed API we ensure that IHunks that are aggregated by IFilePatch2 are always sorted in order they apply to the file. Moreover IHunks' the "after" postition is updated after Hunks are added/removed from the  IFilePatch2 to keep the patch coherent.
Comment 9 Pawel Pogorzelski CLA 2009-02-23 10:47:14 EST
Created attachment 126468 [details]
Patch_v04

Adding @noextend tag to PatchBuilder and a few javadocs.
Comment 10 Szymon Brandys CLA 2009-02-23 11:24:23 EST
Released to HEAD.