Skip to contentSkip to conversation

Add config_int() method to the Git perl module

PATCH In review Jakub Narebski 5 participants 6 messages Jun 15, 2016 Documentation

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Add config_int() method to the Git perl module

Integer variables can have optional 'k', 'm' or 'g' suffix. config_int() method will return simple decimal number, taking care of those suffixes.

---
For completness (I don't think anything in Perl uses / tries to use integer configuration variables).

(Commit message could be better. Hmmm...)

 perl/Git.pm |   31 +++++++++++++++++++++++++++++++
 1 files changed, 31 insertions(+), 0 deletions(-)
perl/Git.pm +31 -0
index dca92c8..7468460 100644
@@ -549,6 +549,37 @@ sub config_bool {
 	};
 }
 
+=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 ident ( TYPE | IDENTSTR )
 
Signature · 1 line
1.5.3.5
#1 lore raw
Quoted 3 lines · Jakub Narebski

Integer variables can have optional 'k', 'm' or 'g' suffix. config_int() method will return simple decimal number, taking care of those suffixes.

Good. I forgot about --int option to "git config" already.

Maybe in a similar way, we might want to add --color to "git config" to return ANSI sequence, so that Git::config_color() can work without even loading Term::ANSIColor?

#2 lore raw
Quoted 3 lines · Junio C Hamano

Maybe in a similar way, we might want to add --color to "git config" to return ANSI sequence, so that Git::config_color() can work without even loading Term::ANSIColor?

We would also need a way of parsing the 'default' values, so git-config would need a way of saying "turn this value into its internal representation" (or the perl script would have to specify its defaults as raw ANSI codes, which is a bit ugly, but is that the C programs do).

-Peff

#3 lore raw
In reply to Junio C Hamano
Quoted 11 lines · Junio C Hamano
Quoted 3 lines · Jakub Narebski

Integer variables can have optional 'k', 'm' or 'g' suffix. config_int() method will return simple decimal number, taking care of those suffixes.

Good. I forgot about --int option to "git config" already.

Maybe in a similar way, we might want to add --color to "git config" to return ANSI sequence, so that Git::config_color() can work without even loading Term::ANSIColor?

Or failing that, it would be very easy to remove the dependency on Term::ANSIColor by adding something like this to Git.pm:

+my $COLOR = {

+       "reset"         => "\e[0m",
+       "normal"        => "",
+       "black"         => "\e[30m",
+       "red"           => "\e[31m",
+       "green"         => "\e[32m",
+       "yellow"        => "\e[33m",
+       "blue"          => "\e[34m",
+       "magenta"       => "\e[35m",
+       "cyan"          => "\e[36m",
+       "white"         => "\e[37m",
+       "bold"          => "\e[1m",
+       "ul"            => "\e[4m",
+       "blink"         => "\e[5m",
+       "reverse"       => "\e[7m",
+       "on_red"        => "\e[41m",

+};
+
+sub color {
+ my $desired_color = shift;
+ return $COLOR->{$desired_color} || "";
+}

Cheers,
Wincent

#4 lore raw

PATCH 1/1 git-config --get-color: get configured color

In reply to Junio C Hamano

This new option allows scripts to grab color setting from the user configuration, translated to ANSI color escape sequence.

---

Quoted 11 lines · Junio C Hamano
Quoted 3 lines · Jakub Narebski

Integer variables can have optional 'k', 'm' or 'g' suffix. config_int() method will return simple decimal number, taking care of those suffixes.

Good. I forgot about --int option to "git config" already.

Maybe in a similar way, we might want to add --color to "git config" to return ANSI sequence, so that Git::config_color() can work without even loading Term::ANSIColor?

 Documentation/git-config.txt |   16 ++++++++++++
 builtin-config.c             |   55 +++++++++++++++++++++++++++++++++++++++--
 2 files changed, 68 insertions(+), 3 deletions(-)
Documentation/git-config.txt +16 -0
index a592b61..ed3076f 100644
@@ -20,6 +20,7 @@ SYNOPSIS
 'git-config' [<file-option>] --rename-section old_name new_name
 'git-config' [<file-option>] --remove-section name
 'git-config' [<file-option>] [-z|--null] -l | --list
+'git-config' [<file-option>] --get-color name [default]
 
 DESCRIPTION
 -----------
@@ -134,6 +135,12 @@ See also <<FILES>>.
 	output without getting confused e.g. by values that
 	contain line breaks.
 
+--get-color name default::
+
+	Find the color configured for `name` (e.g. `color.diff.new`) and
+	output it as the ANSI color escape sequence to the standard
+	output.  The optional `default` parameter is used if the
+	configuration for `name` is missing.
 
 [[FILES]]
 FILES
@@ -292,6 +299,15 @@ To add a new proxy, without altering any of the existing ones, use
 % git config core.gitproxy '"proxy-command" for example.com'
 ------------
 
+An example to use customized color from the configuration in your
+script:
+
+------------
+#!/bin/sh
+WS=$(git config --get-color color.diff.whitespace "blue reverse")
+RESET==$(git config --get-color "" "reset")
+echo "${WS}your whitespace color or blue reverse${RESET}"
+------------
 
 include::config.txt[]
 
builtin-config.c +52 -3

74 lines not on this page · raw message

Signature · 1 line
1.5.3.6.2039.g0495
#5 lore raw

Re: [PATCH] git-config --get-color: get configured color

Documentation/git-config.txt See also <<FILES>>. · line 143

Quoted 6 lines · Junio C Hamano

+--get-color name default::

Documentation/git-config.txt +5 -0
+
+	Find the color configured for `name` (e.g. `color.diff.new`) and
+	output it as the ANSI color escape sequence to the standard
+	output.  The optional `default` parameter is used if the
+	configuration for `name` is missing.

Perhaps you could mention here that the second parameter is a normal string, not an ansi code? Just adding 'for example, "blue"' would probably help.

Quoted 1 line

+RESET==$(git config --get-color "" "reset")

This is a typo, no?
+RESET=$(git config --get-color "" "reset")

Dan