]> git.ipfire.org Git - thirdparty/git.git/blame - alias.c
Documentation/RelNotes/2.45.0.txt: fix typo
[thirdparty/git.git] / alias.c
CommitLineData
36bf1958 1#include "git-compat-util.h"
65b5f948 2#include "alias.h"
b2141fc1 3#include "config.h"
36bf1958 4#include "gettext.h"
4960e5c7 5#include "strbuf.h"
3301d36b 6#include "string-list.h"
94351118 7
a9bcf658
JS
8struct config_alias_data {
9 const char *alias;
10 char *v;
3301d36b 11 struct string_list *list;
a9bcf658
JS
12};
13
a4e7e317
GC
14static int config_alias_cb(const char *key, const char *value,
15 const struct config_context *ctx UNUSED, void *d)
a9bcf658
JS
16{
17 struct config_alias_data *data = d;
18 const char *p;
19
3301d36b
NTND
20 if (!skip_prefix(key, "alias.", &p))
21 return 0;
22
23 if (data->alias) {
24 if (!strcasecmp(p, data->alias))
25 return git_config_string((const char **)&data->v,
26 key, value);
27 } else if (data->list) {
28 string_list_append(data->list, p);
29 }
a9bcf658
JS
30
31 return 0;
32}
33
94351118
JK
34char *alias_lookup(const char *alias)
35{
a9bcf658
JS
36 struct config_alias_data data = { alias, NULL };
37
38 read_early_config(config_alias_cb, &data);
39
40 return data.v;
94351118 41}
0989fe96 42
3301d36b
NTND
43void list_aliases(struct string_list *list)
44{
45 struct config_alias_data data = { NULL, NULL, list };
46
47 read_early_config(config_alias_cb, &data);
48}
49
4960e5c7
PW
50void quote_cmdline(struct strbuf *buf, const char **argv)
51{
52 for (const char **argp = argv; *argp; argp++) {
53 if (argp != argv)
54 strbuf_addch(buf, ' ');
55 strbuf_addch(buf, '"');
56 for (const char *p = *argp; *p; p++) {
57 const char c = *p;
58
59 if (c == '"' || c =='\\')
60 strbuf_addch(buf, '\\');
61 strbuf_addch(buf, c);
62 }
63 strbuf_addch(buf, '"');
64 }
65}
66
ad9ac6db
GB
67#define SPLIT_CMDLINE_BAD_ENDING 1
68#define SPLIT_CMDLINE_UNCLOSED_QUOTE 2
0ca6ead8 69#define SPLIT_CMDLINE_ARGC_OVERFLOW 3
ad9ac6db 70static const char *split_cmdline_errors[] = {
a7412ae1 71 N_("cmdline ends with \\"),
0ca6ead8
KB
72 N_("unclosed quote"),
73 N_("too many arguments"),
ad9ac6db
GB
74};
75
0989fe96
MV
76int split_cmdline(char *cmdline, const char ***argv)
77{
0ca6ead8 78 size_t src, dst, count = 0, size = 16;
0989fe96
MV
79 char quoted = 0;
80
b32fa95f 81 ALLOC_ARRAY(*argv, size);
0989fe96
MV
82
83 /* split alias_string */
84 (*argv)[count++] = cmdline;
85 for (src = dst = 0; cmdline[src];) {
86 char c = cmdline[src];
87 if (!quoted && isspace(c)) {
88 cmdline[dst++] = 0;
89 while (cmdline[++src]
90 && isspace(cmdline[src]))
91 ; /* skip */
cc108379 92 ALLOC_GROW(*argv, count + 1, size);
0989fe96
MV
93 (*argv)[count++] = cmdline + dst;
94 } else if (!quoted && (c == '\'' || c == '"')) {
95 quoted = c;
96 src++;
97 } else if (c == quoted) {
98 quoted = 0;
99 src++;
100 } else {
101 if (c == '\\' && quoted != '\'') {
102 src++;
103 c = cmdline[src];
104 if (!c) {
6a83d902 105 FREE_AND_NULL(*argv);
ad9ac6db 106 return -SPLIT_CMDLINE_BAD_ENDING;
0989fe96
MV
107 }
108 }
109 cmdline[dst++] = c;
110 src++;
111 }
112 }
113
114 cmdline[dst] = 0;
115
116 if (quoted) {
6a83d902 117 FREE_AND_NULL(*argv);
ad9ac6db 118 return -SPLIT_CMDLINE_UNCLOSED_QUOTE;
0989fe96
MV
119 }
120
0ca6ead8
KB
121 if (count >= INT_MAX) {
122 FREE_AND_NULL(*argv);
123 return -SPLIT_CMDLINE_ARGC_OVERFLOW;
124 }
125
cc108379 126 ALLOC_GROW(*argv, count + 1, size);
27d5438d
JK
127 (*argv)[count] = NULL;
128
0989fe96
MV
129 return count;
130}
131
cc108379
FC
132const char *split_cmdline_strerror(int split_cmdline_errno)
133{
134 return split_cmdline_errors[-split_cmdline_errno - 1];
ad9ac6db 135}