1 # Copyright (C) 2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 package PublicInbox::GitCredential;
5 use PublicInbox::Spawn qw(popen_rd);
10 pipe($in_r, $in_w) or die "pipe: $!";
11 my $out_r = popen_rd([qw(git credential), $op], undef, { 0 => $in_r });
12 close $in_r or die "close in_r: $!";
15 for my $k (qw(url protocol host username password)) {
16 defined(my $v = $self->{$k}) or next;
17 die "`$k' contains `\\n' or `\\0'\n" if $v =~ /[\n\0]/;
21 print $in_w $out or die "print (git credential $op): $!";
22 close $in_w or die "close (git credential $op): $!";
23 return $out_r if $op eq 'fill';
24 <$out_r> and die "unexpected output from `git credential $op'\n";
25 close $out_r or die "`git credential $op' failed: \$!=$! \$?=$?\n";
31 # part of the standard library, but distributions may split it out
32 eval { require Net::Netrc };
34 warn "W: Net::Netrc missing: $@\n";
37 if (my $x = Net::Netrc->lookup($self->{host}, $self->{username})) {
38 $self->{username} //= $x->login;
39 $self->{password} = $x->password;
45 my $out_r = run($self, 'fill');
49 /\A([^=]+)=(.*)\z/ or die "bad line: $_\n";
52 close $out_r or die "git credential fill failed: \$!=$! \$?=$?\n";