Use environment variables when creating signatures

When creating an action signature (e.g. for a commit author and
committer) read the following environment variables that can override
the configuration options:

 * `GIT_AUTHOR_NAME` is the human-readable name in the "author" field.
 * `GIT_AUTHOR_EMAIL` is the email for the "author" field.
 * `GIT_AUTHOR_DATE` is the timestamp used for the "author" field.
 * `GIT_COMMITTER_NAME` sets the human name for the "committer" field.
 * `GIT_COMMITTER_EMAIL` is the email address for the "committer" field.
 * `GIT_COMMITTER_DATE` is used for the timestamp in the "committer"
   field.
 * `EMAIL` is the fallback email address in case the user.email
   configuration value isn't set. If this isn't set, Git falls back to
   the system user and host names.

This is taken from the git documentation chapter "10.8 Environment
Variables":

https://git-scm.com/book/en/v2/Git-Internals-Environment-Variables

This PR adds support for reading these environment variables by adding
two new functions `git_signature_default_author` and
`git_signature_default_committer` and deprecates the
`git_signature_default` function.

Fixes: https://github.com/libgit2/libgit2/issues/3751

Prior work:
 * https://github.com/libgit2/libgit2/pull/4409
 * https://github.com/libgit2/libgit2/pull/5479
 * https://github.com/libgit2/libgit2/pull/6290
This commit is contained in:
u_quark
2023-12-18 15:26:55 +00:00
parent 25e2b9d8c2
commit f62abd00db
15 changed files with 258 additions and 21 deletions

View File

@@ -39,7 +39,7 @@ int lg2_commit(git_repository *repo, int argc, char **argv)
git_index *index;
git_object *parent = NULL;
git_reference *ref = NULL;
git_signature *signature;
git_signature *author_signature, *committer_signature;
/* Validate args */
if (argc < 3 || strcmp(opt, "-m") != 0) {
@@ -63,21 +63,25 @@ int lg2_commit(git_repository *repo, int argc, char **argv)
check_lg2(git_tree_lookup(&tree, repo, &tree_oid), "Error looking up tree", NULL);
check_lg2(git_signature_default(&signature, repo), "Error creating signature", NULL);
check_lg2(git_signature_default_author(&author_signature, repo),
"Error creating author signature", NULL);
check_lg2(git_signature_default_committer(&committer_signature, repo),
"Error creating committer signature", NULL);
check_lg2(git_commit_create_v(
&commit_oid,
repo,
"HEAD",
signature,
signature,
author_signature,
committer_signature,
NULL,
comment,
tree,
parent ? 1 : 0, parent), "Error creating commit", NULL);
git_index_free(index);
git_signature_free(signature);
git_signature_free(author_signature);
git_signature_free(committer_signature);
git_tree_free(tree);
git_object_free(parent);
git_reference_free(ref);

View File

@@ -123,14 +123,15 @@ int lg2_init(git_repository *repo, int argc, char *argv[])
*/
static void create_initial_commit(git_repository *repo)
{
git_signature *sig;
git_signature *author_sig, *committer_sig;
git_index *index;
git_oid tree_id, commit_id;
git_tree *tree;
/** First use the config to initialize a commit signature for the user. */
if (git_signature_default(&sig, repo) < 0)
if ((git_signature_default_author(&author_sig, repo) < 0) ||
(git_signature_default_committer(&committer_sig, repo) < 0))
fatal("Unable to create a commit signature.",
"Perhaps 'user.name' and 'user.email' are not set");
@@ -162,14 +163,15 @@ static void create_initial_commit(git_repository *repo)
*/
if (git_commit_create_v(
&commit_id, repo, "HEAD", sig, sig,
&commit_id, repo, "HEAD", author_sig, committer_sig,
NULL, "Initial commit", tree, 0) < 0)
fatal("Could not create the initial commit", NULL);
/** Clean up so we don't leak memory. */
git_tree_free(tree);
git_signature_free(sig);
git_signature_free(author_sig);
git_signature_free(committer_sig);
}
static void usage(const char *error, const char *arg)

View File

@@ -108,7 +108,7 @@ static int cmd_push(git_repository *repo, struct opts *opts)
if (opts->argc)
usage("push does not accept any parameters");
check_lg2(git_signature_default(&signature, repo),
check_lg2(git_signature_default_author(&signature, repo),
"Unable to get signature", NULL);
check_lg2(git_stash_save(&stashid, repo, signature, NULL, GIT_STASH_DEFAULT),
"Unable to save stash", NULL);

View File

@@ -226,7 +226,7 @@ static void action_create_tag(tag_state *state)
check_lg2(git_revparse_single(&target, repo, opts->target),
"Unable to resolve spec", opts->target);
check_lg2(git_signature_default(&tagger, repo),
check_lg2(git_signature_default_author(&tagger, repo),
"Unable to create signature", NULL);
check_lg2(git_tag_create(&oid, repo, opts->tag_name,