13
13
14
14
use strict;
15
15
use MediaWiki::API;
16
+ use Git;
16
17
use DateTime::Format::ISO8601;
17
18
18
19
# By default, use UTF-8 to communicate with Git and the user
156
157
157
158
# ######################### Functions ##############################
158
159
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
-
210
160
# MediaWiki API instance, created lazily.
211
161
my $mediawiki ;
212
162
@@ -217,22 +167,24 @@ sub mw_connect_maybe {
217
167
$mediawiki = MediaWiki::API-> new;
218
168
$mediawiki -> {config }-> {api_url } = " $url /api.php" ;
219
169
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 );
224
176
my $request = {lgname => $credential {username },
225
177
lgpassword => $credential {password },
226
178
lgdomain => $wiki_domain };
227
179
if ($mediawiki -> login($request )) {
228
- credential_run( " approve " , \%credential );
180
+ Git::credential( \%credential , ' approve ' );
229
181
print STDERR " Logged in mediawiki user \" $credential {username}\" .\n " ;
230
182
} else {
231
183
print STDERR " Failed to log in mediawiki user \" $credential {username}\" on $url \n " ;
232
184
print STDERR " (error " .
233
185
$mediawiki -> {error }-> {code } . ' : ' .
234
186
$mediawiki -> {error }-> {details } . " )\n " ;
235
- credential_run( " reject " , \%credential );
187
+ Git::credential( \%credential , ' reject ' );
236
188
exit 1;
237
189
}
238
190
}
0 commit comments