{"thread":{"id":"51637","subject":"How to reset selected lines?","startedAt":"2019-08-13T14:18:23Z","lastAt":"2019-08-14T14:52:22Z","messageCount":7,"participants":["Pratyush Yadav","Jeff King","Junio C Hamano","Johannes Sixt"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"380386","messageId":"20190813141816.yoer6pfjdnlgtj76@localhost.localdomain","threadId":"51637","inReplyTo":null,"subject":"How to reset selected lines?","fromName":"Pratyush Yadav","fromEmail":"me@yadavpratyush.com","sentAt":"2019-08-13T14:18:16Z","receivedAt":"2019-08-13T14:18:23Z","isPatch":false,"sender":{"key":"me@yadavpratyush.com","avatar":"https://avatars.githubusercontent.com/u/8817931?v=4"},"body":"Hi,\n\nTo put things into context of why I am asking this, git-gui has a \nfeature where you can select parts of a displayed diff, and can \nstage/unstage those parts. That feature is implemented in git-gui by \njust generating a diff from the selected lines, and then applying it. \nCheck git-gui/lib/diff.tcl:643 for the implementation.\n\nNow, I want to add a similar feature, but one that discards/resets the \nselected lines. And I'd like to avoid the hack that git-gui's \napply_range_or_line is. So, is there a cleaner way to do this that does \nnot involve generating a diff and then applying it?\n\nAlso, if there is a better way of staging and unstaging selected lines \nas well, do let me know, and I will try to fix git-gui's hacky way of \ndoing it.\n\n-- \nRegards,\nPratyush Yadav\n"},{"id":"380390","messageId":"20190813154239.GA22514@sigill.intra.peff.net","threadId":"51637","inReplyTo":"20190813141816.yoer6pfjdnlgtj76@localhost.localdomain","subject":"Re: How to reset selected lines?","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2019-08-13T15:42:39Z","receivedAt":"2019-08-13T15:42:41Z","isPatch":false,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Tue, Aug 13, 2019 at 07:48:16PM +0530, Pratyush Yadav wrote:\n\n> To put things into context of why I am asking this, git-gui has a \n> feature where you can select parts of a displayed diff, and can \n> stage/unstage those parts. That feature is implemented in git-gui by \n> just generating a diff from the selected lines, and then applying it. \n> Check git-gui/lib/diff.tcl:643 for the implementation.\n> \n> Now, I want to add a similar feature, but one that discards/resets the \n> selected lines. And I'd like to avoid the hack that git-gui's \n> apply_range_or_line is. So, is there a cleaner way to do this that does \n> not involve generating a diff and then applying it?\n\nTo answer your second question first:\n\nGit's index and trees only understand whole files, so at some point you\nmust generate the final file content. A diff is an easy way to represent\nthe changes, apply them to the existing state, and then get that final\ncontent. But it doesn't _have_ to be. You could make some modifications\nto what is in the working tree and then say \"OK, now stage this.\".\n\nBUT. That is probably not what the user wants, if the content in the\nindex actually has some modifications that are not in the working tree\n(i.e., you wouldn't want to overwrite them). Hence we tend to work with\ndiffs, saying \"make these changes to what is already in the index, and\nif they conflict, then bail\".\n\nSo \"git add -p\", for example, also works by creating diffs, modifying\nthem, and feeding the result to \"apply\". You can see the implementation\nin git-add--interactive.perl, where it literally calls diff and apply\ncommands.\n\nAnd that leads us to the answer to the first question. That script\nimplements \"add -p\", but also \"checkout -p\" (which is what you want),\n\"reset -p\", \"stash -p\", etc. They differ only in what we diff and how we\napply the result; the main engine of slicing and dicing the diff through\nuser interaction is the same. See the %patch_modes hash for the list.\n\n-Peff\n"},{"id":"380400","messageId":"xmqqblwt7yyr.fsf@gitster-ct.c.googlers.com","threadId":"51637","inReplyTo":"20190813154239.GA22514@sigill.intra.peff.net","subject":"Re: How to reset selected lines?","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2019-08-13T17:32:44Z","receivedAt":"2019-08-13T17:32:49Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Jeff King <peff@peff.net> writes:\n\n> Git's index and trees only understand whole files, so at some point you\n> must generate the final file content. A diff is an easy way to represent\n> the changes, apply them to the existing state, and then get that final\n> content. But it doesn't _have_ to be. You could make some modifications\n> to what is in the working tree and then say \"OK, now stage this.\".\n>\n> BUT. That is probably not what the user wants, if the content in the\n> index actually has some modifications that are not in the working tree\n> (i.e., you wouldn't want to overwrite them). Hence we tend to work with\n> diffs, saying \"make these changes to what is already in the index, and\n> if they conflict, then bail\".\n>\n> So \"git add -p\", for example, also works by creating diffs, modifying\n> them, and feeding the result to \"apply\". You can see the implementation\n> in git-add--interactive.perl, where it literally calls diff and apply\n> commands.\n>\n> And that leads us to the answer to the first question. That script\n> implements \"add -p\", but also \"checkout -p\" (which is what you want),\n> \"reset -p\", \"stash -p\", etc. They differ only in what we diff and how we\n> apply the result; the main engine of slicing and dicing the diff through\n> user interaction is the same. See the %patch_modes hash for the list.\n\nI was about to respond but you said everything I wanted to say and\nsaid it a lot better than I could ;-)\n\n"},{"id":"380423","messageId":"1f31dd2c-3023-2d13-3466-bffabab703cc@kdbg.org","threadId":"51637","inReplyTo":"20190813141816.yoer6pfjdnlgtj76@localhost.localdomain","subject":"Re: How to reset selected lines?","fromName":"Johannes Sixt","fromEmail":"j6t@kdbg.org","sentAt":"2019-08-13T22:07:26Z","receivedAt":"2019-08-13T22:07:30Z","isPatch":false,"sender":{"key":"j6t@kdbg.org","avatar":"https://avatars.githubusercontent.com/u/14810926?v=4"},"body":"Am 13.08.19 um 16:18 schrieb Pratyush Yadav:\n> Now, I want to add a similar feature, but one that discards/resets the \n> selected lines. And I'd like to avoid the hack that git-gui's \n> apply_range_or_line is. So, is there a cleaner way to do this that does \n> not involve generating a diff and then applying it?\n\nDon't waste your time. I'm using this feature since years. It's\navailable in these three commits:\n\nhttps://github.com/j6t/git/commit/55276690b2bc6a\nhttps://github.com/j6t/git/commit/f12b294b68b595\nhttps://github.com/j6t/git/commit/00860615691604\n\nThanks go to Bert Wesarg. The patches are also available in this thread:\n\nhttps://public-inbox.org/git/cccd6193cf3bfe170e14270204d735a842bb8563.1326116492.git.bert.wesarg@googlemail.com/#t\n\n-- Hannes\n"},{"id":"380433","messageId":"20190814111108.lymvzdwicpil53at@localhost.localdomain","threadId":"51637","inReplyTo":"1f31dd2c-3023-2d13-3466-bffabab703cc@kdbg.org","subject":"Re: How to reset selected lines?","fromName":"Pratyush Yadav","fromEmail":"me@yadavpratyush.com","sentAt":"2019-08-14T11:11:08Z","receivedAt":"2019-08-14T11:11:14Z","isPatch":false,"sender":{"key":"me@yadavpratyush.com","avatar":"https://avatars.githubusercontent.com/u/8817931?v=4"},"body":"On 14/08/19 12:07AM, Johannes Sixt wrote:\n> Am 13.08.19 um 16:18 schrieb Pratyush Yadav:\n> > Now, I want to add a similar feature, but one that discards/resets the \n> > selected lines. And I'd like to avoid the hack that git-gui's \n> > apply_range_or_line is. So, is there a cleaner way to do this that does \n> > not involve generating a diff and then applying it?\n> \n> Don't waste your time. I'm using this feature since years. It's\n> available in these three commits:\n> \n> https://github.com/j6t/git/commit/55276690b2bc6a\n> https://github.com/j6t/git/commit/f12b294b68b595\n> https://github.com/j6t/git/commit/00860615691604\n> \n> Thanks go to Bert Wesarg. The patches are also available in this thread:\n> \n> https://public-inbox.org/git/cccd6193cf3bfe170e14270204d735a842bb8563.1326116492.git.bert.wesarg@googlemail.com/#t\n\nThanks for letting me know. I did try a similar change, before asking \nhere, but git-apply complained about corrupt patches, so I thought more \nwork had to be done. I'll look into the patches and see if I can work \nthem into patches fit for merging.\n\n-- \nRegards,\nPratyush Yadav\n"},{"id":"380434","messageId":"20190814114844.gvb5znje7cpzehkd@localhost.localdomain","threadId":"51637","inReplyTo":"20190813154239.GA22514@sigill.intra.peff.net","subject":"Re: How to reset selected lines?","fromName":"Pratyush Yadav","fromEmail":"me@yadavpratyush.com","sentAt":"2019-08-14T11:48:44Z","receivedAt":"2019-08-14T11:48:51Z","isPatch":false,"sender":{"key":"me@yadavpratyush.com","avatar":"https://avatars.githubusercontent.com/u/8817931?v=4"},"body":"On 13/08/19 11:42AM, Jeff King wrote:\n> On Tue, Aug 13, 2019 at 07:48:16PM +0530, Pratyush Yadav wrote:\n> \n> > To put things into context of why I am asking this, git-gui has a \n> > feature where you can select parts of a displayed diff, and can \n> > stage/unstage those parts. That feature is implemented in git-gui by \n> > just generating a diff from the selected lines, and then applying it. \n> > Check git-gui/lib/diff.tcl:643 for the implementation.\n> > \n> > Now, I want to add a similar feature, but one that discards/resets the \n> > selected lines. And I'd like to avoid the hack that git-gui's \n> > apply_range_or_line is. So, is there a cleaner way to do this that does \n> > not involve generating a diff and then applying it?\n> \n> To answer your second question first:\n> \n> Git's index and trees only understand whole files, so at some point you\n> must generate the final file content. A diff is an easy way to represent\n> the changes, apply them to the existing state, and then get that final\n> content. But it doesn't _have_ to be. You could make some modifications\n> to what is in the working tree and then say \"OK, now stage this.\".\n> \n> BUT. That is probably not what the user wants, if the content in the\n> index actually has some modifications that are not in the working tree\n> (i.e., you wouldn't want to overwrite them). Hence we tend to work with\n> diffs, saying \"make these changes to what is already in the index, and\n> if they conflict, then bail\".\n> \n> So \"git add -p\", for example, also works by creating diffs, modifying\n> them, and feeding the result to \"apply\". You can see the implementation\n> in git-add--interactive.perl, where it literally calls diff and apply\n> commands.\n> \n> And that leads us to the answer to the first question. That script\n> implements \"add -p\", but also \"checkout -p\" (which is what you want),\n> \"reset -p\", \"stash -p\", etc. They differ only in what we diff and how we\n> apply the result; the main engine of slicing and dicing the diff through\n> user interaction is the same. See the %patch_modes hash for the list.\n\nAh, so that means I do have to dive into generating diffs. Too bad, I \nwas hoping for a cleaner (read: easier) way.\n\nOn that note, I don't suppose there is a way to use \ngit-add--interactive's diff engine from a script, is there?  That'd \nallow me to not write potentially buggy code and solve problems someone \nalready solved.\n\nThanks for your detailed answer.\n\n-- \nRegards,\nPratyush Yadav\n"},{"id":"380439","messageId":"20190814145219.GA12093@sigill.intra.peff.net","threadId":"51637","inReplyTo":"20190814114844.gvb5znje7cpzehkd@localhost.localdomain","subject":"Re: How to reset selected lines?","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2019-08-14T14:52:20Z","receivedAt":"2019-08-14T14:52:22Z","isPatch":false,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Wed, Aug 14, 2019 at 05:18:44PM +0530, Pratyush Yadav wrote:\n\n> > So \"git add -p\", for example, also works by creating diffs, modifying\n> > them, and feeding the result to \"apply\". You can see the implementation\n> > in git-add--interactive.perl, where it literally calls diff and apply\n> > commands.\n> > \n> > And that leads us to the answer to the first question. That script\n> > implements \"add -p\", but also \"checkout -p\" (which is what you want),\n> > \"reset -p\", \"stash -p\", etc. They differ only in what we diff and how we\n> > apply the result; the main engine of slicing and dicing the diff through\n> > user interaction is the same. See the %patch_modes hash for the list.\n> \n> Ah, so that means I do have to dive into generating diffs. Too bad, I \n> was hoping for a cleaner (read: easier) way.\n> \n> On that note, I don't suppose there is a way to use \n> git-add--interactive's diff engine from a script, is there?  That'd \n> allow me to not write potentially buggy code and solve problems someone \n> already solved.\n\nThe actual diffing is all done by lower-level commands. So I'm not sure\nthere's much \"engine\" to reuse from the script. It takes care of asking\nthe user about which lines to look at, how to split the diff, etc, but\nthat's the part you'd want a totally different interface for.\n\nThe one thing you _might_ want to pick up is the line recounting bits.\nWe used to just call \"apply --recount\", but I think there were some\ncorner cases that it couldn't handle well. The details are in 2b8ea7f3c7\n(add -p: calculate offset delta for edited patches, 2018-03-05) and\n3a8522f41f (add -p: don't rely on apply's '--recount' option,\n2018-03-05), and probably some mailing list around those.\n\nYou could probably start with just not handling those corner cases,\nthough (either relying on \"apply --recount\", or just using whatever\ngit-gui already does now for staging).\n\n-Peff\n"}]}