]> code.delx.au - gnu-emacs/blobdiff - lisp/vc/vc-git.el
Merge branch 'emacs-24'.
[gnu-emacs] / lisp / vc / vc-git.el
index ae6b13a6c6097d4b0451e9fd513548cc6b1096fd..3a943132435d20d025075fe89fdfbff9ea367028 100644 (file)
@@ -66,7 +66,7 @@
 ;; - unregister (file)                             OK
 ;; * checkin (files rev comment)                   OK
 ;; * find-revision (file rev buffer)               OK
-;; * checkout (file &optional editable rev)        OK
+;; * checkout (file &optional rev)                 OK
 ;; * revert (file &optional contents-done)         OK
 ;; - rollback (files)                              COULD BE SUPPORTED
 ;; - merge (file rev1 rev2)                   It would be possible to merge
 ;; - clear-headers ()                              NOT NEEDED
 ;; - delete-file (file)                            OK
 ;; - rename-file (old new)                         OK
-;; - find-file-hook ()                             NOT NEEDED
+;; - find-file-hook ()                             OK
+;; - conflicted-files                              OK
 
 ;;; Code:
 
@@ -664,7 +665,7 @@ If toggling on, also insert its message into the buffer."
   "Major mode for editing Git log messages.
 It is based on `log-edit-mode', and has Git-specific extensions.")
 
-(defun vc-git-checkin (files _rev comment)
+(defun vc-git-checkin (files comment)
   (let* ((file1 (or (car files) default-directory))
          (root (vc-git-root file1))
          (default-directory (expand-file-name root))
@@ -708,7 +709,7 @@ It is based on `log-edit-mode', and has Git-specific extensions.")
   (expand-file-name ".gitignore"
                    (vc-git-root file)))
 
