Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.

Commit da608b1

Browse files
Benoit Persongitster
authored andcommitted
git-remote-mediawiki: use Git.pm functions for credentials
In 52dce6d, a new credential function was added to Git.pm, based on git-remote-mediawiki's functions. The logical follow-up is to use those functions in git-remote-mediawiki. Signed-off-by: Benoit Person <[email protected]> Signed-off-by: Matthieu Moy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent edca415 commit da608b1

File tree

1 file changed

+9
-57
lines changed

1 file changed

+9
-57
lines changed

contrib/mw-to-git/git-remote-mediawiki.perl

Lines changed: 9 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use strict;
1515
use MediaWiki::API;
16+
use Git;
1617
use DateTime::Format::ISO8601;
1718

1819
# By default, use UTF-8 to communicate with Git and the user
@@ -156,57 +157,6 @@
156157

157158
########################## Functions ##############################
158159

159-
## credential API management (generic functions)
160-
161-
sub credential_read {
162-
my %credential;
163-
my $reader = shift;
164-
my $op = shift;
165-
while (<$reader>) {
166-
my ($key, $value) = /([^=]*)=(.*)/;
167-
if (not defined $key) {
168-
die "ERROR receiving response from git credential $op:\n$_\n";
169-
}
170-
$credential{$key} = $value;
171-
}
172-
return %credential;
173-
}
174-
175-
sub credential_write {
176-
my $credential = shift;
177-
my $writer = shift;
178-
# url overwrites other fields, so it must come first
179-
print $writer "url=$credential->{url}\n" if exists $credential->{url};
180-
while (my ($key, $value) = each(%$credential) ) {
181-
if (length $value && $key ne 'url') {
182-
print $writer "$key=$value\n";
183-
}
184-
}
185-
}
186-
187-
sub credential_run {
188-
my $op = shift;
189-
my $credential = shift;
190-
my $pid = open2(my $reader, my $writer, "git credential $op");
191-
credential_write($credential, $writer);
192-
print $writer "\n";
193-
close($writer);
194-
195-
if ($op eq "fill") {
196-
%$credential = credential_read($reader, $op);
197-
} else {
198-
if (<$reader>) {
199-
die "ERROR while running git credential $op:\n$_";
200-
}
201-
}
202-
close($reader);
203-
waitpid($pid, 0);
204-
my $child_exit_status = $? >> 8;
205-
if ($child_exit_status != 0) {
206-
die "'git credential $op' failed with code $child_exit_status.";
207-
}
208-
}
209-
210160
# MediaWiki API instance, created lazily.
211161
my $mediawiki;
212162

@@ -217,22 +167,24 @@ sub mw_connect_maybe {
217167
$mediawiki = MediaWiki::API->new;
218168
$mediawiki->{config}->{api_url} = "$url/api.php";
219169
if ($wiki_login) {
220-
my %credential = (url => $url);
221-
$credential{username} = $wiki_login;
222-
$credential{password} = $wiki_passwd;
223-
credential_run("fill", \%credential);
170+
my %credential = (
171+
'url' => $url,
172+
'username' => $wiki_login,
173+
'password' => $wiki_passwd
174+
);
175+
Git::credential(\%credential);
224176
my $request = {lgname => $credential{username},
225177
lgpassword => $credential{password},
226178
lgdomain => $wiki_domain};
227179
if ($mediawiki->login($request)) {
228-
credential_run("approve", \%credential);
180+
Git::credential(\%credential, 'approve');
229181
print STDERR "Logged in mediawiki user \"$credential{username}\".\n";
230182
} else {
231183
print STDERR "Failed to log in mediawiki user \"$credential{username}\" on $url\n";
232184
print STDERR " (error " .
233185
$mediawiki->{error}->{code} . ': ' .
234186
$mediawiki->{error}->{details} . ")\n";
235-
credential_run("reject", \%credential);
187+
Git::credential(\%credential, 'reject');
236188
exit 1;
237189
}
238190
}

0 commit comments

Comments
 (0)