Hello,

I got tired of always doing M-x magit-status and then nagivating to the
same repositories over and over again.   All of my git repositories are under 
the same directories, so I came up with the following code to utilize ido's 
selection mechanisms:

--------------------------------------------------

(defun is-git-repo (dir)
  (file-exists-p (expand-file-name ".git" dir)))

(defun find-git-repos (dir)
  (when (file-directory-p dir)
    (or (char-equal ?/ (aref dir (1- (length dir))))
          (setq dir (file-name-as-directory dir)))
    (if (is-git-repo dir) dir
        (let ((files (directory-files dir t nil t))
              fullname file)
          (eshell-flatten-list (remove-if
                                'null
                                (mapcar
                                 (lambda (file)
                                   (unless
                                       (or
                                        (string= (substring file -3) "/..")
                                        (string= (substring file -2) "/."))
                                     (find-git-repos file)))
                                 files)))))))

(defun magit-status-repos (arg)
  "Provides, via ido, a list of repos to choose from to display their status 
buffer.
When called normally, the repo directory that the current buffer is in is 
selected
by default. If called with the universal argument, none are selected."
  (interactive "P")
  ;; my-repo-dirs is a list of directories containing repos underneath them
  (let* ((repo-dirs (eshell-flatten-list (mapcar 'find-git-repos my-repo-dirs)))
           (buffer-dir (when (not (null buffer-file-name))
                         (expand-file-name (if (file-directory-p 
buffer-file-name)
                                               buffer-file-name
                                             (file-name-directory 
buffer-file-name)))))
           (this-repo-dir (when (and
                                 (not (null buffer-dir))
                                 (null arg))
                            (file-name-as-directory (magit-get-top-dir 
buffer-dir)))))
    (magit-status (ido-completing-read "Git repository? " repo-dirs nil nil 
this-repo-dir))))

(global-set-key "\C-x\C-g" 'magit-status-repos)

------------------------------------------------------

It uses features from ido and eshell.

All you have to do to utilize this is set my-repo-dirs to be a list of the 
directories under which you have your git repos, like this:

(setq my-repo-dirs '("/home/nafai/Projects" "/home/nafai/Source"))

I don't know that this belongs in magit proper, but I do find it as a useful 
wrapper 
around magit.

A current limitation of this is that it won't find repos that are submodules of 
another.

If anyone has a better way of doing this or any improvements, please share!

-----
Travis B. Hartwell
Software Toolsmith

Blog:
http://www.travishartwell.net/blog

Where to find me:
http://www.travishartwell.net/blog/static/where_to_find_me

--~--~---------~--~----~------------~-------~--~----~
-- 
http://groups.google.com/group/magit
-~----------~----~----~----~------~----~------~--~---

Reply via email to