NeogitOrg / neogit

An interactive and powerful Git interface for Neovim, inspired by Magit
MIT License
4.12k stars 242 forks source link

Add a keybinding for 'git add --intent-to-add' ('git add -D') #520

Open fnune opened 1 year ago

fnune commented 1 year ago

Is your feature request related to a problem? Please describe.

image

Sometimes I want to see a diff in Neogit for a file that hasn't been added yet. The only way to do it inside Neogit is by staging it (pressing s).

Describe the solution you'd like

The git add command supports the flag --intent-to-add (docs here). Neogit could support it with a top-level keybinding for the NeogitStatus buffer, for example i.

Pressing i would run git add --intent-to-add <file-under-cursor> and result in this:

image

Note that the file is in the Unstaged area.

Describe alternatives you've considered

Run git add --intent-to-add <file-under-cursor> in the CLI. Defeats the purpose of Neogit.

CKolkey commented 1 year ago

I actually solved this a different way! You can diff against /dev/null, which shows every line as added. It's on the way ;)

If you're curious, here's the implementation.

fnune commented 1 year ago

Fugitive solves it with I: https://github.com/tpope/vim-fugitive/blob/master/doc/fugitive.txt#L312-L314

Magit solves it with another keybinding (can't find proper docs): https://emacs.stackexchange.com/questions/37340/use-git-add-n-in-magit

It seems like --intent-to-add is the standard way to do this.

CKolkey commented 1 year ago

Sure, but if you don't need to do anything and can still get a diff, wouldn't you say that's better?

fnune commented 1 year ago

Ah, so your patch will show the diff anyway even if the file is untracked. I guess that's reasonable.

Either way: supporting git add --intent-to-add is something I would consider in scope for Neogit's interface.

CKolkey commented 1 year ago

Yea, in my fork, untracked files just have a diff like every other file.

That said, I've got no argument against adding a command/key for to run --intent-to-add. Super simple too. I hadn't considered wanting to stage only a part of an untracked file, so to that end, it may be needed, too.

fnune commented 1 year ago

I hadn't considered wanting to stage only a part of an untracked file, so to that end, it may be needed, too.

I think that's not possible. --intent-to-add just works on full files AFAIK.

       -N, --intent-to-add
           Record only the fact that the path will be added later. An entry for the path is placed in the index with no content. This is
           useful for, among other things, showing the unstaged content of such files with git diff and committing them with git commit -a.

Says "path" only.

CKolkey commented 1 year ago

Was just basing it off the answer in your stackexchange link - you might be right, and it's either no possible, or magit does some stuff internally to make it happen. Regardless, adding the simple command is straightforward... though I'm not sure the use case 🤷🏼

fnune commented 1 year ago

Well, I tend to review things in the "Unstaged" area. While I'm working on a PR, it's useful to differentiate between "untracked" vs. "unstaged" by adding some of the new files with --intent-to-add.

IDK, I used to do it all the time with fugitive. I'd do it anyway even if Neogit showed diffs for untracked files.

wrightjjw commented 1 year ago

I actually solved this a different way! You can diff against /dev/null, which shows every line as added. It's on the way ;)

This probably doesn't work on Windows.

CKolkey commented 1 year ago

Well, I tend to review things in the "Unstaged" area. While I'm working on a PR, it's useful to differentiate between "untracked" vs. "unstaged" by adding some of the new files with --intent-to-add.

IDK, I used to do it all the time with fugitive. I'd do it anyway even if Neogit showed diffs for untracked files.

So, in doom/magit i is occupied by Ignore and I by Init. Ignore is planned, Init already exists. I'm open to ideas, or maybe <c-i>?

Screenshot 2023-06-08 at 14 06 04

I actually solved this a different way! You can diff against /dev/null, which shows every line as added. It's on the way ;)

This probably doesn't work on Windows.

Do people still use that OS?

...kidding. Maybe you could test for me? Just clone my fork and try to view the diff of an untracked file. I assume WSL has /dev/null...

edit:

Do people use neovim without wsl?

fnune commented 1 year ago

a for add?

CKolkey commented 1 year ago

Screenshot 2023-06-08 at 14 30 44

Sorry, I should have just posted the whole screenshot. That's Apply - brings up a buffer with branches to pick a commit from. Sort of the inverse of using A when the cursor is on a commit.

wrightjjw commented 1 year ago

Do people use neovim without wsl?

It actually works really well! 😆 I'm locked into Windows at work and I use native neovim with neovide. Neovide is my system default text editor.

@CKolkey I'll give your fork a shot. Should get a chance to mess around with something today.

fnune commented 1 year ago

Is it too confusing if Neogit makes s do git add --intent-to-add first instead of "stage" on an untracked file? You can then hit s again to stage it.

wrightjjw commented 1 year ago

@CKolkey Well I'll be damned, your solution actually does work in Windows (11, anyway). I tried it both in WSL and native on PowerShell and even in good old CMD. Turns out git-diff plays nice with /dev/null on platforms where it doesn't exist!

CKolkey commented 1 year ago

Is it too confusing if Neogit makes s do git add --intent-to-add first instead of "stage" on an untracked file? You can then hit s again to stage it.

I think that's just going to be too unintuitive - some users will want one behaviour, others a different one. How about this though - key-mappings are customisable - just bind the command to a key. If I expose the command, you can bind it to whatever suits you :)

@CKolkey Well I'll be damned, your solution actually does work in Windows (11, anyway). I tried it both in WSL and native on PowerShell and even in good old CMD. Turns out git-diff plays nice with /dev/null on platforms where it doesn't exist!

I'm as surprised as you are! Glad to hear it :D

fnune commented 1 year ago

Yah, feels like the feature is fringe enough that a simple method that can be bound to a customized key should be enough.

CKolkey commented 1 year ago

TBH thats pretty in-line with Magit - there are loads of functions that are not available via the UI, but can be invoked via M-x magit-whatever

Anywho, no trouble, I'll throw that in.

alextes commented 5 months ago

This feature for me is not about seeing diffs. I use it when i create a new directory with multiple new files that i think should be in different commits. often i won't know which files belong together in which commit until im done. if you open neogit at this stage, it only shows a untracked directory which you can add. the only way i see to select which files to add is to stage the whole dir, then unstage selectively. this works fine but to be fair its not how my thinking works. i want to stage selectively, not stage all and unstage selectively. --intent-to-add as the name suggests, fulfills exactly this need. This is also exactly when i would use it in fugitive previously.

Thanks for a great plugin btw ❤️