Skip to content

A peer-to-peer stack for code collaboration

10.0 KB
Raw Blame
#!/usr/bin/env radicle

(load! (find-module-file! "prelude.rad"))
(load! (find-module-file! "monadic/patch.rad"))
(load! (find-module-file! "prelude/io-utils.rad"))
(load! (find-module-file! "monadic/project.rad"))

(file-module! "prelude/error-messages.rad")
(file-module! "monadic/items.rad")

(import prelude/error-messages :as 'error)
(import monadic/items :as 'items)

(import prelude/validation :as 'validation)

(def machine-name (fn [] (get-rsm-of-type! :rad-patch)))

(def valid-states
  ["pending" "retracted" "accepted" "rejected"])

(def status-opts ["-s" "--state" "--filter-by-state" ])

(def list-opts-help
  (string-append
    "[("
    (string/intercalate " | " status-opts)
    ") <state>] [--fancy]"))

(def help
  (string-append
  "rad patch - Radicle Patch CLI

   Usage:
        rad patch list " list-opts-help "
        rad patch propose <commit>
        rad patch [show | retract | accept | reject | checkout | mark-read | mark-unread] <patch-number>
        rad patch comment <patch-number> <comment>
        rad patch all-read
        rad patch help

     list         - Lists all patches
                    The list can be filtered by state:
                      " (string/intercalate ", " valid-states) "
                    The flags for filtering by state:
                      " (string/intercalate ", " status-opts) "
                    Filtering by multiple states is possible via '-s <state1> -s
                    <state2> ...'.
                    If the option '--fancy' is appended, the list is opened via
                    fzf (note that fzf has to be installed).
                    If the option '--unread' is appended, only unread patches
                    are listed.
                    Unread patches are marked with a * after the timestamp.
     propose      - Create a new patch from a single commit
     show         - Show a patch
     comment      - Add a comment to a patch
     retract      - Retract your own patch
                    This command is restricted to the creator of the patch or the
                    maintainer of the project.
     accept       - Accept a patch
                    Automatically merges to master and pushes to origin. This
                    command is restricted to the maintainer of the project.
     checkout     - Checkout a patch locally
                    Creates a new branch 'patch/<patch-number>' and applies the
                    patch to it.
     reject       - Reject a patch
                    This command is restricted to the maintainer of the project.
     mark-read    - Mark patch as read
     mark-unread  - Mark patch as unread
     all-read     - Mark all patches as read
     help         - Print this help and exit
  "))

(def cmd-parse-failure
  (fn [error]
    (parse-failure error help)))

;; git interactions

(def verify-commit
  (fn [commit]
    (match (process-with-stdout-stderr-exitcode! "git" ["show" commit] "")
      [_ _ :ok] commit
      _         (do
                  (put-str! (error/unknown-commit commit))
                  (exit! 1)))))

(def get-patch!
  (fn [commit]
    (string/unlines (process-with-stdout! "git" ["format-patch" "-1" commit "--stdout"] ""))))

(def get-title!
  (fn [commit]
    (get-git-commit-data! "%s" commit)))

(def get-description!
  (fn [commit]
    (get-git-commit-data! "%b" commit)))

(def get-author-name!
  (fn [commit]
    (get-git-commit-data! "%aN" commit)))

(def get-short-hash!
  (fn [commit]
    (string/unlines (process-with-stdout! "git" ["rev-parse" "--short" commit] ""))))

(def get-commit-parent!
  (fn [commit]
    (string/unlines (process-with-stdout! "git" ["rev-parse" (string-append commit "^")] ""))))

(def get-head-origin-master!
  (fn []
    (string/unlines (process-with-stdout! "git" ["rev-parse" "origin/master"] ""))))

(def apply-patch!
  "Given a patch, apply it to the current branch"
  (fn [patch msg]
    (def file (unlines (shell-with-stdout! "mktemp" "")))
    (put-str! file)
    (write-file! file patch)
    (process-git-with-exit! ["am" file] msg)))

(def checkout-patch!
  "Creates a new branch patch/<PATCH-ID> based on the PATCH, and switches to it."
  (fn [machine patch-no]
    (def patch (items/verify-item-number patch-no (list-patches machine) :patch))
    (def patch-branch (string-append "patch/" (show patch-no)))
    (process-git-with-exit!
      ["checkout" "-b" patch-branch]
      (error/checkout-new-branch-failure patch-branch))
    (apply-patch!
      (lookup :patch patch)
      (error/applying-patch-failure))))

(def push-patch!
  "Merges the PATCH to master, and pushes the changes."
  (fn [machine patch-no]
    (def patch (items/verify-item-number patch-no (list-patches machine) :patch))
    (process-git-with-exit!
      ["checkout" "master"]
      (error/checkout-master-failure))
    (apply-patch!
      (lookup :patch patch)
      (error/applying-accepted-patch-failure))
    (process-git-with-exit!
      ["push"]
      (error/push-patch-failure))))


(def verify-patch
  "Verify that the patch is on top of origin/master"
  (fn [commit]
    (if (eq? (get-commit-parent! commit) (get-head-origin-master!))
      commit
      (do
        (put-str! (error/parent-commit-not-master commit))
        (exit! 1)))))

(def new-patch!
  "Creates a new patch on `machine` with a patch from the given `commit`."
  (fn [machine commit]
    (verify-commit commit)
    (verify-patch commit)
    (def patch (get-patch! commit))
    (def author-name (get-author-name! commit))
    (def short-hash (get-short-hash! commit))
    (def title (get-title! commit))
    (def description (get-description! commit))
    (def patch {:short-hash    short-hash
               :title         title
               :description   description
               :author-name   author-name
               :patch         patch})
    (match (simple-create-patch! machine patch)
           ['n] (do (put-str! (string-append "Proposing patch #" (show n)
                                             " to " machine))
                    (newness/mark-read! machine n))
           _    (put-str! (error/no-number-returned :patch)))))

;; showing patches

(def show-patch!
  "Shows a single PATCH `n`"
  (fn [machine n]
    (def patch (items/verify-item-number n (list-patches machine) :patch))
    (put-str! (items/pretty-item-view (items/enrich-item machine patch)))
    (newness/mark-read! machine n)))

(def edit-patch-state!
  "Edit the state of a PATCH. If the new state is `:accepted` the PATCH is then
  also merged to master and the changes pushed."
  (fn [machine n new-state]
    (def edit-state-with-feedback!
      (fn [message]
        (catch 'daemon-error
            (do (simple-edit-patch! machine n {:state new-state})
                (newness/mark-read! machine n))
          (fn [x]
            (put-str!
             (error/state-change-failure :patch (items/pretty-state new-state)))
            (exit! 1)))
        (put-str! message)))
    (items/verify-item-number n (list-patches machine) :patch)
    (if (eq? new-state :accepted)
      (match (push-patch! machine n)
             :ok (edit-state-with-feedback!
                  (string-append "Patch #" (show n) " has been " (items/pretty-state new-state) " and merged."))
             _   (exit! 1))
      (edit-state-with-feedback!
       (string-append "Patch #" (show n) " has been " (items/pretty-state new-state))))))

(def create-comment!
  "Add a new `comment` to PATCH."
  (fn [machine n comment]
    (items/verify-item-number n (list-patches machine) :patch)
    (simple-add-comment! machine n comment (get-git-username!))
    (put-str! (string-append "Added comment to patch #" (show n)))
    (newness/mark-read! machine n)))

(def add-comment-from-editor!
  "Add a new `comment` to PATCH opened via the list command with option `--fancy`."
  (fn [machine n added-comment]
    (simple-add-comment! machine n added-comment (get-git-username!))
    (string-append "Sent comment: " added-comment)))

(def list
  (fn [machine options]
    (def patches (values (list-patches machine)))
    (items/list-items machine options patches :patch add-comment-from-editor!)))

(def mark-all-read!
  (fn [machine]
    (map (fn [i] (newness/mark-read! machine (lookup :number i)))
         (values (list-patches machine)))))

(def cmd-options
  [
    { :key :state :type :multi-opt :options status-opts :possible-values valid-states :default ["pending"]}
    { :key :fancy :type :flag :options ["--fancy"] :default #f }
    { :key :unread :type :flag :options ["--unread"] :default #f }
  ])

(def /list-cmd
  (fn [opts]
    (/cmd-opts "list" opts cmd-options help)))

(def whole-patch-num
  (fn [action num-str f]
    (whole-num help :patch action num-str f)))

(def args (get-args!))

(machine/catch-daemon!
 (fn []
   (match args
          (/list-cmd 'options) (list (machine-name) options)
          (/cmd-1 "propose" 'commit help) (new-patch! (machine-name) commit)
          (/cmd-1 "checkout" 'n help)  (whole-patch-num "checkout" n (fn [n] (checkout-patch! (machine-name) n)))
          (/cmd-1 "show" 'n help)      (whole-patch-num "show" n (fn [n] (show-patch! (machine-name) n)))
          (/cmd-1 "mark-read" 'n help) (whole-patch-num "mark as read" n (fn [n] (newness/mark-read! (machine-name) n)))
          (/cmd-1 "mark-unread" 'n help) (whole-patch-num "mark as unread" n (fn [n] (newness/mark-unread! (machine-name) n)))
          (/cmd-1 "retract" 'n help)   (whole-patch-num "retract" n (fn [n] (edit-patch-state! (machine-name) n :retracted)))
          (/cmd-1 "accept" 'n help)    (whole-patch-num "accept" n (fn [n] (edit-patch-state! (machine-name) n :accepted)))
          (/cmd-1 "reject" 'n help)    (whole-patch-num "reject" n (fn [n] (edit-patch-state! (machine-name) n :rejected)))
          (/cmd-2 "comment" 'n 'comment help) (whole-patch-num "comment on" n (fn [n] (create-comment! (machine-name) n comment)))
          (/cmd-0 "create-machine" help) (new-patch-machine! (machine-name))
          (/cmd-0 "all-read" help) (mark-all-read! (machine-name))
          (/cmd-help) (put-str! help)
          (/cons 'cmd _) (cmd-parse-failure (error/unknown-command cmd))
          [] (put-str! help))))