Merge branch 'jk/daemon-tolower'
authorJunio C Hamano <gitster@pobox.com>
Mon, 16 Jun 2014 17:07:14 +0000 (10:07 -0700)
committerJunio C Hamano <gitster@pobox.com>
Mon, 16 Jun 2014 17:07:15 +0000 (10:07 -0700)
* jk/daemon-tolower:
daemon/config: factor out duplicate xstrdup_tolower

1  2 
builtin/config.c
daemon.c
strbuf.c
strbuf.h
diff --combined builtin/config.c
index 5677c942b6936f3332b1b4ed12541f45c4228dfb,00b0c240fcd788edc3d33cd60f48cb49f963ff7f..fcd84747015a3d0a1da048612f5ac119a7e161d1
@@@ -21,7 -21,8 +21,7 @@@ static char key_delim = ' '
  static char term = '\n';
  
  static int use_global_config, use_system_config, use_local_config;
 -static const char *given_config_file;
 -static const char *given_config_blob;
 +static struct git_config_source given_config_source;
  static int actions, types;
  static const char *get_color_slot, *get_colorbool_slot;
  static int end_null;
@@@ -54,8 -55,8 +54,8 @@@ static struct option builtin_config_opt
        OPT_BOOL(0, "global", &use_global_config, N_("use global config file")),
        OPT_BOOL(0, "system", &use_system_config, N_("use system config file")),
        OPT_BOOL(0, "local", &use_local_config, N_("use repository config file")),
 -      OPT_STRING('f', "file", &given_config_file, N_("file"), N_("use given config file")),
 -      OPT_STRING(0, "blob", &given_config_blob, N_("blob-id"), N_("read config from given blob object")),
 +      OPT_STRING('f', "file", &given_config_source.file, N_("file"), N_("use given config file")),
 +      OPT_STRING(0, "blob", &given_config_source.blob, N_("blob-id"), N_("read config from given blob object")),
        OPT_GROUP(N_("Action")),
        OPT_BIT(0, "get", &actions, N_("get value: name [value-regex]"), ACTION_GET),
        OPT_BIT(0, "get-all", &actions, N_("get all values: key [value-regex]"), ACTION_GET_ALL),
@@@ -223,7 -224,8 +223,7 @@@ static int get_value(const char *key_, 
        }
  
        git_config_with_options(collect_config, &values,
 -                              given_config_file, given_config_blob,
 -                              respect_includes);
 +                              &given_config_source, respect_includes);
  
        ret = !values.nr;
  
@@@ -307,7 -309,8 +307,7 @@@ static void get_color(const char *def_c
        get_color_found = 0;
        parsed_color[0] = '\0';
        git_config_with_options(git_get_color_config, NULL,
 -                              given_config_file, given_config_blob,
 -                              respect_includes);
 +                              &given_config_source, respect_includes);
  
        if (!get_color_found && def_color)
                color_parse(def_color, "command line", parsed_color);
@@@ -336,7 -339,8 +336,7 @@@ static int get_colorbool(int print
        get_diff_color_found = -1;
        get_color_ui_found = -1;
        git_config_with_options(git_get_colorbool_config, NULL,
 -                              given_config_file, given_config_blob,
 -                              respect_includes);
 +                              &given_config_source, respect_includes);
  
        if (get_colorbool_found < 0) {
                if (!strcmp(get_colorbool_slot, "color.diff"))
                return get_colorbool_found ? 0 : 1;
  }
  
 -static void check_blob_write(void)
 +static void check_write(void)
  {
 -      if (given_config_blob)
 +      if (given_config_source.use_stdin)
 +              die("writing to stdin is not supported");
 +
 +      if (given_config_source.blob)
                die("writing config blobs is not supported");
  }
  
@@@ -395,19 -396,6 +395,6 @@@ static int urlmatch_collect_fn(const ch
        return 0;
  }
  
