Skip to content

Instantly share code, notes, and snippets.

@travisbhartwell
Created January 27, 2009 19:29
Show Gist options
  • Save travisbhartwell/53495 to your computer and use it in GitHub Desktop.
Save travisbhartwell/53495 to your computer and use it in GitHub Desktop.
;; TODO: Clean this up a bit, but at least it works
;; It's actually broken, if two different subtrees of the directory both
;; have repos in their sub-directories, it gets set to nil
(defun find-git-repos-internal (dir found-dirs)
(when (file-directory-p dir)
(or (char-equal ?/ (aref dir(1- (length dir))))
(setq dir (file-name-as-directory dir)))
(let ((lst (directory-files dir nil nil t))
fullname file)
(while lst
(setq file (car lst))
(setq lst (cdr lst))
(setq fullname (expand-file-name file dir))
(cond ((member file '("." "..")))
((file-exists-p (expand-file-name ".git" fullname))
(add-to-list 'found-dirs (file-name-as-directory fullname)))
(t
(find-git-repos-internal fullname found-dirs))))))
found-dirs)
(defun find-git-repos (dir)
(find-git-repos-internal dir '()))
(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")
;; tbh-repo-dirs is a list of directories containing repos underneath them
(let* ((repo-dirs (eshell-flatten-list (mapcar 'find-git-repos tbh-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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment