我发现hg annotate
在跟踪代码的来源方面非常有用,但我发现在emacs的vc模式中,vc-annotate
是......不太好。这是一个片段:
297 Wed Oct 06 15:21:30 2010 -0600 aws/lib/survey/creator/dbTemplates/web/views.sql: $$,$$
687 Mon Dec 20 10:25:41 2010 -0700 aws/lib/survey/creator/dbTemplates/web/views.sql: GRANT SELECT ON survey_length_view TO reportuser,surveyuser,sampleserver,sampleloader;
687 Mon Dec 20 10:25:41 2010 -0700 aws/lib/survey/creator/dbTemplates/web/views.sql: GRANT ALL ON survey_length_view TO adminuser, GROUP staff;
297 Wed Oct 06 15:21:30 2010 -0600 aws/lib/survey/creator/dbTemplates/web/views.sql: $$);
正如您所看到的,那里有一个很多的序言,以及相当多的冗余信息。我不需要知道完整的ISO时间戳(大多数时候,无论如何),当我选择注释时,我在文件中的事实涵盖了文件的路径。我做需要知道是谁做出了改变,这在这里显然不存在。
如何修复此注释工具以使其变得有用?
答案 0 :(得分:4)
vc-hg.el似乎将参数硬编码为hg annotate
,因此您需要在加载vc-hg后重新定义命令:
(require 'vc-hg)
(defun vc-hg-annotate-command (file buffer &optional revision)
"Execute \"hg annotate\" on FILE, inserting the contents in BUFFER.
Optional arg REVISION is a revision to annotate from."
(vc-hg-command buffer 0 file "annotate" "-d" "-n" "--follow"
(when revision (concat "-r" revision))))
简单地删除参数可能会导致您因缺少信息而失去功能,因此更好的解决方案是遵循vc-bzr.el中的示例,其中一些信息被删除并放入工具提示中而不是。如果沿着这条路走下去,请考虑将您的改进贡献给Emacs。
答案 1 :(得分:0)
我也有这个问题,这是我的解决方案:
;; vc-mode
(defconst vc-hg-annotate-better-re
"^[ ]*\\(.+?\\) \\([0-9]+\\) \\([0-9a-f]+\\) \\(.+?\\+[0-9]+\\) +\\(.+?\\):\\([0-9]+\\):\\(.*\n?\\)")
(defconst vc-hg-annotate-result-re
"^[\t ]*\\([0-9]+\\) +\\(.+?\\) .*")
(eval-after-load "vc-hg"
'(defun vc-hg-annotate-command (file buffer &optional revision)
"Execute \"hg annotate\" on FILE, inserting the contents in BUFFER.
Optional arg REVISION is a revision to annotate from."
(vc-hg-command buffer 'async file "annotate" "-d" "-n" "-u" "-c" "-l" "--follow"
(when revision (concat "-r" revision)))
(lexical-let ((table (make-hash-table :test 'equal)))
(set-process-filter
(get-buffer-process buffer)
(lambda (proc string)
(when (process-buffer proc)
(with-current-buffer (process-buffer proc)
(setq string (concat (process-get proc :vc-left-over) string))
(while (string-match vc-hg-annotate-better-re string)
(let* ((author (match-string 1 string))
(rev (match-string 2 string))
(changeset (match-string 3 string))
(date (match-string 4 string))
(key (concat rev author date))
(file (match-string 5 string))
(lineno (match-string 6 string))
(content (match-string 7 string))
(tag (gethash key table))
(inhibit-read-only t))
(setq string (substring string (match-end 0)))
(unless tag
(setq tag
(propertize
(format "%-5s %-8.8s" rev author)
'help-echo (format "Revision: %d, author: %s, date: %s"
(string-to-number rev)
author date)
'mouse-face 'highlight))
(puthash key tag table))
(goto-char (process-mark proc))
(insert tag content)
(move-marker (process-mark proc) (point))))
(process-put proc :vc-left-over string))))))))
(eval-after-load "vc-hg"
'(defun vc-hg-annotate-time ()
(save-excursion
(beginning-of-line)
(when (looking-at vc-hg-annotate-result-re)
(let ((prop (get-text-property (line-beginning-position) 'help-echo)))
(string-match ", date: \\(.+\\)\\'" prop)
(let ((str (match-string-no-properties 1 prop)))
(vc-annotate-convert-time (date-to-time str))))))))
(eval-after-load "vc-hg"
'(defun vc-hg-annotate-extract-revision-at-line ()
(save-excursion
(beginning-of-line)
(when (looking-at vc-hg-annotate-result-re)
(match-string-no-properties 1)))))
将它添加到您的init文件中,您将获得与bzr annotate类似的输出格式:
266 jimb@re | /* Window creation, deletion and examination for GNU Emacs.
266 jimb@re | Does not include redisplay.
102971 rgm@gnu | Copyright (C) 1985-1987, 1993-1998, 2000-2011
77438.1.2234 rgm@gnu | Free Software Foundation, Inc.
266 jimb@re |
266 jimb@re | This file is part of GNU Emacs.
可悲的是,像这样过滤注释输出确实会使大文件上的注释变慢,我还没有找到解决方法(对bzr中的文件进行注释也很慢)。
答案 2 :(得分:0)
这已在Emacs 25中解决。
;; One line printed by "hg annotate -dq -n -u --follow" looks like this:
;; b56girard 114590 2012-03-13 CLOBBER: Lorem ipsum dolor sit
;; i.e. AUTHOR REVISION DATE FILENAME: CONTENTS
;; The user can omit options "-u" and/or "--follow". Then it'll look like:
;; 114590 2012-03-13 CLOBBER:
;; or
;; b56girard 114590 2012-03-13: