Skip to content

Commit

Permalink
add code for identifying profiles from remotes
Browse files Browse the repository at this point in the history
  • Loading branch information
sigma committed Mar 18, 2013
1 parent 77db4cd commit 8fa4f9b
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions gh-profile.el
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,46 @@
(eval-when-compile
(require 'cl))

(require 'rx)
(require 'url-parse)

(defgroup gh-profile nil
"Github profile."
:group 'gh)

(defcustom gh-profile-alist '(("github" :url "https://api.github.com"))
(defun gh-profile-remote-regexp (domain)
(eval
`(rx bol (or ,(concat "git@" domain ":")
(and (or "git" "ssh" "http" "https") "://"
(* nonl) (? "@") ,domain "/"))
(and (group (* nonl)) "/" (group (* nonl))) (? ".git"))))

(defcustom gh-profile-alist `(("github"
:url "https://api.github.com"
:remote-regexp
,(gh-profile-remote-regexp "github.com")))
"List of profiles for Github access. List every Github
Enterprise server and/or Github accounts you have access
to here."
:type '(alist :key-type string
:value-type (plist :key-type (choice (const :url)
(const :username)
(const :password)
(const :token))
(const :token)
(const :remote-regexp))
:value-type string))
:group 'gh-profile)

(defun gh-profile-get-remote-regexp (profile)
(let* ((profile-plist (cdr (assoc profile gh-profile-alist)))
(regexp (plist-get profile-plist :remote-regexp)))
(if regexp
regexp
;; try to guess remote format (just use the hostname)
(let* ((url (url-generic-parse-url (plist-get profile-plist :url)))
(host (url-host url)))
(gh-profile-remote-regexp host)))))

(defcustom gh-profile-default-profile "github"
"Default profile. This needs to be a key present in
`gh-profile-alist'"
Expand All @@ -69,5 +93,11 @@ to here."
(completing-read "Github profile: " profiles)
(car profiles))))

(defun gh-profile-get-remote-profile (remote-url)
(loop for (id . props) in gh-profile-alist
if (string-match (gh-profile-get-remote-regexp id)
remote-url)
return id))

(provide 'gh-profile)
;;; gh-profile.el ends here

0 comments on commit 8fa4f9b

Please sign in to comment.