Git-var currently only accepts a single value to print. This
is inefficient if the caller is interested in finding
multiple values, as they must invoke git-var multiple times.

This patch lets callers specify multiple variables, and
prints one per line.

While we're in the area, let's add some basic tests for "git
var", which until now was largely untested (and of course a
test for multiple values on top).

Signed-off-by: Jeff King <p...@peff.net>
---
 Documentation/git-var.txt |  9 +++++++--
 builtin/var.c             | 17 +++++++++-------
 t/t0007-git-var.sh        | 50 +++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 67 insertions(+), 9 deletions(-)
 create mode 100755 t/t0007-git-var.sh

diff --git a/Documentation/git-var.txt b/Documentation/git-var.txt
index 67edf58..53abba5 100644
--- a/Documentation/git-var.txt
+++ b/Documentation/git-var.txt
@@ -9,11 +9,16 @@ git-var - Show a git logical variable
 SYNOPSIS
 --------
 [verse]
-'git var' ( -l | <variable> )
+'git var' ( -l | <variable>... )
 
 DESCRIPTION
 -----------
-Prints a git logical variable.
+Prints one or more git logical variables, separated by newlines.
+
+Note that some variables may contain newlines themselves (e.g.,
+`GIT_EDITOR`), and it is therefore possible to receive ambiguous output
+when requesting multiple variables. This can be mitigated by putting any
+such variables at the end of the list.
 
 OPTIONS
 -------
diff --git a/builtin/var.c b/builtin/var.c
index aedbb53..49ab300 100644
--- a/builtin/var.c
+++ b/builtin/var.c
@@ -5,7 +5,7 @@
  */
 #include "builtin.h"
 
-static const char var_usage[] = "git var (-l | <variable>)";
+static const char var_usage[] = "git var (-l | <variable>...)";
 
 static const char *editor(int flag)
 {
@@ -73,21 +73,24 @@ static int show_config(const char *var, const char *value, 
void *cb)
 
 int cmd_var(int argc, const char **argv, const char *prefix)
 {
-       const char *val = NULL;
-       if (argc != 2)
+       if (argc < 2)
                usage(var_usage);
 
        if (strcmp(argv[1], "-l") == 0) {
+               if (argc > 2)
+                       usage(var_usage);
                git_config(show_config, NULL);
                list_vars();
                return 0;
        }
        git_config(git_default_config, NULL);
-       val = read_var(argv[1]);
-       if (!val)
-               usage(var_usage);
 
-       printf("%s\n", val);
+       while (*++argv) {
+               const char *val = read_var(*argv);
+               if (!val)
+                       usage(var_usage);
+               printf("%s\n", val);
+       }
 
        return 0;
 }
diff --git a/t/t0007-git-var.sh b/t/t0007-git-var.sh
new file mode 100755
index 0000000..ec5d946
--- /dev/null
+++ b/t/t0007-git-var.sh
@@ -0,0 +1,50 @@
+#!/bin/sh
+
+test_description='basic sanity checks for git var'
+. ./test-lib.sh
+
+test_expect_success 'get GIT_AUTHOR_IDENT' '
+       test_tick &&
+       echo "$GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL> $GIT_AUTHOR_DATE" >expect &&
+       git var GIT_AUTHOR_IDENT >actual &&
+       test_cmp expect actual
+'
+
+test_expect_success 'get GIT_COMMITTER_IDENT' '
+       test_tick &&
+       echo "$GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE" 
>expect &&
+       git var GIT_COMMITTER_IDENT >actual &&
+       test_cmp expect actual
+'
+
+# For git var -l, we check only a representative variable;
+# testing the whole output would make our test too brittle with
+# respect to unrelated changes in the test suite's environment.
+test_expect_success 'git var -l lists variables' '
+       git var -l >actual &&
+       echo "$GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL> $GIT_AUTHOR_DATE" >expect &&
+       sed -n s/GIT_AUTHOR_IDENT=//p <actual >actual.author &&
+       test_cmp expect actual.author
+'
+
+test_expect_success 'git var -l lists config' '
+       git var -l >actual &&
+       echo false >expect &&
+       sed -n s/core\\.bare=//p <actual >actual.bare &&
+       test_cmp expect actual.bare
+'
+
+test_expect_success 'listing and asking for variables are exclusive' '
+       test_must_fail git var -l GIT_COMMITTER_IDENT
+'
+
+test_expect_success 'git var can show multiple values' '
+       cat >expect <<-EOF &&
+       $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL> $GIT_AUTHOR_DATE
+       $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+       EOF
+       git var GIT_AUTHOR_IDENT GIT_COMMITTER_IDENT >actual &&
+       test_cmp expect actual
+'
+
+test_done
-- 
1.8.0.207.gdf2154c

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to