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 "") "all" projs)))

(defun cr-todo (id)
  "See your todos for a given request"
  (interactive "sRequest Id (and any other args): ")
  ;; This next line is an attempt to fix a bug in emacs where it
  ;; appends /dev/null to the command line even though we have this
  ;; variable set to false.  Perhaps setting it to false in this weird
  ;; manner will make it "more false".
  (grep-apply-setting 'grep-use-null-device nil)
  (grep (concat cr-command " todo " id " -fullpath -local")))

(defun cr-grep ()
  "See your todos in projects or repo, even if there is no request"
  (interactive)
  (let ((projections (get-projections))
        (show-unassigned (y-or-n-p "Show unassigned? "))
        (show-all-dates  (y-or-n-p "Show future-dated CRs? "))
        (show-all-dirs   (not (y-or-n-p "Restrict to this directory? ")))
        )
    (grep (concat cr-command " grep -fullpath -projections " projections
                  (if show-unassigned (concat " -unassigned-owner " (user-login-name)) " ")
                  (if show-all-dates "" (concat " -cutoff-date " (format-time-string "%Y%m")))
                  (if show-all-dirs (concat " -cwd " (get-hg-root-or-current-dir)) " ")
                  ))))

;; 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)