chiark / gitweb /
Emacs mode: automatically cd up to root of worktree
authorKarl Hasselström <kha@treskal.com>
Wed, 14 May 2008 00:04:32 +0000 (02:04 +0200)
committerKarl Hasselström <kha@treskal.com>
Wed, 14 May 2008 00:04:32 +0000 (02:04 +0200)
git's emacs mode automatically finds the root of the worktree, so that
the user doesn't have to. Teach StGit's emacs mode the same trick by
borrowing the git-get-top-dir function from git.

The borrowed code was written by Alexandre Julliard <julliard@winehq.org>.

Signed-off-by: Karl Hasselström <kha@treskal.com>
contrib/stgit.el

index 339ef13eb950de0cffc0e734cfea7ddaee514a28..2a6fee3becf56aaf598faab77a12e41f67f279b6 100644 (file)
 (defun stgit (dir)
   "Manage stgit patches"
   (interactive "DDirectory: \n")
-  (switch-to-stgit-buffer dir)
+  (switch-to-stgit-buffer (git-get-top-dir dir))
   (stgit-refresh))
 
+(defun git-get-top-dir (dir)
+  "Retrieve the top-level directory of a git tree."
+  (let ((cdup (with-output-to-string
+                (with-current-buffer standard-output
+                  (cd dir)
+                  (unless (eq 0 (call-process "git" nil t nil
+                                              "rev-parse" "--show-cdup"))
+                    (error "cannot find top-level git tree for %s." dir))))))
+    (expand-file-name (concat (file-name-as-directory dir)
+                              (car (split-string cdup "\n"))))))
+
 (defun switch-to-stgit-buffer (dir)
   "Switch to a (possibly new) buffer displaying StGit patches for DIR"
   (setq dir (file-name-as-directory dir))