git-plus icon indicating copy to clipboard operation
git-plus copied to clipboard

New Feature: Add selected lines to index

Open code-tree opened this issue 10 years ago • 12 comments

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

code-tree avatar Aug 03 '15 04:08 code-tree

This would be awesome!

yfr avatar Aug 20 '15 07:08 yfr

:+1: This is probably the most useful feature of vim-fugitive!

wmayner avatar Aug 25 '15 21:08 wmayner

:+1:

mdlincoln avatar Aug 27 '15 00:08 mdlincoln

:+1:

AntonioMeireles avatar Sep 08 '15 10:09 AntonioMeireles

:+1:

superdump avatar Aug 02 '16 08:08 superdump

The Stage Hunk command provides this. It could use some polishing I think but that is the interactive add command.

akonwi avatar Nov 07 '16 19:11 akonwi

@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.

superdump avatar Nov 07 '16 21:11 superdump

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.

akonwi avatar Nov 07 '16 21:11 akonwi

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

akonwi avatar Jan 18 '17 21:01 akonwi

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 :)

code-tree avatar Jan 19 '17 01:01 code-tree

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...?

superdump avatar Jan 19 '17 11:01 superdump

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.

akonwi avatar Jan 19 '17 17:01 akonwi