Source

js-elisp / js-cr.el

Full commit

;;----------------------------------------------------------------------------;;
;; code review tools                                                          ;;
;;----------------------------------------------------------------------------;;

(defvar cr-command "cr")

(defun get-hg-root-or-current-dir ()
  (or
   (locate-dominating-file default-directory ".hg")
   default-directory))

(defun pre-get-projections ()
  (let* ((l (directory-files (expand-file-name ".projections/build-info/"
                                               (get-hg-root-or-current-dir))))
         (l (delete ".." (delete "." l))))
    (mapconcat 'identity
               (completing-read-multiple
                "Comma-separated projections (or empty for entire hg repo): " l)
               ",")))

(defun get-projections ()
  (let ((projs (pre-get-projections)))
    (if (equal projs "")
        " "
      (concat "-p " projs))))

(defun cr-todo (id)
  (interactive "sRequest Id (and any other args): ")
  (let ((new-buff (generate-new-buffer (concat "*cr-todo-" id "*")))
        )
    (set-buffer new-buff)
    (setq default-directory (get-hg-root-or-current-dir))
    (shell-command (concat cr-command " todo " id) new-buff)
    (grep-mode)
    )
  )

;; I think the following functions only works for old-style cr
(defun cr-unassigned ()
  "List XCRs by you and unassigned CRs that should be done now."
  (interactive)
  (let ((projections (get-projections)))
    (grep (concat cr-command " grep " projections " -fullpath -filter "
                  "'(or "
                  "   (and (reported_by " (user-login-name) ") xcr)"
                  "   (and (date lte " (format-time-string "%Y%m") ")"
                  "        unassigned))'"
                  ))))

(defun cr-todos-helper (time)
  (let ((projections (get-projections)))
    (grep (concat cr-command " grep " projections " -fullpath -filter '(or "
                  "(and (reported_by " (user-login-name) ") xcr)"
                  "(and (date lte " time ")"
                  "     (or (and (reported_by " (user-login-name)") unassigned)"
                  "         (and (not xcr) (assigned_to " (user-login-name)")))))'"))))

;; CR moconnor for smclaughlin: I think this is out-of-date.  At
;; least, it doesn't work for me now.
(defun cr-todos ()
  "List XCRs by you and CRs for you that should be done now."
  (interactive)
  (cr-todos-helper (format-time-string "%Y%m")))

(defun cr-todos-undated ()
  "List XCRs by you and CRs for you that are undated (or dated in the distant past)."
  (interactive)
  (cr-todos-helper "201001"))

(defun cr-todos-all ()
  "List XCRs and CRs by and for anyone that should be done now."
  (interactive)
  (let ((projections (get-projections)))
    (grep (concat cr-command " grep " projections " -fullpath -filter "
                  "'(or xcr (date lte " (format-time-string "%Y%m") "))'"))))
;; bound to \C-cr in dot-emacs.el
(defun cr-dwim ()               ; dwim = do what I mean
  "Add a code review comment starting on current line or cycle CR/CRvYYYYMM/XCR"
  (interactive)
  (comment-dwim nil)
  (cond ((looking-at "CR ")
         (replace-match
          ;; inserts CRvYYYYMM where date will be between today and a year from now
          (concat "CRv" (format-time-string
                         "%Y%m" (time-add (current-time)
                                          (encode-time 0 0 0 0 (+ (random 12) 3)
                                                       1970)))
                  " ")
          t t))
        ((looking-at "CRv[0-9]+ ")
         (replace-match "XCR " t t))
        ((looking-at "XCR ")
         (replace-match "CR " t t))
        (t
         (insert "CR " (user-login-name) ": ")
         (backward-word)))
  (forward-word)
  (forward-char 2))

(provide 'js-cr)