threads / discuss / 39428

Staging commits with visual diff tools?

Subject: Staging commits with visual diff tools?

## tl;dr

10 messages between May 26, 2015 and Jun 28, 2015.

replies: 9people: 4as markdown or json

John Lee· May 26, 2015, 20:50 UTC · lore
Hi

Does anybody have code to stage commits using a the visual diff/merge tools supported by git-difftool? Is there support in git itself somewhere, even?

I'm looking for something functionally similar to git add -p

Looking at the git-difftool source I can see how to write a command to do it, but wanted to check if it had already been implemented.

Did I miss a way that already exists?
Thanks
John
Jeff King· May 27, 2015, 07:28 UTC · re: John Lee · lore

Re: Staging commits with visual diff tools?

On Tue, May 26, 2015 at 09:50:49PM +0100, John Lee wrote:
Show 9 quoted lines
> Does anybody have code to stage commits using a the visual diff/merge tools
> supported by git-difftool?  Is there support in git itself somewhere, even?
> 
> I'm looking for something functionally similar to git add -p
> 
> Looking at the git-difftool source I can see how to write a command to do
> it, but wanted to check if it had already been implemented.
> 
> Did I miss a way that already exists?

git-gui has a visual staging tool, and I think some other git interfaces do, too (e.g., tig). But I don't think there is a way to interact with an arbitrary 3rd-party diff tool. I would think doing so would depend on the diff tool itself; what facility does it provide for picking lines or a hunk out of a diff and communicating it back to git to stage?

If you are interested in looking further, see how the %patch_mode hash is defined in git-add--interactive.perl. Specifically, note that "add -p" is just one case: diff against the index and apply with "apply --cached". But once you have a separate tool for picking hunks, you should be able to drop in the different DIFF/APPLY pairs to implement "checkout -p", etc.

-Peff
John Lee· May 27, 2015, 07:34 UTC · re: Jeff King · lore

Re: Staging commits with visual diff tools?

On Wed, 27 May 2015, Jeff King wrote:
Show 6 quoted lines
> If you are interested in looking further, see how the %patch_mode hash
> is defined in git-add--interactive.perl. Specifically, note that "add
> -p" is just one case: diff against the index and apply with "apply
> --cached". But once you have a separate tool for picking hunks, you
> should be able to drop in the different DIFF/APPLY pairs to implement
> "checkout -p", etc.

My plan was to use symlinks, checkout-index, and update-index --index-info, similar to how difftool does it. I verified on the command line that that seems to work.

John
Michael J Gruber· May 29, 2015, 09:57 UTC · re: John Lee · lore

Re: Staging commits with visual diff tools?

John Lee venit, vidit, dixit 27.05.2015 09:34:
Show 16 quoted lines
> On Wed, 27 May 2015, Jeff King wrote:
> 
>> If you are interested in looking further, see how the %patch_mode hash
>> is defined in git-add--interactive.perl. Specifically, note that "add
>> -p" is just one case: diff against the index and apply with "apply
>> --cached". But once you have a separate tool for picking hunks, you
>> should be able to drop in the different DIFF/APPLY pairs to implement
>> "checkout -p", etc.
> 
> My plan was to use symlinks, checkout-index, and update-index 
> --index-info, similar to how difftool does it.  I verified on the command 
> line that that seems to work.
> 
> 
> John
> 

Yes, having "git addtool" (or "stagetool") to go with difftool and merge tool would be nice, and it could hopefully resuse code or infrastructure from those tools, as well as the tool config.

I vaguely remember a sketch or proof-of-concept by Jeff (not reusing *tool) but may be wrong.

Michael
David Aguilar· May 31, 2015, 01:59 UTC · re: John Lee · lore

Re: Staging commits with visual diff tools?

On Tue, May 26, 2015 at 09:50:49PM +0100, John Lee wrote:
Show 17 quoted lines
> Hi
> 
> Does anybody have code to stage commits using a the visual
> diff/merge tools supported by git-difftool?  Is there support in git
> itself somewhere, even?
> 
> I'm looking for something functionally similar to git add -p
> 
> Looking at the git-difftool source I can see how to write a command
> to do it, but wanted to check if it had already been implemented.
> 
> Did I miss a way that already exists?
> 
> Thanks
> 
> 
> John

If you're interested in existing staging tools then I would highly suggest looking at git-cola.

    https://github.com/git-cola/git-cola
    git clone git://github.com/git-cola/git-cola.git

git-cola is free software so it's pretty unique in many ways. I also wrote it, so that makes it that much more awesome ;-)

cola has a very slick interactive staging editor, interactive rebase, and a whole lot more.

I'm a g/vim user, so git-cola is finely tuned for keyboard usage. If we implement these feature in Git, we should consider providing the same workflows/hotkeys as cola.

For staging, you can select lines, hit "s", and they'll be staged/unstaged. ctrl-u reverts unstaged edits, etc. The '?' hotkey shows the help, etc.

