give "nbuf" strbuf a more meaningful name
authorJeff King <peff@peff.net>
Sun, 31 Jan 2016 11:25:26 +0000 (06:25 -0500)
committerJunio C Hamano <gitster@pobox.com>
Mon, 1 Feb 2016 21:43:02 +0000 (13:43 -0800)
It's a common pattern in our code to read paths from stdin,
separated either by newlines or NULs, and unquote as
necessary. In each of these five cases we use "nbuf" to
temporarily store the unquoted value. Let's give it the more
meaningful name "unquoted", which makes it easier to
understand the purpose of the variable.

While we're at it, let's also static-initialize all of our
strbufs. It's not wrong to call strbuf_init, but it
increases the cognitive load on the reader, who might wonder
"do we sometimes avoid initializing them? why?".

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/check-attr.c
builtin/check-ignore.c
builtin/checkout-index.c
builtin/hash-object.c
builtin/update-index.c
index 087325ef6954e4a2364d194f8d7ac27fedb7fc67..53a5a18c1681f5f13f6ddd4fd4d7d30c0ea14ce6 100644 (file)
@@ -72,24 +72,23 @@ static void check_attr(const char *prefix, int cnt,
 static void check_attr_stdin_paths(const char *prefix, int cnt,
        struct git_attr_check *check)
 {
-       struct strbuf buf, nbuf;
+       struct strbuf buf = STRBUF_INIT;
+       struct strbuf unquoted = STRBUF_INIT;
        strbuf_getline_fn getline_fn;
 
        getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf;
-       strbuf_init(&buf, 0);
-       strbuf_init(&nbuf, 0);
        while (getline_fn(&buf, stdin) != EOF) {
                if (!nul_term_line && buf.buf[0] == '"') {
-                       strbuf_reset(&nbuf);
-                       if (unquote_c_style(&nbuf, buf.buf, NULL))
+                       strbuf_reset(&unquoted);
+                       if (unquote_c_style(&unquoted, buf.buf, NULL))
                                die("line is badly quoted");
-                       strbuf_swap(&buf, &nbuf);
+                       strbuf_swap(&buf, &unquoted);
                }
                check_attr(prefix, cnt, check, buf.buf);
                maybe_flush_or_die(stdout, "attribute to stdout");
        }
        strbuf_release(&buf);
-       strbuf_release(&nbuf);
+       strbuf_release(&unquoted);
 }
 
 static NORETURN void error_with_usage(const char *msg)
index 4f0b09e2db5c0e0bc4665b06dc543b7f8ea1d467..1d73d3ca3d0509d71b927d685f3239cdab3cc885 100644 (file)
@@ -115,20 +115,19 @@ static int check_ignore(struct dir_struct *dir,
 
 static int check_ignore_stdin_paths(struct dir_struct *dir, const char *prefix)
 {
-       struct strbuf buf, nbuf;
+       struct strbuf buf = STRBUF_INIT;
+       struct strbuf unquoted = STRBUF_INIT;
        char *pathspec[2] = { NULL, NULL };
        strbuf_getline_fn getline_fn;
        int num_ignored = 0;
 
        getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf;
-       strbuf_init(&buf, 0);
-       strbuf_init(&nbuf, 0);
        while (getline_fn(&buf, stdin) != EOF) {
                if (!nul_term_line && buf.buf[0] == '"') {
-                       strbuf_reset(&nbuf);
-                       if (unquote_c_style(&nbuf, buf.buf, NULL))
+                       strbuf_reset(&unquoted);
+                       if (unquote_c_style(&unquoted, buf.buf, NULL))
                                die("line is badly quoted");
-                       strbuf_swap(&buf, &nbuf);
+                       strbuf_swap(&buf, &unquoted);
                }
                pathspec[0] = buf.buf;
                num_ignored += check_ignore(dir, prefix,
@@ -136,7 +135,7 @@ static int check_ignore_stdin_paths(struct dir_struct *dir, const char *prefix)
                maybe_flush_or_die(stdout, "check-ignore to stdout");
        }
        strbuf_release(&buf);
-       strbuf_release(&nbuf);
+       strbuf_release(&unquoted);
        return num_ignored;
 }
 
index ed888a5b9e8a2978369c404ab050b237ab5c7b4d..d8d7bd385408a344a6ed4d07132864577b4c7d99 100644 (file)
@@ -251,7 +251,8 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
        }
 
        if (read_from_stdin) {
-               struct strbuf buf = STRBUF_INIT, nbuf = STRBUF_INIT;
+               struct strbuf buf = STRBUF_INIT;
+               struct strbuf unquoted = STRBUF_INIT;
                strbuf_getline_fn getline_fn;
 
                if (all)
@@ -261,16 +262,16 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
                while (getline_fn(&buf, stdin) != EOF) {
                        char *p;
                        if (!nul_term_line && buf.buf[0] == '"') {
-                               strbuf_reset(&nbuf);
-                               if (unquote_c_style(&nbuf, buf.buf, NULL))
+                               strbuf_reset(&unquoted);
+                               if (unquote_c_style(&unquoted, buf.buf, NULL))
                                        die("line is badly quoted");
-                               strbuf_swap(&buf, &nbuf);
+                               strbuf_swap(&buf, &unquoted);
                        }
                        p = prefix_path(prefix, prefix_length, buf.buf);
                        checkout_file(p, prefix);
                        free(p);
                }
-               strbuf_release(&nbuf);
+               strbuf_release(&unquoted);
                strbuf_release(&buf);
        }
 
index 3bc5ec1d2194133fd392ca5bfbaaa5a26b901e5f..d3cb4e53453281c60c0ff97a976a2aa621b087a8 100644 (file)
@@ -58,20 +58,21 @@ static void hash_object(const char *path, const char *type, const char *vpath,
 static void hash_stdin_paths(const char *type, int no_filters, unsigned flags,
                             int literally)
 {
-       struct strbuf buf = STRBUF_INIT, nbuf = STRBUF_INIT;
+       struct strbuf buf = STRBUF_INIT;
+       struct strbuf unquoted = STRBUF_INIT;
 
        while (strbuf_getline_lf(&buf, stdin) != EOF) {
                if (buf.buf[0] == '"') {
-                       strbuf_reset(&nbuf);
-                       if (unquote_c_style(&nbuf, buf.buf, NULL))
+                       strbuf_reset(&unquoted);
+                       if (unquote_c_style(&unquoted, buf.buf, NULL))
                                die("line is badly quoted");
-                       strbuf_swap(&buf, &nbuf);
+                       strbuf_swap(&buf, &unquoted);
                }
                hash_object(buf.buf, type, no_filters ? NULL : buf.buf, flags,
                            literally);
        }
        strbuf_release(&buf);
-       strbuf_release(&nbuf);
+       strbuf_release(&unquoted);
 }
 
 int cmd_hash_object(int argc, const char **argv, const char *prefix)
index 7c5c143de5dc74dda2b17832037268fc4e4b9d60..f052fafb540e14c2c2448ac207c6bde8e0d8b0f3 100644 (file)
@@ -1075,16 +1075,17 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
        }
 
        if (read_from_stdin) {
-               struct strbuf buf = STRBUF_INIT, nbuf = STRBUF_INIT;
+               struct strbuf buf = STRBUF_INIT;
+               struct strbuf unquoted = STRBUF_INIT;
 
                setup_work_tree();
                while (getline_fn(&buf, stdin) != EOF) {
                        char *p;
                        if (!nul_term_line && buf.buf[0] == '"') {
-                               strbuf_reset(&nbuf);
-                               if (unquote_c_style(&nbuf, buf.buf, NULL))
+                               strbuf_reset(&unquoted);
+                               if (unquote_c_style(&unquoted, buf.buf, NULL))
                                        die("line is badly quoted");
-                               strbuf_swap(&buf, &nbuf);
+                               strbuf_swap(&buf, &unquoted);
                        }
                        p = prefix_path(prefix, prefix_length, buf.buf);
                        update_one(p);
@@ -1092,7 +1093,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
                                chmod_path(set_executable_bit, p);
                        free(p);
                }
-               strbuf_release(&nbuf);
+               strbuf_release(&unquoted);
                strbuf_release(&buf);
        }