From da5e24260a60d65479b3d64c608b8b072f5bdf19 Mon Sep 17 00:00:00 2001 From: Balakrishnan Balasubramanian Date: Tue, 20 Dec 2022 13:57:14 -0500 Subject: [PATCH] Use shlex to split custom vim command This allows to include spaces in vim command Add example to change default diff algorithm Move to first line on each file --- README.md | 15 ++++++++------- vimtabdiff.py | 12 +++++++----- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index af08b9c..226faa4 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,7 @@ Do you use `git difftool` to review changes before making a commit? The problem # Install +```bash mkdir -p ~/bin # for python version >= 3.10 @@ -12,6 +13,7 @@ Do you use `git difftool` to review changes before making a commit? The problem curl -o ~/bin/vimtabdiff.py "https://raw.githubusercontent.com/balki/vimtabdiff/py38/vimtabdiff.py" chmod +x ~/bin/vimtabdiff.py +``` You may need to add `~/bin` to your PATH variable if not already done. See [here](https://wiki.archlinux.org/title/Environment_variables#Per_user) for help. 👍 this [issue](https://github.com/balki/vimtabdiff/issues/1) for `pip install` support @@ -69,6 +71,7 @@ You may need to add `~/bin` to your PATH variable if not already done. See [here ## Using custom vim command +Using clean vim without reading `vimrc` ```bash git config --global difftool.vimtabdiff.cmd 'vimtabdiff.py --vim "vim --clean" $LOCAL $REMOTE' ``` @@ -82,16 +85,14 @@ Git config file (`~/.gitconfig`) should look like this [difftool "vimtabdiff"] cmd = vimtabdiff.py --vim \"vim --clean\" $LOCAL $REMOTE ``` +Using better diff algorithm +```bash + git config --global difftool.vimtabdiff.cmd 'vimtabdiff.py --vim "vim +\"set diffopt+=algorithm:patience\"" $LOCAL $REMOTE' -# Known issues +``` - 1. If your path to vim has a space, it does not work, i.e. Following does **not** work - - ```bash - git config difftool.vimtabdiff.cmd 'vimtabdiff.py --vim "/home/foo/my program files/bin/vim" $LOCAL $REMOTE' - ``` - 2. Not tested in non-linux OS. Pull requests welcome if found any issues but hopefully should work fine. +*Note:* Not tested in non-linux OS. But I guess it should work fine. Pull requests welcome if found any issues. # Similar diff --git a/vimtabdiff.py b/vimtabdiff.py index 2feb79b..e49f40e 100755 --- a/vimtabdiff.py +++ b/vimtabdiff.py @@ -9,6 +9,7 @@ import argparse import itertools import tempfile import subprocess +import shlex from pathlib import Path from typing import Callable, TypeVar from collections.abc import Iterator, Sequence @@ -23,8 +24,8 @@ def star(f: Callable[..., T]) -> Callable[[Sequence], T]: def parse_args() -> argparse.Namespace: parser = argparse.ArgumentParser( - description="Show diff of files from two directories in vim tabs", - epilog="See https://github.com/balki/vimtabdiff for more info") + description="Show diff of files from two directories in vim tabs", + epilog="See https://github.com/balki/vimtabdiff for more info") parser.add_argument("pathA", type=Path) parser.add_argument("pathB", type=Path) parser.add_argument("--vim", help="vim command to run", default="vim") @@ -52,7 +53,7 @@ def get_pairs(aItems: list[Path], for _, items in itertools.groupby(abItems, key=star(lambda item, _: item.name)): match list(items): - case [(aItem, _), (bItem, _)]: + case [(aItem, _), (bItem, _)]: yield aItem, bItem case [(item, 'A'),]: yield item, None @@ -77,14 +78,15 @@ def main() -> None: aPath = a.resolve() if a else os.devnull bPath = b.resolve() if b else os.devnull print( - f"tabedit {aPath} | diffthis | vsp {bPath} | diffthis | diffupdate", + f"tabedit {aPath} | vsp {bPath} | windo diffthis | windo diffupdate", file=vimCmdFile) cmds = f""" + tabdo windo :1 tabfirst | tabclose call delete("{vimCmdFile.name}") """ print(cmds, file=vimCmdFile) - subprocess.run(args.vim.split() + ["-S", vimCmdFile.name]) + subprocess.run(shlex.split(args.vim) + ["-S", vimCmdFile.name]) if __name__ == '__main__':