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 <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Jeff King 2020-07-28 16:23:25 -04:00 committed by Junio C Hamano
parent 819f0e76b1
commit 873cd28a8b
12 changed files with 74 additions and 61 deletions

View File

@ -2,18 +2,18 @@
#include "argv-array.h"
#include "strbuf.h"
const char *empty_argv[] = { NULL };
const char *empty_strvec[] = { NULL };
void argv_array_init(struct argv_array *array)
void strvec_init(struct strvec *array)
{
array->argv = empty_argv;
array->argv = empty_strvec;
array->argc = 0;
array->alloc = 0;
}
static void argv_array_push_nodup(struct argv_array *array, const char *value)
static void strvec_push_nodup(struct strvec *array, const char *value)
{
if (array->argv == empty_argv)
if (array->argv == empty_strvec)
array->argv = NULL;
ALLOC_GROW(array->argv, array->argc + 2, array->alloc);
@ -21,13 +21,13 @@ static void argv_array_push_nodup(struct argv_array *array, const char *value)
array->argv[array->argc] = NULL;
}
const char *argv_array_push(struct argv_array *array, const char *value)
const char *strvec_push(struct strvec *array, const char *value)
{
argv_array_push_nodup(array, xstrdup(value));
strvec_push_nodup(array, xstrdup(value));
return array->argv[array->argc - 1];
}
const char *argv_array_pushf(struct argv_array *array, const char *fmt, ...)
const char *strvec_pushf(struct strvec *array, const char *fmt, ...)
{
va_list ap;
struct strbuf v = STRBUF_INIT;
@ -36,28 +36,28 @@ const char *argv_array_pushf(struct argv_array *array, const char *fmt, ...)
strbuf_vaddf(&v, fmt, ap);
va_end(ap);
argv_array_push_nodup(array, strbuf_detach(&v, NULL));
strvec_push_nodup(array, strbuf_detach(&v, NULL));
return array->argv[array->argc - 1];
}
void argv_array_pushl(struct argv_array *array, ...)
void strvec_pushl(struct strvec *array, ...)
{
va_list ap;
const char *arg;
va_start(ap, array);
while ((arg = va_arg(ap, const char *)))
argv_array_push(array, arg);
strvec_push(array, arg);
va_end(ap);
}
void argv_array_pushv(struct argv_array *array, const char **argv)
void strvec_pushv(struct strvec *array, const char **argv)
{
for (; *argv; argv++)
argv_array_push(array, *argv);
strvec_push(array, *argv);
}
void argv_array_pop(struct argv_array *array)
void strvec_pop(struct strvec *array)
{
if (!array->argc)
return;
@ -66,7 +66,7 @@ void argv_array_pop(struct argv_array *array)
array->argc--;
}
void argv_array_split(struct argv_array *array, const char *to_split)
void strvec_split(struct strvec *array, const char *to_split)
{
while (isspace(*to_split))
to_split++;
@ -78,7 +78,7 @@ void argv_array_split(struct argv_array *array, const char *to_split)
while (*p && !isspace(*p))
p++;
argv_array_push_nodup(array, xstrndup(to_split, p - to_split));
strvec_push_nodup(array, xstrndup(to_split, p - to_split));
while (isspace(*p))
p++;
@ -86,24 +86,24 @@ void argv_array_split(struct argv_array *array, const char *to_split)
}
}
void argv_array_clear(struct argv_array *array)
void strvec_clear(struct strvec *array)
{
if (array->argv != empty_argv) {
if (array->argv != empty_strvec) {
int i;
for (i = 0; i < array->argc; i++)
free((char *)array->argv[i]);
free(array->argv);
}
argv_array_init(array);
strvec_init(array);
}
const char **argv_array_detach(struct argv_array *array)
const char **strvec_detach(struct strvec *array)
{
if (array->argv == empty_argv)
if (array->argv == empty_strvec)
return xcalloc(1, sizeof(const char *));
else {
const char **ret = array->argv;
argv_array_init(array);
strvec_init(array);
return ret;
}
}

View File

@ -14,42 +14,42 @@
* it contains an item structure with a `util` field that is not compatible
* with the traditional argv interface.
*
* Each `argv_array` manages its own memory. Any strings pushed into the
* array are duplicated, and all memory is freed by argv_array_clear().
* Each `strvec` manages its own memory. Any strings pushed into the
* array are duplicated, and all memory is freed by strvec_clear().
*/
extern const char *empty_argv[];
extern const char *empty_strvec[];
/**
* A single array. This should be initialized by assignment from
* `ARGV_ARRAY_INIT`, or by calling `argv_array_init`. The `argv`
* `STRVEC_INIT`, or by calling `strvec_init`. The `argv`
* member contains the actual array; the `argc` member contains the
* number of elements in the array, not including the terminating
* NULL.
*/
struct argv_array {
struct strvec {
const char **argv;
size_t argc;
size_t alloc;
};
#define ARGV_ARRAY_INIT { empty_argv, 0, 0 }
#define STRVEC_INIT { empty_strvec, 0, 0 }
/**
* Initialize an array. This is no different than assigning from
* `ARGV_ARRAY_INIT`.
* `STRVEC_INIT`.
*/
void argv_array_init(struct argv_array *);
void strvec_init(struct strvec *);
/* Push a copy of a string onto the end of the array. */
const char *argv_array_push(struct argv_array *, const char *);
const char *strvec_push(struct strvec *, const char *);
/**
* Format a string and push it onto the end of the array. This is a
* convenience wrapper combining `strbuf_addf` and `argv_array_push`.
* convenience wrapper combining `strbuf_addf` and `strvec_push`.
*/
__attribute__((format (printf,2,3)))
const char *argv_array_pushf(struct argv_array *, const char *fmt, ...);
const char *strvec_pushf(struct strvec *, const char *fmt, ...);
/**
* Push a list of strings onto the end of the array. The arguments
@ -57,33 +57,46 @@ const char *argv_array_pushf(struct argv_array *, const char *fmt, ...);
* argument.
*/
LAST_ARG_MUST_BE_NULL
void argv_array_pushl(struct argv_array *, ...);
void strvec_pushl(struct strvec *, ...);
/* Push a null-terminated array of strings onto the end of the array. */
void argv_array_pushv(struct argv_array *, const char **);
void strvec_pushv(struct strvec *, const char **);
/**
* Remove the final element from the array. If there are no
* elements in the array, do nothing.
*/
void argv_array_pop(struct argv_array *);
void strvec_pop(struct strvec *);
/* Splits by whitespace; does not handle quoted arguments! */
void argv_array_split(struct argv_array *, const char *);
void strvec_split(struct strvec *, const char *);
/**
* Free all memory associated with the array and return it to the
* initial, empty state.
*/
void argv_array_clear(struct argv_array *);
void strvec_clear(struct strvec *);
/**
* Disconnect the `argv` member from the `argv_array` struct and
* Disconnect the `argv` member from the `strvec` struct and
* return it. The caller is responsible for freeing the memory used
* by the array, and by the strings it references. After detaching,
* the `argv_array` is in a reinitialized state and can be pushed
* the `strvec` is in a reinitialized state and can be pushed
* into again.
*/
const char **argv_array_detach(struct argv_array *);
const char **strvec_detach(struct strvec *);
/* compatibility for historic argv_array interface */
#define argv_array strvec
#define ARGV_ARRAY_INIT STRVEC_INIT
#define argv_array_init strvec_init
#define argv_array_push strvec_push
#define argv_array_pushf strvec_pushf
#define argv_array_pushl strvec_pushl
#define argv_array_pushv strvec_pushv
#define argv_array_pop strvec_pop
#define argv_array_split strvec_split
#define argv_array_clear strvec_clear
#define argv_array_detach strvec_detach
#endif /* ARGV_ARRAY_H */

View File

@ -1,13 +1,13 @@
#ifndef GIT_EXEC_CMD_H
#define GIT_EXEC_CMD_H
struct argv_array;
struct strvec;
void git_set_exec_path(const char *exec_path);
void git_resolve_executable_dir(const char *path);
const char *git_exec_path(void);
void setup_path(void);
const char **prepare_git_cmd(struct argv_array *out, const char **argv);
const char **prepare_git_cmd(struct strvec *out, const char **argv);
int execv_git_cmd(const char **argv); /* NULL terminated */
LAST_ARG_MUST_BE_NULL
int execl_git_cmd(const char *cmd, ...);

View File

@ -2,9 +2,9 @@
#define LS_REFS_H
struct repository;
struct argv_array;
struct strvec;
struct packet_reader;
int ls_refs(struct repository *r, struct argv_array *keys,
int ls_refs(struct repository *r, struct strvec *keys,
struct packet_reader *request);
#endif /* LS_REFS_H */

View File

@ -60,8 +60,8 @@ int sq_dequote_to_argv(char *arg, const char ***argv, int *nr, int *alloc);
* still modify arg in place, but unlike sq_dequote_to_argv, the argv_array
* will duplicate and take ownership of the strings.
*/
struct argv_array;
int sq_dequote_to_argv_array(char *arg, struct argv_array *);
struct strvec;
int sq_dequote_to_argv_array(char *arg, struct strvec *);
int unquote_c_style(struct strbuf *, const char *quoted, const char **endp);
size_t quote_c_style(const char *name, struct strbuf *, FILE *, int no_dq);

4
refs.h
View File

@ -145,8 +145,8 @@ int refname_match(const char *abbrev_name, const char *full_name);
* Given a 'prefix' expand it by the rules in 'ref_rev_parse_rules' and add
* the results to 'prefixes'
*/
struct argv_array;
void expand_ref_prefix(struct argv_array *prefixes, const char *prefix);
struct strvec;
void expand_ref_prefix(struct strvec *prefixes, const char *prefix);
int expand_ref(struct repository *r, const char *str, int len, struct object_id *oid, char **ref);
int repo_dwim_ref(struct repository *r, const char *str, int len, struct object_id *oid, char **ref);

View File

@ -60,12 +60,12 @@ void refspec_clear(struct refspec *rs);
int valid_fetch_refspec(const char *refspec);
struct argv_array;
struct strvec;
/*
* Determine what <prefix> values to pass to the peer in ref-prefix lines
* (see Documentation/technical/protocol-v2.txt).
*/
void refspec_ref_prefixes(const struct refspec *rs,
struct argv_array *ref_prefixes);
struct strvec *ref_prefixes);
#endif /* REFSPEC_H */

View File

@ -168,7 +168,7 @@ void free_refs(struct ref *ref);
struct oid_array;
struct packet_reader;
struct argv_array;
struct strvec;
struct string_list;
struct ref **get_remote_heads(struct packet_reader *reader,
struct ref **list, unsigned int flags,
@ -178,7 +178,7 @@ struct ref **get_remote_heads(struct packet_reader *reader,
/* Used for protocol v2 in order to retrieve refs from a remote */
struct ref **get_remote_refs(int fd_out, struct packet_reader *reader,
struct ref **list, int for_push,
const struct argv_array *ref_prefixes,
const struct strvec *ref_prefixes,
const struct string_list *server_options,
int stateless_rpc);

View File

@ -1,8 +1,8 @@
#ifndef SERVE_H
#define SERVE_H
struct argv_array;
int has_capability(const struct argv_array *keys, const char *capability,
struct strvec;
int has_capability(const struct strvec *keys, const char *capability,
const char **value);
struct serve_options {

View File

@ -1,7 +1,7 @@
#ifndef SUBMODULE_H
#define SUBMODULE_H
struct argv_array;
struct strvec;
struct cache_entry;
struct diff_options;
struct index_state;
@ -84,7 +84,7 @@ int should_update_submodules(void);
const struct submodule *submodule_from_ce(const struct cache_entry *ce);
void check_for_new_submodule_commits(struct object_id *oid);
int fetch_populated_submodules(struct repository *r,
const struct argv_array *options,
const struct strvec *options,
const char *prefix,
int command_line_option,
int default_option,
@ -143,7 +143,7 @@ void submodule_unset_core_worktree(const struct submodule *sub);
* a submodule by clearing any repo-specific environment variables, but
* retaining any config in the environment.
*/
void prepare_submodule_repo_env(struct argv_array *out);
void prepare_submodule_repo_env(struct strvec *out);
#define ABSORB_GITDIR_RECURSE_SUBMODULES (1<<0)
void absorb_git_dir_into_superproject(const char *path,

View File

@ -3,7 +3,7 @@
struct ref;
struct transport;
struct argv_array;
struct strvec;
struct transport_vtable {
/**

View File

@ -11,9 +11,9 @@ struct upload_pack_options {
void upload_pack(struct upload_pack_options *options);
struct repository;
struct argv_array;
struct strvec;
struct packet_reader;
int upload_pack_v2(struct repository *r, struct argv_array *keys,
int upload_pack_v2(struct repository *r, struct strvec *keys,
struct packet_reader *request);
struct strbuf;