Commit 402ef22efe4c

Vincent Demeester <vincent@sbr.pm>
2019-12-13 15:40:35
setup-vcs.el: add git-blame-line function
Signed-off-by: Vincent Demeester <vincent@sbr.pm>
1 parent 17b1b49
Changed files (1)
lisp/setup-vcs.el
@@ -89,6 +89,27 @@
               (")" . dired-git-info-mode))
   :defer 2)
 
+(defun git-blame-line ()
+  "Runs `git blame` on the current line and
+   adds the commit id to the kill ring"
+  (interactive)
+  (let* ((line-number (save-excursion
+                        (goto-char (point-at-bol))
+                        (+ 1 (count-lines 1 (point)))))
+         (line-arg (format "%d,%d" line-number line-number))
+         (commit-buf (generate-new-buffer "*git-blame-line-commit*")))
+    (call-process "git" nil commit-buf nil 
+                  "blame" (buffer-file-name) "-L" line-arg)
+    (let* ((commit-id (with-current-buffer commit-buf
+                        (buffer-substring 1 9)))
+           (log-buf (generate-new-buffer "*git-blame-line-log*")))
+      (kill-new commit-id)
+      (call-process "git" nil log-buf nil 
+                    "log" "-1" "--pretty=%h   %an   %s" commit-id)
+      (with-current-buffer log-buf
+        (message "Line %d: %s" line-number (buffer-string)))
+      (kill-buffer log-buf))
+    (kill-buffer commit-buf)))
 
 (provide 'setup-vcs)