summaryrefslogtreecommitdiffstats
path: root/src/cli/common.c
blob: 60b0358662b84a9a7a51dde2c17a3ecf7a3abed9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
 * 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;
}