Add login commands

- Add command ‘vc-p4-logged-in-p’ to check if the user is currently logged in.

- Add command ‘vc-p4-login’ to log in if the user isn’t already logged in.

- Add better error reporting for the situation where a user isn’t logged in.
This commit is contained in:
Tom Willemse 2021-01-14 22:39:21 -08:00
parent 174054e58f
commit 23bacc0d4b
2 changed files with 32 additions and 0 deletions

View file

@ -64,6 +64,13 @@ a portion of the error string you wish to ignore."
:type '(repeat (string :tag "Error to Ignore")) :type '(repeat (string :tag "Error to Ignore"))
:group 'p4-lowlevel) :group 'p4-lowlevel)
(define-error 'p4-error
"A Perforce error occurred")
(define-error 'p4-not-logged-in-error
"Youre not logged in to Perforce"
'p4-error)
(defun p4-lowlevel-locate-p4 () (defun p4-lowlevel-locate-p4 ()
"Attempts to locate the p4 command. Used by `vc-p4-registered' to "Attempts to locate the p4 command. Used by `vc-p4-registered' to
avoid an error on systems on which the Perforce client is not installed." avoid an error on systems on which the Perforce client is not installed."
@ -294,6 +301,8 @@ rather than raising an error."
(setq errors (or (p4-lowlevel-errors output-alist) "Unknown error")) (setq errors (or (p4-lowlevel-errors output-alist) "Unknown error"))
(kill-buffer output-buffer) (kill-buffer output-buffer)
(or noerror (or noerror
(when (string-match (rx string-start "Perforce password (P4PASSWD) invalid or unset.") errors)
(signal 'p4-not-logged-in-error nil))
(if (not (string-match "\n" errors)) (if (not (string-match "\n" errors))
(error "P4 error: %s" errors) (error "P4 error: %s" errors)
(setq error-buffer (p4-lowlevel-get-buffer-create (setq error-buffer (p4-lowlevel-get-buffer-create
@ -772,4 +781,18 @@ already exists."
(error "Clients returned non-zero exit code."))) (error "Clients returned non-zero exit code.")))
(_ acc))) (_ acc)))
(cl-defun p4-lowlevel-login (&key password status)
"Call p4 login with arguments.
PASSWORD should be a string representing the password to use to
log in to Perforce. If STATUS is specified the status of the
current ticket is displayed (if there is one) instead."
(let* ((status-args (and status '("-s")))
(args (append '("login") status-args)))
(condition-case err
(with-temp-buffer
(when password (insert password))
(p4-lowlevel-command-or-error args (current-buffer)))
(p4-not-logged-in-error
(unless status (signal (car err) (cdr err)))))))
(provide 'p4-lowlevel) (provide 'p4-lowlevel)

View file

@ -980,4 +980,13 @@ The difference to vc-do-command is that this function always invokes `p4'."
(list (completing-read "Client: " (p4-lowlevel-local-clients)))) (list (completing-read "Client: " (p4-lowlevel-local-clients))))
(p4-lowlevel-command-or-error `("set" ,(format "P4CLIENT=%s" client)))) (p4-lowlevel-command-or-error `("set" ,(format "P4CLIENT=%s" client))))
(defun vc-p4-login (password)
"Call the p4 login command user PASSWORD."
(interactive (list (read-passwd "P4 Password: ")))
(p4-lowlevel-login :password password))
(defun vc-p4-logged-in-p ()
"Check if there is an active session for Perforce."
(p4-lowlevel-login :status t))
(provide 'vc-p4) (provide 'vc-p4)