give "nbuf" strbuf a more meaningful name

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>

Jeff King committed Jan 31, 2016 at 06:25 UTC 0d4cc1b45bf063b3a46654098a9fb85f82f386a7
5 files changed +30 -29
builtin/check-attr.c
+6 -7
@@ -72,24 +72,23 @@ static void check_attr(const char *prefix, int cnt,
72 static void check_attr_stdin_paths(const char *prefix, int cnt,
73 struct git_attr_check *check)
74 {
75 - struct strbuf buf, nbuf;
75 + struct strbuf buf = STRBUF_INIT;
76 + struct strbuf unquoted = STRBUF_INIT;
77 strbuf_getline_fn getline_fn;
78
79 getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf;
79 - strbuf_init(&buf, 0);
80 - strbuf_init(&nbuf, 0);
80 while (getline_fn(&buf, stdin) != EOF) {
81 if (!nul_term_line && buf.buf[0] == '"') {
83 - strbuf_reset(&nbuf);
84 - if (unquote_c_style(&nbuf, buf.buf, NULL))
82 + strbuf_reset(&unquoted);
83 + if (unquote_c_style(&unquoted, buf.buf, NULL))
84 die("line is badly quoted");
86 - strbuf_swap(&buf, &nbuf);
85 + strbuf_swap(&buf, &unquoted);
86 }
87 check_attr(prefix, cnt, check, buf.buf);
88 maybe_flush_or_die(stdout, "attribute to stdout");
89 }
90 strbuf_release(&buf);
92 - strbuf_release(&nbuf);
91 + strbuf_release(&unquoted);
92 }
93
94 static NORETURN void error_with_usage(const char *msg)
builtin/check-ignore.c
+6 -7
@@ -115,20 +115,19 @@ static int check_ignore(struct dir_struct *dir,
115
116 static int check_ignore_stdin_paths(struct dir_struct *dir, const char *prefix)
117 {
118 - struct strbuf buf, nbuf;
118 + struct strbuf buf = STRBUF_INIT;
119 + struct strbuf unquoted = STRBUF_INIT;
120 char *pathspec[2] = { NULL, NULL };
121 strbuf_getline_fn getline_fn;
122 int num_ignored = 0;
123
124 getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf;
124 - strbuf_init(&buf, 0);
125 - strbuf_init(&nbuf, 0);
125 while (getline_fn(&buf, stdin) != EOF) {
126 if (!nul_term_line && buf.buf[0] == '"') {
128 - strbuf_reset(&nbuf);
129 - if (unquote_c_style(&nbuf, buf.buf, NULL))
127 + strbuf_reset(&unquoted);
128 + if (unquote_c_style(&unquoted, buf.buf, NULL))
129 die("line is badly quoted");
131 - strbuf_swap(&buf, &nbuf);
130 + strbuf_swap(&buf, &unquoted);
131 }
132 pathspec[0] = buf.buf;
133 num_ignored += check_ignore(dir, prefix,
@@ -136,7 +135,7 @@ static int check_ignore_stdin_paths(struct dir_struct *dir, const char *prefix)
135 maybe_flush_or_die(stdout, "check-ignore to stdout");
136 }
137 strbuf_release(&buf);
139 - strbuf_release(&nbuf);
138 + strbuf_release(&unquoted);
139 return num_ignored;
140 }
141
builtin/checkout-index.c
+6 -5
@@ -251,7 +251,8 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
251 }
252
253 if (read_from_stdin) {
254 - struct strbuf buf = STRBUF_INIT, nbuf = STRBUF_INIT;
254 + struct strbuf buf = STRBUF_INIT;
255 + struct strbuf unquoted = STRBUF_INIT;
256 strbuf_getline_fn getline_fn;
257
258 if (all)
@@ -261,16 +262,16 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
262 while (getline_fn(&buf, stdin) != EOF) {
263 char *p;
264 if (!nul_term_line && buf.buf[0] == '"') {
264 - strbuf_reset(&nbuf);
265 - if (unquote_c_style(&nbuf, buf.buf, NULL))
265 + strbuf_reset(&unquoted);
266 + if (unquote_c_style(&unquoted, buf.buf, NULL))
267 die("line is badly quoted");
267 - strbuf_swap(&buf, &nbuf);
268 + strbuf_swap(&buf, &unquoted);
269 }
270 p = prefix_path(prefix, prefix_length, buf.buf);
271 checkout_file(p, prefix);
272 free(p);
273 }
273 - strbuf_release(&nbuf);
274 + strbuf_release(&unquoted);
275 strbuf_release(&buf);
276 }
277
builtin/hash-object.c
+6 -5
@@ -58,20 +58,21 @@ static void hash_object(const char *path, const char *type, const char *vpath,
58 static void hash_stdin_paths(const char *type, int no_filters, unsigned flags,
59 int literally)
60 {
61 - struct strbuf buf = STRBUF_INIT, nbuf = STRBUF_INIT;
61 + struct strbuf buf = STRBUF_INIT;
62 + struct strbuf unquoted = STRBUF_INIT;
63
64 while (strbuf_getline_lf(&buf, stdin) != EOF) {
65 if (buf.buf[0] == '"') {
65 - strbuf_reset(&nbuf);
66 - if (unquote_c_style(&nbuf, buf.buf, NULL))
66 + strbuf_reset(&unquoted);
67 + if (unquote_c_style(&unquoted, buf.buf, NULL))
68 die("line is badly quoted");
68 - strbuf_swap(&buf, &nbuf);
69 + strbuf_swap(&buf, &unquoted);
70 }
71 hash_object(buf.buf, type, no_filters ? NULL : buf.buf, flags,
72 literally);
73 }
74 strbuf_release(&buf);
74 - strbuf_release(&nbuf);
75 + strbuf_release(&unquoted);
76 }
77
78 int cmd_hash_object(int argc, const char **argv, const char *prefix)
builtin/update-index.c
+6 -5
@@ -1075,16 +1075,17 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
1075 }
1076
1077 if (read_from_stdin) {
1078 - struct strbuf buf = STRBUF_INIT, nbuf = STRBUF_INIT;
1078 + struct strbuf buf = STRBUF_INIT;
1079 + struct strbuf unquoted = STRBUF_INIT;
1080
1081 setup_work_tree();
1082 while (getline_fn(&buf, stdin) != EOF) {
1083 char *p;
1084 if (!nul_term_line && buf.buf[0] == '"') {
1084 - strbuf_reset(&nbuf);
1085 - if (unquote_c_style(&nbuf, buf.buf, NULL))
1085 + strbuf_reset(&unquoted);
1086 + if (unquote_c_style(&unquoted, buf.buf, NULL))
1087 die("line is badly quoted");
1087 - strbuf_swap(&buf, &nbuf);
1088 + strbuf_swap(&buf, &unquoted);
1089 }
1090 p = prefix_path(prefix, prefix_length, buf.buf);
1091 update_one(p);
@@ -1092,7 +1093,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
1093 chmod_path(set_executable_bit, p);
1094 free(p);
1095 }
1095 - strbuf_release(&nbuf);
1096 + strbuf_release(&unquoted);
1097 strbuf_release(&buf);
1098 }
1099