Skip to content

Commit 0d4cc1b

Browse files
peffgitster
authored andcommitted
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>
1 parent 1a0c8df commit 0d4cc1b

File tree

5 files changed

+30
-29
lines changed

5 files changed

+30
-29
lines changed

builtin/check-attr.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,24 +72,23 @@ static void check_attr(const char *prefix, int cnt,
7272
static void check_attr_stdin_paths(const char *prefix, int cnt,
7373
struct git_attr_check *check)
7474
{
75-
struct strbuf buf, nbuf;
75+
struct strbuf buf = STRBUF_INIT;
76+
struct strbuf unquoted = STRBUF_INIT;
7677
strbuf_getline_fn getline_fn;
7778

7879
getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf;
79-
strbuf_init(&buf, 0);
80-
strbuf_init(&nbuf, 0);
8180
while (getline_fn(&buf, stdin) != EOF) {
8281
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))
8584
die("line is badly quoted");
86-
strbuf_swap(&buf, &nbuf);
85+
strbuf_swap(&buf, &unquoted);
8786
}
8887
check_attr(prefix, cnt, check, buf.buf);
8988
maybe_flush_or_die(stdout, "attribute to stdout");
9089
}
9190
strbuf_release(&buf);
92-
strbuf_release(&nbuf);
91+
strbuf_release(&unquoted);
9392
}
9493

9594
static NORETURN void error_with_usage(const char *msg)

builtin/check-ignore.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -115,28 +115,27 @@ static int check_ignore(struct dir_struct *dir,
115115

116116
static int check_ignore_stdin_paths(struct dir_struct *dir, const char *prefix)
117117
{
118-
struct strbuf buf, nbuf;
118+
struct strbuf buf = STRBUF_INIT;
119+
struct strbuf unquoted = STRBUF_INIT;
119120
char *pathspec[2] = { NULL, NULL };
120121
strbuf_getline_fn getline_fn;
121122
int num_ignored = 0;
122123

123124
getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf;
124-
strbuf_init(&buf, 0);
125-
strbuf_init(&nbuf, 0);
126125
while (getline_fn(&buf, stdin) != EOF) {
127126
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))
130129
die("line is badly quoted");
131-
strbuf_swap(&buf, &nbuf);
130+
strbuf_swap(&buf, &unquoted);
132131
}
133132
pathspec[0] = buf.buf;
134133
num_ignored += check_ignore(dir, prefix,
135134
1, (const char **)pathspec);
136135
maybe_flush_or_die(stdout, "check-ignore to stdout");
137136
}
138137
strbuf_release(&buf);
139-
strbuf_release(&nbuf);
138+
strbuf_release(&unquoted);
140139
return num_ignored;
141140
}
142141

builtin/checkout-index.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,8 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
251251
}
252252

253253
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;
255256
strbuf_getline_fn getline_fn;
256257

257258
if (all)
@@ -261,16 +262,16 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
261262
while (getline_fn(&buf, stdin) != EOF) {
262263
char *p;
263264
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))
266267
die("line is badly quoted");
267-
strbuf_swap(&buf, &nbuf);
268+
strbuf_swap(&buf, &unquoted);
268269
}
269270
p = prefix_path(prefix, prefix_length, buf.buf);
270271
checkout_file(p, prefix);
271272
free(p);
272273
}
273-
strbuf_release(&nbuf);
274+
strbuf_release(&unquoted);
274275
strbuf_release(&buf);
275276
}
276277

builtin/hash-object.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,20 +58,21 @@ static void hash_object(const char *path, const char *type, const char *vpath,
5858
static void hash_stdin_paths(const char *type, int no_filters, unsigned flags,
5959
int literally)
6060
{
61-
struct strbuf buf = STRBUF_INIT, nbuf = STRBUF_INIT;
61+
struct strbuf buf = STRBUF_INIT;
62+
struct strbuf unquoted = STRBUF_INIT;
6263

6364
while (strbuf_getline_lf(&buf, stdin) != EOF) {
6465
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))
6768
die("line is badly quoted");
68-
strbuf_swap(&buf, &nbuf);
69+
strbuf_swap(&buf, &unquoted);
6970
}
7071
hash_object(buf.buf, type, no_filters ? NULL : buf.buf, flags,
7172
literally);
7273
}
7374
strbuf_release(&buf);
74-
strbuf_release(&nbuf);
75+
strbuf_release(&unquoted);
7576
}
7677

7778
int cmd_hash_object(int argc, const char **argv, const char *prefix)

builtin/update-index.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,24 +1075,25 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
10751075
}
10761076

10771077
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;
10791080

10801081
setup_work_tree();
10811082
while (getline_fn(&buf, stdin) != EOF) {
10821083
char *p;
10831084
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))
10861087
die("line is badly quoted");
1087-
strbuf_swap(&buf, &nbuf);
1088+
strbuf_swap(&buf, &unquoted);
10881089
}
10891090
p = prefix_path(prefix, prefix_length, buf.buf);
10901091
update_one(p);
10911092
if (set_executable_bit)
10921093
chmod_path(set_executable_bit, p);
10931094
free(p);
10941095
}
1095-
strbuf_release(&nbuf);
1096+
strbuf_release(&unquoted);
10961097
strbuf_release(&buf);
10971098
}
10981099

0 commit comments

Comments
 (0)