1 # Copyright (C) 2020-2021 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);
8 my ($self, $op, $lei) = @_;
9 my ($in_r, $in_w, $out_r);
10 my $cmd = [ qw(git credential), $op ];
11 pipe($in_r, $in_w) or die "pipe: $!";
12 if ($lei) { # we'll die if disconnected:
13 pipe($out_r, my $out_w) or die "pipe: $!";
14 $lei->send_exec_cmd([ $in_r, $out_w ], $cmd, {});
16 $out_r = popen_rd($cmd, undef, { 0 => $in_r });
18 close $in_r or die "close in_r: $!";
21 for my $k (qw(url protocol host username password)) {
22 defined(my $v = $self->{$k}) or next;
23 die "`$k' contains `\\n' or `\\0'\n" if $v =~ /[\n\0]/;
27 print $in_w $out or die "print (git credential $op): $!";
28 close $in_w or die "close (git credential $op): $!";
29 return $out_r if $op eq 'fill';
30 <$out_r> and die "unexpected output from `git credential $op'\n";
31 close $out_r or die "`git credential $op' failed: \$!=$! \$?=$?\n";
35 my ($self, $lei) = @_;
37 # n.b. lei doesn't load ~/.netrc by default, public-inbox-watch does,
38 # which may've been a mistake, but we have to live with it.
39 return if ($lei && !$lei->{opt}->{netrc});
41 # part of the standard library, but distributions may split it out
42 eval { require Net::Netrc };
44 warn "W: Net::Netrc missing: $@\n";
47 if (my $x = Net::Netrc->lookup($self->{host}, $self->{username})) {
48 $self->{username} //= $x->login;
49 $self->{password} = $x->password;
54 my ($self, $lei) = @_;
55 my $out_r = run($self, 'fill', $lei);
59 /\A([^=]+)=(.*)\z/ or die "bad line: $_\n";
62 close $out_r or die "git credential fill failed: \$!=$! \$?=$?\n";