aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJunio C Hamano <gitster@pobox.com>2025-07-14 11:19:25 -0700
committerJunio C Hamano <gitster@pobox.com>2025-07-14 11:19:25 -0700
commitdb4a912c4ab822d494ba0a1695d6a0e731dde0ca (patch)
tree140ea5d798669bfa777f07f4506f60ba1dcb75ab
parentMerge branch 'jc/coccicheck-fails-make-when-it-fails' (diff)
parentcontrib: better support symbolic port names in git-credential-netrc (diff)
downloadgit-db4a912c4ab822d494ba0a1695d6a0e731dde0ca.tar.gz
git-db4a912c4ab822d494ba0a1695d6a0e731dde0ca.zip
Merge branch 'mc/netrc-service-names'
"netrc" credential helper has been improved to understand textual service names (like smtp) in addition to the numeric port numbers (like 25). * mc/netrc-service-names: contrib: better support symbolic port names in git-credential-netrc contrib: warn for invalid netrc file ports in git-credential-netrc contrib: use a more portable shebang for git-credential-netrc
-rwxr-xr-xcontrib/credential/netrc/git-credential-netrc.perl14
-rwxr-xr-xcontrib/credential/netrc/test.pl8
-rwxr-xr-xgit-send-email.perl11
-rw-r--r--perl/Git.pm13
-rwxr-xr-xt/t9001-send-email.sh7
5 files changed, 46 insertions, 7 deletions
diff --git a/contrib/credential/netrc/git-credential-netrc.perl b/contrib/credential/netrc/git-credential-netrc.perl
index 9fb998ae09..3c0a532d0e 100755
--- a/contrib/credential/netrc/git-credential-netrc.perl
+++ b/contrib/credential/netrc/git-credential-netrc.perl
@@ -1,4 +1,4 @@
-#!/usr/bin/perl
+#!/usr/bin/env perl
use strict;
use warnings;
@@ -267,8 +267,16 @@ sub load_netrc {
if (!defined $nentry->{machine}) {
next;
}
- if (defined $nentry->{port} && $nentry->{port} =~ m/^\d+$/) {
- $num_port = $nentry->{port};
+ if (defined $nentry->{port}) {
+ $num_port = Git::port_num($nentry->{port});
+ unless ($num_port) {
+ printf(STDERR "ignoring invalid port `%s' " .
+ "from netrc file\n", $nentry->{port});
+ }
+ # Since we've already validated and converted
+ # the port to its numerical value, do not
+ # capture it as the `protocol' value, as used
+ # to be the case for symbolic port names.
delete $nentry->{port};
}
diff --git a/contrib/credential/netrc/test.pl b/contrib/credential/netrc/test.pl
index 67a0ede564..8a7fc2588a 100755
--- a/contrib/credential/netrc/test.pl
+++ b/contrib/credential/netrc/test.pl
@@ -45,7 +45,7 @@ chmod 0600, $netrc;
diag "Testing with invalid data\n";
$cred = run_credential(['-f', $netrc, 'get'],
"bad data");
-ok(scalar keys %$cred == 4, "Got first found keys with bad data");
+ok(scalar keys %$cred == 3, "Got first found keys with bad data");
diag "Testing netrc file for a missing corovamilkbar entry\n";
$cred = run_credential(['-f', $netrc, 'get'],
@@ -64,12 +64,12 @@ is($cred->{username}, 'carol', "Got correct Github username");
diag "Testing netrc file for a username-specific entry\n";
$cred = run_credential(['-f', $netrc, 'get'],
- { host => 'imap', username => 'bob' });
+ { host => 'imap:993', username => 'bob' });
-ok(scalar keys %$cred == 2, "Got 2 username-specific keys");
+# Only the password field gets returned.
+ok(scalar keys %$cred == 1, "Got 1 username-specific keys");
is($cred->{password}, 'bobwillknow', "Got correct user-specific password");
-is($cred->{protocol}, 'imaps', "Got correct user-specific protocol");
diag "Testing netrc file for a host:port-specific entry\n";
$cred = run_credential(['-f', $netrc, 'get'],
diff --git a/git-send-email.perl b/git-send-email.perl
index cb6dca2500..437f8ac46a 100755
--- a/git-send-email.perl
+++ b/git-send-email.perl
@@ -2112,6 +2112,17 @@ if ($validate) {
}
}
+ # Validate the SMTP server port, if provided.
+ if (defined $smtp_server_port) {
+ my $port = Git::port_num($smtp_server_port);
+ if ($port) {
+ $smtp_server_port = $port;
+ } else {
+ die sprintf(__("error: invalid SMTP port '%s'\n"),
+ $smtp_server_port);
+ }
+ }
+
# Run the loop once again to avoid gaps in the counter due to FIFO
# arguments provided by the user.
my $num = 1;
diff --git a/perl/Git.pm b/perl/Git.pm
index 6f47d653ab..090cf77dab 100644
--- a/perl/Git.pm
+++ b/perl/Git.pm
@@ -1061,6 +1061,19 @@ sub _close_cat_blob {
delete @$self{@vars};
}
+# Given PORT, a port number or service name, return its numerical
+# value else undef.
+sub port_num {
+ my ($port) = @_;
+
+ # Port can be either a positive integer within the 16-bit range...
+ if ($port =~ /^\d+$/ && $port > 0 && $port <= (2**16 - 1)) {
+ return $port;
+ }
+
+ # ... or a symbolic port (service name).
+ return scalar getservbyname($port, '');
+}
=item credential_read( FILEHANDLE )
diff --git a/t/t9001-send-email.sh b/t/t9001-send-email.sh
index 0c1af43f6f..e56e0c8d77 100755
--- a/t/t9001-send-email.sh
+++ b/t/t9001-send-email.sh
@@ -201,6 +201,13 @@ test_expect_success $PREREQ 'cc trailer with get_maintainer.pl output' '
test_cmp expected-cc commandline1
'
+test_expect_failure $PREREQ 'invalid smtp server port value' '
+ clean_fake_sendmail &&
+ git send-email -1 --to=recipient@example.com \
+ --smtp-server-port=bogus-symbolic-name \
+ --smtp-server="$(pwd)/fake.sendmail"
+'
+
test_expect_success $PREREQ 'setup expect' "
cat >expected-show-all-headers <<\EOF
0001-Second.patch