Subject: Re: bug#23952: 25.0.95; Regression: vc (git) over TRAMP doesn't work anymore

From: Michael Albinus <[email-masked]>
Date: Tue, 12 Jul 2016 16:53:15 +0200

Andreas Schwab <[email-masked]> writes:

> env -u is a GNU extension.

Indeed. The following patch should care for non-gnu remote systems:

--8<---------------cut here---------------start------------->8---
*** /home/albinus/src/emacs-25/lisp/net/tramp-sh.el.~f981b3136742a8597674dd4915afeb1b220a0464~	2016-07-12 16:47:21.714648048 +0200
--- /home/albinus/src/emacs-25/lisp/net/tramp-sh.el	2016-07-12 16:36:03.505265390 +0200
***************
*** 3027,3044 ****
  			   tramp-initial-end-of-output))
  	   ;; We use as environment the difference to toplevel
  	   ;; `process-environment'.
! 	   env
! 	   (env
! 	    (dolist
! 		(elt
! 		 (cons prompt (nreverse (copy-sequence process-environment)))
! 		 env)
! 	      (or (member elt (default-toplevel-value 'process-environment))
! 		  (setq env (cons elt env)))))
  	   (command
  	    (when (stringp program)
! 	      (format "cd %s && exec %s env %s %s"
  		      (tramp-shell-quote-argument localname)
  		      (if heredoc (format "<<'%s'" tramp-end-of-heredoc) "")
  		      (mapconcat 'tramp-shell-quote-argument env " ")
  		      (if heredoc
--- 3027,3049 ----
  			   tramp-initial-end-of-output))
  	   ;; We use as environment the difference to toplevel
  	   ;; `process-environment'.
! 	   env uenv
! 	   (env (dolist (elt (cons prompt process-environment) env)
!                   (or (member elt (default-toplevel-value 'process-environment))
!                       (if (string-match "=" elt)
!                           (setq env (append env `(,elt)))
!                         (if (tramp-get-env-with-u-option v)
!                             (setq env (append `("-u" ,elt) env))
!                           (setq uenv (cons elt uenv)))))))
  	   (command
  	    (when (stringp program)
! 	      (format "cd %s && %s exec %s env %s %s"
  		      (tramp-shell-quote-argument localname)
+                       (if uenv
+                           (format
+                            "unset %s &&"
+                            (mapconcat 'tramp-shell-quote-argument uenv " "))
+                         "")
  		      (if heredoc (format "<<'%s'" tramp-end-of-heredoc) "")
  		      (mapconcat 'tramp-shell-quote-argument env " ")
  		      (if heredoc
***************
*** 3127,3146 ****
      (error "Implementation does not handle immediate return"))
  
    (with-parsed-tramp-file-name default-directory nil
!     (let (command env input tmpinput stderr tmpstderr outbuf ret)
        ;; Compute command.
        (setq command (mapconcat 'tramp-shell-quote-argument
  			       (cons program args) " "))
        ;; We use as environment the difference to toplevel `process-environment'.
!       (setq env
! 	    (dolist (elt (nreverse (copy-sequence process-environment)) env)
! 	      (or (member elt (default-toplevel-value 'process-environment))
! 		  (setq env (cons elt env)))))
        (when env
  	(setq command
  	      (format
  	       "env %s %s"
  	       (mapconcat 'tramp-shell-quote-argument env " ") command)))
        ;; Determine input.
        (if (null infile)
  	  (setq input "/dev/null")
--- 3132,3159 ----
      (error "Implementation does not handle immediate return"))
  
    (with-parsed-tramp-file-name default-directory nil
!     (let (command env uenv input tmpinput stderr tmpstderr outbuf ret)
        ;; Compute command.
        (setq command (mapconcat 'tramp-shell-quote-argument
  			       (cons program args) " "))
        ;; We use as environment the difference to toplevel `process-environment'.
!       (dolist (elt process-environment)
!         (or (member elt (default-toplevel-value 'process-environment))
!             (if (string-match "=" elt)
!                 (setq env (append env `(,elt)))
!               (if (tramp-get-env-with-u-option v)
!                   (setq env (append `("-u" ,elt) env))
!                 (setq uenv (cons elt uenv))))))
        (when env
  	(setq command
  	      (format
  	       "env %s %s"
  	       (mapconcat 'tramp-shell-quote-argument env " ") command)))
+       (when uenv
+         (setq command
+               (format
+                "unset %s && %s"
+                (mapconcat 'tramp-shell-quote-argument uenv " ") command)))
        ;; Determine input.
        (if (null infile)
  	  (setq input "/dev/null")
***************
*** 5695,5700 ****
--- 5708,5720 ----
         ((and (equal id-format 'string) (not (stringp res))) "UNKNOWN")
         (t res)))))
  
+ (defun tramp-get-env-with-u-option (vec)
+   (with-tramp-connection-property vec "env-u-option"
+     (tramp-message vec 5 "Checking, whether `env -u' works")
+     ;; Option "-u" is a GNU extension.
+     (tramp-send-command-and-check
+      vec "env FOO=foo env -u FOO 2>/dev/null | grep -qv FOO" t)))
+ 
  ;; Some predefined connection properties.
  (defun tramp-get-inline-compress (vec prop size)
    "Return the compress command related to PROP.
--8<---------------cut here---------------end--------------->8---

Should this still go to the emacs-25 branch?

> Andreas.

Best regards, Michael.