Skip to content

Commit 873cd28

Browse files
peffgitster
authored andcommitted
argv-array: rename to strvec
The name "argv-array" isn't very good, because it describes what the data type can be used for (program argument arrays), not what it actually is (a dynamically-growing string array that maintains a NULL-terminator invariant). This leads to people being hesitant to use it for other cases where it would actually be a good fit. The existing name is also clunky to use. It's overly long, and the name often leads to saying things like "argv.argv" (i.e., the field names overlap with variable names, since they're describing the use, not the type). Let's give it a more neutral name. I settled on "strvec" because "vector" is the name for a dynamic array type in many programming languages. "strarray" would work, too, but it's longer and a bit more awkward to say (and don't we all say these things in our mind as we type them?). A more extreme direction would be a generic data structure which stores a NULL-terminated of _any_ type. That would be easy to do with void pointers, but we'd lose some type safety for the existing cases. Plus it raises questions about memory allocation and ownership. So I limited myself here to changing names only, and not semantics. If we do find a use for that more generic data type, we could perhaps implement it at a lower level and then provide type-safe wrappers around it for strings. But that can come later. This patch does the minimum to convert the struct and function names in the header and implementation, leaving a few things for follow-on patches: - files retain their original names for now - struct field names are retained for now - there's a preprocessor compat layer that lets most users remain the same for now. The exception is headers which made a manual forward declaration of the struct. I've converted them (and their dependent function declarations) here. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 819f0e7 commit 873cd28

File tree

12 files changed

+74
-61
lines changed

12 files changed

+74
-61
lines changed

argv-array.c

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,32 @@
22
#include "argv-array.h"
33
#include "strbuf.h"
44

5-
const char *empty_argv[] = { NULL };
5+
const char *empty_strvec[] = { NULL };
66

7-
void argv_array_init(struct argv_array *array)
7+
void strvec_init(struct strvec *array)
88
{
9-
array->argv = empty_argv;
9+
array->argv = empty_strvec;
1010
array->argc = 0;
1111
array->alloc = 0;
1212
}
1313

14-
static void argv_array_push_nodup(struct argv_array *array, const char *value)
14+
static void strvec_push_nodup(struct strvec *array, const char *value)
1515
{
16-
if (array->argv == empty_argv)
16+
if (array->argv == empty_strvec)
1717
array->argv = NULL;
1818

1919
ALLOC_GROW(array->argv, array->argc + 2, array->alloc);
2020
array->argv[array->argc++] = value;
2121
array->argv[array->argc] = NULL;
2222
}
2323

24-
const char *argv_array_push(struct argv_array *array, const char *value)
24+
const char *strvec_push(struct strvec *array, const char *value)
2525
{
26-
argv_array_push_nodup(array, xstrdup(value));
26+
strvec_push_nodup(array, xstrdup(value));
2727
return array->argv[array->argc - 1];
2828
}
2929

30-
const char *argv_array_pushf(struct argv_array *array, const char *fmt, ...)
30+
const char *strvec_pushf(struct strvec *array, const char *fmt, ...)
3131
{
3232
va_list ap;
3333
struct strbuf v = STRBUF_INIT;
@@ -36,28 +36,28 @@ const char *argv_array_pushf(struct argv_array *array, const char *fmt, ...)
3636
strbuf_vaddf(&v, fmt, ap);
3737
va_end(ap);
3838

39-
argv_array_push_nodup(array, strbuf_detach(&v, NULL));
39+
strvec_push_nodup(array, strbuf_detach(&v, NULL));
4040
return array->argv[array->argc - 1];
4141
}
4242

43-
void argv_array_pushl(struct argv_array *array, ...)
43+
void strvec_pushl(struct strvec *array, ...)
4444
{
4545
va_list ap;
4646
const char *arg;
4747

4848
va_start(ap, array);
4949
while ((arg = va_arg(ap, const char *)))
50-
argv_array_push(array, arg);
50+
strvec_push(array, arg);
5151
va_end(ap);
5252
}
5353

