Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 358527 - Applying a Git patch needs too much user work
Summary: Applying a Git patch needs too much user work
Status: VERIFIED FIXED
Alias: None
Product: Platform
Classification: Eclipse Project
Component: Compare (show other bugs)
Version: 3.8   Edit
Hardware: All All
: P3 normal (vote)
Target Milestone: 3.8 M6   Edit
Assignee: Dani Megert CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-09-22 04:00 EDT by Dani Megert CLA
Modified: 2014-10-09 06:51 EDT (History)
9 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Dani Megert CLA 2011-09-22 04:00:57 EDT
Juno M2.

Now that we switch(ed) to Git more patches will be in Git format. Applying such a patch is currently painful as the user has to manually set the leading segment ignore count and also manually select 'Apply patch to the workspace root' on the first page. And he has to know those tricks in the first place.

Given that the Git patch can be recognized (has "diff --git " in it) we should use smarter defaults:

- Apply the patch to workspace root if the patch crosses more than one project.
  NOTE: In case we can't detect this, we should always choose the root.

- Set ignore leading segment count to
  1 if workspace root is selected
  2 for the other case

Changing those defaults does not prevent any other (Git) patches to be applicable since the user can still change the initial values.
Comment 1 Szymon Brandys CLA 2011-09-22 08:19:54 EDT
Another issues related to the recent switch to Git.
Comment 2 Tomasz Zarna CLA 2011-09-23 06:34:38 EDT
(In reply to comment #0)
> - Apply the patch to workspace root if the patch crosses more than one project.
> NOTE: In case we can't detect this, we should always choose the root.

Currently we cannot detect this. The patching mechanism is not aware of projects unless it's dealing with a "workspace patch" (indicated with "### Eclipse Workspace Patch 1.0" in the first line). For CVS this is added by the "Create Patch" action, which makes the patch "Apply Patch" wizard specific. Having a similar thing in EGit would greatly improve handling patches that span multiple projects.

> - Set ignore leading segment count to
> 1 if workspace root is selected
> 2 for the other case

I'm not sure about this part. Skipping a/ b/ prefixes would be great, but what if a git patch comes without them? Given these patches are now generated in the command line (no "Create Patch" in EGit), there is little we can assume about the format or options used. Also note, that Eclipse projects can be located at any level of a git repo, so it's hard to decide how many segments to skip. Again, I think that making EGit produce "workspace patches" would help here a lot.

> Changing those defaults does not prevent any other (Git) patches to be
> applicable since the user can still change the initial values.

Right, to be honest I consider this bugzilla more as an enhancement request since you are able to apply git patches with the current "Apply Patch" wizard. You need to be aware how some of its options work, which is not always obvious indeed.
Comment 3 Dani Megert CLA 2011-09-23 06:37:38 EDT
> I'm not sure about this part. Skipping a/ b/ prefixes would be great, but what
> if a git patch comes without them? Given these patches are now generated in the
> command line (no "Create Patch" in EGit), there is little we can assume about
> the format or options used. Also note, that Eclipse projects can be located at
> any level of a git repo, so it's hard to decide how many segments to skip.
> Again, I think that making EGit produce "workspace patches" would help here a
> lot.
> 
> > Changing those defaults does not prevent any other (Git) patches to be
> > applicable since the user can still change the initial values.
> 
> Right, to be honest I consider this bugzilla more as an enhancement request
> since you are able to apply git patches with the current "Apply Patch" wizard.
> You need to be aware how some of its options work, which is not always obvious
> indeed.

If you tell me that not having a/ b/ is the 80% case then I agree. If not, it's a bug and a pain when using such patches.
Comment 4 Szymon Brandys CLA 2011-09-26 08:32:58 EDT
I would wait for the fix for bug 341036. Then we won't need any workarounds or defaults that depend on how the patch was generated. 

I hope that EGit guys understand the problem and will fix it ASAP. If not, we will handle it in Compare as Dani suggested.
Comment 5 Dani Megert CLA 2011-09-26 08:37:42 EDT
(In reply to comment #4)
> I would wait for the fix for bug 341036. Then we won't need any workarounds or
> defaults that depend on how the patch was generated. 
> 
> I hope that EGit guys understand the problem and will fix it ASAP. If not, we
> will handle it in Compare as Dani suggested.

This does not solve the problem. Even with the fix for said bug, I can still create a simple Git patch via History view and then want to be able to easily apply it.
Comment 6 Tomasz Zarna CLA 2011-09-26 08:52:39 EDT
I think EGit could use the same wizard to generate a "workspace" patch (if possible) in both History view and Team menu. That would make life much easier for the Apply Patch wizard
Comment 7 Dani Megert CLA 2011-10-26 06:06:54 EDT
> I think EGit could use the same wizard to generate a "workspace" patch (if
> possible) in both History view and Team menu. That would make life much easier
> for the Apply Patch wizard
Sure, but as said it won't help me to apply all the patches that are already attached to some bugs. The problem with the current Git patches is that one has to try by hand how much segments one need to remove and this differs from patch to patch. E.g. in case where the real bundles are nested inside some folder, the count needs to be bigger.
Comment 8 Dani Megert CLA 2011-10-26 06:10:22 EDT
> - Set ignore leading segment count to
>  1 if workspace root is selected
The patch wizard should be smarter and try to find the best value. See e.g. bug 361821 where a segment of 2 was needed.
Comment 9 Szymon Brandys CLA 2011-11-28 06:22:20 EST
(In reply to comment #4)
> I hope that EGit guys understand the problem and will fix it ASAP. If not, we
> will handle it in Compare as Dani suggested.

Moving to M5. It seems that we have to apply the Dani's suggestion
Comment 10 Dani Megert CLA 2012-02-07 04:21:56 EST
Ping! Every time I apply a patch I have to do additional manually work.
Comment 11 Tomasz Zarna CLA 2012-02-07 04:44:50 EST
There is a patch with pending review in EGit. It's about making EGit produce "workspace" patches. See the blocking bug or go directly to https://git.eclipse.org/r/#/c/5046/
Comment 12 Dani Megert CLA 2012-02-07 04:47:18 EST
(In reply to comment #11)
> There is a patch with pending review in EGit. It's about making EGit produce
> "workspace" patches. See the blocking bug or go directly to
> https://git.eclipse.org/r/#/c/5046/

I'm sorry, I might not be smart enough, but how does that help with existing patches attached to bugzilla?
Comment 13 Tomasz Zarna CLA 2012-02-07 05:03:37 EST
It doesn't. The patch for EGit fixes the root cause, which is that EGit diffing mechanism can produce only plain unified diffs and is not aware of the workspace whatsoever.
Comment 14 Dani Megert CLA 2012-02-07 05:16:52 EST
(In reply to comment #13)
> It doesn't. 

I knew that :-)


What would help as a quick remedy is if the wizard would remember the previous choices i.e.  root vs. selected file and the leading segments.
Comment 15 Tomasz Zarna CLA 2012-02-13 06:40:31 EST
(In reply to comment #14)
> remember the previous choices i.e.  root vs. selected file and the leading segments.

The former is a result of your selection when opening the wizard ie. if the selection is empty the workspace root option is suggested, otherwise a project you had selected is used as the patch target. I wouldn't change that.

The latter sounds like a good idea as long as you don't mind manually setting the combo to '0' from time to time. Or did you want it to be smart enough to check if the patch can be applied with no ignored segments and then do the same with the saved value? Or the other way round, check the saved value first, and when failed fallback to the default 0.

Maybe a better solution would be having EGit provide their own Apply Patch wizard instead of tweaking the one in Compare? The first step has been made, JGit ApplyCommand is on its way (see bug 361548).
Comment 16 Dani Megert CLA 2012-02-15 08:54:05 EST
(In reply to comment #15)
> (In reply to comment #14)
> > remember the previous choices i.e.  root vs. selected file and the leading 
> > segments.
> 
> The former is a result of your selection when opening the wizard ie. if the
> selection is empty the workspace root option is suggested, otherwise a project
> you had selected is used as the patch target. I wouldn't change that.

I see your point and you're right.

Yes, for the segment it would be best if it would try to compute the correct segment option value based on the patch, but that will only work if workspace root was chosen or the correct projects selected (which is a pain).


> Maybe a better solution would be having EGit provide their own Apply Patch
> wizard 
-1 for that. There are also other features like pasting a patch in the Package Explorer, which would then have to be adapted.


Let's look again at the original idea from comment 0: why can't we check whether the first segment in a git patch that contains "--- a" and/or "+++ b"  are present in the workspace and if so, default to workspace root and 1 leading segment? In all other cases we behave as we currently do.
Comment 17 Tomasz Zarna CLA 2012-03-06 06:57:54 EST
(In reply to comment #16)
> Let's look again at the original idea from comment 0: why can't we check whether
> the first segment in a git patch that contains "--- a" and/or "+++ b"  are
> present in the workspace and if so, default to workspace root and 1 leading
> segment? In all other cases we behave as we currently do.

Implemented in c16d33cb372c32c25cb422662c7f86d9761572b3. Marking as fixed and no longer dependent on bug 367735.
Comment 18 Dani Megert CLA 2012-03-12 11:54:14 EDT
The auto-detection seems to have a problem with Git patches in e-mail format.

Test Case:
1. clone the 'eclipse.platform.text' repository
2. import 'org.eclipse.jface.text'
3. open https://bugs.eclipse.org/bugs/attachment.cgi?id=212117
4. copy the patch but without the last two lines (see bug 373975 why)
5. start to apply the patch
   ==> no auto-detection
6. copy the patch starting at "diff --git " but without the last two lines 
7. start to apply the patch
   ==> auto-detection works
Comment 19 Dani Megert CLA 2012-03-14 12:31:35 EDT
http://git.eclipse.org/c/platform/eclipse.platform.team.git/commit/?id=1607e13426ba2abc67ae4017efab88bcb93d9733 plus parent commit.

Also rebased 'integration'.
Comment 20 Dani Megert CLA 2012-03-15 03:53:33 EDT
Verified in I20120314-1800.
Comment 21 Daniel Sokolowski CLA 2014-10-08 10:27:21 EDT
Just thought I would share that I was just stumped by this for about an hour. 

The 'Apply Patch' was complaining the 'file does not exist' - the solution was to of course Ignore one leading path segment. However I see that it was set as fixed - should the 'Apply Patch' logic auto ignored the first leading path segment for me?

Also please note that Help docs state you actually can not do it which is misleading: https://wiki.eclipse.org/EGit/User_Guide#Applying_Patches

Thanks awesome Eclipsonauts!
Comment 22 Dani Megert CLA 2014-10-09 06:51:29 EDT
(In reply to Daniel Sokolowski from comment #21)
> Just thought I would share that I was just stumped by this for about an
> hour. 
> 
> The 'Apply Patch' was complaining the 'file does not exist' - the solution
> was to of course Ignore one leading path segment. However I see that it was
> set as fixed - should the 'Apply Patch' logic auto ignored the first leading
> path segment for me?

I suggest to file a new bug if you can provide a reproducible test case. 


> Also please note that Help docs state you actually can not do it which is
> misleading: https://wiki.eclipse.org/EGit/User_Guide#Applying_Patches

This is still true for general Git patches. If the patch doesn't include special stuff (e.g. extensions), then it works.