Commit 1c381acf by Edward Thomson

cli: accept configuration on the command line

Introduce `cli_repository_open` which will reparse command-line options
looking for `-c` or `--config-env`. Add those values to an in-memory
configuration database and configure the opened repository with that.
parent 87796841
/*
* Copyright (C) the libgit2 contributors. All rights reserved.
*
* This file is part of libgit2, distributed under the GNU GPL v2 with
* a Linking Exception. For full terms see the included COPYING file.
*/
#ifndef CLI_cli_h__
#define CLI_cli_h__
#define PROGRAM_NAME "git2"
#include "git2_util.h"
#include "error.h"
#include "opt.h"
#include "opt_usage.h"
#include "sighandler.h"
#endif /* CLI_cli_h__ */
......@@ -5,7 +5,7 @@
* a Linking Exception. For full terms see the included COPYING file.
*/
#include "cli.h"
#include "common.h"
#include "cmd.h"
const cli_cmd_spec *cli_cmd_spec_byname(const char *name)
......
......@@ -6,7 +6,7 @@
*/
#include <git2.h>
#include "cli.h"
#include "common.h"
#include "cmd.h"
#define COMMAND_NAME "cat-file"
......
......@@ -7,7 +7,7 @@
#include <stdio.h>
#include <git2.h>
#include "cli.h"
#include "common.h"
#include "cmd.h"
#include "error.h"
#include "sighandler.h"
......
......@@ -6,7 +6,8 @@
*/
#include <git2.h>
#include "cli.h"
#include "common.h"
#include "cmd.h"
#define COMMAND_NAME "config"
......@@ -24,10 +25,20 @@ static int show_help;
static int null_separator;
static char *name;
#define CLI_COMMON_OPT_HELP \
CLI_OPT_TYPE_SWITCH, "help", 0, &show_help, 1, \
CLI_OPT_USAGE_HIDDEN | CLI_OPT_USAGE_STOP_PARSING
#define CLI_COMMON_OPT_CONFIG \
CLI_OPT_TYPE_VALUE, NULL, 'c', NULL, 0, \
CLI_OPT_USAGE_HIDDEN
#define CLI_COMMON_OPT_CONFIG_ENV \
CLI_OPT_TYPE_VALUE, "config-env", 0, NULL, 0, \
CLI_OPT_USAGE_HIDDEN
static const cli_opt_spec opts[] = {
{ CLI_OPT_TYPE_SWITCH, "help", 0, &show_help, 1,
CLI_OPT_USAGE_HIDDEN | CLI_OPT_USAGE_STOP_PARSING, NULL,
"display help about the " COMMAND_NAME " command" },
{ CLI_COMMON_OPT_HELP },
{ CLI_COMMON_OPT_CONFIG },
{ CLI_COMMON_OPT_CONFIG_ENV },
{ CLI_OPT_TYPE_SWITCH, "null", 'z', &null_separator, 1,
0, NULL, "use NUL as a separator" },
......@@ -137,6 +148,7 @@ int cmd_config(int argc, char **argv)
{
git_repository *repo = NULL;
git_config *config = NULL;
cli_repository_open_options open_opts = { argv + 1, argc - 1};
cli_opt invalid_opt;
int ret = 0;
......@@ -148,7 +160,7 @@ int cmd_config(int argc, char **argv)
return 0;
}
if (git_repository_open_ext(&repo, ".", GIT_REPOSITORY_OPEN_FROM_ENV, NULL) < 0 ||
if (cli_repository_open(&repo, &open_opts) < 0 ||
git_repository_config(&config, repo) < 0) {
ret = cli_error_git();
goto done;
......
......@@ -6,7 +6,7 @@
*/
#include <git2.h>
#include "cli.h"
#include "common.h"
#include "cmd.h"
#include "futils.h"
......
......@@ -7,7 +7,7 @@
#include <stdio.h>
#include <git2.h>
#include "cli.h"
#include "common.h"
#include "cmd.h"
#define COMMAND_NAME "help"
......
/*
* Copyright (C) the libgit2 contributors. All rights reserved.
*
* This file is part of libgit2, distributed under the GNU GPL v2 with
* a Linking Exception. For full terms see the included COPYING file.
*/
#include <git2.h>
#include <git2/sys/config.h>
#include "git2_util.h"
#include "vector.h"
#include "common.h"
#include "error.h"
static int parse_option(cli_opt *opt, void *data)
{
git_str kv = GIT_STR_INIT, env = GIT_STR_INIT;
git_vector *cmdline_config = data;
int error = 0;
if (opt->spec && opt->spec->alias == 'c') {
if (git_str_puts(&kv, opt->value) < 0) {
error = cli_error_git();
goto done;
}
}
else if (opt->spec && !strcmp(opt->spec->name, "config-env")) {
char *val = strchr(opt->value, '=');
if (val == NULL || *(val + 1) == '\0') {
error = cli_error("invalid config format: '%s'", opt->value);
goto done;
}
if (git_str_put(&kv, opt->value, (val - opt->value)) < 0) {
error = cli_error_git();
goto done;
}
val++;
if ((error = git__getenv(&env, val)) == GIT_ENOTFOUND) {
error = cli_error("missing environment variable '%s' for configuration '%s'", val, kv.ptr);
goto done;
} else if (error) {
error = cli_error_git();
goto done;
}
if (git_str_putc(&kv, '=') < 0 ||
git_str_puts(&kv, env.ptr) < 0) {
error = cli_error_git();
goto done;
}
}
if (kv.size > 0 &&
git_vector_insert(cmdline_config, git_str_detach(&kv)) < 0)
error = cli_error_git();
done:
git_str_dispose(&env);
git_str_dispose(&kv);
return error;
}
static int parse_common_options(
git_repository *repo,
cli_repository_open_options *opts)
{
cli_opt_spec common_opts[] = {
{ CLI_COMMON_OPT_CONFIG },
{ CLI_COMMON_OPT_CONFIG_ENV },
{ 0 }
};
git_config_backend_memory_options config_opts =
GIT_CONFIG_BACKEND_MEMORY_OPTIONS_INIT;
git_vector cmdline = GIT_VECTOR_INIT;
git_config *config = NULL;
git_config_backend *backend = NULL;
int error = 0;
config_opts.backend_type = "command line";
if ((error = cli_opt_foreach(common_opts, opts->args,
opts->args_len, CLI_OPT_PARSE_GNU, parse_option,
&cmdline)) < 0)
goto done;
if (git_vector_length(&cmdline) == 0)
goto done;
if (git_repository_config(&config, repo) < 0 ||
git_config_backend_from_values(&backend,
(const char **)cmdline.contents, cmdline.length,
&config_opts) < 0 ||
git_config_add_backend(config, backend, GIT_CONFIG_LEVEL_APP,
repo, 0) < 0)
error = cli_error_git();
done:
if (error && backend)
backend->free(backend);
git_config_free(config);
git_vector_free_deep(&cmdline);
return error;
}
int cli_repository_open(
git_repository **out,
cli_repository_open_options *opts)
{
git_repository *repo;
if (git_repository_open_ext(&repo, ".", GIT_REPOSITORY_OPEN_FROM_ENV, NULL) < 0)
return -1;
if (opts && parse_common_options(repo, opts) < 0)
return -1;
*out = repo;
return 0;
}
/*
* Copyright (C) the libgit2 contributors. All rights reserved.
*
* This file is part of libgit2, distributed under the GNU GPL v2 with
* a Linking Exception. For full terms see the included COPYING file.
*/
#ifndef CLI_common_h__
#define CLI_common_h__
#define PROGRAM_NAME "git2"
#include "git2_util.h"
#include "error.h"
#include "opt.h"
#include "opt_usage.h"
/*
* Common command arguments.
*/
#define CLI_COMMON_OPT_HELP \
CLI_OPT_TYPE_SWITCH, "help", 0, &show_help, 1, \
CLI_OPT_USAGE_HIDDEN | CLI_OPT_USAGE_STOP_PARSING
#define CLI_COMMON_OPT_CONFIG \
CLI_OPT_TYPE_VALUE, NULL, 'c', NULL, 0, \
CLI_OPT_USAGE_HIDDEN
#define CLI_COMMON_OPT_CONFIG_ENV \
CLI_OPT_TYPE_VALUE, "config-env", 0, NULL, 0, \
CLI_OPT_USAGE_HIDDEN
#define CLI_COMMON_OPT \
{ CLI_COMMON_OPT_HELP }, \
{ CLI_COMMON_OPT_CONFIG }, \
{ CLI_COMMON_OPT_CONFIG_ENV }
typedef struct {
char **args;
int args_len;
} cli_repository_open_options;
extern int cli_repository_open(
git_repository **out,
cli_repository_open_options *opts);
#endif /* CLI_common_h__ */
......@@ -8,7 +8,7 @@
#ifndef CLI_error_h__
#define CLI_error_h__
#include "cli.h"
#include "common.h"
#include <stdio.h>
#define CLI_EXIT_OK 0
......
......@@ -7,7 +7,7 @@
#include <stdio.h>
#include <git2.h>
#include "cli.h"
#include "common.h"
#include "cmd.h"
static int show_help = 0;
......
......@@ -19,7 +19,7 @@
#include <limits.h>
#include <assert.h>
#include "cli.h"
#include "common.h"
#include "opt.h"
#ifdef _WIN32
......@@ -73,7 +73,7 @@ GIT_INLINE(const cli_opt_spec *) spec_for_long(
/* Handle --option=value arguments */
if (spec->type == CLI_OPT_TYPE_VALUE &&
eql &&
spec->name && eql &&
strncmp(arg, spec->name, eql_pos) == 0 &&
spec->name[eql_pos] == '\0') {
*has_value = 1;
......@@ -575,6 +575,28 @@ cli_opt_status_t cli_opt_parse(
return validate_required(opt, specs, given_specs);
}
int cli_opt_foreach(
const cli_opt_spec specs[],
char **args,
size_t args_len,
unsigned int flags,
int (*callback)(cli_opt *, void *),
void *callback_data)
{
cli_opt_parser parser;
cli_opt opt;
int ret;
cli_opt_parser_init(&parser, specs, args, args_len, flags);
while (cli_opt_parser_next(&opt, &parser)) {
if ((ret = callback(&opt, callback_data)) != 0)
return ret;
}
return 0;
}
static int spec_name_fprint(FILE *file, const cli_opt_spec *spec)
{
int error;
......
......@@ -300,6 +300,14 @@ cli_opt_status_t cli_opt_parse(
size_t args_len,
unsigned int flags);
int cli_opt_foreach(
const cli_opt_spec specs[],
char **args,
size_t args_len,
unsigned int flags,
int (*callback)(cli_opt *, void *),
void *callback_data);
/**
* Initializes a parser that parses the given arguments according to the
* given specifications.
......
......@@ -5,7 +5,7 @@
* a Linking Exception. For full terms see the included COPYING file.
*/
#include "cli.h"
#include "common.h"
#include "str.h"
static int print_spec_name(git_str *out, const cli_opt_spec *spec)
......
......@@ -8,7 +8,8 @@
#include <stdint.h>
#include <signal.h>
#include "git2_util.h"
#include "cli.h"
#include "common.h"
#include "sighandler.h"
static void (*interrupt_handler)(void) = NULL;
......
#include <git2.h>
#include "cli.h"
#include "common.h"
......@@ -8,7 +8,7 @@
#include "git2_util.h"
#include <windows.h>
#include "cli.h"
#include "sighandler.h"
static void (*interrupt_handler)(void) = NULL;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment