From 774ab55e9ed91ac41ef694f20ac351a354e68dcb Mon Sep 17 00:00:00 2001 From: Tom Willemse Date: Mon, 2 Mar 2020 21:08:23 -0800 Subject: [PATCH] Add simplistic vc-p4-switch-client command --- p4-lowlevel.el | 41 +++++++++++++++++++++++++++++++++++++++++ vc-p4.el | 5 +++++ 2 files changed, 46 insertions(+) diff --git a/p4-lowlevel.el b/p4-lowlevel.el index c16a4cd..67e8f82 100644 --- a/p4-lowlevel.el +++ b/p4-lowlevel.el @@ -726,4 +726,45 @@ already exists." (setq default-directory caller-default-directory)) buf)) +(defun p4-lowlevel-local-clients () + (mapcar (lambda (client) (alist-get "client" client nil nil #'string=)) + (seq-filter (lambda (client) (string= (alist-get "Host" client nil nil #'string=) + (system-name))) + (p4-lowlevel-clients)))) + +(defun p4-lowlevel-clients () + "Get the clients available." + (let ((lines (p4-lowlevel-command-to-alist `("-z" "tag" "clients" "-u" ,(user-login-name))))) + (seq-reduce (lambda (acc itm) + (p4-lowlevel-clients-parse-line acc itm)) + lines nil))) + +(defun p4-lowlevel-clients-parse-line (acc line) + (pcase (car line) + ((pred (string= "info1")) + (let ((values (split-string (cdr line) " "))) + (pcase (car values) + ((pred (string= "client")) + (cons (list (cons (car values) (cadr values))) acc)) + ((or (pred (string= "Update")) + (pred (string= "Access")) + (pred (string= "Owner")) + (pred (string= "Options")) + (pred (string= "SubmitOptions")) + (pred (string= "LineEnd")) + (pred (string= "Root")) + (pred (string= "Host")) + (pred (string= "Stream")) + (pred (string= "Type")) + (pred (string= "Description"))) + (cons (cons (cons (car values) + (cadr values)) + (car acc)) + (cdr acc))) + (_ acc)))) + (`("exit" . ,result) + (if (not (= result 0)) + (error "Clients returned non-zero exit code."))) + (_ acc))) + (provide 'p4-lowlevel) diff --git a/vc-p4.el b/vc-p4.el index e7357fb..d08d94e 100644 --- a/vc-p4.el +++ b/vc-p4.el @@ -980,4 +980,9 @@ third subblock in each conflict block." The difference to vc-do-command is that this function always invokes `p4'." (apply 'vc-do-command buffer okstatus "p4" file flags)) +(defun vc-p4-switch-client (client) + (interactive + (list (completing-read "Client: " (p4-lowlevel-local-clients)))) + (p4-lowlevel-command-or-error `("set" ,(format "P4CLIENT=%s" client)))) + (provide 'vc-p4)