54-
void argv_array_pushv(struct argv_array *array, const char **argv)
54+
void strvec_pushv(struct strvec *array, const char **argv)
5555
{
5656
for (; *argv; argv++)
57-
argv_array_push(array, *argv);
57+
strvec_push(array, *argv);
5858
}
5959

60-
void argv_array_pop(struct argv_array *array)
60+
void strvec_pop(struct strvec *array)
6161
{
6262
if (!array->argc)
6363
return;
@@ -66,7 +66,7 @@ void argv_array_pop(struct argv_array *array)
6666
array->argc--;
6767
}
6868

69-
void argv_array_split(struct argv_array *array, const char *to_split)
69+
void strvec_split(struct strvec *array, const char *to_split)
7070
{
7171
while (isspace(*to_split))
7272
to_split++;
@@ -78,32 +78,32 @@ void argv_array_split(struct argv_array *array, const char *to_split)
7878

7979
while (*p && !isspace(*p))
8080
p++;
81-
argv_array_push_nodup(array, xstrndup(to_split, p - to_split));
81+
strvec_push_nodup(array, xstrndup(to_split, p - to_split));
8282

8383
while (isspace(*p))
8484
p++;
8585
to_split = p;
8686
}
8787
}
8888

89-
void argv_array_clear(struct argv_array *array)
89+
void strvec_clear(struct strvec *array)
9090
{
91-
if (array->argv != empty_argv) {
91+
if (array->argv != empty_strvec) {
9292
int i;
9393
for (i = 0; i < array->argc; i++)
9494
free((char *)array->argv[i]);
9595
free(array->argv);
9696
}
97-
argv_array_init(array);
97+
strvec_init(array);
9898
}
9999

100-
const char **argv_array_detach(struct argv_array *array)
100+
const char **strvec_detach(struct strvec *array)
101101
{
102-
if (array->argv == empty_argv)
102+
if (array->argv == empty_strvec)
103103
return xcalloc(1, sizeof(const char *));
104104
else {
105105
const char **ret = array->argv;
106-
argv_array_init(array);
106+
strvec_init(array);
107107
return ret;
108108
}
109109
}

argv-array.h

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,76 +14,89 @@
1414
* it contains an item structure with a `util` field that is not compatible
1515
* with the traditional argv interface.
1616
*
17-
* Each `argv_array` manages its own memory. Any strings pushed into the
18-
* array are duplicated, and all memory is freed by argv_array_clear().
17+
* Each `strvec` manages its own memory. Any strings pushed into the
18+
* array are duplicated, and all memory is freed by strvec_clear().
1919
*/
2020

21-
extern const char *empty_argv[];
21+
extern const char *empty_strvec[];
2222

2323
/**
2424
* A single array. This should be initialized by assignment from
25-
* `ARGV_ARRAY_INIT`, or by calling `argv_array_init`. The `argv`
25+
* `STRVEC_INIT`, or by calling `strvec_init`. The `argv`
2626
* member contains the actual array; the `argc` member contains the
2727
* number of elements in the array, not including the terminating
2828
* NULL.
2929
*/
30-
struct argv_array {
30+
struct strvec {
3131
const char **argv;
3232
size_t argc;
3333
size_t alloc;
3434
};
3535

36-
#define ARGV_ARRAY_INIT { empty_argv, 0, 0 }
36+
#define STRVEC_INIT { empty_strvec, 0, 0 }
3737

3838
/**
3939
* Initialize an array. This is no different than assigning from
40-
* `ARGV_ARRAY_INIT`.
40+
* `STRVEC_INIT`.
4141
*/
42-
void argv_array_init(struct argv_array *);
42+
void strvec_init(struct strvec *);
4343

4444
/* Push a copy of a string onto the end of the array. */
45-
const char *argv_array_push(struct argv_array *, const char *);
45+
const char *strvec_push(struct strvec *, const char *);
4646

4747
/**
4848
* Format a string and push it onto the end of the array. This is a
49-
* convenience wrapper combining `strbuf_addf` and `argv_array_push`.
49+
* convenience wrapper combining `strbuf_addf` and `strvec_push`.
5050
*/
5151
__attribute__((format (printf,2,3)))
52-
const char *argv_array_pushf(struct argv_array *, const char *fmt, ...);
52+
const char *strvec_pushf(struct strvec *, const char *fmt, ...);
5353

5454
/**
5555
* Push a list of strings onto the end of the array. The arguments
5656
* should be a list of `const char *` strings, terminated by a NULL
5757
* argument.
5858
*/
5959
LAST_ARG_MUST_BE_NULL
60-
void argv_array_pushl(struct argv_array *, ...);
60+
void strvec_pushl(struct strvec *, ...);
6161

6262
/* Push a null-terminated array of strings onto the end of the array. */
63-
void argv_array_pushv(struct argv_array *, const char **);
63+
void strvec_pushv(struct strvec *, const char **);
6464

6565
/**
6666
* Remove the final element from the array. If there are no
6767
* elements in the array, do nothing.
6868
*/
69-
void argv_array_pop(struct argv_array *);
69+
void strvec_pop(struct strvec *);
7070

7171
/* Splits by whitespace; does not handle quoted arguments! */
72-
void argv_array_split(struct argv_array *, const char *);
72+
void strvec_split(struct strvec *, const char *);
7373

7474
/**
7575
* Free all memory associated with the array and return it to the
7676
* initial, empty state.
7777
*/
78-
void argv_array_clear(struct argv_array *);
78+
void strvec_clear(struct strvec *);
7979

8080
/**
81-
* Disconnect the `argv` member from the `argv_array` struct and
81+
* Disconnect the `argv` member from the `strvec` struct and
8282
* return it. The caller is responsible for freeing the memory used
8383
* by the array, and by the strings it references. After detaching,
84-
* the `argv_array` is in a reinitialized state and can be pushed
84+
* the `strvec` is in a reinitialized state and can be pushed
8585
* into again.
8686
*/
87-
const char **argv_array_detach(struct argv_array *);
87+
const char **strvec_detach(struct strvec *);
88+
89+
/* compatibility for historic argv_array interface */
90+
#define argv_array strvec
91+
#define ARGV_ARRAY_INIT STRVEC_INIT
92+
#define argv_array_init strvec_init
93+
#define argv_array_push strvec_push
94+
#define argv_array_pushf strvec_pushf
95+
#define argv_array_pushl strvec_pushl
96+
#define argv_array_pushv strvec_pushv
97+
#define argv_array_pop strvec_pop
98+
#define argv_array_split strvec_split
99+
#define argv_array_clear strvec_clear
100+
#define argv_array_detach strvec_detach
88101

89102
#endif /* ARGV_ARRAY_H */

exec-cmd.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
#ifndef GIT_EXEC_CMD_H
22
#define GIT_EXEC_CMD_H
33

4-
struct argv_array;
4+
struct strvec;
55

66
void git_set_exec_path(const char *exec_path);
77
void git_resolve_executable_dir(const char *path);
88
const char *git_exec_path(void);
99
void setup_path(void);
10-
const char **prepare_git_cmd(struct argv_array *out, const char **argv);
10+
const char **prepare_git_cmd(struct strvec *out, const char **argv);
1111
int execv_git_cmd(const char **argv); /* NULL terminated */
1212
LAST_ARG_MUST_BE_NULL
1313
int execl_git_cmd(const char *cmd, ...);

ls-refs.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
#define LS_REFS_H
33

44
struct repository;
5-
struct argv_array;
5+
struct strvec;
66
struct packet_reader;
7-
int ls_refs(struct repository *r, struct argv_array *keys,
7+
int ls_refs(struct repository *r, struct strvec *keys,
88
struct packet_reader *request);
99

1010
#endif /* LS_REFS_H */

quote.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ int sq_dequote_to_argv(char *arg, const char ***argv, int *nr, int *alloc);
6060
* still modify arg in place, but unlike sq_dequote_to_argv, the argv_array
6161
* will duplicate and take ownership of the strings.
6262
*/
63-
struct argv_array;
64-
int sq_dequote_to_argv_array(char *arg, struct argv_array *);
63+
struct strvec;
64+
int sq_dequote_to_argv_array(char *arg, struct strvec *);
6565

6666
int unquote_c_style(struct strbuf *, const char *quoted, const char **endp);
6767
size_t quote_c_style(const char *name, struct strbuf *, FILE *, int no_dq);

refs.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,8 @@ int refname_match(const char *abbrev_name, const char *full_name);
145145
* Given a 'prefix' expand it by the rules in 'ref_rev_parse_rules' and add
146146
* the results to 'prefixes'
147147
*/
148-
struct argv_array;
149-
void expand_ref_prefix(struct argv_array *prefixes, const char *prefix);
148+
struct strvec;
149+
void expand_ref_prefix(struct strvec *prefixes, const char *prefix);
150150

151151
int expand_ref(struct repository *r, const char *str, int len, struct object_id *oid, char **ref);
152152
int repo_dwim_ref(struct repository *r, const char *str, int len, struct object_id *oid, char **ref);

refspec.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,12 @@ void refspec_clear(struct refspec *rs);
6060

6161
int valid_fetch_refspec(const char *refspec);
6262

63-
struct argv_array;
63+
struct strvec;
6464
/*
6565
* Determine what <prefix> values to pass to the peer in ref-prefix lines
6666
* (see Documentation/technical/protocol-v2.txt).
6767
*/
6868
void refspec_ref_prefixes(const struct refspec *rs,
69-
struct argv_array *ref_prefixes);
69+
struct strvec *ref_prefixes);
7070

7171
#endif /* REFSPEC_H */

remote.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ void free_refs(struct ref *ref);
168168

169169
struct oid_array;
170170
struct packet_reader;
171-
struct argv_array;
171+
struct strvec;
172172
struct string_list;
173173
struct ref **get_remote_heads(struct packet_reader *reader,
174174
struct ref **list, unsigned int flags,
@@ -178,7 +178,7 @@ struct ref **get_remote_heads(struct packet_reader *reader,
178178
/* Used for protocol v2 in order to retrieve refs from a remote */
179179
struct ref **get_remote_refs(int fd_out, struct packet_reader *reader,
180180
struct ref **list, int for_push,
181-
const struct argv_array *ref_prefixes,
181+
const struct strvec *ref_prefixes,
182182
const struct string_list *server_options,
183183
int stateless_rpc);
184184

serve.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#ifndef SERVE_H
22
#define SERVE_H
33

4-
struct argv_array;
5-
int has_capability(const struct argv_array *keys, const char *capability,
4+
struct strvec;
5+
int has_capability(const struct strvec *keys, const char *capability,
66
const char **value);
77

88
struct serve_options {

submodule.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#ifndef SUBMODULE_H
22
#define SUBMODULE_H
33

4-
struct argv_array;
4+
struct strvec;
55
struct cache_entry;
66
struct diff_options;
77
struct index_state;
@@ -84,7 +84,7 @@ int should_update_submodules(void);
8484
const struct submodule *submodule_from_ce(const struct cache_entry *ce);
8585
void check_for_new_submodule_commits(struct object_id *oid);
8686
int fetch_populated_submodules(struct repository *r,
87-
const struct argv_array *options,
87+
const struct strvec *options,
8888
const char *prefix,
8989
int command_line_option,
9090
int default_option,
@@ -143,7 +143,7 @@ void submodule_unset_core_worktree(const struct submodule *sub);
143143
* a submodule by clearing any repo-specific environment variables, but
144144
* retaining any config in the environment.
145145
*/
146-
void prepare_submodule_repo_env(struct argv_array *out);
146+
void prepare_submodule_repo_env(struct strvec *out);
147147

148148
#define ABSORB_GITDIR_RECURSE_SUBMODULES (1<<0)
149149
void absorb_git_dir_into_superproject(const char *path,

0 commit comments

Comments
 (0)