-
Notifications
You must be signed in to change notification settings - Fork 219
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add shortcut to discard changes #30
Conversation
Do you want to fix these before merging? |
Unsure. The problem with hunk detection is a bit bigger of an issue I'd like to tackle separately. |
Can't you just copy most of the logic from unstage/stage? |
I sometimes can't toggle the diff of some changes. Could this be related to this issue? Also could you give me a TLDR about this issue? |
Probably. You don't write an actual patch though, right? You're just writing a modified object for the object database there. As far as I could tell, I can't do the equivalent for resetting though, so I'm using
I don't think so. The problem is really simple: |
|
I wanted to suggest that yes, seems like the do-it-all solution. I appreciate your approach with manually inserting objects though, I learned something from that :) Also, maybe I shouldn't code past 23:00... I added discarding untracked files. Apparently it's not too hard when you order things correctly 😅 |
Ok I'm having real trouble creating correct patches when selecting only part of a replacement operation and stuff like that. I'll study how magit accomplishes it's magic on the weekend and see what I can apply. |
My solution for stage/unstage correctly generates a "hunk" from the selection and then inserts it in the file. Maybe you can extract the hunk generation and use that for |
I added a function that generates a valid patch from a selection, but now I am not sure how to correctly apply any patches? Even using the most basic |
You're missing a step inbetween. Since your working copy has the patch already applied (since you created the diff from that) you first need to reset to HEAD to apply the patch again. Alternatively, you can run |
I'm afraid we're falling into the same trap. Hunks are a subsequent chain of changes, that need to be read in order. This part of the code is not always correct, since the hunk indices can shift when you apply them out of order: I feel the magit docs actually explain that really well: (defun magit-apply--adjust-hunk-new-starts (hunks)
"Adjust new line numbers in headers of HUNKS for partial application.
HUNKS should be a list of ordered, contiguous hunks to be applied
from a file. For example, if there is a sequence of hunks with
the headers
@@ -2,6 +2,7 @@
@@ -10,6 +11,7 @@
@@ -18,6 +20,7 @@
and only the second and third are to be applied, they would be
adjusted as \"@@ -10,6 +10,7 @@\" and \"@@ -18,6 +19,7 @@\"."
) (in this example, the first hunk appends a line to the new version that is not present if you only take the second hunk, thus it's index would need to shift to accomodate) |
Shouldn't this only matter if we support staging multiple hunks at once? Right now this is not supported. |
No, this is already an issue with only one hunk, In their example they say they'd apply the 2nd and 3rd hunk, but you actually have the same problem just applying the 2nd hunk. The problem is that the first hunk is skipped, since the insert from the first hunk is the reason the indices are different (-10, +11) in the second hunk. |
Add a new command `x` that will discard changes under the cursor, after user confirmation. Works on hunks, unstaged and staged files. Does not currently work on untracked files due to a bug in hunk detection firing on those. Does not currently work on whole sections or selections.
@TimUntersberger That means, for the specific case of staging a single hunk, we just need to set -local diff_header = string.format("@@ -%d,%d +%d,%d @@", hunk.index_from, hunk.index_len, hunk.disk_from, applied_len)
+local diff_header = string.format("@@ -%d,%d +%d,%d @@", hunk.index_from, hunk.index_len, hunk.index_from, applied_len) should solve the issue. |
Adapt `generate_patch_from_selection` to behave correctly in various edge cases, as well as when used for patches which will be applied in reverse. Allow `discard` to be used from visual mode.
Header line offset of a hunk is now adapted before being discarded, so it can be used in isolation, without information from prios hunks.
I had to adapt it a little bit to work in certain edge cases, but discard via selection now works :) Discarding a single hunk should now also work in all cases, so I'm good to merge now! |
@RianFuro it should also be possible to use the |
I haven't tested it, but I took care of still allowing for the normal staging case, yes. |
Add a new command
x
that will discard changes under the cursor,after user confirmation. Works on hunks, unstaged and staged files.
Does not currently work on untracked files due to a bug in hunk
detection firing on those.
Does not currently work on whole sections or selections.