highlight changed lines and changed bytes in each

2019-01-21 03:04发布

Open Source project Trac has an excellent diff highlighter — it highlight changed lines and changed bytes in each changed line! See https://trac.transmissionbt.com/changeset/12148 or http://trac.gajim.org/changeset/297ad7711d20bfee1491768640d9bc5384464363 for examples.

Is there way to use the same color highlight (i.e. changed lines and changed bytes too) in bash terminal, git or vim for diff output (patch-file)?

11条回答
贼婆χ
2楼-- · 2019-01-21 03:35

I use --color-words option and it works fine for me :

$ git diff --color-words | less -RS
查看更多
Summer. ? 凉城
3楼-- · 2019-01-21 03:39

Diffy

GitLab is using Diffy https://github.com/samg/diffy (Ruby) to achieve output similar to GitHub and diff-highlight:

enter image description here

Diffy makes the diff itself using the same algorithm ad Git, and supports different types of outputs, including the HTML output that GitLab uses:

gem install diffy
echo '
  require "diffy"    
  puts Diffy::Diff.new("a b c\n", "a B c\n").to_s(:html)
' | ruby

Output:

<div class="diff">
  <ul>
    <li class="del"><del>a <strong>b</strong> c</del></li>
    <li class="ins"><ins>a <strong>B</strong> c</ins></li>
  </ul>
</div>

Note how strong was added to the changed bytes.

查看更多
混吃等死
4楼-- · 2019-01-21 03:40

Emacs has the ediff-patch-buffer function which should fulfill your needs.

Open the un-patched file in emacs type ESC-x, ediff-patch-buffer.

Follow the prompts and you should see a highlighted comparison of the patched and original versions of your file.

As per your comment the following will will give you a bash solution requiring only dwdiff:

#!/bin/bash
paste -d'\n' <(dwdiff -2 -L -c <(cat $2) <(patch $2 -i $1 -o -)) <(dwdiff -1 -L -c <(cat $2) <(patch $2 -i $1 -o -))| uniq
查看更多
可以哭但决不认输i
5楼-- · 2019-01-21 03:45

This is much simpler in 2019

Byte-based diffs are now distributed with official git. You just have to locate where it is installed on your machine and enable it.

First: add diff-highlight to your path

GIT_HOME=/usr/local/opt/git/
ln -s ${GIT_HOME}/share/git-core/contrib/diff-highlight/diff-highlight /usr/local/bin/diff-highlight

Second: Enable in your Git config

git config --global interactive.diffFilter diff-highlight # Use on interactive prompts
git config --global pager.diff "diff-highlight | less"    # Use on git diff
git config --global pager.log  "diff-highlight | less"    # Use on git log
git config --global pager.show "diff-highlight | less"    # Use on git show

If you're not on macOS or didn't install git via brew then set GIT_HOME to git's base install directory on your machine before running.

Windows

Git is likely installed in a user-specific directory. Open a git shell and run this to find where it is:

cd / && pwd -W

Then use the path from that command as your GIT_HOME. See here for alternative methods of finding Git's install directory on Windows.

I have not personally verified this on Windows, but it should work

Linux

Nerd. If you don't already know where git is installed is then ll $(which git) or locate git should help.

查看更多
Juvenile、少年°
6楼-- · 2019-01-21 03:46

The behaviour you want is now available in git itself (as was pointed out in a comment by naught101). To enable it you need to set your pager to

perl /usr/share/doc/git/contrib/diff-highlight/diff-highlight | less

where /usr/share/doc/git/contrib/diff-highlight/diff-highlight is the location of the highlighter script on Ubuntu 13.10 (I have no idea why it's in a doc folder). If it isn't there on your system try using locate diff-highlight to find it. Note that the highlighting script is not executable (at least on my machine), hence the requirement for perl.

To always use the highlighter for the various diff-like commands just add the following to your ~/.gitconfig file:

[pager]
    log = perl /usr/share/doc/git/contrib/diff-highlight/diff-highlight | less
    show = perl /usr/share/doc/git/contrib/diff-highlight/diff-highlight | less
    diff = perl /usr/share/doc/git/contrib/diff-highlight/diff-highlight | less

I added this as a new answer naught101's comment is buried and because the set up is not quite as trivial as it should be and at least on the version of Ubuntu that I have the instructions in the README don't work.

查看更多
登录 后发表回答