-(defun vc-git-checkout (file &optional _editable rev)
+(defun vc-git-checkout (file &optional rev)
   (vc-git-command nil 0 file "checkout" (or rev "HEAD")))
 
 (defun vc-git-revert (file &optional contents-done)
@@ -769,6 +770,47 @@ This prompts for a branch to merge from."
     (with-current-buffer buffer (vc-run-delayed (vc-compilation-mode 'git)))
     (vc-set-async-update buffer)))
 
+(defun vc-git-conflicted-files (directory)
+  "Return the list of files with conflicts in DIRECTORY."
+  (let* ((status
+          (vc-git--run-command-string directory "status" "--porcelain" "--"))
+         (lines (when status (split-string status "\n" 'omit-nulls)))
+         files)
+    (dolist (line lines files)
+      (when (string-match "\\([ MADRCU?!][ MADRCU?!]\\) \\(.+\\)\\(?: -> \\(.+\\)\\)?"
+                          line)
+        (let ((state (match-string 1 line))
+              (file (match-string 2 line)))
+          ;; See git-status(1).
+          (when (member state '("AU" "UD" "UA" ;; "DD"
+                                "DU" "AA" "UU"))
+            (push file files)))))))
+
+(defun vc-git-resolve-when-done ()
+  "Call \"git add\" if the conflict markers have been removed."
+  (save-excursion
+    (goto-char (point-min))
+    (unless (re-search-forward "^<<<<<<< " nil t)
+      (vc-git-command nil 0 buffer-file-name "add")
+      ;; Remove the hook so that it is not called multiple times.
+      (remove-hook 'after-save-hook 'vc-git-resolve-when-done t))))
+
+(defun vc-git-find-file-hook ()
+  "Activate `smerge-mode' if there is a conflict."
+  (when (and buffer-file-name
+             ;; FIXME
+             ;; 1) the net result is to call git twice per file.
+             ;; 2) v-g-c-f is documented to take a directory.
+             ;; http://lists.gnu.org/archive/html/emacs-devel/2014-01/msg01126.html
+             (vc-git-conflicted-files buffer-file-name)
+             (save-excursion
+               (goto-char (point-min))
+               (re-search-forward "^<<<<<<< " nil 'noerror)))
+    (vc-file-setprop buffer-file-name 'vc-state 'conflict)
+    (smerge-start-session)
+    (add-hook 'after-save-hook 'vc-git-resolve-when-done nil 'local)
+    (message "There are unresolved conflicts in this file")))
+
 ;;; HISTORY FUNCTIONS
 
 (autoload 'vc-setup-buffer "vc-dispatcher")
@@ -842,7 +884,7 @@ If LIMIT is non-nil, show no more than this many entries."
        (if (not (eq vc-log-view-type 'long))
           (cadr vc-git-root-log-format)
         "^commit *\\([0-9a-z]+\\)"))
-  ;; Allow expanding short log entries
+  ;; Allow expanding short log entries.
   (when (eq vc-log-view-type 'short)
     (setq truncate-lines t)
     (set (make-local-variable 'log-view-expanded-log-entry-function)
@@ -899,16 +941,71 @@ or BRANCH^ (where \"^\" can be repeated)."
       (indent-region (point-min) (point-max) 2)
       (buffer-string))))
 
+
+(defun vc-git-region-history (file buffer lfrom lto)
+  (vc-git-command buffer 'async nil "log" "-p" ;"--follow" ;FIXME: not supported?
+                  (format "-L%d,%d:%s" lfrom lto (file-relative-name file))))
+
+(require 'diff-mode)
+
+(defvar vc-git-region-history-mode-map
+  (let ((map (make-composed-keymap
+              nil (make-composed-keymap
+                   (list diff-mode-map vc-git-log-view-mode-map)))))
+    map))
+
+(defvar vc-git--log-view-long-font-lock-keywords nil)
+(defvar font-lock-keywords)
+(defvar vc-git-region-history-font-lock-keywords
+  `((vc-git-region-history-font-lock)))
+
+(defun vc-git-region-history-font-lock (limit)
+  (let ((in-diff (save-excursion
+                   (beginning-of-line)
+                   (or (looking-at "^\\(?:diff\\|commit\\)\\>")
+                       (re-search-backward "^\\(?:diff\\|commit\\)\\>" nil t))
+                   (eq ?d (char-after (match-beginning 0))))))
+    (while
+        (let ((end (save-excursion
+                     (if (re-search-forward "\n\\(diff\\|commit\\)\\>"
+                                            limit t)
+                         (match-beginning 1)
+                       limit))))
+          (let ((font-lock-keywords (if in-diff diff-font-lock-keywords
+                                      vc-git--log-view-long-font-lock-keywords)))
+            (font-lock-fontify-keywords-region (point) end))
+          (goto-char end)
+          (prog1 (< (point) limit)
+            (setq in-diff (eq ?d (char-after))))))
+    nil))
+
+(define-derived-mode vc-git-region-history-mode
+    vc-git-log-view-mode "Git-Region-History"
+  "Major mode to browse Git's \"log -p\" output."
+  (setq-local vc-git--log-view-long-font-lock-keywords
+              log-view-font-lock-keywords)
+  (setq-local font-lock-defaults
+              (cons 'vc-git-region-history-font-lock-keywords
+                    (cdr font-lock-defaults))))
+
+
 (autoload 'vc-switches "vc")
 
 (defun vc-git-diff (files &optional rev1 rev2 buffer)
   "Get a difference report using Git between two revisions of FILES."
   (let (process-file-side-effects)
-    (apply #'vc-git-command (or buffer "*vc-diff*") 1 files
-          (if (and rev1 rev2) "diff-tree" "diff-index")
-          "--exit-code"
-          (append (vc-switches 'git 'diff)
-                  (list "-p" (or rev1 "HEAD") rev2 "--")))))
+    (if vc-git-diff-switches
+        (apply #'vc-git-command (or buffer "*vc-diff*") 1 files
+               (if (and rev1 rev2) "diff-tree" "diff-index")
+               "--exit-code"
+               (append (vc-switches 'git 'diff)
+                       (list "-p" (or rev1 "HEAD") rev2 "--")))
+      (vc-git-command (or buffer "*vc-diff*") 1 files
+                      "difftool" "--exit-code" "--no-prompt" "-x"
+                      (concat "diff "
+                              (mapconcat 'identity
+                                         (vc-switches nil 'diff) " "))
+                      (or rev1 "HEAD") rev2 "--"))))
 
 (defun vc-git-revision-table (_files)
   ;; What about `files'?!?  --Stef