Skip to content

Commit

Permalink
Replace calls to strbuf_init(&foo, 0) with STRBUF_INIT initializer
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
drafnel authored and spearce committed Oct 12, 2008
1 parent 7e7abea commit f285a2d
Show file tree
Hide file tree
Showing 46 changed files with 91 additions and 197 deletions.
6 changes: 2 additions & 4 deletions archive-tar.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,10 @@ static int write_tar_entry(struct archiver_args *args,
unsigned int mode, void *buffer, unsigned long size)
{
struct ustar_header header;
struct strbuf ext_header;
struct strbuf ext_header = STRBUF_INIT;
int err = 0;

memset(&header, 0, sizeof(header));
strbuf_init(&ext_header, 0);

if (!sha1) {
*header.typeflag = TYPEFLAG_GLOBAL_HEADER;
Expand Down Expand Up @@ -211,10 +210,9 @@ static int write_tar_entry(struct archiver_args *args,
static int write_global_extended_header(struct archiver_args *args)
{
const unsigned char *sha1 = args->commit_sha1;
struct strbuf ext_header;
struct strbuf ext_header = STRBUF_INIT;
int err;

strbuf_init(&ext_header, 0);
strbuf_append_ext_header(&ext_header, "comment", sha1_to_hex(sha1), 40);
err = write_tar_entry(args, NULL, NULL, 0, 0, ext_header.buf,
ext_header.len);
Expand Down
6 changes: 2 additions & 4 deletions archive.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,10 @@ static void format_subst(const struct commit *commit,
struct strbuf *buf)
{
char *to_free = NULL;
struct strbuf fmt;
struct strbuf fmt = STRBUF_INIT;

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

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

buffer = read_sha1_file(sha1, type, sizep);
if (buffer && S_ISREG(mode)) {
struct strbuf buf;
struct strbuf buf = STRBUF_INIT;
size_t size = 0;

strbuf_init(&buf, 0);
strbuf_attach(&buf, buffer, *sizep, *sizep + 1);
convert_to_working_tree(path, buf.buf, buf.len, &buf);
if (commit)
Expand Down
26 changes: 8 additions & 18 deletions builtin-apply.c
Original file line number Diff line number Diff line change
Expand Up @@ -321,13 +321,12 @@ static char *find_name(const char *line, char *def, int p_value, int terminate)
const char *start = line;

if (*line == '"') {
struct strbuf name;
struct strbuf name = STRBUF_INIT;

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

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

if (*line == '"') {
const char *cp;
struct strbuf first;
struct strbuf sp;

strbuf_init(&first, 0);
strbuf_init(&sp, 0);
struct strbuf first = STRBUF_INIT;
struct strbuf sp = STRBUF_INIT;

if (unquote_c_style(&first, line, &second))
goto free_and_fail1;
Expand Down Expand Up @@ -741,10 +737,9 @@ static char *git_header_name(char *line, int llen)
*/
for (second = name; second < line + llen; second++) {
if (*second == '"') {
struct strbuf sp;
struct strbuf sp = STRBUF_INIT;
const char *np;

strbuf_init(&sp, 0);
if (unquote_c_style(&sp, second, NULL))
goto free_and_fail2;

Expand Down Expand Up @@ -1508,11 +1503,10 @@ static const char minuses[]=

static void show_stats(struct patch *patch)
{
struct strbuf qname;
struct strbuf qname = STRBUF_INIT;
char *cp = patch->new_name ? patch->new_name : patch->old_name;
int max, add, del;

strbuf_init(&qname, 0);
quote_c_style(cp, &qname, NULL, 0);

/*
Expand Down Expand Up @@ -2292,14 +2286,12 @@ static void add_to_fn_table(struct patch *patch)

static int apply_data(struct patch *patch, struct stat *st, struct cache_entry *ce)
{
struct strbuf buf;
struct strbuf buf = STRBUF_INIT;
struct image image;
size_t len;
char *img;
struct patch *tpatch;

strbuf_init(&buf, 0);

if (!(patch->is_copy || patch->is_rename) &&
((tpatch = in_fn_table(patch->old_name)) != NULL)) {
if (tpatch == (struct patch *) -1) {
Expand Down Expand Up @@ -2779,7 +2771,7 @@ static void add_index_file(const char *path, unsigned mode, void *buf, unsigned
static int try_create_file(const char *path, unsigned int mode, const char *buf, unsigned long size)
{
int fd;
struct strbuf nbuf;
struct strbuf nbuf = STRBUF_INIT;

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

strbuf_init(&nbuf, 0);
if (convert_to_working_tree(path, buf, size, &nbuf)) {
size = nbuf.len;
buf = nbuf.buf;
Expand Down Expand Up @@ -3060,13 +3051,12 @@ static void prefix_patches(struct patch *p)
static int apply_patch(int fd, const char *filename, int options)
{
size_t offset;
struct strbuf buf;
struct strbuf buf = STRBUF_INIT;
struct patch *list = NULL, **listp = &list;
int skipped_patch = 0;

/* FIXME - memory leak when using multiple patch files as inputs */
memset(&fn_table, 0, sizeof(struct string_list));
strbuf_init(&buf, 0);
patch_input_file = filename;
read_patch_file(&buf, fd);
offset = 0;
Expand Down
3 changes: 1 addition & 2 deletions builtin-blame.c
Original file line number Diff line number Diff line change
Expand Up @@ -2062,7 +2062,7 @@ static struct commit *fake_working_tree_commit(const char *path, const char *con
struct commit *commit;
struct origin *origin;
unsigned char head_sha1[20];
struct strbuf buf;
struct strbuf buf = STRBUF_INIT;
const char *ident;
time_t now;
int size, len;
Expand All @@ -2082,7 +2082,6 @@ static struct commit *fake_working_tree_commit(const char *path, const char *con

origin = make_origin(commit, path);

strbuf_init(&buf, 0);
if (!contents_from || strcmp("-", contents_from)) {
struct stat st;
const char *read_from;
Expand Down
3 changes: 1 addition & 2 deletions builtin-branch.c
Original file line number Diff line number Diff line change
Expand Up @@ -334,11 +334,10 @@ static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,
}

if (verbose) {
struct strbuf subject;
struct strbuf subject = STRBUF_INIT;
const char *sub = " **** invalid ref ****";
char stat[128];

strbuf_init(&subject, 0);
stat[0] = '\0';

commit = item->commit;
Expand Down
3 changes: 1 addition & 2 deletions builtin-cat-file.c
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,8 @@ static int batch_one_object(const char *obj_name, int print_contents)

static int batch_objects(int print_contents)
{
struct strbuf buf;
struct strbuf buf = STRBUF_INIT;

strbuf_init(&buf, 0);
while (strbuf_getline(&buf, stdin, '\n') != EOF) {
int error = batch_one_object(buf.buf, print_contents);
if (error)
Expand Down
4 changes: 1 addition & 3 deletions builtin-checkout-index.c
Original file line number Diff line number Diff line change
Expand Up @@ -268,13 +268,11 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
}

if (read_from_stdin) {
struct strbuf buf, nbuf;
struct strbuf buf = STRBUF_INIT, nbuf = STRBUF_INIT;

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

strbuf_init(&buf, 0);
strbuf_init(&nbuf, 0);
while (strbuf_getline(&buf, stdin, line_termination) != EOF) {
const char *p;
if (line_termination && buf.buf[0] == '"') {
Expand Down
12 changes: 4 additions & 8 deletions builtin-checkout.c
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,7 @@ static void show_local_changes(struct object *head)

static void describe_detached_head(char *msg, struct commit *commit)
{
struct strbuf sb;
strbuf_init(&sb, 0);
struct strbuf sb = STRBUF_INIT;
parse_commit(commit);
pretty_print_commit(CMIT_FMT_ONELINE, commit, &sb, 0, NULL, NULL, 0, 0);
fprintf(stderr, "%s %s... %s\n", msg,
Expand Down Expand Up @@ -360,8 +359,7 @@ struct branch_info {

static void setup_branch_path(struct branch_info *branch)
{
struct strbuf buf;
strbuf_init(&buf, 0);
struct strbuf buf = STRBUF_INIT;
strbuf_addstr(&buf, "refs/heads/");
strbuf_addstr(&buf, branch->name);
branch->path = strbuf_detach(&buf, NULL);
Expand Down Expand Up @@ -484,7 +482,7 @@ static void update_refs_for_switch(struct checkout_opts *opts,
struct branch_info *old,
struct branch_info *new)
{
struct strbuf msg;
struct strbuf msg = STRBUF_INIT;
const char *old_desc;
if (opts->new_branch) {
create_branch(old->name, opts->new_branch, new->name, 0,
Expand All @@ -493,7 +491,6 @@ static void update_refs_for_switch(struct checkout_opts *opts,
setup_branch_path(new);
}

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

if (opts.new_branch) {
struct strbuf buf;
strbuf_init(&buf, 0);
struct strbuf buf = STRBUF_INIT;
strbuf_addstr(&buf, "refs/heads/");
strbuf_addstr(&buf, opts.new_branch);
if (!get_sha1(buf.buf, rev))
Expand Down
6 changes: 2 additions & 4 deletions builtin-clean.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
int i;
int show_only = 0, remove_directories = 0, quiet = 0, ignored = 0;
int ignored_only = 0, baselen = 0, config_set = 0, errors = 0;
struct strbuf directory;
struct strbuf directory = STRBUF_INIT;
struct dir_struct dir;
const char *path, *base;
static const char **pathspec;
struct strbuf buf;
struct strbuf buf = STRBUF_INIT;
const char *qname;
char *seen = NULL;
struct option options[] = {
Expand All @@ -58,7 +58,6 @@ int cmd_clean(int argc, const char **argv, const char *prefix)

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

strbuf_init(&buf, 0);
memset(&dir, 0, sizeof(dir));
if (ignored_only)
dir.show_ignored = 1;
Expand Down Expand Up @@ -88,7 +87,6 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
if (baselen)
path = base = xmemdupz(*pathspec, baselen);
read_directory(&dir, path, base, baselen, pathspec);
strbuf_init(&directory, 0);

if (pathspec)
seen = xmalloc(argc > 0 ? argc : 1);
Expand Down
9 changes: 3 additions & 6 deletions builtin-clone.c
Original file line number Diff line number Diff line change
Expand Up @@ -264,10 +264,9 @@ pid_t junk_pid;

static void remove_junk(void)
{
struct strbuf sb;
struct strbuf sb = STRBUF_INIT;
if (getpid() != junk_pid)
return;
strbuf_init(&sb, 0);
if (junk_git_dir) {
strbuf_addstr(&sb, junk_git_dir);
remove_dir_recursively(&sb, 0);
Expand Down Expand Up @@ -354,7 +353,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
char *path, *dir;
const struct ref *refs, *head_points_at, *remote_head, *mapped_refs;
char branch_top[256], key[256], value[256];
struct strbuf reflog_msg;
struct strbuf reflog_msg = STRBUF_INIT;
struct transport *transport = NULL;
char *src_ref_prefix = "refs/heads/";

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

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

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

if (!option_bare) {
struct strbuf head_ref;
struct strbuf head_ref = STRBUF_INIT;
const char *head = head_points_at->name;

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

strbuf_init(&head_ref, 0);
strbuf_addstr(&head_ref, branch_top);
strbuf_addstr(&head_ref, "HEAD");

Expand Down
Loading

0 comments on commit f285a2d

Please sign in to comment.