argv-array.con commit Merge branch 'pj/remote-set-branches-usage-fix' (bba6123)
   1#include "cache.h"
   2#include "argv-array.h"
   3#include "strbuf.h"
   4
   5static const char *empty_argv_storage = NULL;
   6const char **empty_argv = &empty_argv_storage;
   7
   8void argv_array_init(struct argv_array *array)
   9{
  10        array->argv = empty_argv;
  11        array->argc = 0;
  12        array->alloc = 0;
  13}
  14
  15static void argv_array_push_nodup(struct argv_array *array, const char *value)
  16{
  17        if (array->argv == empty_argv)
  18                array->argv = NULL;
  19
  20        ALLOC_GROW(array->argv, array->argc + 2, array->alloc);
  21        array->argv[array->argc++] = value;
  22        array->argv[array->argc] = NULL;
  23}
  24
  25void argv_array_push(struct argv_array *array, const char *value)
  26{
  27        argv_array_push_nodup(array, xstrdup(value));
  28}
  29
  30void argv_array_pushf(struct argv_array *array, const char *fmt, ...)
  31{
  32        va_list ap;
  33        struct strbuf v = STRBUF_INIT;
  34
  35        va_start(ap, fmt);
  36        strbuf_vaddf(&v, fmt, ap);
  37        va_end(ap);
  38
  39        argv_array_push_nodup(array, strbuf_detach(&v, NULL));
  40}
  41
  42void argv_array_clear(struct argv_array *array)
  43{
  44        if (array->argv != empty_argv) {
  45                int i;
  46                for (i = 0; i < array->argc; i++)
  47                        free((char **)array->argv[i]);
  48                free(array->argv);
  49        }
  50        argv_array_init(array);
  51}