426
429
currently it is simply the command name but in future the context might
427
430
have more complicated structure.
432
C<PIPE_IN> and C<PIPE_OUT> may be C<undef> if they have been closed prior to
433
calling this function. This may be useful in a query-response type of
434
commands where caller first writes a query and later reads response, eg:
436
my ($pid, $in, $out, $ctx) = $r->command_bidi_pipe('cat-file --batch-check');
437
print $out "000000000\n";
439
while (<$in>) { ... }
440
$r->command_close_bidi_pipe($pid, $in, undef, $ctx);
442
This idiom may prevent potential dead locks caused by data sent to the output
443
pipe not being flushed and thus not reaching the executed command.
431
447
sub command_close_bidi_pipe {
433
my ($pid, $in, $out, $ctx) = @_;
434
foreach my $fh ($in, $out) {
437
carp "error closing pipe: $!";
439
throw Git::Error::Command($ctx, $? >>8);
449
my ($self, $pid, $in, $out, $ctx) = _maybe_self(@_);
450
_cmd_close($ctx, (grep { defined } ($in, $out)));
447
453
throw Git::Error::Command($ctx, $? >>8);
512
518
sub html_path { command_oneline('--html-path') }
521
=item get_tz_offset ( TIME )
523
Return the time zone offset from GMT in the form +/-HHMM where HH is
524
the number of hours from GMT and MM is the number of minutes. This is
525
the equivalent of what strftime("%z", ...) would provide on a GNU
528
If TIME is not supplied, the current local time is used.
533
# some systmes don't handle or mishandle %z, so be creative.
534
my $t = shift || time;
535
my $gm = timegm(localtime($t));
536
my $sign = qw( + + - )[ $gm <=> $t ];
537
return sprintf("%s%02d%02d", $sign, (gmtime(abs($t - $gm)))[2,1]);
541
=item prompt ( PROMPT , ISPASSWORD )
543
Query user C<PROMPT> and return answer from user.
545
Honours GIT_ASKPASS and SSH_ASKPASS environment variables for querying
546
the user. If no *_ASKPASS variable is set or an error occoured,
547
the terminal is tried as a fallback.
548
If C<ISPASSWORD> is set and true, the terminal disables echo.
553
my ($prompt, $isPassword) = @_;
555
if (exists $ENV{'GIT_ASKPASS'}) {
556
$ret = _prompt($ENV{'GIT_ASKPASS'}, $prompt);
558
if (!defined $ret && exists $ENV{'SSH_ASKPASS'}) {
559
$ret = _prompt($ENV{'SSH_ASKPASS'}, $prompt);
562
print STDERR $prompt;
564
if (defined $isPassword && $isPassword) {
565
require Term::ReadKey;
566
Term::ReadKey::ReadMode('noecho');
568
while (defined(my $key = Term::ReadKey::ReadKey(0))) {
569
last if $key =~ /[\012\015]/; # \n\r
572
Term::ReadKey::ReadMode('restore');
576
chomp($ret = <STDIN>);
583
my ($askpass, $prompt) = @_;
584
return unless length $askpass;
587
open my $fh, "-|", $askpass, $prompt or return;
589
$ret =~ s/[\015\012]//g; # strip \r\n, chomp does not work on all systems (i.e. windows) as expected
515
594
=item repo_path ()
517
596
Return path to the git repository. Must be called on a repository instance.
1027
=item credential_read( FILEHANDLE )
1029
Reads credential key-value pairs from C<FILEHANDLE>. Reading stops at EOF or
1030
when an empty line is encountered. Each line must be of the form C<key=value>
1031
with a non-empty key. Function returns hash with all read values. Any white
1032
space (other than new-line character) is preserved.
1036
sub credential_read {
1037
my ($self, $reader) = _maybe_self(@_);
1043
} elsif (!/^([^=]+)=(.*)$/) {
1044
throw Error::Simple("unable to parse git credential data:\n$_");
1046
$credential{$1} = $2;
1051
=item credential_write( FILEHANDLE, CREDENTIAL_HASHREF )
1053
Writes credential key-value pairs from hash referenced by
1054
C<CREDENTIAL_HASHREF> to C<FILEHANDLE>. Keys and values cannot contain
1055
new-lines or NUL bytes characters, and key cannot contain equal signs nor be
1056
empty (if they do Error::Simple is thrown). Any white space is preserved. If
1057
value for a key is C<undef>, it will be skipped.
1059
If C<'url'> key exists it will be written first. (All the other key-value
1060
pairs are written in sorted order but you should not depend on that). Once
1061
all lines are written, an empty line is printed.
1065
sub credential_write {
1066
my ($self, $writer, $credential) = _maybe_self(@_);
1069
# Check if $credential is valid prior to writing anything
1070
while (($key, $value) = each %$credential) {
1071
if (!defined $key || !length $key) {
1072
throw Error::Simple("credential key empty or undefined");
1073
} elsif ($key =~ /[=\n\0]/) {
1074
throw Error::Simple("credential key contains invalid characters: $key");
1075
} elsif (defined $value && $value =~ /[\n\0]/) {
1076
throw Error::Simple("credential value for key=$key contains invalid characters: $value");
1081
# url overwrites other fields, so it must come first
1082
return -1 if $a eq 'url';
1083
return 1 if $b eq 'url';
1085
} keys %$credential) {
1086
if (defined $credential->{$key}) {
1087
print $writer $key, '=', $credential->{$key}, "\n";
1093
sub _credential_run {
1094
my ($self, $credential, $op) = _maybe_self(@_);
1095
my ($pid, $reader, $writer, $ctx) = command_bidi_pipe('credential', $op);
1097
credential_write $writer, $credential;
1100
if ($op eq "fill") {
1101
%$credential = credential_read $reader;
1104
throw Error::Simple("unexpected output from git credential $op response:\n$_\n");
1107
command_close_bidi_pipe($pid, $reader, undef, $ctx);
1110
=item credential( CREDENTIAL_HASHREF [, OPERATION ] )
1112
=item credential( CREDENTIAL_HASHREF, CODE )
1114
Executes C<git credential> for a given set of credentials and specified
1115
operation. In both forms C<CREDENTIAL_HASHREF> needs to be a reference to
1116
a hash which stores credentials. Under certain conditions the hash can
1119
In the first form, C<OPERATION> can be C<'fill'>, C<'approve'> or C<'reject'>,
1120
and function will execute corresponding C<git credential> sub-command. If
1121
it's omitted C<'fill'> is assumed. In case of C<'fill'> the values stored in
1122
C<CREDENTIAL_HASHREF> will be changed to the ones returned by the C<git
1123
credential fill> command. The usual usage would look something like:
1126
'protocol' => 'https',
1127
'host' => 'example.com',
1130
Git::credential \%cred;
1131
if (try_to_authenticate($cred{'username'}, $cred{'password'})) {
1132
Git::credential \%cred, 'approve';
1133
... do more stuff ...
1135
Git::credential \%cred, 'reject';
1138
In the second form, C<CODE> needs to be a reference to a subroutine. The
1139
function will execute C<git credential fill> to fill the provided credential
1140
hash, then call C<CODE> with C<CREDENTIAL_HASHREF> as the sole argument. If
1141
C<CODE>'s return value is defined, the function will execute C<git credential
1142
approve> (if return value yields true) or C<git credential reject> (if return
1143
value is false). If the return value is undef, nothing at all is executed;
1144
this is useful, for example, if the credential could neither be verified nor
1145
rejected due to an unrelated network error. The return value is the same as
1146
what C<CODE> returns. With this form, the usage might look as follows:
1148
if (Git::credential {
1149
'protocol' => 'https',
1150
'host' => 'example.com',
1154
return !!try_to_authenticate($cred->{'username'},
1155
$cred->{'password'});
1157
... do more stuff ...
1163
my ($self, $credential, $op_or_code) = (_maybe_self(@_), 'fill');
1165
if ('CODE' eq ref $op_or_code) {
1166
_credential_run $credential, 'fill';
1167
my $ret = $op_or_code->($credential);
1169
_credential_run $credential, $ret ? 'approve' : 'reject';
1173
_credential_run $credential, $op_or_code;
951
1177
{ # %TEMP_* Lexical Context
953
1179
my (%TEMP_FILEMAP, %TEMP_FILES);
955
1181
=item temp_acquire ( NAME )
957
Attempts to retreive the temporary file mapped to the string C<NAME>. If an
1183
Attempts to retrieve the temporary file mapped to the string C<NAME>. If an
958
1184
associated temp file has not been created this session or was closed, it is
959
1185
created, cached, and set for autoflush and binmode.