Using git-diff Commands

29 views
Skip to first unread message

Thomas Passin

unread,
May 28, 2024, 10:38:31 AMMay 28
to leo-editor
Sometimes I want to diff a specific external file between two git branches.  They might be the currently checked out version and some other, or even between two different but not checked out commit hashes.

Can I do this using the existing Leo commands?  If so, how?

I use the Git-Extensions program to do most of my interactions with git, and it can do this kind of diff. It opens an external diff program, like KDiff3, to display the differences.  But it would be useful to be able to do so within Leo itself.

Edward K. Ream

unread,
May 28, 2024, 11:14:07 AMMay 28
to leo-e...@googlegroups.com
On Tue, May 28, 2024 at 9:38 AM Thomas Passin <tbp1...@gmail.com> wrote:
Sometimes I want to diff a specific external file between two git branches.  They might be the currently checked out version and some other, or even between two different but not checked out commit hashes.

Can I do this using the existing Leo commands?

No, but LeoPyRef.leo contains two scripts that you can use:

script: diff-branches/revs (all files)

from leo.commands import editFileCommands as efc

efc.GitDiffController(c).diff_two_revs(
    rev1='ekr-3744-pr3-token-based-orange',  # Old
    rev2='tbo-test',   # New
)

script: diff-branches/revs (one file)

g.cls()
import os
import leo.commands.editFileCommands as efc
path = g.finalize_join(g.app.loadDir, '..', '..')
os.chdir(path)

# Any revspec is valid as an argument to the "branch1" and "branch2" args.
# See https://git-scm.com/book/en/v2/Git-Tools-Revision-Selection

efc.GitDiffController(c).diff_two_branches(
    branch1='97282792e', # old branch/rev
    branch2='fd7537f5c', # new branch/rev
    fn='leo/plugins/viewrendered.py',  # Don't use backslashes.


They aren't commands because you have to fill data.  I have used both from time to time.

HTH. Please let me know if you have questions.

Edward

Thomas Passin

unread,
May 28, 2024, 11:33:00 AMMay 28
to leo-editor
Thanks, this looks like just the right thing.

Edward K. Ream

unread,
May 28, 2024, 11:52:00 AMMay 28
to leo-e...@googlegroups.com
On Tue, May 28, 2024 at 10:33 AM Thomas Passin <tbp1...@gmail.com> wrote:

Thanks, this looks like just the right thing.

You're welcome. Btw, the comment about revspecs applies to both scripts.

Edward

Thomas Passin

unread,
May 29, 2024, 6:04:50 PMMay 29
to leo-editor
This command, the one to compare two revspecs,  turned out not to work for me.  It uses too many built-in assumptions about how it will be used and where it will be used from.  For example, it has to run in an outline whose .leo file is in a repo.  I don't want to develop experimental scripts like this in PyLeoRef.leo - I want to develop them somewhere else like the workbook. And I don't always want their results to get added to an outline controlled by a git repo.

So I reworked the command and its supporting methods.  The command now takes an additional argument, gitdir, which specifies the top of a git repo, such as c:\Tom\git\leo-editor.  If this is missing or empty, the command tries to find a git repo above the current directory.  This usually works like the original command does, since the current directory is normally the same as the outline's directory.  If you want, however, you can use a different one by changing the current working directory before invoking the command.  

You specify the file to diff by its path relative to the git top level directory of that repo.  For example leo/plugins/viewrendered3.py.

As in the original command, you have to edit the script to specify the revspecs to be compared, the gitdir, and the filename.  I have commented out code to let you enter the two revspecs, but you still have to edit the file and gitdir.  It shouldn't be hard to write a command using this one that takes the currently selected node of an external file, finds its git directory, inputs the revspecs from the user, and runs the diff,  all as one command.  I haven't done that as yet.

This command is very new and is probably not bullet-proof.  But it's good enough that I have already used it for actual work.  An outline containing the command is attached.
git_diff_command.zip

Edward K. Ream

unread,
May 30, 2024, 5:53:51 AMMay 30
to leo-e...@googlegroups.com
On Wed, May 29, 2024 at 5:04 PM Thomas Passin <tbp1...@gmail.com> wrote:

This command, the one to compare two revspecs,  turned out not to work for me.
...
So I reworked the command and its supporting methods.
A worthwhile project. Please keep us all informed. I'll be happy to look at a PR.

Edward
Reply all
Reply to author
Forward
0 new messages