Skip to content

Commit f285a2d

Browse files
drafnelspearce
authored andcommitted
Replace calls to strbuf_init(&foo, 0) with STRBUF_INIT initializer
Many call sites use strbuf_init(&foo, 0) to initialize local strbuf variable "foo" which has not been accessed since its declaration. These can be replaced with a static initialization using the STRBUF_INIT macro which is just as readable, saves a function call, and takes up fewer lines. Signed-off-by: Brandon Casey <[email protected]> Signed-off-by: Shawn O. Pearce <[email protected]>
1 parent 7e7abea commit f285a2d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+91
-197
lines changed

archive-tar.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,10 @@ static int write_tar_entry(struct archiver_args *args,
124124
unsigned int mode, void *buffer, unsigned long size)
125125
{
126126
struct ustar_header header;
127-
struct strbuf ext_header;
127+
struct strbuf ext_header = STRBUF_INIT;
128128
int err = 0;
129129

130130
memset(&header, 0, sizeof(header));
131-
strbuf_init(&ext_header, 0);
132131

133132
if (!sha1) {
134133
*header.typeflag = TYPEFLAG_GLOBAL_HEADER;
@@ -211,10 +210,9 @@ static int write_tar_entry(struct archiver_args *args,
211210
static int write_global_extended_header(struct archiver_args *args)
212211
{
213212
const unsigned char *sha1 = args->commit_sha1;
214-
struct strbuf ext_header;
213+
struct strbuf ext_header = STRBUF_INIT;
215214
int err;
216215

217-
strbuf_init(&ext_header, 0);
218216
strbuf_append_ext_header(&ext_header, "comment", sha1_to_hex(sha1), 40);
219217
err = write_tar_entry(args, NULL, NULL, 0, 0, ext_header.buf,
220218
ext_header.len);

archive.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,10 @@ static void format_subst(const struct commit *commit,
2929
struct strbuf *buf)
3030
{
3131
char *to_free = NULL;
32-
struct strbuf fmt;
32+
struct strbuf fmt = STRBUF_INIT;
3333

3434
if (src == buf->buf)
3535
to_free = strbuf_detach(buf, NULL);
36-
strbuf_init(&fmt, 0);
3736
for (;;) {
3837
const char *b, *c;
3938

@@ -65,10 +64,9 @@ static void *sha1_file_to_archive(const char *path, const unsigned char *sha1,
6564

6665
buffer = read_sha1_file(sha1, type, sizep);
6766
if (buffer && S_ISREG(mode)) {
68-
struct strbuf buf;
67+
struct strbuf buf = STRBUF_INIT;
6968
size_t size = 0;
7069

71-
strbuf_init(&buf, 0);
7270
strbuf_attach(&buf, buffer, *sizep, *sizep + 1);
7371
convert_to_working_tree(path, buf.buf, buf.len, &buf);
7472
if (commit)

builtin-apply.c

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -321,13 +321,12 @@ static char *find_name(const char *line, char *def, int p_value, int terminate)
321321
const char *start = line;
322322

323323
if (*line == '"') {
324-
struct strbuf name;
324+
struct strbuf name = STRBUF_INIT;
325325

326326
/*
327327
* Proposed "new-style" GNU patch/diff format; see
328328
* http://marc.theaimsgroup.com/?l=git&m=112927316408690&w=2
329329
*/
330-
strbuf_init(&name, 0);
331330
if (!unquote_c_style(&name, line, NULL)) {
332331
char *cp;
333332

@@ -675,11 +674,8 @@ static char *git_header_name(char *line, int llen)
675674

676675
if (*line == '"') {
677676
const char *cp;
678-
struct strbuf first;
679-
struct strbuf sp;
680-
681-
strbuf_init(&first, 0);
682-
strbuf_init(&sp, 0);
677+
struct strbuf first = STRBUF_INIT;
678+
struct strbuf sp = STRBUF_INIT;
683679

684680
if (unquote_c_style(&first, line, &second))
685681
goto free_and_fail1;
@@ -741,10 +737,9 @@ static char *git_header_name(char *line, int llen)
741737
*/
742738
for (second = name; second < line + llen; second++) {
743739
if (*second == '"') {
744-
struct strbuf sp;
740+
struct strbuf sp = STRBUF_INIT;
745741
const char *np;
746742

747-
strbuf_init(&sp, 0);
748743
if (unquote_c_style(&sp, second, NULL))
749744
goto free_and_fail2;
750745

@@ -1508,11 +1503,10 @@ static const char minuses[]=
15081503

15091504
static void show_stats(struct patch *patch)
15101505
{
1511-
struct strbuf qname;
1506+
struct strbuf qname = STRBUF_INIT;
15121507
char *cp = patch->new_name ? patch->new_name : patch->old_name;
15131508
int max, add, del;
15141509

1515-
strbuf_init(&qname, 0);
15161510
quote_c_style(cp, &qname, NULL, 0);
15171511

15181512
/*
@@ -2292,14 +2286,12 @@ static void add_to_fn_table(struct patch *patch)
22922286

22932287
static int apply_data(struct patch *patch, struct stat *st, struct cache_entry *ce)
22942288
{
2295-
struct strbuf buf;
2289+
struct strbuf buf = STRBUF_INIT;
22962290
struct image image;
22972291
size_t len;
22982292
char *img;
22992293
struct patch *tpatch;
23002294

2301-
strbuf_init(&buf, 0);
2302-
23032295
if (!(patch->is_copy || patch->is_rename) &&
23042296
((tpatch = in_fn_table(patch->old_name)) != NULL)) {
23052297
if (tpatch == (struct patch *) -1) {
@@ -2779,7 +2771,7 @@ static void add_index_file(const char *path, unsigned mode, void *buf, unsigned
27792771
static int try_create_file(const char *path, unsigned int mode, const char *buf, unsigned long size)
27802772
{
27812773
int fd;
2782-
struct strbuf nbuf;
2774+
struct strbuf nbuf = STRBUF_INIT;
27832775

27842776
if (S_ISGITLINK(mode)) {
27852777
struct stat st;
@@ -2798,7 +2790,6 @@ static int try_create_file(const char *path, unsigned int mode, const char *buf,
27982790
if (fd < 0)
27992791
return -1;
28002792

2801-
strbuf_init(&nbuf, 0);
28022793
if (convert_to_working_tree(path, buf, size, &nbuf)) {
28032794
size = nbuf.len;
28042795
buf = nbuf.buf;
@@ -3060,13 +3051,12 @@ static void prefix_patches(struct patch *p)
30603051
static int apply_patch(int fd, const char *filename, int options)
30613052
{
30623053
size_t offset;
3063-
struct strbuf buf;
3054+
struct strbuf buf = STRBUF_INIT;
30643055
struct patch *list = NULL, **listp = &list;
30653056
int skipped_patch = 0;
30663057

30673058
/* FIXME - memory leak when using multiple patch files as inputs */
30683059
memset(&fn_table, 0, sizeof(struct string_list));
3069-
strbuf_init(&buf, 0);
30703060
patch_input_file = filename;
30713061
read_patch_file(&buf, fd);
30723062
offset = 0;

builtin-blame.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2062,7 +2062,7 @@ static struct commit *fake_working_tree_commit(const char *path, const char *con
20622062
struct commit *commit;
20632063
struct origin *origin;
20642064
unsigned char head_sha1[20];
2065-
struct strbuf buf;
2065+
struct strbuf buf = STRBUF_INIT;
20662066
const char *ident;
20672067
time_t now;
20682068
int size, len;
@@ -2082,7 +2082,6 @@ static struct commit *fake_working_tree_commit(const char *path, const char *con
20822082

20832083
origin = make_origin(commit, path);
20842084

2085-
strbuf_init(&buf, 0);
20862085
if (!contents_from || strcmp("-", contents_from)) {
20872086
struct stat st;
20882087
const char *read_from;

builtin-branch.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,11 +334,10 @@ static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,
334334
}
335335

336336
if (verbose) {
337-
struct strbuf subject;
337+
struct strbuf subject = STRBUF_INIT;
338338
const char *sub = " **** invalid ref ****";
339339
char stat[128];
340340

341-
strbuf_init(&subject, 0);
342341
stat[0] = '\0';
343342

344343
commit = item->commit;

builtin-cat-file.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,8 @@ static int batch_one_object(const char *obj_name, int print_contents)
189189

190190
static int batch_objects(int print_contents)
191191
{
192-
struct strbuf buf;
192+
struct strbuf buf = STRBUF_INIT;
193193

194-
strbuf_init(&buf, 0);
195194
while (strbuf_getline(&buf, stdin, '\n') != EOF) {
196195
int error = batch_one_object(buf.buf, print_contents);
197196
if (error)

builtin-checkout-index.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -268,13 +268,11 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
268268
}
269269

270270
if (read_from_stdin) {
271-
struct strbuf buf, nbuf;
271+
struct strbuf buf = STRBUF_INIT, nbuf = STRBUF_INIT;
272272

273273
if (all)
274274
die("git checkout-index: don't mix '--all' and '--stdin'");
275275

276-
strbuf_init(&buf, 0);
277-
strbuf_init(&nbuf, 0);
278276
while (strbuf_getline(&buf, stdin, line_termination) != EOF) {
279277
const char *p;
280278
if (line_termination && buf.buf[0] == '"') {

builtin-checkout.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -310,8 +310,7 @@ static void show_local_changes(struct object *head)
310310

311311
static void describe_detached_head(char *msg, struct commit *commit)
312312
{
313-
struct strbuf sb;
314-
strbuf_init(&sb, 0);
313+
struct strbuf sb = STRBUF_INIT;
315314
parse_commit(commit);
316315
pretty_print_commit(CMIT_FMT_ONELINE, commit, &sb, 0, NULL, NULL, 0, 0);
317316
fprintf(stderr, "%s %s... %s\n", msg,
@@ -360,8 +359,7 @@ struct branch_info {
360359

361360
static void setup_branch_path(struct branch_info *branch)
362361
{
363-
struct strbuf buf;
364-
strbuf_init(&buf, 0);
362+
struct strbuf buf = STRBUF_INIT;
365363
strbuf_addstr(&buf, "refs/heads/");
366364
strbuf_addstr(&buf, branch->name);
367365
branch->path = strbuf_detach(&buf, NULL);
@@ -484,7 +482,7 @@ static void update_refs_for_switch(struct checkout_opts *opts,
484482
struct branch_info *old,
485483
struct branch_info *new)
486484
{
487-
struct strbuf msg;
485+
struct strbuf msg = STRBUF_INIT;
488486
const char *old_desc;
489487
if (opts->new_branch) {
490488
create_branch(old->name, opts->new_branch, new->name, 0,
@@ -493,7 +491,6 @@ static void update_refs_for_switch(struct checkout_opts *opts,
493491
setup_branch_path(new);
494492
}
495493

496-
strbuf_init(&msg, 0);
497494
old_desc = old->name;
498495
if (!old_desc)
499496
old_desc = sha1_to_hex(old->commit->object.sha1);
@@ -738,8 +735,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
738735
}
739736

740737
if (opts.new_branch) {
741-
struct strbuf buf;
742-
strbuf_init(&buf, 0);
738+
struct strbuf buf = STRBUF_INIT;
743739
strbuf_addstr(&buf, "refs/heads/");
744740
strbuf_addstr(&buf, opts.new_branch);
745741
if (!get_sha1(buf.buf, rev))

builtin-clean.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
3131
int i;
3232
int show_only = 0, remove_directories = 0, quiet = 0, ignored = 0;
3333
int ignored_only = 0, baselen = 0, config_set = 0, errors = 0;
34-
struct strbuf directory;
34+
struct strbuf directory = STRBUF_INIT;
3535
struct dir_struct dir;
3636
const char *path, *base;
3737
static const char **pathspec;
38-
struct strbuf buf;
38+
struct strbuf buf = STRBUF_INIT;
3939
const char *qname;
4040
char *seen = NULL;
4141
struct option options[] = {
@@ -58,7 +58,6 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
5858

5959
argc = parse_options(argc, argv, options, builtin_clean_usage, 0);
6060

61-
strbuf_init(&buf, 0);
6261
memset(&dir, 0, sizeof(dir));
6362
if (ignored_only)
6463
dir.show_ignored = 1;
@@ -88,7 +87,6 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
8887
if (baselen)
8988
path = base = xmemdupz(*pathspec, baselen);
9089
read_directory(&dir, path, base, baselen, pathspec);
91-
strbuf_init(&directory, 0);
9290

9391
if (pathspec)
9492
seen = xmalloc(argc > 0 ? argc : 1);

builtin-clone.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -264,10 +264,9 @@ pid_t junk_pid;
264264

265265
static void remove_junk(void)
266266
{
267-
struct strbuf sb;
267+
struct strbuf sb = STRBUF_INIT;
268268
if (getpid() != junk_pid)
269269
return;
270-
strbuf_init(&sb, 0);
271270
if (junk_git_dir) {
272271
strbuf_addstr(&sb, junk_git_dir);
273272
remove_dir_recursively(&sb, 0);
@@ -354,7 +353,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
354353
char *path, *dir;
355354
const struct ref *refs, *head_points_at, *remote_head, *mapped_refs;
356355
char branch_top[256], key[256], value[256];
357-
struct strbuf reflog_msg;
356+
struct strbuf reflog_msg = STRBUF_INIT;
358357
struct transport *transport = NULL;
359358
char *src_ref_prefix = "refs/heads/";
360359

@@ -404,7 +403,6 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
404403
if (!stat(dir, &buf))
405404
die("destination directory '%s' already exists.", dir);
406405

407-
strbuf_init(&reflog_msg, 0);
408406
strbuf_addf(&reflog_msg, "clone: from %s", repo);
409407

410408
if (option_bare)
@@ -526,7 +524,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
526524
create_symref("HEAD", head_points_at->name, NULL);
527525

528526
if (!option_bare) {
529-
struct strbuf head_ref;
527+
struct strbuf head_ref = STRBUF_INIT;
530528
const char *head = head_points_at->name;
531529

532530
if (!prefixcmp(head, "refs/heads/"))
@@ -539,7 +537,6 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
539537
head_points_at->old_sha1,
540538
NULL, 0, DIE_ON_ERR);
541539

542-
strbuf_init(&head_ref, 0);
543540
strbuf_addstr(&head_ref, branch_top);
544541
strbuf_addstr(&head_ref, "HEAD");
545542

0 commit comments

Comments
 (0)