git-p4: Unset P4DIFF environment variable when using 'p4 -du diff'
[gitweb.git] / perl / Git.pm
index c1729bafd20d38d56b3178356e50c72b2822f83c..a2812ea612b997c1a76d89075dd1263a3af4fd37 100644 (file)
@@ -354,7 +354,7 @@ sub command_input_pipe {
 =item command_close_pipe ( PIPE [, CTX ] )
 
 Close the C<PIPE> as returned from C<command_*_pipe()>, checking
-whether the command finished successfuly. The optional C<CTX> argument
+whether the command finished successfully. The optional C<CTX> argument
 is required if you want to see the command name in the error message,
 and it is the second value returned by C<command_*_pipe()> when
 called in array context. The call idiom is:
@@ -482,14 +482,14 @@ sub wc_chdir {
 
 =item config ( VARIABLE )
 
-Retrieve the configuration C<VARIABLE> in the same manner as C<repo-config>
+Retrieve the configuration C<VARIABLE> in the same manner as C<config>
 does. In scalar context requires the variable to be set only one time
 (exception is thrown otherwise), in array context returns allows the
 variable to be set multiple times and returns all the values.
 
 Must be called on a repository instance.
 
-This currently wraps command('repo-config') so it is not so fast.
+This currently wraps command('config') so it is not so fast.
 
 =cut
 
@@ -500,9 +500,9 @@ sub config {
 
        try {
                if (wantarray) {
-                       return $self->command('repo-config', '--get-all', $var);
+                       return $self->command('config', '--get-all', $var);
                } else {
-                       return $self->command_oneline('repo-config', '--get', $var);
+                       return $self->command_oneline('config', '--get', $var);
                }
        } catch Git::Error::Command with {
                my $E = shift;
@@ -516,6 +516,106 @@ sub config {
 }
 
 
+=item config_bool ( VARIABLE )
+
+Retrieve the bool configuration C<VARIABLE>. The return value
+is usable as a boolean in perl (and C<undef> if it's not defined,
+of course).
+
+Must be called on a repository instance.
+
+This currently wraps command('config') so it is not so fast.
+
+=cut
+
+sub config_bool {
+       my ($self, $var) = @_;
+       $self->repo_path()
+               or throw Error::Simple("not a repository");
+
+       try {
+               my $val = $self->command_oneline('config', '--bool', '--get',
+                                             $var);
+               return undef unless defined $val;
+               return $val eq 'true';
+       } catch Git::Error::Command with {
+               my $E = shift;
+               if ($E->value() == 1) {
+                       # Key not found.
+                       return undef;
+               } else {
+                       throw $E;
+               }
+       };
+}
+
+=item config_int ( VARIABLE )
+
+Retrieve the integer configuration C<VARIABLE>. The return value
+is simple decimal number.  An optional value suffix of 'k', 'm',
+or 'g' in the config file will cause the value to be multiplied
+by 1024, 1048576 (1024^2), or 1073741824 (1024^3) prior to output.
+It would return C<undef> if configuration variable is not defined,
+
+Must be called on a repository instance.
+
+This currently wraps command('config') so it is not so fast.
+
+=cut
+
+sub config_int {
+       my ($self, $var) = @_;
+       $self->repo_path()
+               or throw Error::Simple("not a repository");
+
+       try {
+               return $self->command_oneline('config', '--int', '--get', $var);
+       } catch Git::Error::Command with {
+               my $E = shift;
+               if ($E->value() == 1) {
+                       # Key not found.
+                       return undef;
+               } else {
+                       throw $E;
+               }
+       };
+}
+
+=item get_colorbool ( NAME )
+
+Finds if color should be used for NAMEd operation from the configuration,
+and returns boolean (true for "use color", false for "do not use color").
+
+=cut
+
+sub get_colorbool {
+       my ($self, $var) = @_;
+       my $stdout_to_tty = (-t STDOUT) ? "true" : "false";
+       my $use_color = $self->command_oneline('config', '--get-colorbool',
+                                              $var, $stdout_to_tty);
+       return ($use_color eq 'true');
+}
+
+=item get_color ( SLOT, COLOR )
+
+Finds color for SLOT from the configuration, while defaulting to COLOR,
+and returns the ANSI color escape sequence:
+
+       print $repo->get_color("color.interactive.prompt", "underline blue white");
+       print "some text";
+       print $repo->get_color("", "normal");
+
+=cut
+
+sub get_color {
+       my ($self, $slot, $default) = @_;
+       my $color = $self->command_oneline('config', '--get-color', $slot, $default);
+       if (!defined $color) {
+               $color = "";
+       }
+       return $color;
+}
+
 =item ident ( TYPE | IDENTSTR )
 
 =item ident_person ( TYPE | IDENTSTR | IDENTARRAY )
@@ -778,7 +878,7 @@ sub _cmd_exec {
                $self->wc_subdir() and chdir($self->wc_subdir());
        }
        _execv_git_cmd(@args);
-       die "exec failed: $!";
+       die qq[exec "@args" failed: $!];
 }
 
 # Execute the given Git command ($_[0]) with arguments ($_[1..])
@@ -826,7 +926,13 @@ sub READLINE {
        if ($self->{i} >= scalar @{$self->{data}}) {
                return undef;
        }
-       return $self->{'data'}->[ $self->{i}++ ];
+       my $i = $self->{i};
+       if (wantarray) {
+               $self->{i} = $#{$self->{'data'}} + 1;
+               return splice(@{$self->{'data'}}, $i);
+       }
+       $self->{i} = $i + 1;
+       return $self->{'data'}->[ $i ];
 }
 
 sub CLOSE {