packaging: Add contrib installation
[platform/upstream/git.git] / perl / Git.pm
index 49eb88a..02eacef 100644 (file)
@@ -9,7 +9,10 @@ package Git;
 
 use 5.008;
 use strict;
+use warnings $ENV{GIT_PERL_FATAL_WARNINGS} ? qw(FATAL all) : ();
 
+use File::Temp ();
+use File::Spec ();
 
 BEGIN {
 
@@ -59,9 +62,10 @@ require Exporter;
                 command_bidi_pipe command_close_bidi_pipe
                 version exec_path html_path hash_object git_cmd_try
                 remote_refs prompt
-                get_tz_offset
+                get_tz_offset get_record
                 credential credential_read credential_write
-                temp_acquire temp_is_locked temp_release temp_reset temp_path);
+                temp_acquire temp_is_locked temp_release temp_reset temp_path
+                unquote_path);
 
 
 =head1 DESCRIPTION
@@ -100,7 +104,7 @@ increase notwithstanding).
 
 
 use Carp qw(carp croak); # but croak is bad - throw instead
-use Error qw(:try);
+use Git::LoadCPAN::Error qw(:try);
 use Cwd qw(abs_path cwd);
 use IPC::Open2 qw(open2);
 use Fcntl qw(SEEK_SET SEEK_CUR);