- static char *dup_downcase(const char *string)
- {
-       char *result;
-       size_t len, i;
-       len = strlen(string);
-       result = xmalloc(len + 1);
-       for (i = 0; i < len; i++)
-               result[i] = tolower(string[i]);
-       result[i] = '\0';
-       return result;
- }
  static int get_urlmatch(const char *var, const char *url)
  {
        char *section_tail;
        if (!url_normalize(url, &config.url))
                die("%s", config.url.err);
  
-       config.section = dup_downcase(var);
+       config.section = xstrdup_tolower(var);
        section_tail = strchr(config.section, '.');
        if (section_tail) {
                *section_tail = '\0';
        }
  
        git_config_with_options(urlmatch_config_entry, &config,
 -                              given_config_file, NULL, respect_includes);
 +                              &given_config_source, respect_includes);
  
        for_each_string_list_item(item, &values) {
                struct urlmatch_current_candidate_value *matched = item->util;
@@@ -463,24 -451,18 +450,24 @@@ int cmd_config(int argc, const char **a
        int nongit = !startup_info->have_repository;
        char *value;
  
 -      given_config_file = getenv(CONFIG_ENVIRONMENT);
 +      given_config_source.file = getenv(CONFIG_ENVIRONMENT);
  
        argc = parse_options(argc, argv, prefix, builtin_config_options,
                             builtin_config_usage,
                             PARSE_OPT_STOP_AT_NON_OPTION);
  
        if (use_global_config + use_system_config + use_local_config +
 -          !!given_config_file + !!given_config_blob > 1) {
 +          !!given_config_source.file + !!given_config_source.blob > 1) {
                error("only one config file at a time.");
                usage_with_options(builtin_config_usage, builtin_config_options);
        }
  
 +      if (given_config_source.file &&
 +                      !strcmp(given_config_source.file, "-")) {
 +              given_config_source.file = NULL;
 +              given_config_source.use_stdin = 1;
 +      }
 +
        if (use_global_config) {
                char *user_config = NULL;
                char *xdg_config = NULL;
  
                if (access_or_warn(user_config, R_OK, 0) &&
                    xdg_config && !access_or_warn(xdg_config, R_OK, 0))
 -                      given_config_file = xdg_config;
 +                      given_config_source.file = xdg_config;
                else
 -                      given_config_file = user_config;
 +                      given_config_source.file = user_config;
        }
        else if (use_system_config)
 -              given_config_file = git_etc_gitconfig();
 +              given_config_source.file = git_etc_gitconfig();
        else if (use_local_config)
 -              given_config_file = git_pathdup("config");
 -      else if (given_config_file) {
 -              if (!is_absolute_path(given_config_file) && prefix)
 -                      given_config_file =
 +              given_config_source.file = git_pathdup("config");
 +      else if (given_config_source.file) {
 +              if (!is_absolute_path(given_config_source.file) && prefix)
 +                      given_config_source.file =
                                xstrdup(prefix_filename(prefix,
                                                        strlen(prefix),
 -                                                      given_config_file));
 +                                                      given_config_source.file));
        }
  
        if (respect_includes == -1)
 -              respect_includes = !given_config_file;
 +              respect_includes = !given_config_source.file;
  
        if (end_null) {
                term = '\0';
        if (actions == ACTION_LIST) {
                check_argc(argc, 0, 0);
                if (git_config_with_options(show_all_config, NULL,
 -                                          given_config_file,
 -                                          given_config_blob,
 +                                          &given_config_source,
                                            respect_includes) < 0) {
 -                      if (given_config_file)
 +                      if (given_config_source.file)
                                die_errno("unable to read config file '%s'",
 -                                        given_config_file);
 +                                        given_config_source.file);
                        else
                                die("error processing config file(s)");
                }
        }
        else if (actions == ACTION_EDIT) {
                check_argc(argc, 0, 0);
 -              if (!given_config_file && nongit)
 +              if (!given_config_source.file && nongit)
                        die("not in a git directory");
 -              if (given_config_blob)
 +              if (given_config_source.use_stdin)
 +                      die("editing stdin is not supported");
 +              if (given_config_source.blob)
                        die("editing blobs is not supported");
                git_config(git_default_config, NULL);
 -              launch_editor(given_config_file ?
 -                            given_config_file : git_path("config"),
 +              launch_editor(given_config_source.file ?
 +                            given_config_source.file : git_path("config"),
                              NULL, NULL);
        }
        else if (actions == ACTION_SET) {
                int ret;
 -              check_blob_write();
 +              check_write();
                check_argc(argc, 2, 2);
                value = normalize_value(argv[0], argv[1]);
 -              ret = git_config_set_in_file(given_config_file, argv[0], value);
 +              ret = git_config_set_in_file(given_config_source.file, argv[0], value);
                if (ret == CONFIG_NOTHING_SET)
                        error("cannot overwrite multiple values with a single value\n"
                        "       Use a regexp, --add or --replace-all to change %s.", argv[0]);
                return ret;
        }
        else if (actions == ACTION_SET_ALL) {
 -              check_blob_write();
 +              check_write();
                check_argc(argc, 2, 3);
                value = normalize_value(argv[0], argv[1]);
 -              return git_config_set_multivar_in_file(given_config_file,
 +              return git_config_set_multivar_in_file(given_config_source.file,
                                                       argv[0], value, argv[2], 0);
        }
        else if (actions == ACTION_ADD) {
 -              check_blob_write();
 +              check_write();
                check_argc(argc, 2, 2);
                value = normalize_value(argv[0], argv[1]);
 -              return git_config_set_multivar_in_file(given_config_file,
 +              return git_config_set_multivar_in_file(given_config_source.file,
                                                       argv[0], value, "^$", 0);
        }
        else if (actions == ACTION_REPLACE_ALL) {
 -              check_blob_write();
 +              check_write();
                check_argc(argc, 2, 3);
                value = normalize_value(argv[0], argv[1]);
 -              return git_config_set_multivar_in_file(given_config_file,
 +              return git_config_set_multivar_in_file(given_config_source.file,
                                                       argv[0], value, argv[2], 1);
        }
        else if (actions == ACTION_GET) {
                return get_urlmatch(argv[0], argv[1]);
        }
        else if (actions == ACTION_UNSET) {
 -              check_blob_write();
 +              check_write();
                check_argc(argc, 1, 2);
                if (argc == 2)
 -                      return git_config_set_multivar_in_file(given_config_file,
 +                      return git_config_set_multivar_in_file(given_config_source.file,
                                                               argv[0], NULL, argv[1], 0);
                else
 -                      return git_config_set_in_file(given_config_file,
 +                      return git_config_set_in_file(given_config_source.file,
                                                      argv[0], NULL);
        }
        else if (actions == ACTION_UNSET_ALL) {
 -              check_blob_write();
 +              check_write();
                check_argc(argc, 1, 2);
 -              return git_config_set_multivar_in_file(given_config_file,
 +              return git_config_set_multivar_in_file(given_config_source.file,
                                                       argv[0], NULL, argv[1], 1);
        }
        else if (actions == ACTION_RENAME_SECTION) {
                int ret;
 -              check_blob_write();
 +              check_write();
                check_argc(argc, 2, 2);
 -              ret = git_config_rename_section_in_file(given_config_file,
 +              ret = git_config_rename_section_in_file(given_config_source.file,
                                                        argv[0], argv[1]);
                if (ret < 0)
                        return ret;
        }
        else if (actions == ACTION_REMOVE_SECTION) {
                int ret;
 -              check_blob_write();
 +              check_write();
                check_argc(argc, 1, 1);
 -              ret = git_config_rename_section_in_file(given_config_file,
 +              ret = git_config_rename_section_in_file(given_config_source.file,
                                                        argv[0], NULL);
                if (ret < 0)
                        return ret;
  
        return 0;
  }
 -
 -int cmd_repo_config(int argc, const char **argv, const char *prefix)
 -{
 -      fprintf(stderr, "WARNING: git repo-config is deprecated in favor of git config.\n");
 -      return cmd_config(argc, argv, prefix);
 -}
diff --combined daemon.c
index eba12556848e975cd6f1a55ae25760823e90cdc8,d514ba47ff84a407dd72025913a324d9ba77b478..f9c63e96137aff6b5ae2376af3fca789ff03e975
+++ b/daemon.c
@@@ -235,7 -235,7 +235,7 @@@ static int service_enabled
  
  static int git_daemon_config(const char *var, const char *value, void *cb)
  {
 -      if (!prefixcmp(var, "daemon.") &&
 +      if (starts_with(var, "daemon.") &&
            !strcmp(var + 7, service_looking_at->config_name)) {
                service_enabled = git_config_bool(var, value);
                return 0;
@@@ -475,14 -475,6 +475,6 @@@ static void make_service_overridable(co
        die("No such service %s", name);
  }
  
- static char *xstrdup_tolower(const char *str)
- {
-       char *p, *dup = xstrdup(str);
-       for (p = dup; *p; p++)
-               *p = tolower(*p);
-       return dup;
- }
  static void parse_host_and_port(char *hostport, char **host,
        char **port)
  {
@@@ -633,7 -625,7 +625,7 @@@ static int execute(void
        for (i = 0; i < ARRAY_SIZE(daemon_service); i++) {
                struct daemon_service *s = &(daemon_service[i]);
                int namelen = strlen(s->name);
 -              if (!prefixcmp(line, "git-") &&
 +              if (starts_with(line, "git-") &&
                    !strncmp(s->name, line + 4, namelen) &&
                    line[namelen + 4] == ' ') {
                        /*
@@@ -1056,6 -1048,11 +1048,6 @@@ static void drop_privileges(struct cred
        /* nothing */
  }
  
 -static void daemonize(void)
 -{
 -      die("--detach not supported on this platform");
 -}
 -
  static struct credentials *prepare_credentials(const char *user_name,
      const char *group_name)
  {
@@@ -1097,6 -1094,24 +1089,6 @@@ static struct credentials *prepare_cred
  
        return &c;
  }
 -
 -static void daemonize(void)
 -{
 -      switch (fork()) {
 -              case 0:
 -                      break;
 -              case -1:
 -                      die_errno("fork failed");
 -              default:
 -                      exit(0);
 -      }
 -      if (setsid() == -1)
 -              die_errno("setsid failed");
 -      close(0);
 -      close(1);
 -      close(2);
 -      sanitize_stdfds();
 -}
  #endif
  
  static void store_pid(const char *path)
@@@ -1142,11 -1157,11 +1134,11 @@@ int main(int argc, char **argv
        for (i = 1; i < argc; i++) {
                char *arg = argv[i];
  
 -              if (!prefixcmp(arg, "--listen=")) {
 +              if (starts_with(arg, "--listen=")) {
                        string_list_append(&listen_addr, xstrdup_tolower(arg + 9));
                        continue;
                }
 -              if (!prefixcmp(arg, "--port=")) {
 +              if (starts_with(arg, "--port=")) {
                        char *end;
                        unsigned long n;
                        n = strtoul(arg+7, &end, 0);
                        export_all_trees = 1;
                        continue;
                }
 -              if (!prefixcmp(arg, "--access-hook=")) {
 +              if (starts_with(arg, "--access-hook=")) {
                        access_hook = arg + 14;
                        continue;
                }
 -              if (!prefixcmp(arg, "--timeout=")) {
 +              if (starts_with(arg, "--timeout=")) {
                        timeout = atoi(arg+10);
                        continue;
                }
 -              if (!prefixcmp(arg, "--init-timeout=")) {
 +              if (starts_with(arg, "--init-timeout=")) {
                        init_timeout = atoi(arg+15);
                        continue;
                }
 -              if (!prefixcmp(arg, "--max-connections=")) {
 +              if (starts_with(arg, "--max-connections=")) {
                        max_connections = atoi(arg+18);
                        if (max_connections < 0)
                                max_connections = 0;            /* unlimited */
                        strict_paths = 1;
                        continue;
                }
 -              if (!prefixcmp(arg, "--base-path=")) {
 +              if (starts_with(arg, "--base-path=")) {
                        base_path = arg+12;
                        continue;
                }
                        base_path_relaxed = 1;
                        continue;
                }
 -              if (!prefixcmp(arg, "--interpolated-path=")) {
 +              if (starts_with(arg, "--interpolated-path=")) {
                        interpolated_path = arg+20;
                        continue;
                }
                        user_path = "";
                        continue;
                }
 -              if (!prefixcmp(arg, "--user-path=")) {
 +              if (starts_with(arg, "--user-path=")) {
                        user_path = arg + 12;
                        continue;
                }
 -              if (!prefixcmp(arg, "--pid-file=")) {
 +              if (starts_with(arg, "--pid-file=")) {
                        pid_file = arg + 11;
                        continue;
                }
                        log_syslog = 1;
                        continue;
                }
 -              if (!prefixcmp(arg, "--user=")) {
 +              if (starts_with(arg, "--user=")) {
                        user_name = arg + 7;
                        continue;
                }
 -              if (!prefixcmp(arg, "--group=")) {
 +              if (starts_with(arg, "--group=")) {
                        group_name = arg + 8;
                        continue;
                }
 -              if (!prefixcmp(arg, "--enable=")) {
 +              if (starts_with(arg, "--enable=")) {
                        enable_service(arg + 9, 1);
                        continue;
                }
 -              if (!prefixcmp(arg, "--disable=")) {
 +              if (starts_with(arg, "--disable=")) {
                        enable_service(arg + 10, 0);
                        continue;
                }
 -              if (!prefixcmp(arg, "--allow-override=")) {
 +              if (starts_with(arg, "--allow-override=")) {
                        make_service_overridable(arg + 17, 1);
                        continue;
                }
 -              if (!prefixcmp(arg, "--forbid-override=")) {
 +              if (starts_with(arg, "--forbid-override=")) {
                        make_service_overridable(arg + 18, 0);
                        continue;
                }
        if (inetd_mode || serve_mode)
                return execute();
  
 -      if (detach)
 -              daemonize();
 -      else
 +      if (detach) {
 +              if (daemonize())
 +                      die("--detach not supported on this platform");
 +      } else
                sanitize_stdfds();
  
        if (pid_file)
diff --combined strbuf.c
index 4d31567a1a6a67ae9697816d78da442f87ab5803,e26cb2c7fa38318d6065b7779e448c9e8e662857..f5d609a51f346117ea43db91bb54085e7d0e7192
+++ b/strbuf.c
@@@ -1,22 -1,22 +1,22 @@@
  #include "cache.h"
  #include "refs.h"
  
 -int prefixcmp(const char *str, const char *prefix)
 +int starts_with(const char *str, const char *prefix)
  {
        for (; ; str++, prefix++)
                if (!*prefix)
 -                      return 0;
 +                      return 1;
                else if (*str != *prefix)
 -                      return (unsigned char)*prefix - (unsigned char)*str;
 +                      return 0;
  }
  
 -int suffixcmp(const char *str, const char *suffix)
 +int ends_with(const char *str, const char *suffix)
  {
        int len = strlen(str), suflen = strlen(suffix);
        if (len < suflen)
 -              return -1;
 +              return 0;
        else
 -              return strcmp(str + len - suflen, suffix);
 +              return !strcmp(str + len - suflen, suffix);
  }
  
  /*
@@@ -78,8 -78,15 +78,8 @@@ void strbuf_grow(struct strbuf *sb, siz
  
  void strbuf_trim(struct strbuf *sb)
  {
 -      char *b = sb->buf;
 -      while (sb->len > 0 && isspace((unsigned char)sb->buf[sb->len - 1]))
 -              sb->len--;
 -      while (sb->len > 0 && isspace(*b)) {
 -              b++;
 -              sb->len--;
 -      }
 -      memmove(sb->buf, b, sb->len);
 -      sb->buf[sb->len] = '\0';
 +      strbuf_rtrim(sb);
 +      strbuf_ltrim(sb);
  }
  void strbuf_rtrim(struct strbuf *sb)
  {
@@@ -563,3 -570,16 +563,16 @@@ int fprintf_ln(FILE *fp, const char *fm
                return -1;
        return ret + 1;
  }
+ char *xstrdup_tolower(const char *string)
+ {
+       char *result;
+       size_t len, i;
+       len = strlen(string);
+       result = xmalloc(len + 1);
+       for (i = 0; i < len; i++)
+               result[i] = tolower(string[i]);
+       result[i] = '\0';
+       return result;
+ }
diff --combined strbuf.h
index 39c14cfa384c5154abd96899a7c02417295f8555,7bd36216ff85aca61728b06cd06e451747676257..4de7531c434a0e58006f5bb3ebdb947069ba5a9c
+++ b/strbuf.h
@@@ -17,23 -17,20 +17,23 @@@ extern void strbuf_init(struct strbuf *
  extern void strbuf_release(struct strbuf *);
  extern char *strbuf_detach(struct strbuf *, size_t *);
  extern void strbuf_attach(struct strbuf *, void *, size_t, size_t);
 -static inline void strbuf_swap(struct strbuf *a, struct strbuf *b) {
 +static inline void strbuf_swap(struct strbuf *a, struct strbuf *b)
 +{
        struct strbuf tmp = *a;
        *a = *b;
        *b = tmp;
  }
  
  /*----- strbuf size related -----*/
 -static inline size_t strbuf_avail(const struct strbuf *sb) {
 +static inline size_t strbuf_avail(const struct strbuf *sb)
 +{
        return sb->alloc ? sb->alloc - sb->len - 1 : 0;
  }
  
  extern void strbuf_grow(struct strbuf *, size_t);
  
 -static inline void strbuf_setlen(struct strbuf *sb, size_t len) {
 +static inline void strbuf_setlen(struct strbuf *sb, size_t len)
 +{
        if (len > (sb->alloc ? sb->alloc - 1 : 0))
                die("BUG: strbuf_setlen() beyond buffer");
        sb->len = len;
@@@ -100,8 -97,7 +100,8 @@@ static inline struct strbuf **strbuf_sp
  extern void strbuf_list_free(struct strbuf **);
  
  /*----- add data in your buffer -----*/
 -static inline void strbuf_addch(struct strbuf *sb, int c) {
 +static inline void strbuf_addch(struct strbuf *sb, int c)
 +{
        strbuf_grow(sb, 1);
        sb->buf[sb->len++] = c;
        sb->buf[sb->len] = '\0';
@@@ -117,12 -113,10 +117,12 @@@ extern void strbuf_splice(struct strbu
  extern void strbuf_add_commented_lines(struct strbuf *out, const char *buf, size_t size);
  
  extern void strbuf_add(struct strbuf *, const void *, size_t);
 -static inline void strbuf_addstr(struct strbuf *sb, const char *s) {
 +static inline void strbuf_addstr(struct strbuf *sb, const char *s)
 +{
        strbuf_add(sb, s, strlen(s));
  }
 -static inline void strbuf_addbuf(struct strbuf *sb, const struct strbuf *sb2) {
 +static inline void strbuf_addbuf(struct strbuf *sb, const struct strbuf *sb2)
 +{
        strbuf_grow(sb, sb2->len);
        strbuf_add(sb, sb2->buf, sb2->len);
  }
@@@ -183,4 -177,6 +183,6 @@@ extern int printf_ln(const char *fmt, .
  __attribute__((format (printf,2,3)))
  extern int fprintf_ln(FILE *fp, const char *fmt, ...);
  
+ char *xstrdup_tolower(const char *);
  #endif /* STRBUF_H */