Skip to contentSkip to conversation

send-email: recognize absolute path on Windows

PATCH In review Erik Faye-Lund 3 participants 7 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 send-email: recognize absolute path on Windows

From: Erik Faye-Lund <[email protected]>

On Windows, absolute paths might start with a DOS drive prefix, which these two checks failed to recognize.

Unfortunately, we cannot simply use the file_name_is_absolute helper in File::Spec::Functions, because Git for Windows has an MSYS-based Perl, where this helper doesn't grok DOS drive-prefixes.

So let's manually check for these in that case, and fall back to the File::Spec-helper on other platforms (e.g Win32 with native Perl)

---

So here's a version that does the old and long-time tested approach without requiring breaking changes to msysGit's perl.

 git-send-email.perl | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)
git-send-email.perl +14 -2
index fdb0029..8f5f986 100755
@@ -1113,6 +1113,18 @@ sub ssl_verify_params {
 	}
 }
 
+sub file_name_is_absolute {
+	my ($path) = @_;
+
+	# msys does not grok DOS drive-prefixes
+	if ($^O eq 'msys') {
+		return ($path =~ m#^/# || $path =~ m#[a-zA-Z]\:#)
+	}
+
+	require File::Spec::Functions;
+	return File::Spec::Functions::file_name_is_absolute($path);
+}
+
 # Returns 1 if the message was sent, and 0 otherwise.
 # In actuality, the whole program dies when there
 # is an error sending a message.
@@ -1197,7 +1209,7 @@ X-Mailer: git-send-email $gitversion
 
 	if ($dry_run) {
 		# We don't want to send the email.
-	} elsif ($smtp_server =~ m#^/#) {
+	} elsif (file_name_is_absolute($smtp_server)) {
 		my $pid = open my $sm, '|-';
 		defined $pid or die $!;
 		if (!$pid) {
@@ -1271,7 +1283,7 @@ X-Mailer: git-send-email $gitversion
 		printf (($dry_run ? "Dry-" : "")."Sent %s\n", $subject);
 	} else {
 		print (($dry_run ? "Dry-" : "")."OK. Log says:\n");
-		if ($smtp_server !~ m#^/#) {
+		if (!file_name_is_absolute($smtp_server)) {
 			print "Server: $smtp_server\n";
 			print "MAIL FROM:<$raw_from>\n";
 			foreach my $entry (@recipients) {
Signature · 21 lines
1.9.0.msysgit.0

-- 
-- 
*** Please reply-to-all at all times ***
*** (do not pretend to know who is subscribed and who is not) ***
*** Please avoid top-posting. ***
The msysGit Wiki is here: https://github.com/msysgit/msysgit/wiki - Github accounts are free.

You received this message because you are subscribed to the Google
Groups "msysGit" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, and view previous threads, visit this group at
http://groups.google.com/group/msysgit?hl=en_US?hl=en

--- 
You received this message because you are subscribed to the Google Groups "msysGit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
For more options, visit https://groups.google.com/d/optout.
#1 lore raw

git-send-email.perl sub ssl_verify_params { · line 1121

Quoted 10 lines · Erik Faye-Lund

So let's manually check for these in that case, and fall back to the File::Spec-helper on other platforms (e.g Win32 with native Perl)
...

git-send-email.perl +6 -0
+sub file_name_is_absolute {
+	my ($path) = @_;
+
+	# msys does not grok DOS drive-prefixes
+	if ($^O eq 'msys') {
+		return ($path =~ m#^/# || $path =~ m#[a-zA-Z]\:#)

Shouldn't the latter also be anchored at the beginning of the string with a leading "^"?

git-send-email.perl sub ssl_verify_params { · line 1125

Quoted 4 lines
git-send-email.perl +4 -0
+	}
+
+	require File::Spec::Functions;
+	return File::Spec::Functions::file_name_is_absolute($path);

We already "use File::Spec qw(something else)" at the beginning, no? Why not throw file_name_is_absolute into that qw() instead?

Quoted 27 lines
git-send-email.perl +4 -2
+}
+
 # Returns 1 if the message was sent, and 0 otherwise.
 # In actuality, the whole program dies when there
 # is an error sending a message.
@@ -1197,7 +1209,7 @@ X-Mailer: git-send-email $gitversion
 
 	if ($dry_run) {
 		# We don't want to send the email.
-	} elsif ($smtp_server =~ m#^/#) {
+	} elsif (file_name_is_absolute($smtp_server)) {
 		my $pid = open my $sm, '|-';
 		defined $pid or die $!;
 		if (!$pid) {
@@ -1271,7 +1283,7 @@ X-Mailer: git-send-email $gitversion
 		printf (($dry_run ? "Dry-" : "")."Sent %s\n", $subject);
 	} else {
 		print (($dry_run ? "Dry-" : "")."OK. Log says:\n");
-		if ($smtp_server !~ m#^/#) {
+		if (!file_name_is_absolute($smtp_server)) {
 			print "Server: $smtp_server\n";
 			print "MAIL FROM:<$raw_from>\n";
 			foreach my $entry (@recipients) {
Signature · 3 lines
1.9.0.msysgit.0

-- 
Signature · 18 lines
-- 
*** Please reply-to-all at all times ***
*** (do not pretend to know who is subscribed and who is not) ***
*** Please avoid top-posting. ***
The msysGit Wiki is here: https://github.com/msysgit/msysgit/wiki - Github accounts are free.

You received this message because you are subscribed to the Google
Groups "msysGit" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, and view previous threads, visit this group at
http://groups.google.com/group/msysgit?hl=en_US?hl=en

--- 
You received this message because you are subscribed to the Google Groups "msysGit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
For more options, visit https://groups.google.com/d/optout.
#2 lore raw
Quoted 16 lines · Junio C Hamano

git-send-email.perl sub ssl_verify_params { · line 1121

Quoted 10 lines · Erik Faye-Lund

So let's manually check for these in that case, and fall back to the File::Spec-helper on other platforms (e.g Win32 with native Perl)
...

git-send-email.perl +6 -0
+sub file_name_is_absolute {
+	my ($path) = @_;
+
+	# msys does not grok DOS drive-prefixes
+	if ($^O eq 'msys') {
+		return ($path =~ m#^/# || $path =~ m#[a-zA-Z]\:#)

Shouldn't the latter also be anchored at the beginning of the string with a leading "^"?

Quoted 7 lines

git-send-email.perl sub ssl_verify_params { · line 1125

Quoted 4 lines
git-send-email.perl +4 -0
+	}
+
+	require File::Spec::Functions;
+	return File::Spec::Functions::file_name_is_absolute($path);

We already "use File::Spec qw(something else)" at the beginning, no? Why not throw file_name_is_absolute into that qw() instead?

Ahh, OK, if you did so, you won't have any place to hook the "only on msys do this" trick into.

It somehow feels somewhat confusing that we define a sub with the same name as the system one, while not overriding it entirely but delegate back to the system one. I am debating myself if it is more obvious if it is done this way:

        use File::Spec::Functions qw(file_name_is_absolute);
        if ($^O eq 'msys') {
                sub file_name_is_absolute {
                	return $_[0] =~ /^\// || $_[0] =~ /^[A-Z]:/i;
                }
        }
Quoted 27 lines
Quoted 25 lines

# Returns 1 if the message was sent, and 0 otherwise. # In actuality, the whole program dies when there

 # is an error sending a message.
git-send-email.perl +2 -2
@@ -1197,7 +1209,7 @@ X-Mailer: git-send-email $gitversion
 
 	if ($dry_run) {
 		# We don't want to send the email.
-	} elsif ($smtp_server =~ m#^/#) {
+	} elsif (file_name_is_absolute($smtp_server)) {
 		my $pid = open my $sm, '|-';
 		defined $pid or die $!;
 		if (!$pid) {
@@ -1271,7 +1283,7 @@ X-Mailer: git-send-email $gitversion
 		printf (($dry_run ? "Dry-" : "")."Sent %s\n", $subject);
 	} else {
 		print (($dry_run ? "Dry-" : "")."OK. Log says:\n");
-		if ($smtp_server !~ m#^/#) {
+		if (!file_name_is_absolute($smtp_server)) {
 			print "Server: $smtp_server\n";
 			print "MAIL FROM:<$raw_from>\n";
 			foreach my $entry (@recipients) {
Signature · 3 lines
1.9.0.msysgit.0

-- 
Signature · 18 lines
-- 
*** Please reply-to-all at all times ***
*** (do not pretend to know who is subscribed and who is not) ***
*** Please avoid top-posting. ***
The msysGit Wiki is here: https://github.com/msysgit/msysgit/wiki - Github accounts are free.

You received this message because you are subscribed to the Google
Groups "msysGit" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, and view previous threads, visit this group at
http://groups.google.com/group/msysgit?hl=en_US?hl=en

--- 
You received this message because you are subscribed to the Google Groups "msysGit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
For more options, visit https://groups.google.com/d/optout.
#3 lore raw
Quoted 10 lines · Junio C Hamano

Ahh, OK, if you did so, you won't have any place to hook the "only on msys do this" trick into.

It somehow feels somewhat confusing that we define a sub with the same name as the system one, while not overriding it entirely but delegate back to the system one. I am debating myself if it is more obvious if it is done this way:

        use File::Spec::Functions qw(file_name_is_absolute);
        if ($^O eq 'msys') {

You would probably want a "no warnings 'redefine'" here as well.

Quoted 4 lines
                sub file_name_is_absolute {
                	return $_[0] =~ /^\// || $_[0] =~ /^[A-Z]:/i;
                }
        }
Signature · 3 lines
brian m. carlson / brian with sandals: Houston, Texas, US
+1 832 623 2791 | http://www.crustytoothpaste.net/~bmc | My opinion only
OpenPGP: RSA v4 4096b: 88AC E9B2 9196 305B A994 7552 F1BA 225C 0223 B187
#4 lore raw
In reply to Junio C Hamano
Quoted 34 lines · Junio C Hamano
Quoted 16 lines · Junio C Hamano

git-send-email.perl sub ssl_verify_params { · line 1121

Quoted 10 lines · Erik Faye-Lund

So let's manually check for these in that case, and fall back to the File::Spec-helper on other platforms (e.g Win32 with native Perl)
...

git-send-email.perl +6 -0
+sub file_name_is_absolute {
+    my ($path) = @_;
+
+    # msys does not grok DOS drive-prefixes
+    if ($^O eq 'msys') {
+            return ($path =~ m#^/# || $path =~ m#[a-zA-Z]\:#)

Shouldn't the latter also be anchored at the beginning of the string with a leading "^"?

Quoted 7 lines

git-send-email.perl sub ssl_verify_params { · line 1125

Quoted 4 lines
git-send-email.perl +4 -0
+    }
+
+    require File::Spec::Functions;
+    return File::Spec::Functions::file_name_is_absolute($path);

We already "use File::Spec qw(something else)" at the beginning, no? Why not throw file_name_is_absolute into that qw() instead?

Ahh, OK, if you did so, you won't have any place to hook the "only on msys do this" trick into.

It somehow feels somewhat confusing that we define a sub with the same name as the system one, while not overriding it entirely but delegate back to the system one. I am debating myself if it is more obvious if it is done this way:

Quoted 7 lines
        use File::Spec::Functions qw(file_name_is_absolute);
        if ($^O eq 'msys') {
                sub file_name_is_absolute {
                        return $_[0] =~ /^\// || $_[0] =~ /^[A-Z]:/i;
                }
        }

In this case, we end up requiring that module even when we end up using it, no? Not that I have very strong objections for doing just that, after all, it appears to be built-in. (As you might understand from this message, my perl-fu is really lacking :-P)

Signature · 18 lines
-- 
*** Please reply-to-all at all times ***
*** (do not pretend to know who is subscribed and who is not) ***
*** Please avoid top-posting. ***
The msysGit Wiki is here: https://github.com/msysgit/msysgit/wiki - Github accounts are free.

You received this message because you are subscribed to the Google
Groups "msysGit" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, and view previous threads, visit this group at
http://groups.google.com/group/msysgit?hl=en_US?hl=en

--- 
You received this message because you are subscribed to the Google Groups "msysGit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
For more options, visit https://groups.google.com/d/optout.
#5 lore raw
Quoted 27 lines · Erik Faye-Lund
Quoted 19 lines
Quoted 10 lines

Shouldn't the latter also be anchored at the beginning of the string with a leading "^"?

git-send-email.perl sub ssl_verify_params { · line 1125

Quoted 4 lines
git-send-email.perl +4 -0
+    }
+
+    require File::Spec::Functions;
+    return File::Spec::Functions::file_name_is_absolute($path);

We already "use File::Spec qw(something else)" at the beginning, no? Why not throw file_name_is_absolute into that qw() instead?

Ahh, OK, if you did so, you won't have any place to hook the "only on msys do this" trick into.

It somehow feels somewhat confusing that we define a sub with the same name as the system one, while not overriding it entirely but delegate back to the system one. I am debating myself if it is more obvious if it is done this way:

Quoted 7 lines
        use File::Spec::Functions qw(file_name_is_absolute);
        if ($^O eq 'msys') {
                sub file_name_is_absolute {
                        return $_[0] =~ /^\// || $_[0] =~ /^[A-Z]:/i;
                }
        }
Quoted 2 lines

In this case, we end up requiring that module even when we end up using it, no?

Also somebody earlier mentioned that we would be redefining, which has a different kind of ugliness, so I'd agree with the code structure of what you sent out (which has been queued on 'pu').

My earlier question "don't we want to make sure 'C:' is at the betginning of the string?" still stands, though. I do not think I futzed with your regexp in the version I queued on 'pu'.

Thanks.

Signature · 18 lines
-- 
*** Please reply-to-all at all times ***
*** (do not pretend to know who is subscribed and who is not) ***
*** Please avoid top-posting. ***
The msysGit Wiki is here: https://github.com/msysgit/msysgit/wiki - Github accounts are free.

You received this message because you are subscribed to the Google
Groups "msysGit" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, and view previous threads, visit this group at
http://groups.google.com/group/msysgit?hl=en_US?hl=en

--- 
You received this message because you are subscribed to the Google Groups "msysGit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
For more options, visit https://groups.google.com/d/optout.
#6 lore raw
Quoted 32 lines · Junio C Hamano
Quoted 27 lines · Erik Faye-Lund
Quoted 19 lines
Quoted 10 lines

Shouldn't the latter also be anchored at the beginning of the string with a leading "^"?

git-send-email.perl sub ssl_verify_params { · line 1125

Quoted 4 lines
git-send-email.perl +4 -0
+    }
+
+    require File::Spec::Functions;
+    return File::Spec::Functions::file_name_is_absolute($path);

We already "use File::Spec qw(something else)" at the beginning, no? Why not throw file_name_is_absolute into that qw() instead?

Ahh, OK, if you did so, you won't have any place to hook the "only on msys do this" trick into.

It somehow feels somewhat confusing that we define a sub with the same name as the system one, while not overriding it entirely but delegate back to the system one. I am debating myself if it is more obvious if it is done this way:

Quoted 7 lines
        use File::Spec::Functions qw(file_name_is_absolute);
        if ($^O eq 'msys') {
                sub file_name_is_absolute {
                        return $_[0] =~ /^\// || $_[0] =~ /^[A-Z]:/i;
                }
        }
Quoted 2 lines

In this case, we end up requiring that module even when we end up using it, no?

Quoted 7 lines

Also somebody earlier mentioned that we would be redefining, which has a different kind of ugliness, so I'd agree with the code structure of what you sent out (which has been queued on 'pu').

My earlier question "don't we want to make sure 'C:' is at the betginning of the string?" still stands, though. I do not think I futzed with your regexp in the version I queued on 'pu'.

Ah, yes of course. Thanks for spotting that. I also like the other clean-ups you did to the regex (above).

Signature · 18 lines
-- 
*** Please reply-to-all at all times ***
*** (do not pretend to know who is subscribed and who is not) ***
*** Please avoid top-posting. ***
The msysGit Wiki is here: https://github.com/msysgit/msysgit/wiki - Github accounts are free.

You received this message because you are subscribed to the Google
Groups "msysGit" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, and view previous threads, visit this group at
http://groups.google.com/group/msysgit?hl=en_US?hl=en

--- 
You received this message because you are subscribed to the Google Groups "msysGit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
For more options, visit https://groups.google.com/d/optout.