You can get extremely granular, moreso then "add -p", which is why it exists. When cola's diff editor is not sufficient, you can also use it to drive difftool, but only in ways that you can on the command-line already.

git-cola is written in Python(2+3) so it's easy to hack on, and quite portable.

cheers,
-- 
David
John Lee· May 31, 2015, 09:36 UTC · re: David Aguilar · lore

Re: Staging commits with visual diff tools?

On Sat, 30 May 2015, David Aguilar wrote:
Show 5 quoted lines
> On Tue, May 26, 2015 at 09:50:49PM +0100, John Lee wrote:
>> Hi
>>
>> Does anybody have code to stage commits using a the visual
>> diff/merge tools supported by git-difftool?  Is there support in git
...
> I'm a g/vim user, so git-cola is finely tuned for keyboard
> usage.  If we implement these feature in Git, we should consider
> providing the same workflows/hotkeys as cola.

Just to be clear I'm not planning on contributing my script back to git, it will just be a standalone script in a separate repo.

I'll give git-cola a try, thanks. I just ran it and see it supports e.g. right click to stage and launching difftools -- does it also support launching a difftool to edit the index?

John
David Aguilar· Jun 1, 2015, 02:25 UTC · re: John Lee · lore

Re: Staging commits with visual diff tools?

On Sun, May 31, 2015 at 10:36:52AM +0100, John Lee wrote:
Show 18 quoted lines
> On Sat, 30 May 2015, David Aguilar wrote:
> 
> >On Tue, May 26, 2015 at 09:50:49PM +0100, John Lee wrote:
> >>Hi
> >>
> >>Does anybody have code to stage commits using a the visual
> >>diff/merge tools supported by git-difftool?  Is there support in git
> ...
> >I'm a g/vim user, so git-cola is finely tuned for keyboard
> >usage.  If we implement these feature in Git, we should consider
> >providing the same workflows/hotkeys as cola.
> 
> Just to be clear I'm not planning on contributing my script back to
> git, it will just be a standalone script in a separate repo.
> 
> I'll give git-cola a try, thanks.  I just ran it and see it supports
> e.g. right click to stage and launching difftools -- does it also
> support launching a difftool to edit the index?

If you have stuff staged and use ctrl-d (or right-click->difftool), git cola will run, "git difftool --staged -- <path>".

difftool will already point back to your worktree whenever possible through the GIT_EXTERNAL_DIFF machinery that it uses.

After changes are made in the worktree you select files, lines, or hunks and stage them. Doing it in one step could be as simple as having a script to automatically call "git add" after difftool, or "ctrl-s" in cola to stage the whole file.

A not as simple (but potentially quite helpful) use case is a tool that does a 3-way merge between HEAD, index, and worktree, and writes the result back to the worktree (or index?). The mergetool--lib backend could be leveraged for that feature.

Were you thinking of something like this, or something else? Can you describe your use case a bit more?

It seems like we have most of the use cases covered with stock git, but I'm not sure if I'm missing something.

cheers,
-- 
David
John Lee· Jun 1, 2015, 19:36 UTC · re: David Aguilar · lore

Re: Staging commits with visual diff tools?

On Sun, 31 May 2015, David Aguilar wrote:
> Were you thinking of something like this, or something else?
> Can you describe your use case a bit more?

I think now I should just publish my script and then it will be very clear what I'm talking about. If somebody then wants to add a similar feature to git-cola, or even git itself, that would be great.

John
John Lee· Jun 27, 2015, 23:23 UTC · re: John Lee · lore

Re: Staging commits with visual diff tools?

On Mon, 1 Jun 2015, John Lee wrote:
Show 11 quoted lines
> On Sun, 31 May 2015, David Aguilar wrote:
>> Were you thinking of something like this, or something else?
>> Can you describe your use case a bit more?
>
> I think now I should just publish my script and then it will be very clear 
> what I'm talking about.  If somebody then wants to add a similar feature to 
> git-cola, or even git itself, that would be great.
>
>
> John
>
Here it is:
https://github.com/jjlee/git-meld-index
Hasn't seen use for real yet, so be a bit cautious with it.
Comments / patches / whatever appreciated.
John
John Lee· Jun 28, 2015, 17:56 UTC · re: John Lee · lore

Re: Staging commits with visual diff tools?

On Sun, 28 Jun 2015, John Lee wrote:
Show 11 quoted lines
> Here it is:
>
> https://github.com/jjlee/git-meld-index
>
> Hasn't seen use for real yet, so be a bit cautious with it.
>
> Comments / patches / whatever appreciated.
>
>
> John
>

By the way despite the name it should support all git difftools (it uses the same git config parameters and git-mergetool--lib code as git itself).

I just used it to make some changes to its own project, quite nice :-)
It now has a --help manpage and some relevant options from git-difftool.
John

← back to recent threads