Merge branch 'jk/strbuf-tolower'
authorJunio C Hamano <gitster@pobox.com>
Mon, 16 Jun 2014 17:07:16 +0000 (10:07 -0700)
committerJunio C Hamano <gitster@pobox.com>
Mon, 16 Jun 2014 17:07:17 +0000 (10:07 -0700)
* jk/strbuf-tolower:
strbuf: add strbuf_tolower function

1  2 
Documentation/technical/api-strbuf.txt
config.c
strbuf.c
strbuf.h
index 4396be9dda07dfad679f834120b7f1aa2adb16c5,8480f8902d96b55727b99e9bac9dc738f12c12ca..50690186d3949de7168e579b0f7a54d6cb0dd8e5
@@@ -121,19 -121,14 +121,23 @@@ Function
  
  * Related to the contents of the buffer
  
 +`strbuf_trim`::
 +
 +      Strip whitespace from the beginning and end of a string.
 +      Equivalent to performing `strbuf_rtrim()` followed by `strbuf_ltrim()`.
 +
  `strbuf_rtrim`::
  
        Strip whitespace from the end of a string.
  
 +`strbuf_ltrim`::
 +
 +      Strip whitespace from the beginning of a string.
 +
+ `strbuf_tolower`::
+       Lowercase each character in the buffer using `tolower`.
  `strbuf_cmp`::
  
        Compare two buffers. Returns an integer less than, equal to, or greater
diff --combined config.c
index 3c3e31448e5be385f9302ccce9c6993667014e5b,03ce5c6844705f94b17ff839d9610e4a30609fed..9319aa12f3e6a2cb5b82a5466382f083c1bcfc35
+++ b/config.c
@@@ -147,12 -147,6 +147,6 @@@ int git_config_include(const char *var
        return ret;
  }
  
- static void lowercase(char *p)
- {
-       for (; *p; p++)
-               *p = tolower(*p);
- }
  void git_config_push_parameter(const char *text)
  {
        struct strbuf env = STRBUF_INIT;
@@@ -180,7 -174,7 +174,7 @@@ int git_config_parse_parameter(const ch
                strbuf_list_free(pair);
                return error("bogus config parameter: %s", text);
        }
-       lowercase(pair[0]->buf);
+       strbuf_tolower(pair[0]);
        if (fn(pair[0]->buf, pair[1] ? pair[1]->buf : NULL, data) < 0) {
                strbuf_list_free(pair);
                return -1;
@@@ -826,16 -820,9 +820,16 @@@ static int git_default_core_config(cons
        if (!strcmp(var, "core.commentchar")) {
                const char *comment;
                int ret = git_config_string(&comment, var, value);
 -              if (!ret)
 +              if (ret)
 +                      return ret;
 +              else if (!strcasecmp(comment, "auto"))
 +                      auto_comment_line_char = 1;
 +              else if (comment[0] && !comment[1]) {
                        comment_line_char = comment[0];
 -              return ret;
 +                      auto_comment_line_char = 0;
 +              } else
 +                      return error("core.commentChar should only be one character");
 +              return 0;
        }
  
        if (!strcmp(var, "core.askpass"))
@@@ -1643,13 -1630,6 +1637,13 @@@ int git_config_set_multivar_in_file(con
                        MAP_PRIVATE, in_fd, 0);
                close(in_fd);
  
 +              if (fchmod(fd, st.st_mode & 07777) < 0) {
 +                      error("fchmod on %s failed: %s",
 +                              lock->filename, strerror(errno));
 +                      ret = CONFIG_NO_WRITE;
 +                      goto out_free;
 +              }
 +
                if (store.seen == 0)
                        store.seen = 1;
  
@@@ -1798,7 -1778,6 +1792,7 @@@ int git_config_rename_section_in_file(c
        int out_fd;
        char buf[1024];
        FILE *config_file;
 +      struct stat st;
  
        if (new_name && !section_name_is_ok(new_name)) {
                ret = error("invalid section name: %s", new_name);
                goto unlock_and_out;
        }
  
 +      fstat(fileno(config_file), &st);
 +
 +      if (fchmod(out_fd, st.st_mode & 07777) < 0) {
 +              ret = error("fchmod on %s failed: %s",
 +                              lock->filename, strerror(errno));
 +              goto out;
 +      }
 +
        while (fgets(buf, sizeof(buf), config_file)) {
                int i;
                int length;
diff --combined strbuf.c
index f5d609a51f346117ea43db91bb54085e7d0e7192,1a673fc72ee4d8cf748c7519b5261126e2b096c2..c8217755e541f05281d10e07517602f0fdcbde42
+++ b/strbuf.c
@@@ -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)
  {
@@@ -99,6 -106,13 +99,13 @@@ void strbuf_ltrim(struct strbuf *sb
        sb->buf[sb->len] = '\0';
  }
  
+ void strbuf_tolower(struct strbuf *sb)
+ {
+       char *p = sb->buf, *end = sb->buf + sb->len;
+       for (; p < end; p++)
+               *p = tolower(*p);
+ }
  struct strbuf **strbuf_split_buf(const char *str, size_t slen,
                                 int terminator, int max)
  {
@@@ -563,16 -577,3 +570,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 4de7531c434a0e58006f5bb3ebdb947069ba5a9c,6b6f745e01c3eb0cfa5afd71365c6bfab4e78c4d..25328b9e8193e9f56431450b6125d6ce7d667d91
+++ b/strbuf.h
@@@ -45,6 -45,7 +45,7 @@@ static inline void strbuf_setlen(struc
  extern void strbuf_trim(struct strbuf *);
  extern void strbuf_rtrim(struct strbuf *);
  extern void strbuf_ltrim(struct strbuf *);
+ extern void strbuf_tolower(struct strbuf *sb);
  extern int strbuf_cmp(const struct strbuf *, const struct strbuf *);
  
  /*
@@@ -183,6 -184,4 +184,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 */