var: support broken-down idents, signing key, multiple args, and -z

While 'git var' exposes GIT_AUTHOR_IDENT and GIT_COMMITTER_IDENT,
extracting individual components (name, email, or date) currently
requires callers to manually parse the composite string. Furthermore,
there is no way to query the resolved commit signing key through
'git var', and the command only accepts a single variable at a time.

Teach 'git var' to expose individual identity components and commit
signing configuration, and allow querying multiple variables with
optional NUL-termination:

- Add GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and GIT_AUTHOR_DATE.
- Add GIT_COMMITTER_NAME, GIT_COMMITTER_EMAIL, and GIT_COMMITTER_DATE.
- Add GIT_SIGNING_KEY to resolve the key that would be used to sign
  the resulting commit if you were to run 'git commit' right now.
- Allow passing multiple variable arguments (e.g., 'git var
  GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL').
- When a single variable is requested, print its bare value for backward
  compatibility.
- When multiple variables are requested, model the output after
  'git var -l' by printing 'VARIABLE=value' pairs (or 'VARIABLE\nvalue\0'
  when '-z' is given).
- Format multi-valued variables in multi-variable mode as repeated
  'VARIABLE=value' entries (or 'VARIABLE\nvalue\0' with '-z'),
  eliminating stream ambiguity without extra trailing delimiters.
- When querying multiple variables, omit any variable that has no value,
  continue processing remaining variables, and exit with code 1.
- Support '-z' to terminate variable outputs with NUL bytes.
- Format 'git var -l -z' using the same convention as 'git config
  list -z' (newline separating key and value, NUL separating entries).
- Use parse_options() to strictly require options before arguments.
- Update Documentation/git-var.adoc and t/t0007-git-var.sh.

Signed-off-by: Andrew Pleeter <andrewpleeter@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
seen
Andrew Pleeter 2026-09-14 22:50:26 +00:00 committed by Junio C Hamano
parent b8242b093d
commit c064665dc9
3 changed files with 421 additions and 61 deletions

View File

@ -9,12 +9,25 @@ git-var - Show a Git logical variable
SYNOPSIS
--------
[synopsis]
git var (-l | <variable>)
git var [-z] -l
git var [-z] <variable>...

DESCRIPTION
-----------
Prints a Git logical variable. Exits with code 1 if the variable has
no value.
Prints Git logical variables. When a single variable is requested, its
bare value is printed. When multiple variables are requested, they are
printed as `VARIABLE=value` pairs, separated by newlines.

If `-z` is given, the output format changes depending on the mode:

* With a single variable, the bare value is terminated by a NUL byte.
* With multiple variables or with `-l`, the variable name and value are
separated by a newline, and each entry is terminated by a NUL byte
(`VARIABLE\nvalue\0`).

If any requested variable has no value, nothing is printed for that
variable, processing continues for the remaining variables, and the
command exits with code 1.

OPTIONS
-------
@ -24,19 +37,56 @@ OPTIONS
as well. (However, the configuration variables listing functionality
is deprecated in favor of `git config list`.)

`-z`::
Terminate entries with NUL instead of newline. When used with
`-l` or when multiple variables are requested, the variable name
and its value are separated by a newline, and each entry is
terminated with a NUL byte.

EXAMPLES
--------
$ git var GIT_AUTHOR_IDENT
Eric W. Biederman <ebiederm@lnxi.com> 1121223278 -0600
* Get the author identity:
+
------------
$ git var GIT_AUTHOR_IDENT
Eric W. Biederman <ebiederm@lnxi.com> 1121223278 -0600
------------

* Get the author name and email:
+
------------
$ git var GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL
GIT_AUTHOR_NAME=Eric W. Biederman
GIT_AUTHOR_EMAIL=ebiederm@lnxi.com
------------

VARIABLES
---------
`GIT_AUTHOR_IDENT`::
The author of a piece of code.
`GIT_AUTHOR_NAME`::
`GIT_AUTHOR_EMAIL`::
`GIT_AUTHOR_DATE`::
The authorship information that would be recorded in the
resulting commit object if you ran `git commit` right now.
`GIT_AUTHOR_IDENT` consists of the author's name, e-mail
address, and timestamp+timezone. These three pieces of
information are available separately as `GIT_AUTHOR_NAME`,
`GIT_AUTHOR_EMAIL`, and `GIT_AUTHOR_DATE`.

`GIT_COMMITTER_IDENT`::
The person who put a piece of code into Git.
`GIT_COMMITTER_NAME`::
`GIT_COMMITTER_EMAIL`::
`GIT_COMMITTER_DATE`::
The committer information that would be recorded in the
resulting commit object if you ran `git commit` right now.
`GIT_COMMITTER_IDENT` consists of the committer's name, e-mail
address, and timestamp+timezone. These three pieces of
information are available separately as `GIT_COMMITTER_NAME`,
`GIT_COMMITTER_EMAIL`, and `GIT_COMMITTER_DATE`.

`GIT_SIGNING_KEY`::
The key that would be used to sign the resulting commit if you were
to run `git commit` right now.

`GIT_EDITOR`::
Text editor for use by Git commands. The value is meant to be
@ -85,9 +135,11 @@ endif::git-default-pager[]
The path to the global (per-user) configuration files, if any.

Most path values contain only one value. However, some can contain multiple
values, which are separated by newlines, and are listed in order from highest to
lowest priority. Callers should be prepared for any such path value to contain
multiple items.
values, which are separated by newlines (or NUL bytes if `-z` is given),
and are listed in order from highest to lowest priority. When querying
multiple variables (or using `-l`), each value is output as a separate
`VARIABLE=value` entry (or `VARIABLE\nvalue\0` with `-z`). Callers should
be prepared for any such path value to contain multiple items.

Note that paths are printed even if they do not exist, but not if they are
disabled by other environment variables.

View File

@ -12,25 +12,109 @@
#include "config.h"
#include "editor.h"
#include "environment.h"
#include "gpg-interface.h"
#include "ident.h"
#include "pager.h"
#include "refs.h"
#include "parse-options.h"
#include "path.h"
#include "strbuf.h"
#include "refs.h"
#include "run-command.h"
#include "strbuf.h"
#include "string-list.h"

static const char var_usage[] = "git var (-l | <variable>)";
static const char * const var_usage[] = {
N_("git var [-z] -l"),
N_("git var [-z] <variable>..."),
NULL
};

enum ident_part {
IDENT_NAME,
IDENT_MAIL,
IDENT_DATE,
};

static char *committer(int ident_flag)
{
return xstrdup_or_null(git_committer_info(ident_flag));
}

static char *ident_part(const char *ident, enum ident_part part)
{
struct ident_split split;

if (!ident)
return NULL;
if (split_ident_line(&split, ident, strlen(ident)))
return NULL;

switch (part) {
case IDENT_NAME:
if (!split.name_begin || !split.name_end)
BUG("split_ident_line() gave NULL names???");
return xmemdupz(split.name_begin,
split.name_end - split.name_begin);
case IDENT_MAIL:
if (!split.mail_begin || !split.mail_end)
BUG("split_ident_line() gave NULL mail???");
return xmemdupz(split.mail_begin,
split.mail_end - split.mail_begin);
case IDENT_DATE:
if (!split.date_begin || !split.tz_end)
BUG("split_ident_line() gave NULL date/tz???");
return xmemdupz(split.date_begin,
split.tz_end - split.date_begin);
default:
BUG("unknown ident_part %d", part);
}
}

static char *committer_name(int ident_flag)
{
return ident_part(git_committer_info(ident_flag), IDENT_NAME);
}

static char *committer_email(int ident_flag)
{
return ident_part(git_committer_info(ident_flag), IDENT_MAIL);
}

static char *committer_date(int ident_flag)
{
return ident_part(git_committer_info(ident_flag), IDENT_DATE);
}

static char *author(int ident_flag)
{
return xstrdup_or_null(git_author_info(ident_flag));
}

static char *author_name(int ident_flag)
{
return ident_part(git_author_info(ident_flag), IDENT_NAME);
}

static char *author_email(int ident_flag)
{
return ident_part(git_author_info(ident_flag), IDENT_MAIL);
}

static char *author_date(int ident_flag)
{
return ident_part(git_author_info(ident_flag), IDENT_DATE);
}

static char *git_signing_key(int ident_flag UNUSED)
{
char *signing_key = get_signing_key();

if (signing_key && !*signing_key) {
free(signing_key);
return NULL;
}
return signing_key;
}

static char *editor(int ident_flag UNUSED)
{
return xstrdup_or_null(git_editor());
@ -90,45 +174,61 @@ static char *git_config_val_system(int ident_flag UNUSED)
return NULL;
}

static char *git_config_val_global(int ident_flag UNUSED)
static void git_config_val_global(struct string_list *list)
{
struct strbuf buf = STRBUF_INIT;
char *user, *xdg;
size_t unused;

git_global_config_paths(&user, &xdg);
if (xdg && *xdg) {
normalize_path_copy(xdg, xdg);
strbuf_addf(&buf, "%s\n", xdg);
string_list_append(list, xdg);
}
if (user && *user) {
normalize_path_copy(user, user);
strbuf_addf(&buf, "%s\n", user);
string_list_append(list, user);
}
free(xdg);
free(user);
strbuf_trim_trailing_newline(&buf);
if (buf.len == 0) {
strbuf_release(&buf);
return NULL;
}
return strbuf_detach(&buf, &unused);
}

struct git_var {
const char *name;
char *(*read)(int);
int multivalued;
void (*multiread)(struct string_list *);
};
static struct git_var git_vars[] = {
{
.name = "GIT_COMMITTER_IDENT",
.read = committer,
},
{
.name = "GIT_COMMITTER_NAME",
.read = committer_name,
},
{
.name = "GIT_COMMITTER_EMAIL",
.read = committer_email,
},
{
.name = "GIT_COMMITTER_DATE",
.read = committer_date,
},
{
.name = "GIT_AUTHOR_IDENT",
.read = author,
},
{
.name = "GIT_AUTHOR_NAME",
.read = author_name,
},
{
.name = "GIT_AUTHOR_EMAIL",
.read = author_email,
},
{
.name = "GIT_AUTHOR_DATE",
.read = author_date,
},
{
.name = "GIT_EDITOR",
.read = editor,
@ -145,6 +245,10 @@ static struct git_var git_vars[] = {
.name = "GIT_DEFAULT_BRANCH",
.read = default_branch,
},
{
.name = "GIT_SIGNING_KEY",
.read = git_signing_key,
},
{
.name = "GIT_SHELL_PATH",
.read = shell_path,
@ -163,8 +267,7 @@ static struct git_var git_vars[] = {
},
{
.name = "GIT_CONFIG_GLOBAL",
.read = git_config_val_global,
.multivalued = 1,
.multiread = git_config_val_global,
},
{
.name = "",
@ -172,31 +275,37 @@ static struct git_var git_vars[] = {
},
};

static void list_vars(void)
static void list_vars(int nul_term)
{
struct git_var *ptr;
char *val;
char delim = nul_term ? '\n' : '=';
char term = nul_term ? '\0' : '\n';

for (ptr = git_vars; ptr->read; ptr++)
if ((val = ptr->read(0))) {
if (ptr->multivalued && *val) {
struct string_list list = STRING_LIST_INIT_DUP;
for (ptr = git_vars; ptr->read || ptr->multiread; ptr++) {
if (ptr->read) {
char *val = ptr->read(0);

string_list_split(&list, val, "\n", -1);
for (size_t i = 0; i < list.nr; i++)
printf("%s=%s\n", ptr->name, list.items[i].string);
string_list_clear(&list, 0);
} else {
printf("%s=%s\n", ptr->name, val);
if (val) {
printf("%s%c%s%c", ptr->name, delim, val, term);
free(val);
}
free(val);
} else {
struct string_list list = STRING_LIST_INIT_DUP;
size_t i;

ptr->multiread(&list);
for (i = 0; i < list.nr; i++)
printf("%s%c%s%c", ptr->name, delim,
list.items[i].string, term);
string_list_clear(&list, 0);
}
}
}

static const struct git_var *get_git_var(const char *var)
{
struct git_var *ptr;
for (ptr = git_vars; ptr->read; ptr++) {
for (ptr = git_vars; ptr->read || ptr->multiread; ptr++) {
if (strcmp(var, ptr->name) == 0) {
return ptr;
}
@ -207,42 +316,90 @@ static const struct git_var *get_git_var(const char *var)
static int show_config(const char *var, const char *value,
const struct config_context *ctx, void *cb)
{
int *nul_term = cb;
char delim = *nul_term ? '\n' : '=';
char term = *nul_term ? '\0' : '\n';

if (value)
printf("%s=%s\n", var, value);
printf("%s%c%s%c", var, delim, value, term);
else
printf("%s\n", var);
printf("%s%c", var, term);
return git_default_config(var, value, ctx, cb);
}

int cmd_var(int argc,
const char **argv,
const char *prefix UNUSED,
const char *prefix,
struct repository *repo UNUSED)
{
const struct git_var *git_var;
char *val;
int list = 0;
int nul_term = 0;
int ret = 0;
int i;
char delim;
char term;
struct option options[] = {
OPT_BOOL('l', NULL, &list,
N_("list all variables")),
OPT_BOOL('z', NULL, &nul_term,
N_("terminate entries with NUL")),
OPT_END(),
};

show_usage_if_asked(argc, argv, var_usage);
if (argc != 2)
usage(var_usage);
argc = parse_options(argc, argv, prefix, options,
var_usage, PARSE_OPT_STOP_AT_NON_OPTION);

if (strcmp(argv[1], "-l") == 0) {
repo_config(the_repository, show_config, NULL);
list_vars();
if (list) {
if (argc)
usage_with_options(var_usage, options);
repo_config(the_repository, show_config, &nul_term);
list_vars(nul_term);
return 0;
}

if (!argc)
usage_with_options(var_usage, options);

repo_config(the_repository, git_default_config, NULL);

git_var = get_git_var(argv[1]);
if (!git_var)
usage(var_usage);
delim = nul_term ? '\n' : '=';
term = nul_term ? '\0' : '\n';

val = git_var->read(IDENT_STRICT);
if (!val)
return 1;
for (i = 0; i < argc; i++) {
const struct git_var *git_var = get_git_var(argv[i]);

printf("%s\n", val);
free(val);
if (!git_var)
usage_with_options(var_usage, options);

return 0;
if (git_var->read) {
char *val = git_var->read(IDENT_STRICT);

if (!val) {
ret = 1;
continue;
}
if (argc == 1)
printf("%s%c", val, term);
else
printf("%s%c%s%c", git_var->name, delim, val, term);
free(val);
} else {
struct string_list list = STRING_LIST_INIT_DUP;
size_t j;

git_var->multiread(&list);
if (!list.nr)
ret = 1;
for (j = 0; j < list.nr; j++) {
if (argc == 1)
printf("%s%c", list.items[j].string, term);
else
printf("%s%c%s%c", git_var->name, delim,
list.items[j].string, term);
}
string_list_clear(&list, 0);
}
}

return ret;
}

View File

@ -276,4 +276,155 @@ test_expect_success '`git var -l` works even without HOME' '
)
'

test_expect_success 'get author identity components' '
test_tick &&
echo "$GIT_AUTHOR_NAME" >expect.name &&
echo "$GIT_AUTHOR_EMAIL" >expect.email &&
echo "$GIT_AUTHOR_DATE" >expect.date &&
git var GIT_AUTHOR_NAME >actual.name &&
git var GIT_AUTHOR_EMAIL >actual.email &&
git var GIT_AUTHOR_DATE >actual.date &&
test_cmp expect.name actual.name &&
test_cmp expect.email actual.email &&
test_cmp expect.date actual.date
'

test_expect_success 'get committer identity components' '
test_tick &&
echo "$GIT_COMMITTER_NAME" >expect.name &&
echo "$GIT_COMMITTER_EMAIL" >expect.email &&
echo "$GIT_COMMITTER_DATE" >expect.date &&
git var GIT_COMMITTER_NAME >actual.name &&
git var GIT_COMMITTER_EMAIL >actual.email &&
git var GIT_COMMITTER_DATE >actual.date &&
test_cmp expect.name actual.name &&
test_cmp expect.email actual.email &&
test_cmp expect.date actual.date
'

test_expect_success 'get multiple variables' '
test_tick &&
cat >expect <<-EOF &&
GIT_AUTHOR_NAME=$GIT_AUTHOR_NAME
GIT_AUTHOR_EMAIL=$GIT_AUTHOR_EMAIL
GIT_COMMITTER_NAME=$GIT_COMMITTER_NAME
GIT_COMMITTER_EMAIL=$GIT_COMMITTER_EMAIL
EOF
git var GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_COMMITTER_NAME GIT_COMMITTER_EMAIL >actual &&
test_cmp expect actual
'

test_expect_success 'get multiple variables with -z' '
test_tick &&
printf "GIT_AUTHOR_NAME\n%sQGIT_AUTHOR_EMAIL\n%sQ" \
"$GIT_AUTHOR_NAME" "$GIT_AUTHOR_EMAIL" >expect &&
git var -z GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL >actual.raw &&
nul_to_q <actual.raw >actual &&
test_cmp expect actual
'

test_expect_success 'get multi-valued variable with -z' '
TRASHDIR="$(test-tool path-utils normalize_path_copy "$(pwd)")" &&
HOME="$TRASHDIR" XDG_CONFIG_HOME="$TRASHDIR/foo" git var -z GIT_CONFIG_GLOBAL >actual.raw &&
printf "%sQ%sQ" "$TRASHDIR/foo/git/config" "$TRASHDIR/.gitconfig" >expect &&
nul_to_q <actual.raw >actual &&
test_cmp expect actual
'

test_expect_success 'git var -l -z' '
git var -l -z >actual &&
tr "\0" "\n" <actual >actual.lines &&
echo "$GIT_AUTHOR_NAME" >expect &&
sed -n "/^GIT_AUTHOR_NAME$/{n;p;}" actual.lines >actual.author &&
test_cmp expect actual.author &&
echo false >expect &&
sed -n "/^core\.bare$/{n;p;}" actual.lines >actual.bare &&
test_cmp expect actual.bare
'

test_expect_success 'get GIT_SIGNING_KEY with user.signingkey configured' '
test_config user.signingkey "TEST_KEY_ID" &&
echo "TEST_KEY_ID" >expect &&
git var GIT_SIGNING_KEY >actual &&
test_cmp expect actual
'

test_expect_success 'get GIT_SIGNING_KEY fails when unset' '
test_config user.signingkey "" &&
test_must_fail git var GIT_SIGNING_KEY
'

test_expect_success 'git var -l lists new variables' '
git var -l >actual &&
test_grep "^GIT_AUTHOR_NAME=" actual &&
test_grep "^GIT_AUTHOR_EMAIL=" actual &&
test_grep "^GIT_AUTHOR_DATE=" actual &&
test_grep "^GIT_COMMITTER_NAME=" actual &&
test_grep "^GIT_COMMITTER_EMAIL=" actual &&
test_grep "^GIT_COMMITTER_DATE=" actual
'

test_expect_success 'git var -l lists GIT_SIGNING_KEY when configured' '
test_config user.signingkey "TEST_KEY_ID" &&
git var -l >actual &&
test_grep "^GIT_SIGNING_KEY=TEST_KEY_ID" actual
'

test_expect_success 'options must precede variable arguments' '
test_must_fail git var GIT_AUTHOR_NAME -z
'

test_expect_success 'get multiple variables with unset variable exits with 1 and omits unset' '
test_config user.signingkey "" &&
cat >expect <<-EOF &&
GIT_AUTHOR_NAME=$GIT_AUTHOR_NAME
GIT_COMMITTER_NAME=$GIT_COMMITTER_NAME
EOF
test_expect_code 1 git var GIT_AUTHOR_NAME GIT_SIGNING_KEY GIT_COMMITTER_NAME >actual &&
test_cmp expect actual
'

test_expect_success 'get multiple variables with -z and unset variable' '
test_config user.signingkey "" &&
printf "GIT_AUTHOR_NAME\n%sQGIT_COMMITTER_NAME\n%sQ" \
"$GIT_AUTHOR_NAME" "$GIT_COMMITTER_NAME" >expect &&
test_expect_code 1 git var -z GIT_AUTHOR_NAME GIT_SIGNING_KEY GIT_COMMITTER_NAME >actual.raw &&
nul_to_q <actual.raw >actual &&
test_cmp expect actual
'

test_expect_success 'get multiple variables including multi-valued variable' '
TRASHDIR="$(test-tool path-utils normalize_path_copy "$(pwd)")" &&
cat >expect <<-EOF &&
GIT_AUTHOR_NAME=$GIT_AUTHOR_NAME
GIT_CONFIG_GLOBAL=$TRASHDIR/foo/git/config
GIT_CONFIG_GLOBAL=$TRASHDIR/.gitconfig
GIT_AUTHOR_EMAIL=$GIT_AUTHOR_EMAIL
EOF
HOME="$TRASHDIR" XDG_CONFIG_HOME="$TRASHDIR/foo" \
git var GIT_AUTHOR_NAME GIT_CONFIG_GLOBAL GIT_AUTHOR_EMAIL >actual &&
test_cmp expect actual
'

test_expect_success 'get multiple variables including multi-valued variable with -z' '
TRASHDIR="$(test-tool path-utils normalize_path_copy "$(pwd)")" &&
printf "GIT_AUTHOR_NAME\n%sQGIT_CONFIG_GLOBAL\n%sQGIT_CONFIG_GLOBAL\n%sQGIT_AUTHOR_EMAIL\n%sQ" \
"$GIT_AUTHOR_NAME" \
"$TRASHDIR/foo/git/config" "$TRASHDIR/.gitconfig" \
"$GIT_AUTHOR_EMAIL" >expect &&
HOME="$TRASHDIR" XDG_CONFIG_HOME="$TRASHDIR/foo" \
git var -z GIT_AUTHOR_NAME GIT_CONFIG_GLOBAL GIT_AUTHOR_EMAIL >actual.raw &&
nul_to_q <actual.raw >actual &&
test_cmp expect actual
'

test_expect_success 'get multiple variables with unset multi-valued variable' '
cat >expect <<-EOF &&
GIT_AUTHOR_NAME=$GIT_AUTHOR_NAME
GIT_AUTHOR_EMAIL=$GIT_AUTHOR_EMAIL
EOF
test_env GIT_CONFIG_GLOBAL= test_expect_code 1 git var GIT_AUTHOR_NAME GIT_CONFIG_GLOBAL GIT_AUTHOR_EMAIL >actual &&
test_cmp expect actual
'

test_done