summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristophe Junke <christophe.junke@parrot.com>2018-04-13 12:03:58 +0200
committerMathieu Othacehe <mathieu.othacehe@parrot.com>2018-04-13 12:27:06 +0200
commit5fd0956db254e696271af3225eda5959b7bf0c4c (patch)
tree75dd6999bf7a9f9e2bbf46bc6095a2faae8fb2e7
parent2940dd590615b9ab6e6595c43add882ca807e576 (diff)
downloadparrot-5fd0956db254e696271af3225eda5959b7bf0c4c.tar.gz
Gerrit fetch projects and clone
-rw-r--r--parrot-gerrit.el105
1 files changed, 105 insertions, 0 deletions
diff --git a/parrot-gerrit.el b/parrot-gerrit.el
index cf2ecc7..a61601d 100644
--- a/parrot-gerrit.el
+++ b/parrot-gerrit.el
@@ -116,4 +116,109 @@
:max-action-columns 4
:default-action 'gerrit)
+
+;;
+;; GERRIT CLONE
+;;
+
+(defcustom gerrit-server "gerrit-doc.parrot.biz"
+ "Address of gerrit server")
+
+(defcustom gerrit-default-directory "~/projects/"
+ "Default target directory when cloning projects.")
+
+(defcustom gerrit-repo-default-branch "master" "")
+(defcustom gerrit-repo-default-manifest "default.xml" "")
+
+(defun gerrit-ensure-whole-regex (regex)
+ "Allow to match only part of a project name with gerrit ls-projects -r
+
+The ls-project commands assumes regexp matches the whole line,
+but usually regexp can match the middle of a name (that's why
+there ^ and $ anchors exist). This function adds the necessary .*
+before and after the given regexp, unless the regexp starts with
+an anchor."
+ (let ((regex (string-trim regex)))
+ (flet ((maybe-boundary (boundary-regex boundary)
+ (and (not (string-match-p boundary-regex regex))
+ boundary)))
+ (concat (maybe-boundary "^(*\\^" "^.*")
+ regex
+ (maybe-boundary "\\$)*$"
+ ;; avoid ugly double ".*"
+ (if (string-empty-p regex) "$" ".*$"))))))
+
+;; (defun tdd (fun test args-expected)
+;; (loop
+;; for (args expected) in args-expected
+;; for actual = (apply fun (if (listp args) args (list args)))
+;; unless (funcall test actual expected)
+;; do (warn "Test failed: %s with %s => %s but expected %s"
+;; fun args actual expected))
+;; (message "Success: %s" fun))
+
+;; (tdd 'gerrit-ensure-whole-regex
+;; 'string=
+;; '(("kiss" "^.*kiss.*$")
+;; ("" "^.*$")
+;; ("^kiss" "^kiss.*$")
+;; ("kiss$" "^.*kiss$")))
+
+(defun gerrit-fetch-projects (regex)
+ (interactive "MFilter by regex: ")
+ (message "Fetching project list...")
+ (process-lines "ssh"
+ gerrit-server
+ "gerrit"
+ "ls-projects"
+ "-r"
+ (gerrit-ensure-whole-regex regex)))
+
+;; FIXME: no completion
+(defun gerrit-repo-init-project (project &optional dirname branch manifest)
+ (check-type project string)
+ (let ((dirname (or dirname gerrit-default-directory))
+ (branch (or branch gerrit-repo-default-branch))
+ (manifest (or manifest gerrit-repo-default-manifest)))
+ (check-type dirname string)
+ (check-type branch string)
+ (check-type manifest string)
+ (make-directory dirname t)
+ (let ((default-directory dirname))
+ (shell-command (apply 'format
+ "repo init -u ssh://%s/%s -b %s -m %s &"
+ (mapcar 'shell-quote-argument
+ (list gerrit-server
+ project
+ branch
+ manifest)))
+ "*Repo*"
+ nil))))
+
+;;;###autoload
+(defun gerrit-clone-project (project)
+ (interactive (list (ido-completing-read "Select project: "
+ (call-interactively
+ 'gerrit-fetch-projects))))
+ (if (and (search "manifest" project)
+ (yes-or-no-p "Use repo init instead of git clone?"))
+ (gerrit-repo-init-project project)
+ (let ((default-directory
+ (expand-file-name gerrit-default-directory))
+ (url (format "ssh://%s/%s" gerrit-server project)))
+ (cond
+ ((fboundp 'magit-clone)
+ (let ((directory
+ (read-directory-name
+ "Clone to: " nil nil nil
+ (and (string-match "\\([^/:]+?\\)\\(/?\\.git\\)?$" url)
+ (match-string 1 url)))))
+ (magit-clone url (expand-file-name directory))))
+ (t
+ (Message "You should install magit")
+ (shell-command (format "git clone %s"
+ (shell-quote-argument url))
+ "*Gerrit Clone*"
+ "*Gerrit Errors*"))))))
+
(provide 'parrot-gerrit)