contrib / emacs / vc-git.elon commit parse-remote: do not barf on a remote shorthand without any refs to fetch. (ae9c6ff)
   1;;; vc-git.el --- VC backend for the git version control system
   2
   3;; Copyright (C) 2006 Alexandre Julliard
   4
   5;; This program is free software; you can redistribute it and/or
   6;; modify it under the terms of the GNU General Public License as
   7;; published by the Free Software Foundation; either version 2 of
   8;; the License, or (at your option) any later version.
   9;;
  10;; This program is distributed in the hope that it will be
  11;; useful, but WITHOUT ANY WARRANTY; without even the implied
  12;; warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  13;; PURPOSE.  See the GNU General Public License for more details.
  14;;
  15;; You should have received a copy of the GNU General Public
  16;; License along with this program; if not, write to the Free
  17;; Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  18;; MA 02111-1307 USA
  19
  20;;; Commentary:
  21
  22;; This file contains a VC backend for the git version control
  23;; system.
  24;;
  25;; To install: put this file on the load-path and add GIT to the list
  26;; of supported backends in `vc-handled-backends'; the following line,
  27;; placed in your ~/.emacs, will accomplish this:
  28;;
  29;;     (add-to-list 'vc-handled-backends 'GIT)
  30;;
  31;; TODO
  32;;  - changelog generation
  33;;  - working with revisions other than HEAD
  34;;
  35
  36(eval-when-compile (require 'cl))
  37
  38(defvar git-commits-coding-system 'utf-8
  39  "Default coding system for git commits.")
  40
  41(defun vc-git--run-command-string (file &rest args)
  42  "Run a git command on FILE and return its output as string."
  43  (let* ((ok t)
  44         (str (with-output-to-string
  45                (with-current-buffer standard-output
  46                  (unless (eq 0 (apply #'call-process "git" nil '(t nil) nil
  47                                       (append args (list (file-relative-name file)))))
  48                    (setq ok nil))))))
  49    (and ok str)))
  50
  51(defun vc-git--run-command (file &rest args)
  52  "Run a git command on FILE, discarding any output."
  53  (let ((name (file-relative-name file)))
  54    (eq 0 (apply #'call-process "git" nil (get-buffer "*Messages") nil (append args (list name))))))
  55
  56(defun vc-git--run-command-out (output &rest args)
  57  "Run a git command, output to output."
  58  (eq 0 (apply #'call-process "git" nil output nil (append args))))
  59
  60(defun vc-git-registered (file)
  61  "Check whether FILE is registered with git."
  62  (with-temp-buffer
  63    (let* ((dir (file-name-directory file))
  64           (name (file-relative-name file dir)))
  65      (and (ignore-errors
  66             (when dir (cd dir))
  67             (eq 0 (call-process "git" nil '(t nil) nil "ls-files" "-c" "-z" "--" name)))
  68           (let ((str (buffer-string)))
  69             (and (> (length str) (length name))
  70                  (string= (substring str 0 (1+ (length name))) (concat name "\0"))))))))
  71
  72(defun vc-git-state (file)
  73  "git-specific version of `vc-state'."
  74  (let ((diff (vc-git--run-command-string file "diff-index" "-z" "HEAD" "--")))
  75    (if (and diff (string-match ":[0-7]\\{6\\} [0-7]\\{6\\} [0-9a-f]\\{40\\} [0-9a-f]\\{40\\} [ADMU]\0[^\0]+\0" diff))
  76        'edited
  77      'up-to-date)))
  78
  79(defun vc-git-workfile-version (file)
  80  "git-specific version of `vc-workfile-version'."
  81  (let ((str (with-output-to-string
  82               (with-current-buffer standard-output
  83                 (call-process "git" nil '(t nil) nil "symbolic-ref" "HEAD")))))
  84    (if (string-match "^\\(refs/heads/\\)?\\(.+\\)$" str)
  85        (match-string 2 str)
  86      str)))
  87
  88(defun vc-git-revert (file &optional contents-done)
  89  "Revert FILE to the version stored in the git repository."
  90  (if contents-done
  91      (vc-git--run-command file "update-index" "--")
  92    (vc-git--run-command file "checkout" "HEAD")))
  93
  94(defun vc-git-checkout-model (file)
  95  'implicit)
  96
  97(defun vc-git-workfile-unchanged-p (file)
  98  (let ((sha1 (vc-git--run-command-string file "hash-object" "--"))
  99        (head (vc-git--run-command-string file "ls-tree" "-z" "HEAD" "--")))
 100    (and head
 101         (string-match "[0-7]\\{6\\} blob \\([0-9a-f]\\{40\\}\\)\t[^\0]+\0" head)
 102         (string= (car (split-string sha1 "\n")) (match-string 1 head)))))
 103
 104(defun vc-git-register (file &optional rev comment)
 105  "Register FILE into the git version-control system."
 106  (vc-git--run-command file "update-index" "--add" "--"))
 107
 108(defun vc-git-print-log (file &optional buffer)
 109  (let ((name (file-relative-name file))
 110        (coding-system-for-read git-commits-coding-system))
 111    (vc-do-command buffer 'async "git" name "rev-list" "--pretty" "HEAD" "--")))
 112
 113(defun vc-git-diff (file &optional rev1 rev2 buffer)
 114  (let ((name (file-relative-name file))
 115        (buf (or buffer "*vc-diff*")))
 116    (if (and rev1 rev2)
 117        (vc-do-command buf 0 "git" name "diff-tree" "-p" rev1 rev2 "--")
 118      (vc-do-command buf 0 "git" name "diff-index" "-p" (or rev1 "HEAD") "--"))
 119    ; git-diff-index doesn't set exit status like diff does
 120    (if (vc-git-workfile-unchanged-p file) 0 1)))
 121
 122(defun vc-git-checkin (file rev comment)
 123  (let ((coding-system-for-write git-commits-coding-system))
 124    (vc-git--run-command file "commit" "-m" comment "--only" "--")))
 125
 126(defun vc-git-checkout (file &optional editable rev destfile)
 127  (if destfile
 128      (let ((mybuff (get-buffer-create "vc-git-checkout-tmp")))
 129        (let ((rv
 130               (vc-git--run-command-out
 131                mybuff "cat-file" "blob"
 132                (concat (or rev "HEAD")
 133                        ":"
 134                        (let ((output (vc-git--run-command-string
 135                                       (file-relative-name file)
 136                                       "ls-files" "--full-name")))
 137                          (string-match "\\(.*\\)" output)
 138                          (match-string 1 output))
 139                        )))
 140              )
 141          (if rv
 142              (save-current-buffer
 143                (set-buffer mybuff)
 144                (set-visited-file-name destfile t)
 145                (save-buffer)
 146                )
 147            rv)))
 148    (vc-git--run-command file "checkout" (or rev "HEAD"))))
 149
 150(defun vc-git-annotate-command (file buf &optional rev)
 151  ; FIXME: rev is ignored
 152  (let ((name (file-relative-name file)))
 153    (call-process "git" nil buf nil "blame" name)))
 154
 155(defun vc-git-annotate-time ()
 156  (and (re-search-forward "[0-9a-f]+ (.* \\([0-9]+\\)-\\([0-9]+\\)-\\([0-9]+\\) \\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\) \\([-+0-9]+\\) +[0-9]+)" nil t)
 157       (vc-annotate-convert-time
 158        (apply #'encode-time (mapcar (lambda (match) (string-to-number (match-string match))) '(6 5 4 3 2 1 7))))))
 159
 160;; Not really useful since we can't do anything with the revision yet
 161;;(defun vc-annotate-extract-revision-at-line ()
 162;;  (save-excursion
 163;;    (move-beginning-of-line 1)
 164;;    (and (looking-at "[0-9a-f]+")
 165;;         (buffer-substring (match-beginning 0) (match-end 0)))))
 166
 167(provide 'vc-git)