1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
8 #include "string-list.h"
10 struct config_alias_data
{
13 struct string_list
*list
;
16 static int config_alias_cb(const char *key
, const char *value
,
17 const struct config_context
*ctx UNUSED
, void *d
)
19 struct config_alias_data
*data
= d
;
22 if (!skip_prefix(key
, "alias.", &p
))
26 if (!strcasecmp(p
, data
->alias
)) {
27 FREE_AND_NULL(data
->v
);
28 return git_config_string(&data
->v
,
31 } else if (data
->list
) {
32 string_list_append(data
->list
, p
);
38 char *alias_lookup(const char *alias
)
40 struct config_alias_data data
= { alias
, NULL
};
42 read_early_config(the_repository
, config_alias_cb
, &data
);
47 void list_aliases(struct string_list
*list
)
49 struct config_alias_data data
= { NULL
, NULL
, list
};
51 read_early_config(the_repository
, config_alias_cb
, &data
);
54 void quote_cmdline(struct strbuf
*buf
, const char **argv
)
56 for (const char **argp
= argv
; *argp
; argp
++) {
58 strbuf_addch(buf
, ' ');
59 strbuf_addch(buf
, '"');
60 for (const char *p
= *argp
; *p
; p
++) {
63 if (c
== '"' || c
=='\\')
64 strbuf_addch(buf
, '\\');
67 strbuf_addch(buf
, '"');
71 #define SPLIT_CMDLINE_BAD_ENDING 1
72 #define SPLIT_CMDLINE_UNCLOSED_QUOTE 2
73 #define SPLIT_CMDLINE_ARGC_OVERFLOW 3
74 static const char *split_cmdline_errors
[] = {
75 N_("cmdline ends with \\"),
77 N_("too many arguments"),
80 int split_cmdline(char *cmdline
, const char ***argv
)
82 size_t src
, dst
, count
= 0, size
= 16;
85 ALLOC_ARRAY(*argv
, size
);
87 /* split alias_string */
88 (*argv
)[count
++] = cmdline
;
89 for (src
= dst
= 0; cmdline
[src
];) {
90 char c
= cmdline
[src
];
91 if (!quoted
&& isspace(c
)) {
94 && isspace(cmdline
[src
]))
96 ALLOC_GROW(*argv
, count
+ 1, size
);
97 (*argv
)[count
++] = cmdline
+ dst
;
98 } else if (!quoted
&& (c
== '\'' || c
== '"')) {
101 } else if (c
== quoted
) {
105 if (c
== '\\' && quoted
!= '\'') {
109 FREE_AND_NULL(*argv
);
110 return -SPLIT_CMDLINE_BAD_ENDING
;
121 FREE_AND_NULL(*argv
);
122 return -SPLIT_CMDLINE_UNCLOSED_QUOTE
;
125 if (count
>= INT_MAX
) {
126 FREE_AND_NULL(*argv
);
127 return -SPLIT_CMDLINE_ARGC_OVERFLOW
;
130 ALLOC_GROW(*argv
, count
+ 1, size
);
131 (*argv
)[count
] = NULL
;
136 const char *split_cmdline_strerror(int split_cmdline_errno
)
138 return split_cmdline_errors
[-split_cmdline_errno
- 1];