From: Jeff King <peff@peff.net>
To: Junio C Hamano <gitster@pobox.com>
Cc: Lukas Fleischer <git@cryptocrack.de>, git@vger.kernel.org
Subject: [PATCH 4/5] use xstrdup_or_null to replace ternary conditionals
Date: Mon, 12 Jan 2015 20:59:09 -0500 [thread overview]
Message-ID: <20150113015909.GD18986@peff.net> (raw)
In-Reply-To: <20150113015427.GA5497@peff.net>
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>
---
resolve_refdup is the interesting one to look at for readability here.
config.c | 2 +-
grep.c | 4 ++--
notes.c | 2 +-
refs.c | 3 +--
remote.c | 4 ++--
shallow.c | 2 +-
walker.c | 2 +-
7 files changed, 9 insertions(+), 10 deletions(-)
diff --git a/config.c b/config.c
index 752e2e2..e5e64dc 100644
--- a/config.c
+++ b/config.c
@@ -1340,7 +1340,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 6e085f8..b58c7c6 100644
--- a/grep.c
+++ b/grep.c
@@ -1661,8 +1661,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 c763a21..2be4d7f 100644
--- 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 5fcacc6..78fec5c 100644
--- a/refs.c
+++ b/refs.c
@@ -1618,8 +1618,7 @@ const char *resolve_ref_unsafe(const char *refname, int resolve_flags, unsigned
char *resolve_refdup(const char *ref, int resolve_flags, unsigned char *sha1, int *flags)
{
- const char *ret = resolve_ref_unsafe(ref, resolve_flags, sha1, flags);
- return ret ? xstrdup(ret) : NULL;
+ return xstrdup_or_null(resolve_ref_unsafe(ref, resolve_flags, sha1, flags));
}
/* The argument to filter_refs */
diff --git a/remote.c b/remote.c
index 5b9c693..7b71ebf 100644
--- 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;
}
diff --git a/shallow.c b/shallow.c
index cdd0775..f5e6720 100644
--- a/shallow.c
+++ b/shallow.c
@@ -22,7 +22,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)
diff --git a/walker.c b/walker.c
index f149371..483da4e 100644
--- 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);
--
2.2.1.425.g441bb3c
next prev parent reply other threads:[~2015-01-13 1:59 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-01-10 21:33 [PATCH] blame.c: fix garbled error message Lukas Fleischer
2015-01-12 19:08 ` Junio C Hamano
2015-01-12 20:40 ` Jeff King
2015-01-12 22:55 ` Junio C Hamano
2015-01-12 23:12 ` Jeff King
2015-01-13 0:11 ` Junio C Hamano
2015-01-13 1:54 ` Jeff King
2015-01-13 1:57 ` [PATCH 1/5] git-compat-util: add xstrdup_or_null helper Jeff King
2015-01-13 2:21 ` Jonathan Nieder
2015-01-13 2:23 ` Jeff King
2015-01-13 1:58 ` [PATCH 2/5] builtin/apply.c: use xstrdup_or_null instead of null_strdup Jeff King
2015-01-13 1:58 ` [PATCH 3/5] builtin/commit.c: use xstrdup_or_null instead of envdup Jeff King
2015-01-13 1:59 ` Jeff King [this message]
2015-01-13 1:59 ` [PATCH 5/5] blame.c: fix garbled error message Jeff King
2015-01-14 14:21 ` [PATCH] " Lukas Fleischer
2015-01-14 17:22 ` Junio C Hamano
2015-01-14 20:49 ` Jeff King
2015-01-14 21:54 ` Junio C Hamano
2015-01-12 23:18 ` Lukas Fleischer
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20150113015909.GD18986@peff.net \
--to=peff@peff.net \
--cc=git@cryptocrack.de \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).