Subject: bug#71200: TRAMP: Trying to do a project-find-file or (find-directory) using the podman method causes a (wrong-type-argument stringp nil) error

From: Michael Albinus <[email-masked]>
Date: Wed, 29 May 2024 15:57:02 +0200

alexis purslane <[email-masked]> writes:

Hi Alexis,

> One thing: it seems like you forgot to attach the patch? My email
> program isn't seeing any attachments, nor am I seeing a patch pasted
> at the end of your message, unless I'm misunderstanding something?

Strange. Unfortunately, you have removed the Cc: during conversation, so
I cannot point to the archives :-(

I append the patch again, as attachment. And I've reintroduced the Cc,
in case you don't see the attachment, you can check the archives. Pls
keep it.

Best regards, Michael.

diff --git a/lisp/tramp-container.el b/lisp/tramp-container.el
index 2886e25d..450ef653 100644
--- a/lisp/tramp-container.el
+++ b/lisp/tramp-container.el
@@ -76,6 +76,17 @@
 ;;
 ;;
 ;;
+;; Open a file on an existing Distrobox container:
+;;
+;;     C-x C-f /distrobox:CONTAINER:/path/to/file
+;;
+;; Where:
+;;     CONTAINER     is the container to connect to (optional).
+;;
+;; If the container is not running, it is started.
+;;
+;;
+;;
 ;; Open a file on a running Flatpak sandbox:
 ;;
 ;;     C-x C-f /flatpak:SANDBOX:/path/to/file
@@ -153,6 +164,14 @@ If it is nil, the default context will be used."
   :type '(choice (const "toolbox")
                  (string)))

+;;;###tramp-autoload
+(defcustom tramp-distrobox-program "distrobox"
+  "Name of the Distrobxx client program."
+  :group 'tramp
+  :version "30.1"
+  :type '(choice (const "distrobox")
+                 (string)))
+
 ;;;###tramp-autoload
 (defcustom tramp-flatpak-program "flatpak"
   "Name of the Flatpak client program."
@@ -202,6 +221,10 @@ This is for out-of-band connections.")
 (defconst tramp-toolbox-method "toolbox"
   "Tramp method name to use to connect to Toolbox containers.")

+;;;###tramp-autoload
+(defconst tramp-distrobox-method "distrobox"
+  "Tramp method name to use to connect to Distrobox containers.")
+
 ;;;###tramp-autoload
 (defconst tramp-flatpak-method "flatpak"
   "Tramp method name to use to connect to Flatpak sandboxes.")
@@ -391,6 +414,27 @@ see its function help for a description of the format."
                        lines)))
       (mapcar (lambda (name) (list nil name)) names))))

+;;;###tramp-autoload
+(defun tramp-distrobox--completion-function (method)
+  "List Distrobox containers available for connection.
+
+This function is used by `tramp-set-completion-function', please
+see its function help for a description of the format."
+  (tramp-skeleton-completion-function method
+    (when-let ((raw-list (shell-command-to-string (concat program " list")))
+	       ;; Ignore header line.
+               (lines (cdr (split-string raw-list "\n" 'omit)))
+               (names (tramp-compat-seq-keep
+		       (lambda (line)
+			 (when (string-match
+				(rx bol (1+ (not space))
+				    (1+ space) "|" (1+ space)
+				    (group (1+ (not space))) space)
+				line)
+			   (match-string 1 line)))
+                       lines)))
+      (mapcar (lambda (name) (list nil name)) names))))
+
 ;;;###tramp-autoload
 (defun tramp-flatpak--completion-function (method)
   "List Flatpak sandboxes available for connection.
@@ -595,6 +639,26 @@ see its function help for a description of the format."
    tramp-toolbox-method
    `((tramp-toolbox--completion-function ,tramp-toolbox-method))))

+;;;###tramp-autoload
+(defun tramp-enable-distrobox-method ()
+  "Enable connection to Distrobox containers."
+  (add-to-list 'tramp-methods
+	       `(,tramp-distrobox-method
+		 (tramp-login-program ,tramp-distrobox-program)
+		 (tramp-login-args (("enter")
+				    ("-n" "%h")
+				    ("--" "%l")))
+		 ;(tramp-direct-async (,tramp-default-remote-shell "-c"))
+		 (tramp-remote-shell ,tramp-default-remote-shell)
+		 (tramp-remote-shell-login ("-l"))
+		 (tramp-remote-shell-args ("-c"))))
+
+  (add-to-list 'tramp-completion-multi-hop-methods tramp-distrobox-method)
+
+  (tramp-set-completion-function
+   tramp-distrobox-method
+   `((tramp-distrobox--completion-function ,tramp-distrobox-method))))
+
 ;;;###tramp-autoload
 (defun tramp-enable-flatpak-method ()
   "Enable connection to Flatpak sandboxes."