New Feature: Add selected lines to index
This is a feature request for functionality similar to "git add --patch", where git-plus adds only the currently selected text/lines to the index, rather than the whole file.
This might be a lot of work, but could be very useful though.
Cheers
This would be awesome!
:+1: This is probably the most useful feature of vim-fugitive!
:+1:
:+1:
:+1:
The Stage Hunk command provides this. It could use some polishing I think but that is the interactive add command.
@akonwi : A hunk is not a line or selection of lines and doing the equivalent of git add -p and the editing a hunk to only add a subset of the lines changed is far less efficient workflow-wise than being able to easily stage/unstage individual lines. Maybe take a look at how vim's fugitive workflow works to get a good taste of the additional speed this brings in making a bunch of changes and then staging them piece by piece to create a clean history of isolated and readable commits.
You're right @superdump. That comment was intended for the other issue (#484). Line by line staging would be great. It's not a priority for me at this point but it's on the radar.
I've been working on this over the last couple of days and it's not a trivial feature because in order to stage a subset of a hunk, the patch needs to be created manually. Creating a patch for a single line is possible but not straightforward. The problem is in individually staging multiple lines of a hunk because the patch needs to account for the staged parts of the hunk.
I'm leaving this on the backlog and hopefully someone with more git finesse can help out. PS I couldn't figure out how to do it with vim-fugitive either but I did see this issue with workarounds, which isn't possible in atom
Thanks for trying @akonwi. I knew it would be very difficult when I suggested it. Like you said, hopefully someone will be able to implement it one day :)
I think the way fugitive works is to consider two buffers - one which represents the current state of the git index and the other represents the current state of the file.
When you run git add you are, as far as I understand, essentially writing changes from your files into the git index. I suppose then if you want to stage a single line from an open file, you need to insert that line into the git index file that represents staged changes. This StackOverflow thread has some visual representations: http://stackoverflow.com/questions/3689838/difference-between-head-working-tree-index-in-git
As an experiment to see what happens, I created an empty repository, listed all files in the .git directory, created a file, git added it, listed all files in .git again and then took a look at their contents. It seems an index file was written in some binary format (described here: https://github.com/git/git/blob/master/Documentation/technical/index-format.txt) that references an object in .git/objects that contains the full content of the file after that add operation. So then I looked for the git object file format and found this: https://git-scm.com/book/en/v2/Git-Internals-Git-Objects - this seems to describe some git commands that can be used to write objects into git and then update the index. Perhaps these git commands could be used to simplify things...?
Thanks. Unfortunately, I don't think those help.
I can update the index file with a single line by patching. What I don't understand is how to individually stage multiple lines of a particular hunk because I kept running into issues where the patch created to apply the changes to the index file is corrupt, according to git, or the staged lines are incorrectly ordered in the index file.