简体   繁体   English

查看单个文件历史记录的“x”提交的完整文件差异(在git中托管)

[英]View full file diff of `x` commits of a single file's history (that's hosted in git)

Say I have a file in git called filex.code , and I want to see the full code of the last x versions of that file with each changed section highlighted -- all in one place. 假设我在git中有一个名为filex.code的文件,我希望看到该文件的最后x版本的完整代码每个更改的部分都突出显示 - 全部在一个地方。 So an x -paned commit history of filex.code , almost as if I were doing an x -paned diff, but viewing historical versions rather than merging from different branches. 因此,一个x -paned犯下的历史filex.code ,仿佛我正在做一个x -paned差异,但观察历史版本而不是从不同的分支合并。

The greater x , the better. x越大越好。 Crossplatform would be great, but any of the Big Three works. Crossplatform会很棒,但三巨头中的任何一个都可以。 Being able to edit the latest version would also be great, but read-only visualization is plenty. 能够编辑最新版本也很棒,但只读可视化很多。

Note that this is different from a simple history of commits to a file, so the otherwise wonderful gitk path/to/file (or SourceTree or whatever visual git client you love) isn't what I'm looking for. 请注意,这与提交文件的简单历史记录不同,因此其他精彩的gitk path/to/file (或SourceTree或任何您喜欢的可视化git客户端)并不是我想要的。 git log -p also comes close, and its output tantalizingly includes all the information I'd want, just that it's all in a linear, almost "procedural" output format rather than a good, relatively non-hierarchical, visual one like your favorite three-paned GUI'd mergetool's. git log -p也很接近,它的输出很诱人地包含了我想要的所有信息,只是它是一个线性的,几乎是“程序”的输出格式,而不是像你最喜欢的一个好的,相对非分层的,视觉的三层GUI'd合并工具。

( Edit: Another really cool option that ultimately still experiences the shortcomings of only showing each line's latest source & a linear output is git blame , but it's cool.) 编辑:另一个非常酷的选项,最终仍然会遇到只显示每一行的最新来源和线性输出的缺点是git blame ,但它很酷。)

So I'm not precisely looking for setting up difftool either, I don't think. 我也不是在考虑设置difftool ,我不这么认为。 Rather than diffing two known versions of a file, I want to visualize x iterations of historical edits to a single file. 我想要将历史编辑的x次迭代可视化为单个文件,而不是区分文件的两个已知版本。

Asking too much? 问太多了? Is this a WTFA (Write The "Fantastic" App [yourself]) situation? 这是一个WTFA(写出“神奇”App [你自己])的情况吗?

Lesser alternative: Is there a three-paned mergetool that I can trick into displaying the last three commits of a single file? 较小的选择:是否有一个三层合并工具,我可以欺骗显示单个文件的最后三个提交?

This script opens last N revisions of the file side-by-side. 此脚本并排打开文件的最后N个修订版。

#!/usr/bin/env python
import os, sys, tempfile
from shutil import rmtree
from subprocess import call, Popen, PIPE
from optparse import OptionParser
from traceback import print_exc

COMMAND = 'vim -d'

def vcall(cmd, **kwargs):
    if options.verbose:
        print ' '.join(cmd)
    return call(' '.join(cmd) if sys.platform == 'darwin' else cmd, 
                **kwargs)

parser = OptionParser('usage: %s [-n <number of revisions>] filename' % 
                      sys.argv[0])
parser.add_option('-n', '--num', dest='N', type='int', 
                  help='number of revisions', default=3)
parser.add_option('-v', '--verbose', dest='verbose',
                  help='be verbose', default=False, action='store_true')
(options, args) = parser.parse_args()
if len(args) != 1:
    parser.error('incorrect number of arguments')
filename = args[0]

if vcall('git rev-parse'.split()) != 0:
    sys.exit(1)

try:
    cmd = 'git rev-list HEAD --'.split() + [filename]
    if options.verbose:
        print ' '.join(cmd)
    pipe = Popen(' '.join(cmd) if sys.platform == 'darwin' else cmd, 
                 stdout=PIPE).stdout
    revs = []
    for i, line in enumerate(pipe):
        if i == options.N:
            break
        revs.append(line.rstrip())
except:
    print_exc()

N = len(revs)
if N == 0:
    sys.exit('fatal: ambiguous argument %s: path not in the working tree' % 
             filename)
elif N < options.N:
    sys.stderr.write('%s has only %d revision%s' % 
                     (filename, N, 's' if N > 1 else ''))

tempdir = ''
try:
    tempdir = tempfile.mkdtemp()
    head, tail = os.path.split(filename)
    tempfiles = []
    for i in xrange(N):
        tempfiles.append(tail + ('.%d' % i if i else ''))
    for i, f in enumerate(tempfiles):
        with open(os.sep.join((tempdir, f)), 'w') as fout:
            vcall(['git', 'show', '%s:./%s' % (revs[i], filename)], stdout=fout)
    vcall(COMMAND.split() + list(reversed(tempfiles)), shell=True, cwd=tempdir)
except:
    print_exc()
finally:
    try:
        if tempdir and os.path.isdir(tempdir):
            rmtree(tempdir)
    except:
        print_exc()

Notes: 笔记:

  1. Vimdiff has a limitation of highlighting diffs in only 4 (first) buffers, but as for showing side-by-side - all file revisions are shown (eg N=20 works great). Vimdiff仅在4个(第一个)缓冲区中突出显示差异的限制,但是并排显示 - 显示所有文件修订(例如,N = 20非常好)。 To avoid the warning for N>4 use COMMAND = 'vim -O' to see versions side-by-side without any diffs at all. 为了避免N> 4的警告,请使用COMMAND = 'vim -O'并排查看版本,而根本没有任何差异。

  2. The script has grown to be too large for SO style, but it is quite bullet-proof now - yet simple enough for an experienced eye. 脚本已经变得太大而不适合SO风格,但它现在非常防弹 - 但足够简单,经验丰富的眼睛。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM