use xstrdup_or_null to replace ternary conditionals
authorJeff King <peff@peff.net>
Tue, 13 Jan 2015 01:59:09 +0000 (20:59 -0500)
committerJunio C Hamano <gitster@pobox.com>
Tue, 13 Jan 2015 18:05:48 +0000 (10:05 -0800)
This replaces "x ? xstrdup(x) : NULL" with xstrdup_or_null(x).
The change is fairly mechanical, with the exception of
resolve_refdup, which can eliminate a temporary variable.

There are still a few hits grepping for "?.*xstrdup", but
these are of slightly different forms and cannot be
converted (e.g., "x ? xstrdup(x->foo) : NULL").

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
config.c
grep.c
notes.c
refs.c
remote.c
shallow.c
walker.c
index 039647d247e0ac1e3a523800bbb90c86d89354de..400d2e47de5bdab60291ca38e3151745ad35c488 100644 (file)
--- a/config.c
+++ b/config.c
@@ -1329,7 +1329,7 @@ static int configset_add_value(struct config_set *cs, const char *key, const cha
                string_list_init(&e->value_list, 1);
                hashmap_add(&cs->config_hash, e);
        }
-       si = string_list_append_nodup(&e->value_list, value ? xstrdup(value) : NULL);
+       si = string_list_append_nodup(&e->value_list, xstrdup_or_null(value));
 
        ALLOC_GROW(cs->list.items, cs->list.nr + 1, cs->list.alloc);
        l_item = &cs->list.items[cs->list.nr++];
diff --git a/grep.c b/grep.c
index 99217dc04f5d04c761f094069f1053cb090baaf1..f48a648a0d673e5acecef19288b608cc0049659d 100644 (file)
--- a/grep.c
+++ b/grep.c
@@ -1646,8 +1646,8 @@ void grep_source_init(struct grep_source *gs, enum grep_source_type type,
                      const void *identifier)
 {
        gs->type = type;
-       gs->name = name ? xstrdup(name) : NULL;
-       gs->path = path ? xstrdup(path) : NULL;
+       gs->name = xstrdup_or_null(name);
+       gs->path = xstrdup_or_null(path);
        gs->buf = NULL;
        gs->size = 0;
        gs->driver = NULL;
diff --git a/notes.c b/notes.c
index 5fe691dbcdfb9e3f9fa3c768f243234d5bb2e816..ee5f0e71f38b31eed7e1c21618d13ca5219b1725 100644 (file)
--- a/notes.c
+++ b/notes.c
@@ -1006,7 +1006,7 @@ void init_notes(struct notes_tree *t, const char *notes_ref,
        t->root = (struct int_node *) xcalloc(1, sizeof(struct int_node));
        t->first_non_note = NULL;
        t->prev_non_note = NULL;
-       t->ref = notes_ref ? xstrdup(notes_ref) : NULL;
+       t->ref = xstrdup_or_null(notes_ref);
        t->combine_notes = combine_notes;
        t->initialized = 1;
        t->dirty = 0;
diff --git a/refs.c b/refs.c
index ffd45e92922ec52dccccfaecf1ceaf29f9231337..32dce4e41c377cbafbf9ede5f31bd1680a4e20ae 100644 (file)
--- a/refs.c
+++ b/refs.c
@@ -1529,8 +1529,7 @@ const char *resolve_ref_unsafe(const char *refname, unsigned char *sha1, int rea
 
 char *resolve_refdup(const char *ref, unsigned char *sha1, int reading, int *flag)
 {
-       const char *ret = resolve_ref_unsafe(ref, sha1, reading, flag);
-       return ret ? xstrdup(ret) : NULL;
+       return xstrdup_or_null(resolve_ref_unsafe(ref, sha1, reading, flag));
 }
 
 /* The argument to filter_refs */
index ce785f8953bd8a51ef1c3ce57da5c8e7cce7cef5..179bceff8a2ce27f95de797656dd5460950c2d9c 100644 (file)
--- a/remote.c
+++ b/remote.c
@@ -975,8 +975,8 @@ struct ref *copy_ref(const struct ref *ref)
        cpy = xmalloc(sizeof(struct ref) + len + 1);
        memcpy(cpy, ref, sizeof(struct ref) + len + 1);
        cpy->next = NULL;
-       cpy->symref = ref->symref ? xstrdup(ref->symref) : NULL;
-       cpy->remote_status = ref->remote_status ? xstrdup(ref->remote_status) : NULL;
+       cpy->symref = xstrdup_or_null(ref->symref);
+       cpy->remote_status = xstrdup_or_null(ref->remote_status);
        cpy->peer_ref = copy_ref(ref->peer_ref);
        return cpy;
 }
index 57f4afa6b40585c663d0df78494ce2108611ce4b..ee145743346f2c2fcafccfc04407c599dfe2eaae 100644 (file)
--- a/shallow.c
+++ b/shallow.c
@@ -21,7 +21,7 @@ void set_alternate_shallow_file(const char *path, int override)
        if (alternate_shallow_file && !override)
                return;
        free(alternate_shallow_file);
-       alternate_shallow_file = path ? xstrdup(path) : NULL;
+       alternate_shallow_file = xstrdup_or_null(path);
 }
 
 int register_shallow(const unsigned char *sha1)
index 18a67d33cb55b799f7784e52ceac214cf76e5d3e..adabcc9dfe7c9614d10c560dec53c210254e14f9 100644 (file)
--- a/walker.c
+++ b/walker.c
@@ -232,7 +232,7 @@ int walker_targets_stdin(char ***target, const char ***write_ref)
                        REALLOC_ARRAY(*write_ref, targets_alloc);
                }
                (*target)[targets] = xstrdup(tg_one);
-               (*write_ref)[targets] = rf_one ? xstrdup(rf_one) : NULL;
+               (*write_ref)[targets] = xstrdup_or_null(rf_one);
                targets++;
        }
        strbuf_release(&buf);