@@ -188,7 +192,6 @@ sub repository {
                };
 
                if ($dir) {
-                       _verify_require();
                        File::Spec->file_name_is_absolute($dir) or $dir = $opts{Directory} . '/' . $dir;
                        $opts{Repository} = abs_path($dir);
 
@@ -393,7 +396,7 @@ sub command_close_pipe {
 Execute the given C<COMMAND> in the same way as command_output_pipe()
 does but return both an input pipe filehandle and an output pipe filehandle.
 
-The function will return return C<($pid, $pipe_in, $pipe_out, $ctx)>.
+The function will return C<($pid, $pipe_in, $pipe_out, $ctx)>.
 See C<command_close_bidi_pipe()> for details.
 
 =cut
@@ -531,20 +534,36 @@ If TIME is not supplied, the current local time is used.
 =cut
 
 sub get_tz_offset {
-       # some systmes don't handle or mishandle %z, so be creative.
+       # some systems don't handle or mishandle %z, so be creative.
        my $t = shift || time;
-       my $gm = timegm(localtime($t));
+       my @t = localtime($t);
+       $t[5] += 1900;
+       my $gm = timegm(@t);
        my $sign = qw( + + - )[ $gm <=> $t ];
        return sprintf("%s%02d%02d", $sign, (gmtime(abs($t - $gm)))[2,1]);
 }
 
+=item get_record ( FILEHANDLE, INPUT_RECORD_SEPARATOR )
+
+Read one record from FILEHANDLE delimited by INPUT_RECORD_SEPARATOR,
+removing any trailing INPUT_RECORD_SEPARATOR.
+
+=cut
+
+sub get_record {
+       my ($fh, $rs) = @_;
+       local $/ = $rs;
+       my $rec = <$fh>;
+       chomp $rec if defined $rec;
+       $rec;
+}
 
 =item prompt ( PROMPT , ISPASSWORD  )
 
 Query user C<PROMPT> and return answer from user.
 
 Honours GIT_ASKPASS and SSH_ASKPASS environment variables for querying
-the user. If no *_ASKPASS variable is set or an error occoured,
+the user. If no *_ASKPASS variable is set or an error occurred,
 the terminal is tried as a fallback.
 If C<ISPASSWORD> is set and true, the terminal disables echo.
 
@@ -704,6 +723,32 @@ sub config_int {
        return scalar _config_common({'kind' => '--int'}, @_);
 }
 
+=item config_regexp ( RE )
+
+Retrieve the list of configuration key names matching the regular
+expression C<RE>. The return value is a list of strings matching
+this regex.
+
+=cut
+
+sub config_regexp {
+       my ($self, $regex) = _maybe_self(@_);
+       try {
+               my @cmd = ('config', '--name-only', '--get-regexp', $regex);
+               unshift @cmd, $self if $self;
+               my @matches = command(@cmd);
+               return @matches;
+       } catch Git::Error::Command with {
+               my $E = shift;
+               if ($E->value() == 1) {
+                       my @matches = ();
+                       return @matches;
+               } else {
+                       throw $E;
+               }
+       };
+}
+
 # Common subroutine to implement bulk of what the config* family of methods
 # do. This currently wraps command('config') so it is not so fast.
 sub _config_common {
@@ -865,74 +910,6 @@ sub ident_person {
        return "$ident[0] <$ident[1]>";
 }
 
-=item parse_mailboxes
-
-Return an array of mailboxes extracted from a string.
-
-=cut
-
-sub parse_mailboxes {
-       my $re_comment = qr/\((?:[^)]*)\)/;
-       my $re_quote = qr/"(?:[^\"\\]|\\.)*"/;
-       my $re_word = qr/(?:[^]["\s()<>:;@\\,.]|\\.)+/;
-
-       # divide the string in tokens of the above form
-       my $re_token = qr/(?:$re_quote|$re_word|$re_comment|\S)/;
-       my @tokens = map { $_ =~ /\s*($re_token)\s*/g } @_;
-
-       # add a delimiter to simplify treatment for the last mailbox
-       push @tokens, ",";
-
-       my (@addr_list, @phrase, @address, @comment, @buffer) = ();
-       foreach my $token (@tokens) {
-               if ($token =~ /^[,;]$/) {
-                       # if buffer still contains undeterminated strings
-                       # append it at the end of @address or @phrase
-                       if (@address) {
-                               push @address, @buffer;
-                       } else {
-                               push @phrase, @buffer;
-                       }
-
-                       my $str_phrase = join ' ', @phrase;
-                       my $str_address = join '', @address;
-                       my $str_comment = join ' ', @comment;
-
-                       # quote are necessary if phrase contains
-                       # special characters
-                       if ($str_phrase =~ /[][()<>:;@\\,.\000-\037\177]/) {
-                               $str_phrase =~ s/(^|[^\\])"/$1/g;
-                               $str_phrase = qq["$str_phrase"];
-                       }
-
-                       # add "<>" around the address if necessary
-                       if ($str_address ne "" && $str_phrase ne "") {
-                               $str_address = qq[<$str_address>];
-                       }
-
-                       my $str_mailbox = "$str_phrase $str_address $str_comment";
-                       $str_mailbox =~ s/^\s*|\s*$//g;
-                       push @addr_list, $str_mailbox if ($str_mailbox);
-
-                       @phrase = @address = @comment = @buffer = ();
-               } elsif ($token =~ /^\(/) {
-                       push @comment, $token;
-               } elsif ($token eq "<") {
-                       push @phrase, (splice @address), (splice @buffer);
-               } elsif ($token eq ">") {
-                       push @address, (splice @buffer);
-               } elsif ($token eq "@") {
-                       push @address, (splice @buffer), "@";
-               } elsif ($token eq ".") {
-                       push @address, (splice @buffer), ".";
-               } else {
-                       push @buffer, $token;
-               }
-       }
-
-       return @addr_list;
-}
-
 =item hash_object ( TYPE, FILENAME )
 
 Compute the SHA1 object id of the given C<FILENAME> considering it is
@@ -1029,7 +1006,7 @@ sub cat_blob {
                return -1;
        }
 
-       if ($description !~ /^[0-9a-fA-F]{40} \S+ (\d+)$/) {
+       if ($description !~ /^[0-9a-fA-F]{40}(?:[0-9a-fA-F]{24})? \S+ (\d+)$/) {
                carp "Unexpected result returned from git cat-file";
                return -1;
        }
@@ -1341,8 +1318,6 @@ sub temp_release {
 sub _temp_cache {
        my ($self, $name) = _maybe_self(@_);
 
-       _verify_require();
-
        my $temp_fd = \$TEMP_FILEMAP{$name};
        if (defined $$temp_fd and $$temp_fd->opened) {
                if ($TEMP_FILES{$$temp_fd}{locked}) {
@@ -1376,11 +1351,6 @@ sub _temp_cache {
        $$temp_fd;
 }
 
-sub _verify_require {
-       eval { require File::Temp; require File::Spec; };
-       $@ and throw Error::Simple($@);
-}
-
 =item temp_reset ( FILEHANDLE )
 
 Truncates and resets the position of the C<FILEHANDLE>.
@@ -1421,6 +1391,95 @@ sub END {
 
 } # %TEMP_* Lexical Context
 
+=item prefix_lines ( PREFIX, STRING [, STRING... ])
+
+Prefixes lines in C<STRING> with C<PREFIX>.
+
+=cut
+
+sub prefix_lines {
+       my $prefix = shift;
+       my $string = join("\n", @_);
+       $string =~ s/^/$prefix/mg;
+       return $string;
+}
+
+=item unquote_path ( PATH )
+
+Unquote a quoted path containing c-escapes as returned by ls-files etc.
+when not using -z or when parsing the output of diff -u.
+
+=cut
+
+{
+       my %cquote_map = (
+               "a" => chr(7),
+               "b" => chr(8),
+               "t" => chr(9),
+               "n" => chr(10),
+               "v" => chr(11),
+               "f" => chr(12),
+               "r" => chr(13),
+               "\\" => "\\",
+               "\042" => "\042",
+       );
+
+       sub unquote_path {
+               local ($_) = @_;
+               my ($retval, $remainder);
+               if (!/^\042(.*)\042$/) {
+                       return $_;
+               }
+               ($_, $retval) = ($1, "");
+               while (/^([^\\]*)\\(.*)$/) {
+                       $remainder = $2;
+                       $retval .= $1;
+                       for ($remainder) {
+                               if (/^([0-3][0-7][0-7])(.*)$/) {
+                                       $retval .= chr(oct($1));
+                                       $_ = $2;
+                                       last;
+                               }
+                               if (/^([\\\042abtnvfr])(.*)$/) {
+                                       $retval .= $cquote_map{$1};
+                                       $_ = $2;
+                                       last;
+                               }
+                               # This is malformed
+                               throw Error::Simple("invalid quoted path $_[0]");
+                       }
+                       $_ = $remainder;
+               }
+               $retval .= $_;
+               return $retval;
+       }
+}
+
+=item get_comment_line_char ( )
+
+Gets the core.commentchar configuration value.
+The value falls-back to '#' if core.commentchar is set to 'auto'.
+
+=cut
+
+sub get_comment_line_char {
+       my $comment_line_char = config("core.commentchar") || '#';
+       $comment_line_char = '#' if ($comment_line_char eq 'auto');
+       $comment_line_char = '#' if (length($comment_line_char) != 1);
+       return $comment_line_char;
+}
+
+=item comment_lines ( STRING [, STRING... ])
+
+Comments lines following core.commentchar configuration.
+
+=cut
+
+sub comment_lines {
+       my $comment_line_char = get_comment_line_char;
+       return prefix_lines("$comment_line_char ", @_);
+}
+
 =back
 
 =head1 ERROR HANDLING
@@ -1656,7 +1715,6 @@ sub DESTROY {
 # Pipe implementation for ActiveState Perl.
 
 package Git::activestate_pipe;
-use strict;
 
 sub TIEHANDLE {
        my ($class, @params) = @_;