开发者

How do you get a pager in svn diff?

开发者 https://www.devze.com 2023-01-15 11:14 出处:网络
I’d like svn diff to display colored diff through a pager (just like git does). I’ve succeeded to get a colored diff by setting the diff-cmd in ~/.subversion/config:

I’d like svn diff to display colored diff through a pager (just like git does). I’ve succeeded to get a colored diff by setting the diff-cmd in ~/.subversion/config:

diff-cmd = colordiff开发者_如何学运维

Now I’d like to pipe the diff output through a pager, how do I do that? (Short of writing svn diff | less, of course.)


In the past I've used a wrapper script and set diff-cmd to this script:

#!/bin/sh
colordiff "$@" | less -r

But then you get a separate pager for every file, I'm not sure if this is what you want. Nowadays I just write svn diff | less.

Another easy solution is making an alias: alias svndiff='svn diff | less'. Or if you want to use svn diff, make a shell function:

svn() {
    if [ x"$1" = xdiff ] || [ x"$1" = xdi ]; then
        /usr/bin/svn "$@" | less -r
    else
        /usr/bin/svn "$@"
    fi
}


I usually run svn diff | vim -.


Adding

function sdi ()
{
    if tty -s; then
            exec svn diff --diff-cmd=colordiff "$@" | less -R
    else
            exec svn diff --diff-cmd=colordiff "$@"
    fi
}

to my ~/.bashrc did the trick for me. Taken from here


This would've been a comment, but I don't have enough rep. An improvement on schot's answer is to also fail before piping to less if the svn command fails for any reason, for example if you specify a file that doesn't exist. It also passes through the error code as well.

And this answer also adds progress format:

EDIT: An additional approvement is when piping/redirecting output to not use colordiff. To do this, do not change ~/.subversion/config and instead we need to use the --diff-cmd option.

function svn() {
    if [[ "$1" == "diff" && -t 1 ]]; then
        output=$(/usr/bin/svn --diff-cmd=colordiff "$@") || return $?
        [ "${output}" != "" ] && less -rM +Gg <<< "${output}";
    else
        /usr/bin/svn "$@"
    fi
}
0

精彩评论

暂无评论...
验证码 换一张
取 消