mirror of
https://github.com/libgit2/libgit2.git
synced 2026-06-22 06:26:26 +00:00
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
158 lines
3.4 KiB
C
158 lines
3.4 KiB
C
/*
|
|
* libgit2 "stash" example - shows how to use the stash API
|
|
*
|
|
* Written by the libgit2 contributors
|
|
*
|
|
* To the extent possible under law, the author(s) have dedicated all copyright
|
|
* and related and neighboring rights to this software to the public domain
|
|
* worldwide. This software is distributed without any warranty.
|
|
*
|
|
* You should have received a copy of the CC0 Public Domain Dedication along
|
|
* with this software. If not, see
|
|
* <http://creativecommons.org/publicdomain/zero/1.0/>.
|
|
*/
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include "common.h"
|
|
|
|
enum subcmd {
|
|
SUBCMD_APPLY,
|
|
SUBCMD_LIST,
|
|
SUBCMD_POP,
|
|
SUBCMD_PUSH
|
|
};
|
|
|
|
struct opts {
|
|
enum subcmd cmd;
|
|
int argc;
|
|
char **argv;
|
|
};
|
|
|
|
static void usage(const char *fmt, ...)
|
|
{
|
|
va_list ap;
|
|
|
|
fputs("usage: git stash list\n", stderr);
|
|
fputs(" or: git stash ( pop | apply )\n", stderr);
|
|
fputs(" or: git stash [push]\n", stderr);
|
|
fputs("\n", stderr);
|
|
|
|
va_start(ap, fmt);
|
|
vfprintf(stderr, fmt, ap);
|
|
va_end(ap);
|
|
|
|
exit(1);
|
|
}
|
|
|
|
static void parse_subcommand(struct opts *opts, int argc, char *argv[])
|
|
{
|
|
char *arg = (argc < 2) ? "push" : argv[1];
|
|
enum subcmd cmd;
|
|
|
|
if (!strcmp(arg, "apply")) {
|
|
cmd = SUBCMD_APPLY;
|
|
} else if (!strcmp(arg, "list")) {
|
|
cmd = SUBCMD_LIST;
|
|
} else if (!strcmp(arg, "pop")) {
|
|
cmd = SUBCMD_POP;
|
|
} else if (!strcmp(arg, "push")) {
|
|
cmd = SUBCMD_PUSH;
|
|
} else {
|
|
usage("invalid command %s", arg);
|
|
return;
|
|
}
|
|
|
|
opts->cmd = cmd;
|
|
opts->argc = (argc < 2) ? argc - 1 : argc - 2;
|
|
opts->argv = argv;
|
|
}
|
|
|
|
static int cmd_apply(git_repository *repo, struct opts *opts)
|
|
{
|
|
if (opts->argc)
|
|
usage("apply does not accept any parameters");
|
|
|
|
check_lg2(git_stash_apply(repo, 0, NULL),
|
|
"Unable to apply stash", NULL);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int list_stash_cb(size_t index, const char *message,
|
|
const git_oid *stash_id, void *payload)
|
|
{
|
|
UNUSED(stash_id);
|
|
UNUSED(payload);
|
|
printf("stash@{%"PRIuZ"}: %s\n", index, message);
|
|
return 0;
|
|
}
|
|
|
|
static int cmd_list(git_repository *repo, struct opts *opts)
|
|
{
|
|
if (opts->argc)
|
|
usage("list does not accept any parameters");
|
|
|
|
check_lg2(git_stash_foreach(repo, list_stash_cb, NULL),
|
|
"Unable to list stashes", NULL);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int cmd_push(git_repository *repo, struct opts *opts)
|
|
{
|
|
git_signature *signature;
|
|
git_commit *stash;
|
|
git_oid stashid;
|
|
|
|
if (opts->argc)
|
|
usage("push does not accept any parameters");
|
|
|
|
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);
|
|
check_lg2(git_commit_lookup(&stash, repo, &stashid),
|
|
"Unable to lookup stash commit", NULL);
|
|
|
|
printf("Saved working directory %s\n", git_commit_summary(stash));
|
|
|
|
git_signature_free(signature);
|
|
git_commit_free(stash);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int cmd_pop(git_repository *repo, struct opts *opts)
|
|
{
|
|
if (opts->argc)
|
|
usage("pop does not accept any parameters");
|
|
|
|
check_lg2(git_stash_pop(repo, 0, NULL),
|
|
"Unable to pop stash", NULL);
|
|
|
|
printf("Dropped refs/stash@{0}\n");
|
|
|
|
return 0;
|
|
}
|
|
|
|
int lg2_stash(git_repository *repo, int argc, char *argv[])
|
|
{
|
|
struct opts opts = { 0 };
|
|
|
|
parse_subcommand(&opts, argc, argv);
|
|
|
|
switch (opts.cmd) {
|
|
case SUBCMD_APPLY:
|
|
return cmd_apply(repo, &opts);
|
|
case SUBCMD_LIST:
|
|
return cmd_list(repo, &opts);
|
|
case SUBCMD_PUSH:
|
|
return cmd_push(repo, &opts);
|
|
case SUBCMD_POP:
|
|
return cmd_pop(repo, &opts);
|
|
}
|
|
|
|
return -1;
|
|
}
|