contrib / emacs / git-blame.elon commit xsize_t: check whether we lose bits (46be82d)
   1;;; git-blame.el --- Minor mode for incremental blame for Git  -*- coding: utf-8 -*-
   2;;
   3;; Copyright (C) 2007  David Kågedal
   4;;
   5;; Authors:    David Kågedal <davidk@lysator.liu.se>
   6;; Created:    31 Jan 2007
   7;; Message-ID: <87iren2vqx.fsf@morpheus.local>
   8;; License:    GPL
   9;; Keywords:   git, version control, release management
  10;;
  11;; Compatibility: Emacs21
  12
  13
  14;; This file is *NOT* part of GNU Emacs.
  15;; This file is distributed under the same terms as GNU Emacs.
  16
  17;; This program is free software; you can redistribute it and/or
  18;; modify it under the terms of the GNU General Public License as
  19;; published by the Free Software Foundation; either version 2 of
  20;; the License, or (at your option) any later version.
  21
  22;; This program is distributed in the hope that it will be
  23;; useful, but WITHOUT ANY WARRANTY; without even the implied
  24;; warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  25;; PURPOSE.  See the GNU General Public License for more details.
  26
  27;; You should have received a copy of the GNU General Public
  28;; License along with this program; if not, write to the Free
  29;; Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  30;; MA 02111-1307 USA
  31
  32;; http://www.fsf.org/copyleft/gpl.html
  33
  34
  35;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  36;;
  37;;; Commentary:
  38;;
  39;; Here is an Emacs implementation of incremental git-blame.  When you
  40;; turn it on while viewing a file, the editor buffer will be updated by
  41;; setting the background of individual lines to a color that reflects
  42;; which commit it comes from.  And when you move around the buffer, a
  43;; one-line summary will be shown in the echo area.
  44
  45;;; Installation:
  46;;
  47;; To use this package, put it somewhere in `load-path' (or add
  48;; directory with git-blame.el to `load-path'), and add the following
  49;; line to your .emacs:
  50;;
  51;;    (require 'git-blame)
  52;;
  53;; If you do not want to load this package before it is necessary, you
  54;; can make use of the `autoload' feature, e.g. by adding to your .emacs
  55;; the following lines
  56;;
  57;;    (autoload 'git-blame-mode "git-blame"
  58;;              "Minor mode for incremental blame for Git." t)
  59;;
  60;; Then first use of `M-x git-blame-mode' would load the package.
  61
  62;;; Compatibility:
  63;;
  64;; It requires GNU Emacs 21.  If you'are using Emacs 20, try
  65;; changing this:
  66;;
  67;;            (overlay-put ovl 'face (list :background
  68;;                                         (cdr (assq 'color (cddddr info)))))
  69;;
  70;; to
  71;;
  72;;            (overlay-put ovl 'face (cons 'background-color
  73;;                                         (cdr (assq 'color (cddddr info)))))
  74
  75
  76;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  77;;
  78;;; Code:
  79
  80(require 'cl)                         ; to use `push', `pop'
  81
  82(defun color-scale (l)
  83  (let* ((colors ())
  84         r g b)
  85    (setq r l)
  86    (while r
  87      (setq g l)
  88      (while g
  89        (setq b l)
  90        (while b
  91          (push (concat "#" (car r) (car g) (car b)) colors)
  92          (pop b))
  93        (pop g))
  94      (pop r))
  95    colors))
  96
  97(defvar git-blame-dark-colors
  98  (color-scale '("0c" "04" "24" "1c" "2c" "34" "14" "3c")))
  99
 100(defvar git-blame-light-colors
 101  (color-scale '("c4" "d4" "cc" "dc" "f4" "e4" "fc" "ec")))
 102
 103(defvar git-blame-ancient-color "dark green")
 104
 105(defvar git-blame-autoupdate t
 106  "*Automatically update the blame display while editing")
 107
 108(defvar git-blame-proc nil
 109  "The running git-blame process")
 110(make-variable-buffer-local 'git-blame-proc)
 111
 112(defvar git-blame-overlays nil
 113  "The git-blame overlays used in the current buffer.")
 114(make-variable-buffer-local 'git-blame-overlays)
 115
 116(defvar git-blame-cache nil
 117  "A cache of git-blame information for the current buffer")
 118(make-variable-buffer-local 'git-blame-cache)
 119
 120(defvar git-blame-idle-timer nil
 121  "An idle timer that updates the blame")
 122(make-variable-buffer-local 'git-blame-cache)
 123
 124(defvar git-blame-update-queue nil
 125  "A queue of update requests")
 126(make-variable-buffer-local 'git-blame-update-queue)
 127
 128(defvar git-blame-mode nil)
 129(make-variable-buffer-local 'git-blame-mode)
 130(unless (assq 'git-blame-mode minor-mode-alist)
 131  (setq minor-mode-alist
 132        (cons (list 'git-blame-mode " blame")
 133              minor-mode-alist)))
 134
 135;;;###autoload
 136(defun git-blame-mode (&optional arg)
 137  "Minor mode for displaying Git blame"
 138  (interactive "P")
 139  (if arg
 140      (setq git-blame-mode (eq arg 1))
 141    (setq git-blame-mode (not git-blame-mode)))
 142  (make-local-variable 'git-blame-colors)
 143  (if git-blame-autoupdate
 144      (add-hook 'after-change-functions 'git-blame-after-change nil t)
 145    (remove-hook 'after-change-functions 'git-blame-after-change t))
 146  (git-blame-cleanup)
 147  (if git-blame-mode
 148      (progn
 149        (let ((bgmode (cdr (assoc 'background-mode (frame-parameters)))))
 150          (if (eq bgmode 'dark)
 151              (setq git-blame-colors git-blame-dark-colors)
 152            (setq git-blame-colors git-blame-light-colors)))
 153        (setq git-blame-cache (make-hash-table :test 'equal))
 154        (git-blame-run))
 155    (cancel-timer git-blame-idle-timer)))
 156
 157;;;###autoload
 158(defun git-reblame ()
 159  "Recalculate all blame information in the current buffer"
 160  (unless git-blame-mode
 161    (error "git-blame is not active"))
 162  (interactive)
 163  (git-blame-cleanup)
 164  (git-blame-run))
 165
 166(defun git-blame-run (&optional startline endline)
 167  (if git-blame-proc
 168      ;; Should maybe queue up a new run here
 169      (message "Already running git blame")
 170    (let ((display-buf (current-buffer))
 171          (blame-buf (get-buffer-create
 172                      (concat " git blame for " (buffer-name))))
 173          (args '("--incremental" "--contents" "-")))
 174      (if startline
 175          (setq args (append args
 176                             (list "-L" (format "%d,%d" startline endline)))))
 177      (setq args (append args
 178                         (list (file-name-nondirectory buffer-file-name))))
 179      (setq git-blame-proc
 180            (apply 'start-process
 181                   "git-blame" blame-buf
 182                   "git" "blame"
 183                   args))
 184      (with-current-buffer blame-buf
 185        (erase-buffer)
 186        (make-local-variable 'git-blame-file)
 187        (make-local-variable 'git-blame-current)
 188        (setq git-blame-file display-buf)
 189        (setq git-blame-current nil))
 190      (set-process-filter git-blame-proc 'git-blame-filter)
 191      (set-process-sentinel git-blame-proc 'git-blame-sentinel)
 192      (process-send-region git-blame-proc (point-min) (point-max))
 193      (process-send-eof git-blame-proc))))
 194
 195(defun remove-git-blame-text-properties (start end)
 196  (let ((modified (buffer-modified-p))
 197        (inhibit-read-only t))
 198    (remove-text-properties start end '(point-entered nil))
 199    (set-buffer-modified-p modified)))
 200
 201(defun git-blame-cleanup ()
 202  "Remove all blame properties"
 203    (mapcar 'delete-overlay git-blame-overlays)
 204    (setq git-blame-overlays nil)
 205    (remove-git-blame-text-properties (point-min) (point-max)))
 206
 207(defun git-blame-update-region (start end)
 208  "Rerun blame to get updates between START and END"
 209  (let ((overlays (overlays-in start end)))
 210    (while overlays
 211      (let ((overlay (pop overlays)))
 212        (if (< (overlay-start overlay) start)
 213            (setq start (overlay-start overlay)))
 214        (if (> (overlay-end overlay) end)
 215            (setq end (overlay-end overlay)))
 216        (setq git-blame-overlays (delete overlay git-blame-overlays))
 217        (delete-overlay overlay))))
 218  (remove-git-blame-text-properties start end)
 219  ;; We can be sure that start and end are at line breaks
 220  (git-blame-run (1+ (count-lines (point-min) start))
 221                 (count-lines (point-min) end)))
 222
 223(defun git-blame-sentinel (proc status)
 224  (with-current-buffer (process-buffer proc)
 225    (with-current-buffer git-blame-file
 226      (setq git-blame-proc nil)
 227      (if git-blame-update-queue
 228          (git-blame-delayed-update))))
 229  ;;(kill-buffer (process-buffer proc))
 230  ;;(message "git blame finished")
 231  )
 232
 233(defvar in-blame-filter nil)
 234
 235(defun git-blame-filter (proc str)
 236  (save-excursion
 237    (set-buffer (process-buffer proc))
 238    (goto-char (process-mark proc))
 239    (insert-before-markers str)
 240    (goto-char 0)
 241    (unless in-blame-filter
 242      (let ((more t)
 243            (in-blame-filter t))
 244        (while more
 245          (setq more (git-blame-parse)))))))
 246
 247(defun git-blame-parse ()
 248  (cond ((looking-at "\\([0-9a-f]\\{40\\}\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\)\n")
 249         (let ((hash (match-string 1))
 250               (src-line (string-to-number (match-string 2)))
 251               (res-line (string-to-number (match-string 3)))
 252               (num-lines (string-to-number (match-string 4))))
 253           (setq git-blame-current
 254                 (if (string= hash "0000000000000000000000000000000000000000")
 255                     nil
 256                   (git-blame-new-commit
 257                    hash src-line res-line num-lines))))
 258         (delete-region (point) (match-end 0))
 259         t)
 260        ((looking-at "filename \\(.+\\)\n")
 261         (let ((filename (match-string 1)))
 262           (git-blame-add-info "filename" filename))
 263         (delete-region (point) (match-end 0))
 264         t)
 265        ((looking-at "\\([a-z-]+\\) \\(.+\\)\n")
 266         (let ((key (match-string 1))
 267               (value (match-string 2)))
 268           (git-blame-add-info key value))
 269         (delete-region (point) (match-end 0))
 270         t)
 271        ((looking-at "boundary\n")
 272         (setq git-blame-current nil)
 273         (delete-region (point) (match-end 0))
 274         t)
 275        (t
 276         nil)))
 277
 278
 279(defun git-blame-new-commit (hash src-line res-line num-lines)
 280  (save-excursion
 281    (set-buffer git-blame-file)
 282    (let ((info (gethash hash git-blame-cache))
 283          (inhibit-point-motion-hooks t)
 284          (inhibit-modification-hooks t))
 285      (when (not info)
 286        (let ((color (pop git-blame-colors)))
 287          (unless color
 288            (setq color git-blame-ancient-color))
 289          (setq info (list hash src-line res-line num-lines
 290                           (git-describe-commit hash)
 291                           (cons 'color color))))
 292        (puthash hash info git-blame-cache))
 293      (goto-line res-line)
 294      (while (> num-lines 0)
 295        (if (get-text-property (point) 'git-blame)
 296            (forward-line)
 297          (let* ((start (point))
 298                 (end (progn (forward-line 1) (point)))
 299                 (ovl (make-overlay start end)))
 300            (push ovl git-blame-overlays)
 301            (overlay-put ovl 'git-blame info)
 302            (overlay-put ovl 'help-echo hash)
 303            (overlay-put ovl 'face (list :background
 304                                         (cdr (assq 'color (nthcdr 5 info)))))
 305            ;; the point-entered property doesn't seem to work in overlays
 306            ;;(overlay-put ovl 'point-entered
 307            ;;             `(lambda (x y) (git-blame-identify ,hash)))
 308            (let ((modified (buffer-modified-p)))
 309              (put-text-property (if (= start 1) start (1- start)) (1- end)
 310                                 'point-entered
 311                                 `(lambda (x y) (git-blame-identify ,hash)))
 312              (set-buffer-modified-p modified))))
 313        (setq num-lines (1- num-lines))))))
 314
 315(defun git-blame-add-info (key value)
 316  (if git-blame-current
 317      (nconc git-blame-current (list (cons (intern key) value)))))
 318
 319(defun git-blame-current-commit ()
 320  (let ((info (get-char-property (point) 'git-blame)))
 321    (if info
 322        (car info)
 323      (error "No commit info"))))
 324
 325(defun git-describe-commit (hash)
 326  (with-temp-buffer
 327    (call-process "git" nil t nil
 328                  "log" "-1" "--pretty=oneline"
 329                  hash)
 330    (buffer-substring (point-min) (1- (point-max)))))
 331
 332(defvar git-blame-last-identification nil)
 333(make-variable-buffer-local 'git-blame-last-identification)
 334(defun git-blame-identify (&optional hash)
 335  (interactive)
 336  (let ((info (gethash (or hash (git-blame-current-commit)) git-blame-cache)))
 337    (when (and info (not (eq info git-blame-last-identification)))
 338      (message "%s" (nth 4 info))
 339      (setq git-blame-last-identification info))))
 340
 341;; (defun git-blame-after-save ()
 342;;   (when git-blame-mode
 343;;     (git-blame-cleanup)
 344;;     (git-blame-run)))
 345;; (add-hook 'after-save-hook 'git-blame-after-save)
 346
 347(defun git-blame-after-change (start end length)
 348  (when git-blame-mode
 349    (git-blame-enq-update start end)))
 350
 351(defvar git-blame-last-update nil)
 352(make-variable-buffer-local 'git-blame-last-update)
 353(defun git-blame-enq-update (start end)
 354  "Mark the region between START and END as needing blame update"
 355  ;; Try to be smart and avoid multiple callouts for sequential
 356  ;; editing
 357  (cond ((and git-blame-last-update
 358              (= start (cdr git-blame-last-update)))
 359         (setcdr git-blame-last-update end))
 360        ((and git-blame-last-update
 361              (= end (car git-blame-last-update)))
 362         (setcar git-blame-last-update start))
 363        (t
 364         (setq git-blame-last-update (cons start end))
 365         (setq git-blame-update-queue (nconc git-blame-update-queue
 366                                             (list git-blame-last-update)))))
 367  (unless (or git-blame-proc git-blame-idle-timer)
 368    (setq git-blame-idle-timer
 369          (run-with-idle-timer 0.5 nil 'git-blame-delayed-update))))
 370
 371(defun git-blame-delayed-update ()
 372  (setq git-blame-idle-timer nil)
 373  (if git-blame-update-queue
 374      (let ((first (pop git-blame-update-queue))
 375            (inhibit-point-motion-hooks t))
 376        (git-blame-update-region (car first) (cdr first)))))
 377
 378(provide 'git-blame)
 379
 380;;; git-blame.el ends here