all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* [PATCH] Add smerge-mode, conflicted-files support to vc-git.
@ 2014-01-11  1:14 Rüdiger Sonderfeld
  2014-01-12  2:52 ` Stefan Monnier
  0 siblings, 1 reply; 3+ messages in thread
From: Rüdiger Sonderfeld @ 2014-01-11  1:14 UTC (permalink / raw)
  To: emacs-devel

I've implemented `smerge-mode' and `vc-git-conflicted-files' support.
This was requested during the current discussion about moving Emacs to
git[1].  I'm not very familiar with the internals of vc.el and I might
have overlooked some git corner cases.  This patch results in one call
to `git status' per file.  Which might be a problem.  There was a
discussion about adding a config var for this.

There should also be a `vc-git-resolve-when-done' function added.

[1] http://lists.gnu.org/archive/html/emacs-devel/2014-01/msg00509.html

* lisp/vc/vc-git.el (vc-git-find-file-hook): New function.
(vc-git-conflicted-files): New function.

---
 lisp/ChangeLog    |  5 +++++
 lisp/vc/vc-git.el | 32 +++++++++++++++++++++++++++++++-
 2 files changed, 36 insertions(+), 1 deletion(-)

diff --git a/lisp/ChangeLog b/lisp/ChangeLog
index d5104d3..9a4a0d9 100644
--- a/lisp/ChangeLog
+++ b/lisp/ChangeLog
@@ -1,3 +1,8 @@
+2014-01-11  Rüdiger Sonderfeld  <ruediger@c-plusplus.de>
+
+	* vc/vc-git.el (vc-git-find-file-hook): New function.
+	(vc-git-conflicted-files): New function.
+
 2014-01-10  Eric S. Raymond  <esr@thyrsus.com>
 
 	* version.el (emacs-bzr-get-version): Restore compatibilty with
diff --git a/lisp/vc/vc-git.el b/lisp/vc/vc-git.el
index 6706300..369a546 100644
--- a/lisp/vc/vc-git.el
+++ b/lisp/vc/vc-git.el
@@ -101,7 +101,7 @@
 ;; - clear-headers ()                              NOT NEEDED
 ;; - delete-file (file)                            OK
 ;; - rename-file (old new)                         OK
-;; - find-file-hook ()                             NOT NEEDED
+;; - find-file-hook ()                             OK
 
 ;;; Code:
 
@@ -769,6 +769,36 @@ (defun vc-git-merge-branch ()
     (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 (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)))))))
+
+;; Inspired by `vc-hg-find-file-hook'.
+(defun vc-git-find-file-hook ()
+  "Activate `smerge-mode' if there is a conflict."
+  (when (and buffer-file-name
+             (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)
+    ;;TODO (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")
-- 
1.8.5.2




^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] Add smerge-mode, conflicted-files support to vc-git.
  2014-01-11  1:14 [PATCH] Add smerge-mode, conflicted-files support to vc-git Rüdiger Sonderfeld
@ 2014-01-12  2:52 ` Stefan Monnier
  2014-01-13 12:31   ` Rüdiger Sonderfeld
  0 siblings, 1 reply; 3+ messages in thread
From: Stefan Monnier @ 2014-01-12  2:52 UTC (permalink / raw)
  To: Rüdiger Sonderfeld; +Cc: emacs-devel

> +(defun vc-git-find-file-hook ()
> +  "Activate `smerge-mode' if there is a conflict."
> +  (when (and buffer-file-name
> +             (vc-git-conflicted-files buffer-file-name)
> +             (save-excursion
> +               (goto-char (point-min))
> +               (re-search-forward "^<<<<<<< " nil 'noerror)))

This is the main problem, of course.  It means running git twice per
file rather than once.  We could probably improve this by only calling
vc-git-conflicted-files if the file is under Git's control and only if
it's locally modified.  If you can think of some other check we could do
to call the file even less frequently, that'd be even better.

Also, I think the check shouldn't be in find-file-hook but in
vc-git-state (so as to return `conflicted' state).

I haven't looked in detail at the patch but did notice also that
vc-git-conflicted-files is documented to take a `directory' as argument
whereas here you call it with a file name.


        Stefan



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] Add smerge-mode, conflicted-files support to vc-git.
  2014-01-12  2:52 ` Stefan Monnier
@ 2014-01-13 12:31   ` Rüdiger Sonderfeld
  0 siblings, 0 replies; 3+ messages in thread
From: Rüdiger Sonderfeld @ 2014-01-13 12:31 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: emacs-devel

On Saturday 11 January 2014 21:52:23 Stefan Monnier wrote:
> This is the main problem, of course.  It means running git twice per
> file rather than once.  We could probably improve this by only calling
> vc-git-conflicted-files if the file is under Git's control and only if
> it's locally modified.  If you can think of some other check we could do
> to call the file even less frequently, that'd be even better.
>
> Also, I think the check shouldn't be in find-file-hook but in
> vc-git-state (so as to return `conflicted' state).

Yes, `vc-git-state' should indicate conflicts.  Right now it calls `git diff-
index' and treats U as edit instead of `conflicted'.  I'm not very familiar 
with the details of `vc-git' and therefore am not sure if we can simply change 
it.

The hg implementation lacks a `conflicted' state as well and simply checks if 
the file is edited and looks for the merge markers.  Maybe that would be 
enough for git as well.

> I haven't looked in detail at the patch but did notice also that
> vc-git-conflicted-files is documented to take a `directory' as argument
> whereas here you call it with a file name.

Yes, `vc-conflicted-files' is documented to take a `directory'.  But `vc-git-
conflicted-files' should work with a `file name' argument.  I guess this 
should be documented.

Regards,
Rüdiger




^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2014-01-13 12:31 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-01-11  1:14 [PATCH] Add smerge-mode, conflicted-files support to vc-git Rüdiger Sonderfeld
2014-01-12  2:52 ` Stefan Monnier
2014-01-13 12:31   ` Rüdiger Sonderfeld

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.